add isl_space_get_tuple_domain_hash
[isl.git] / isl_aff.c
blobfb1e95828d986d90ddf3188965705048a1f82715
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018 Cerebras Systems
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
12 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_union_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl_local_space_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_mat_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl_val_private.h>
33 #include <isl_point_private.h>
34 #include <isl_config.h>
36 #undef EL_BASE
37 #define EL_BASE aff
39 #include <isl_list_templ.c>
41 #undef EL_BASE
42 #define EL_BASE pw_aff
44 #include <isl_list_templ.c>
46 #undef EL_BASE
47 #define EL_BASE pw_multi_aff
49 #include <isl_list_templ.c>
51 #undef EL_BASE
52 #define EL_BASE union_pw_aff
54 #include <isl_list_templ.c>
56 #undef EL_BASE
57 #define EL_BASE union_pw_multi_aff
59 #include <isl_list_templ.c>
61 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
62 __isl_take isl_vec *v)
64 isl_aff *aff;
66 if (!ls || !v)
67 goto error;
69 aff = isl_calloc_type(v->ctx, struct isl_aff);
70 if (!aff)
71 goto error;
73 aff->ref = 1;
74 aff->ls = ls;
75 aff->v = v;
77 return aff;
78 error:
79 isl_local_space_free(ls);
80 isl_vec_free(v);
81 return NULL;
84 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
86 isl_ctx *ctx;
87 isl_vec *v;
88 isl_size total;
90 if (!ls)
91 return NULL;
93 ctx = isl_local_space_get_ctx(ls);
94 if (!isl_local_space_divs_known(ls))
95 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
96 goto error);
97 if (!isl_local_space_is_set(ls))
98 isl_die(ctx, isl_error_invalid,
99 "domain of affine expression should be a set",
100 goto error);
102 total = isl_local_space_dim(ls, isl_dim_all);
103 if (total < 0)
104 goto error;
105 v = isl_vec_alloc(ctx, 1 + 1 + total);
106 return isl_aff_alloc_vec(ls, v);
107 error:
108 isl_local_space_free(ls);
109 return NULL;
112 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
114 if (!aff)
115 return NULL;
117 aff->ref++;
118 return aff;
121 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
123 if (!aff)
124 return NULL;
126 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
127 isl_vec_copy(aff->v));
130 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
132 if (!aff)
133 return NULL;
135 if (aff->ref == 1)
136 return aff;
137 aff->ref--;
138 return isl_aff_dup(aff);
141 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
143 isl_aff *aff;
145 aff = isl_aff_alloc(ls);
146 if (!aff)
147 return NULL;
149 isl_int_set_si(aff->v->el[0], 1);
150 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
152 return aff;
155 /* Return an affine expression that is equal to zero on domain space "space".
157 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
159 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
162 /* Return a piecewise affine expression defined on the specified domain
163 * that is equal to zero.
165 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
167 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
170 /* Change "aff" into a NaN.
172 * Note that this function gets called from isl_aff_nan_on_domain,
173 * so "aff" may not have been initialized yet.
175 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
177 aff = isl_aff_cow(aff);
178 if (!aff)
179 return NULL;
181 aff->v = isl_vec_clr(aff->v);
182 if (!aff->v)
183 return isl_aff_free(aff);
185 return aff;
188 /* Return an affine expression defined on the specified domain
189 * that represents NaN.
191 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
193 isl_aff *aff;
195 aff = isl_aff_alloc(ls);
196 return isl_aff_set_nan(aff);
199 /* Return an affine expression defined on the specified domain space
200 * that represents NaN.
202 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
204 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
207 /* Return a piecewise affine expression defined on the specified domain space
208 * that represents NaN.
210 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
211 __isl_take isl_space *space)
213 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
216 /* Return a piecewise affine expression defined on the specified domain
217 * that represents NaN.
219 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
221 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
224 /* Return an affine expression that is equal to "val" on
225 * domain local space "ls".
227 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
228 __isl_take isl_val *val)
230 isl_aff *aff;
232 if (!ls || !val)
233 goto error;
234 if (!isl_val_is_rat(val))
235 isl_die(isl_val_get_ctx(val), isl_error_invalid,
236 "expecting rational value", goto error);
238 aff = isl_aff_alloc(isl_local_space_copy(ls));
239 if (!aff)
240 goto error;
242 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
243 isl_int_set(aff->v->el[1], val->n);
244 isl_int_set(aff->v->el[0], val->d);
246 isl_local_space_free(ls);
247 isl_val_free(val);
248 return aff;
249 error:
250 isl_local_space_free(ls);
251 isl_val_free(val);
252 return NULL;
255 /* Return an affine expression that is equal to "val" on domain space "space".
257 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
258 __isl_take isl_val *val)
260 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
263 /* Return an affine expression that is equal to the specified dimension
264 * in "ls".
266 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
267 enum isl_dim_type type, unsigned pos)
269 isl_space *space;
270 isl_aff *aff;
272 if (!ls)
273 return NULL;
275 space = isl_local_space_get_space(ls);
276 if (!space)
277 goto error;
278 if (isl_space_is_map(space))
279 isl_die(isl_space_get_ctx(space), isl_error_invalid,
280 "expecting (parameter) set space", goto error);
281 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
282 goto error;
284 isl_space_free(space);
285 aff = isl_aff_alloc(ls);
286 if (!aff)
287 return NULL;
289 pos += isl_local_space_offset(aff->ls, type);
291 isl_int_set_si(aff->v->el[0], 1);
292 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
293 isl_int_set_si(aff->v->el[1 + pos], 1);
295 return aff;
296 error:
297 isl_local_space_free(ls);
298 isl_space_free(space);
299 return NULL;
302 /* Return a piecewise affine expression that is equal to
303 * the specified dimension in "ls".
305 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
306 enum isl_dim_type type, unsigned pos)
308 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
311 /* Return an affine expression that is equal to the parameter
312 * in the domain space "space" with identifier "id".
314 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
315 __isl_take isl_space *space, __isl_take isl_id *id)
317 int pos;
318 isl_local_space *ls;
320 if (!space || !id)
321 goto error;
322 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
323 if (pos < 0)
324 isl_die(isl_space_get_ctx(space), isl_error_invalid,
325 "parameter not found in space", goto error);
326 isl_id_free(id);
327 ls = isl_local_space_from_space(space);
328 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
329 error:
330 isl_space_free(space);
331 isl_id_free(id);
332 return NULL;
335 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
337 if (!aff)
338 return NULL;
340 if (--aff->ref > 0)
341 return NULL;
343 isl_local_space_free(aff->ls);
344 isl_vec_free(aff->v);
346 free(aff);
348 return NULL;
351 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
353 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
356 /* Return a hash value that digests "aff".
358 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
360 uint32_t hash, ls_hash, v_hash;
362 if (!aff)
363 return 0;
365 hash = isl_hash_init();
366 ls_hash = isl_local_space_get_hash(aff->ls);
367 isl_hash_hash(hash, ls_hash);
368 v_hash = isl_vec_get_hash(aff->v);
369 isl_hash_hash(hash, v_hash);
371 return hash;
374 /* Return the domain local space of "aff".
376 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
377 __isl_keep isl_aff *aff)
379 return aff ? aff->ls : NULL;
382 /* Return the number of variables of the given type in the domain of "aff".
384 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
386 isl_local_space *ls;
388 ls = isl_aff_peek_domain_local_space(aff);
389 return isl_local_space_dim(ls, type);
392 /* Externally, an isl_aff has a map space, but internally, the
393 * ls field corresponds to the domain of that space.
395 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
397 if (!aff)
398 return isl_size_error;
399 if (type == isl_dim_out)
400 return 1;
401 if (type == isl_dim_in)
402 type = isl_dim_set;
403 return isl_aff_domain_dim(aff, type);
406 /* Return the offset of the first coefficient of type "type" in
407 * the domain of "aff".
409 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
411 isl_local_space *ls;
413 ls = isl_aff_peek_domain_local_space(aff);
414 return isl_local_space_offset(ls, type);
417 /* Return the position of the dimension of the given type and name
418 * in "aff".
419 * Return -1 if no such dimension can be found.
421 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
422 const char *name)
424 if (!aff)
425 return -1;
426 if (type == isl_dim_out)
427 return -1;
428 if (type == isl_dim_in)
429 type = isl_dim_set;
430 return isl_local_space_find_dim_by_name(aff->ls, type, name);
433 /* Return the domain space of "aff".
435 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
437 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
440 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
442 return isl_space_copy(isl_aff_peek_domain_space(aff));
445 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
447 isl_space *space;
448 if (!aff)
449 return NULL;
450 space = isl_local_space_get_space(aff->ls);
451 space = isl_space_from_domain(space);
452 space = isl_space_add_dims(space, isl_dim_out, 1);
453 return space;
456 /* Return a copy of the domain space of "aff".
458 __isl_give isl_local_space *isl_aff_get_domain_local_space(
459 __isl_keep isl_aff *aff)
461 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
464 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
466 isl_local_space *ls;
467 if (!aff)
468 return NULL;
469 ls = isl_local_space_copy(aff->ls);
470 ls = isl_local_space_from_domain(ls);
471 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
472 return ls;
475 /* Return the local space of the domain of "aff".
476 * This may be either a copy or the local space itself
477 * if there is only one reference to "aff".
478 * This allows the local space to be modified inplace
479 * if both the expression and its local space have only a single reference.
480 * The caller is not allowed to modify "aff" between this call and
481 * a subsequent call to isl_aff_restore_domain_local_space.
482 * The only exception is that isl_aff_free can be called instead.
484 __isl_give isl_local_space *isl_aff_take_domain_local_space(
485 __isl_keep isl_aff *aff)
487 isl_local_space *ls;
489 if (!aff)
490 return NULL;
491 if (aff->ref != 1)
492 return isl_aff_get_domain_local_space(aff);
493 ls = aff->ls;
494 aff->ls = NULL;
495 return ls;
498 /* Set the local space of the domain of "aff" to "ls",
499 * where the local space of "aff" may be missing
500 * due to a preceding call to isl_aff_take_domain_local_space.
501 * However, in this case, "aff" only has a single reference and
502 * then the call to isl_aff_cow has no effect.
504 __isl_give isl_aff *isl_aff_restore_domain_local_space(
505 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
507 if (!aff || !ls)
508 goto error;
510 if (aff->ls == ls) {
511 isl_local_space_free(ls);
512 return aff;
515 aff = isl_aff_cow(aff);
516 if (!aff)
517 goto error;
518 isl_local_space_free(aff->ls);
519 aff->ls = ls;
521 return aff;
522 error:
523 isl_aff_free(aff);
524 isl_local_space_free(ls);
525 return NULL;
528 /* Externally, an isl_aff has a map space, but internally, the
529 * ls field corresponds to the domain of that space.
531 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
532 enum isl_dim_type type, unsigned pos)
534 if (!aff)
535 return NULL;
536 if (type == isl_dim_out)
537 return NULL;
538 if (type == isl_dim_in)
539 type = isl_dim_set;
540 return isl_local_space_get_dim_name(aff->ls, type, pos);
543 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
544 __isl_take isl_space *space)
546 aff = isl_aff_cow(aff);
547 if (!aff || !space)
548 goto error;
550 aff->ls = isl_local_space_reset_space(aff->ls, space);
551 if (!aff->ls)
552 return isl_aff_free(aff);
554 return aff;
555 error:
556 isl_aff_free(aff);
557 isl_space_free(space);
558 return NULL;
561 /* Reset the space of "aff". This function is called from isl_pw_templ.c
562 * and doesn't know if the space of an element object is represented
563 * directly or through its domain. It therefore passes along both.
565 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
566 __isl_take isl_space *space, __isl_take isl_space *domain)
568 isl_space_free(space);
569 return isl_aff_reset_domain_space(aff, domain);
572 /* Reorder the coefficients of the affine expression based
573 * on the given reordering.
574 * The reordering r is assumed to have been extended with the local
575 * variables.
577 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
578 __isl_take isl_reordering *r, int n_div)
580 isl_space *space;
581 isl_vec *res;
582 isl_size dim;
583 int i;
585 if (!vec || !r)
586 goto error;
588 space = isl_reordering_peek_space(r);
589 dim = isl_space_dim(space, isl_dim_all);
590 if (dim < 0)
591 goto error;
592 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
593 if (!res)
594 goto error;
595 isl_seq_cpy(res->el, vec->el, 2);
596 isl_seq_clr(res->el + 2, res->size - 2);
597 for (i = 0; i < r->len; ++i)
598 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
600 isl_reordering_free(r);
601 isl_vec_free(vec);
602 return res;
603 error:
604 isl_vec_free(vec);
605 isl_reordering_free(r);
606 return NULL;
609 /* Reorder the dimensions of the domain of "aff" according
610 * to the given reordering.
612 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
613 __isl_take isl_reordering *r)
615 aff = isl_aff_cow(aff);
616 if (!aff)
617 goto error;
619 r = isl_reordering_extend(r, aff->ls->div->n_row);
620 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
621 aff->ls->div->n_row);
622 aff->ls = isl_local_space_realign(aff->ls, r);
624 if (!aff->v || !aff->ls)
625 return isl_aff_free(aff);
627 return aff;
628 error:
629 isl_aff_free(aff);
630 isl_reordering_free(r);
631 return NULL;
634 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
635 __isl_take isl_space *model)
637 isl_bool equal_params;
639 if (!aff || !model)
640 goto error;
642 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
643 if (equal_params < 0)
644 goto error;
645 if (!equal_params) {
646 isl_reordering *exp;
648 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
649 exp = isl_reordering_extend_space(exp,
650 isl_aff_get_domain_space(aff));
651 aff = isl_aff_realign_domain(aff, exp);
654 isl_space_free(model);
655 return aff;
656 error:
657 isl_space_free(model);
658 isl_aff_free(aff);
659 return NULL;
662 #undef TYPE
663 #define TYPE isl_aff
664 #include "isl_unbind_params_templ.c"
666 /* Is "aff" obviously equal to zero?
668 * If the denominator is zero, then "aff" is not equal to zero.
670 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
672 int pos;
674 if (!aff)
675 return isl_bool_error;
677 if (isl_int_is_zero(aff->v->el[0]))
678 return isl_bool_false;
679 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
680 return isl_bool_ok(pos < 0);
683 /* Does "aff" represent NaN?
685 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
687 if (!aff)
688 return isl_bool_error;
690 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
693 /* Are "aff1" and "aff2" obviously equal?
695 * NaN is not equal to anything, not even to another NaN.
697 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
698 __isl_keep isl_aff *aff2)
700 isl_bool equal;
702 if (!aff1 || !aff2)
703 return isl_bool_error;
705 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
706 return isl_bool_false;
708 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
709 if (equal < 0 || !equal)
710 return equal;
712 return isl_vec_is_equal(aff1->v, aff2->v);
715 /* Return the common denominator of "aff" in "v".
717 * We cannot return anything meaningful in case of a NaN.
719 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
721 if (!aff)
722 return isl_stat_error;
723 if (isl_aff_is_nan(aff))
724 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
725 "cannot get denominator of NaN", return isl_stat_error);
726 isl_int_set(*v, aff->v->el[0]);
727 return isl_stat_ok;
730 /* Return the common denominator of "aff".
732 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
734 isl_ctx *ctx;
736 if (!aff)
737 return NULL;
739 ctx = isl_aff_get_ctx(aff);
740 if (isl_aff_is_nan(aff))
741 return isl_val_nan(ctx);
742 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
745 /* Return the constant term of "aff".
747 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
749 isl_ctx *ctx;
750 isl_val *v;
752 if (!aff)
753 return NULL;
755 ctx = isl_aff_get_ctx(aff);
756 if (isl_aff_is_nan(aff))
757 return isl_val_nan(ctx);
758 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
759 return isl_val_normalize(v);
762 /* Return the coefficient of the variable of type "type" at position "pos"
763 * of "aff".
765 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
766 enum isl_dim_type type, int pos)
768 isl_ctx *ctx;
769 isl_val *v;
771 if (!aff)
772 return NULL;
774 ctx = isl_aff_get_ctx(aff);
775 if (type == isl_dim_out)
776 isl_die(ctx, isl_error_invalid,
777 "output/set dimension does not have a coefficient",
778 return NULL);
779 if (type == isl_dim_in)
780 type = isl_dim_set;
782 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
783 return NULL;
785 if (isl_aff_is_nan(aff))
786 return isl_val_nan(ctx);
787 pos += isl_local_space_offset(aff->ls, type);
788 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
789 return isl_val_normalize(v);
792 /* Return the sign of the coefficient of the variable of type "type"
793 * at position "pos" of "aff".
795 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
796 int pos)
798 isl_ctx *ctx;
800 if (!aff)
801 return 0;
803 ctx = isl_aff_get_ctx(aff);
804 if (type == isl_dim_out)
805 isl_die(ctx, isl_error_invalid,
806 "output/set dimension does not have a coefficient",
807 return 0);
808 if (type == isl_dim_in)
809 type = isl_dim_set;
811 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
812 return 0;
814 pos += isl_local_space_offset(aff->ls, type);
815 return isl_int_sgn(aff->v->el[1 + pos]);
818 /* Replace the numerator of the constant term of "aff" by "v".
820 * A NaN is unaffected by this operation.
822 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
824 if (!aff)
825 return NULL;
826 if (isl_aff_is_nan(aff))
827 return aff;
828 aff = isl_aff_cow(aff);
829 if (!aff)
830 return NULL;
832 aff->v = isl_vec_cow(aff->v);
833 if (!aff->v)
834 return isl_aff_free(aff);
836 isl_int_set(aff->v->el[1], v);
838 return aff;
841 /* Replace the constant term of "aff" by "v".
843 * A NaN is unaffected by this operation.
845 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
846 __isl_take isl_val *v)
848 if (!aff || !v)
849 goto error;
851 if (isl_aff_is_nan(aff)) {
852 isl_val_free(v);
853 return aff;
856 if (!isl_val_is_rat(v))
857 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
858 "expecting rational value", goto error);
860 if (isl_int_eq(aff->v->el[1], v->n) &&
861 isl_int_eq(aff->v->el[0], v->d)) {
862 isl_val_free(v);
863 return aff;
866 aff = isl_aff_cow(aff);
867 if (!aff)
868 goto error;
869 aff->v = isl_vec_cow(aff->v);
870 if (!aff->v)
871 goto error;
873 if (isl_int_eq(aff->v->el[0], v->d)) {
874 isl_int_set(aff->v->el[1], v->n);
875 } else if (isl_int_is_one(v->d)) {
876 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
877 } else {
878 isl_seq_scale(aff->v->el + 1,
879 aff->v->el + 1, v->d, aff->v->size - 1);
880 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
881 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
882 aff->v = isl_vec_normalize(aff->v);
883 if (!aff->v)
884 goto error;
887 isl_val_free(v);
888 return aff;
889 error:
890 isl_aff_free(aff);
891 isl_val_free(v);
892 return NULL;
895 /* Add "v" to the constant term of "aff".
897 * A NaN is unaffected by this operation.
899 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
901 if (isl_int_is_zero(v))
902 return aff;
904 if (!aff)
905 return NULL;
906 if (isl_aff_is_nan(aff))
907 return aff;
908 aff = isl_aff_cow(aff);
909 if (!aff)
910 return NULL;
912 aff->v = isl_vec_cow(aff->v);
913 if (!aff->v)
914 return isl_aff_free(aff);
916 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
918 return aff;
921 /* Add "v" to the constant term of "aff",
922 * in case "aff" is a rational expression.
924 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
925 __isl_take isl_val *v)
927 aff = isl_aff_cow(aff);
928 if (!aff)
929 goto error;
931 aff->v = isl_vec_cow(aff->v);
932 if (!aff->v)
933 goto error;
935 if (isl_int_is_one(v->d)) {
936 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
937 } else if (isl_int_eq(aff->v->el[0], v->d)) {
938 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
939 aff->v = isl_vec_normalize(aff->v);
940 if (!aff->v)
941 goto error;
942 } else {
943 isl_seq_scale(aff->v->el + 1,
944 aff->v->el + 1, v->d, aff->v->size - 1);
945 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
946 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
947 aff->v = isl_vec_normalize(aff->v);
948 if (!aff->v)
949 goto error;
952 isl_val_free(v);
953 return aff;
954 error:
955 isl_aff_free(aff);
956 isl_val_free(v);
957 return NULL;
960 /* Return the first argument and free the second.
962 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
963 __isl_take isl_val *v)
965 isl_val_free(v);
966 return aff;
969 /* Replace the first argument by NaN and free the second argument.
971 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
972 __isl_take isl_val *v)
974 isl_val_free(v);
975 return isl_aff_set_nan(aff);
978 /* Add "v" to the constant term of "aff".
980 * A NaN is unaffected by this operation.
981 * Conversely, adding a NaN turns "aff" into a NaN.
983 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
984 __isl_take isl_val *v)
986 isl_bool is_nan, is_zero, is_rat;
988 is_nan = isl_aff_is_nan(aff);
989 is_zero = isl_val_is_zero(v);
990 if (is_nan < 0 || is_zero < 0)
991 goto error;
992 if (is_nan || is_zero)
993 return pick_free(aff, v);
995 is_nan = isl_val_is_nan(v);
996 is_rat = isl_val_is_rat(v);
997 if (is_nan < 0 || is_rat < 0)
998 goto error;
999 if (is_nan)
1000 return set_nan_free_val(aff, v);
1001 if (!is_rat)
1002 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1003 "expecting rational value or NaN", goto error);
1005 return isl_aff_add_rat_constant_val(aff, v);
1006 error:
1007 isl_aff_free(aff);
1008 isl_val_free(v);
1009 return NULL;
1012 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1014 isl_int t;
1016 isl_int_init(t);
1017 isl_int_set_si(t, v);
1018 aff = isl_aff_add_constant(aff, t);
1019 isl_int_clear(t);
1021 return aff;
1024 /* Add "v" to the numerator of the constant term of "aff".
1026 * A NaN is unaffected by this operation.
1028 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1030 if (isl_int_is_zero(v))
1031 return aff;
1033 if (!aff)
1034 return NULL;
1035 if (isl_aff_is_nan(aff))
1036 return aff;
1037 aff = isl_aff_cow(aff);
1038 if (!aff)
1039 return NULL;
1041 aff->v = isl_vec_cow(aff->v);
1042 if (!aff->v)
1043 return isl_aff_free(aff);
1045 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1047 return aff;
1050 /* Add "v" to the numerator of the constant term of "aff".
1052 * A NaN is unaffected by this operation.
1054 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1056 isl_int t;
1058 if (v == 0)
1059 return aff;
1061 isl_int_init(t);
1062 isl_int_set_si(t, v);
1063 aff = isl_aff_add_constant_num(aff, t);
1064 isl_int_clear(t);
1066 return aff;
1069 /* Replace the numerator of the constant term of "aff" by "v".
1071 * A NaN is unaffected by this operation.
1073 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1075 if (!aff)
1076 return NULL;
1077 if (isl_aff_is_nan(aff))
1078 return aff;
1079 aff = isl_aff_cow(aff);
1080 if (!aff)
1081 return NULL;
1083 aff->v = isl_vec_cow(aff->v);
1084 if (!aff->v)
1085 return isl_aff_free(aff);
1087 isl_int_set_si(aff->v->el[1], v);
1089 return aff;
1092 /* Replace the numerator of the coefficient of the variable of type "type"
1093 * at position "pos" of "aff" by "v".
1095 * A NaN is unaffected by this operation.
1097 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1098 enum isl_dim_type type, int pos, isl_int v)
1100 if (!aff)
1101 return NULL;
1103 if (type == isl_dim_out)
1104 isl_die(aff->v->ctx, isl_error_invalid,
1105 "output/set dimension does not have a coefficient",
1106 return isl_aff_free(aff));
1107 if (type == isl_dim_in)
1108 type = isl_dim_set;
1110 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1111 return isl_aff_free(aff);
1113 if (isl_aff_is_nan(aff))
1114 return aff;
1115 aff = isl_aff_cow(aff);
1116 if (!aff)
1117 return NULL;
1119 aff->v = isl_vec_cow(aff->v);
1120 if (!aff->v)
1121 return isl_aff_free(aff);
1123 pos += isl_local_space_offset(aff->ls, type);
1124 isl_int_set(aff->v->el[1 + pos], v);
1126 return aff;
1129 /* Replace the numerator of the coefficient of the variable of type "type"
1130 * at position "pos" of "aff" by "v".
1132 * A NaN is unaffected by this operation.
1134 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1135 enum isl_dim_type type, int pos, int v)
1137 if (!aff)
1138 return NULL;
1140 if (type == isl_dim_out)
1141 isl_die(aff->v->ctx, isl_error_invalid,
1142 "output/set dimension does not have a coefficient",
1143 return isl_aff_free(aff));
1144 if (type == isl_dim_in)
1145 type = isl_dim_set;
1147 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1148 return isl_aff_free(aff);
1150 if (isl_aff_is_nan(aff))
1151 return aff;
1152 pos += isl_local_space_offset(aff->ls, type);
1153 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1154 return aff;
1156 aff = isl_aff_cow(aff);
1157 if (!aff)
1158 return NULL;
1160 aff->v = isl_vec_cow(aff->v);
1161 if (!aff->v)
1162 return isl_aff_free(aff);
1164 isl_int_set_si(aff->v->el[1 + pos], v);
1166 return aff;
1169 /* Replace the coefficient of the variable of type "type" at position "pos"
1170 * of "aff" by "v".
1172 * A NaN is unaffected by this operation.
1174 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1175 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1177 if (!aff || !v)
1178 goto error;
1180 if (type == isl_dim_out)
1181 isl_die(aff->v->ctx, isl_error_invalid,
1182 "output/set dimension does not have a coefficient",
1183 goto error);
1184 if (type == isl_dim_in)
1185 type = isl_dim_set;
1187 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1188 return isl_aff_free(aff);
1190 if (isl_aff_is_nan(aff)) {
1191 isl_val_free(v);
1192 return aff;
1194 if (!isl_val_is_rat(v))
1195 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1196 "expecting rational value", goto error);
1198 pos += isl_local_space_offset(aff->ls, type);
1199 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1200 isl_int_eq(aff->v->el[0], v->d)) {
1201 isl_val_free(v);
1202 return aff;
1205 aff = isl_aff_cow(aff);
1206 if (!aff)
1207 goto error;
1208 aff->v = isl_vec_cow(aff->v);
1209 if (!aff->v)
1210 goto error;
1212 if (isl_int_eq(aff->v->el[0], v->d)) {
1213 isl_int_set(aff->v->el[1 + pos], v->n);
1214 } else if (isl_int_is_one(v->d)) {
1215 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1216 } else {
1217 isl_seq_scale(aff->v->el + 1,
1218 aff->v->el + 1, v->d, aff->v->size - 1);
1219 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1220 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1221 aff->v = isl_vec_normalize(aff->v);
1222 if (!aff->v)
1223 goto error;
1226 isl_val_free(v);
1227 return aff;
1228 error:
1229 isl_aff_free(aff);
1230 isl_val_free(v);
1231 return NULL;
1234 /* Add "v" to the coefficient of the variable of type "type"
1235 * at position "pos" of "aff".
1237 * A NaN is unaffected by this operation.
1239 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1240 enum isl_dim_type type, int pos, isl_int v)
1242 if (!aff)
1243 return NULL;
1245 if (type == isl_dim_out)
1246 isl_die(aff->v->ctx, isl_error_invalid,
1247 "output/set dimension does not have a coefficient",
1248 return isl_aff_free(aff));
1249 if (type == isl_dim_in)
1250 type = isl_dim_set;
1252 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1253 return isl_aff_free(aff);
1255 if (isl_aff_is_nan(aff))
1256 return aff;
1257 aff = isl_aff_cow(aff);
1258 if (!aff)
1259 return NULL;
1261 aff->v = isl_vec_cow(aff->v);
1262 if (!aff->v)
1263 return isl_aff_free(aff);
1265 pos += isl_local_space_offset(aff->ls, type);
1266 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1268 return aff;
1271 /* Add "v" to the coefficient of the variable of type "type"
1272 * at position "pos" of "aff".
1274 * A NaN is unaffected by this operation.
1276 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1277 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1279 if (!aff || !v)
1280 goto error;
1282 if (isl_val_is_zero(v)) {
1283 isl_val_free(v);
1284 return aff;
1287 if (type == isl_dim_out)
1288 isl_die(aff->v->ctx, isl_error_invalid,
1289 "output/set dimension does not have a coefficient",
1290 goto error);
1291 if (type == isl_dim_in)
1292 type = isl_dim_set;
1294 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1295 goto error;
1297 if (isl_aff_is_nan(aff)) {
1298 isl_val_free(v);
1299 return aff;
1301 if (!isl_val_is_rat(v))
1302 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1303 "expecting rational value", goto error);
1305 aff = isl_aff_cow(aff);
1306 if (!aff)
1307 goto error;
1309 aff->v = isl_vec_cow(aff->v);
1310 if (!aff->v)
1311 goto error;
1313 pos += isl_local_space_offset(aff->ls, type);
1314 if (isl_int_is_one(v->d)) {
1315 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1316 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1317 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1318 aff->v = isl_vec_normalize(aff->v);
1319 if (!aff->v)
1320 goto error;
1321 } else {
1322 isl_seq_scale(aff->v->el + 1,
1323 aff->v->el + 1, v->d, aff->v->size - 1);
1324 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1325 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1326 aff->v = isl_vec_normalize(aff->v);
1327 if (!aff->v)
1328 goto error;
1331 isl_val_free(v);
1332 return aff;
1333 error:
1334 isl_aff_free(aff);
1335 isl_val_free(v);
1336 return NULL;
1339 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1340 enum isl_dim_type type, int pos, int v)
1342 isl_int t;
1344 isl_int_init(t);
1345 isl_int_set_si(t, v);
1346 aff = isl_aff_add_coefficient(aff, type, pos, t);
1347 isl_int_clear(t);
1349 return aff;
1352 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1354 if (!aff)
1355 return NULL;
1357 return isl_local_space_get_div(aff->ls, pos);
1360 /* Return the negation of "aff".
1362 * As a special case, -NaN = NaN.
1364 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1366 if (!aff)
1367 return NULL;
1368 if (isl_aff_is_nan(aff))
1369 return aff;
1370 aff = isl_aff_cow(aff);
1371 if (!aff)
1372 return NULL;
1373 aff->v = isl_vec_cow(aff->v);
1374 if (!aff->v)
1375 return isl_aff_free(aff);
1377 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1379 return aff;
1382 /* Remove divs from the local space that do not appear in the affine
1383 * expression.
1384 * We currently only remove divs at the end.
1385 * Some intermediate divs may also not appear directly in the affine
1386 * expression, but we would also need to check that no other divs are
1387 * defined in terms of them.
1389 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1391 int pos;
1392 isl_size off;
1393 isl_size n;
1395 n = isl_aff_domain_dim(aff, isl_dim_div);
1396 off = isl_aff_domain_offset(aff, isl_dim_div);
1397 if (n < 0 || off < 0)
1398 return isl_aff_free(aff);
1400 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1401 if (pos == n)
1402 return aff;
1404 aff = isl_aff_cow(aff);
1405 if (!aff)
1406 return NULL;
1408 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1409 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1410 if (!aff->ls || !aff->v)
1411 return isl_aff_free(aff);
1413 return aff;
1416 /* Look for any divs in the aff->ls with a denominator equal to one
1417 * and plug them into the affine expression and any subsequent divs
1418 * that may reference the div.
1420 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1422 int i;
1423 isl_size n;
1424 int len;
1425 isl_int v;
1426 isl_vec *vec;
1427 isl_local_space *ls;
1428 isl_size off;
1430 n = isl_aff_domain_dim(aff, isl_dim_div);
1431 off = isl_aff_domain_offset(aff, isl_dim_div);
1432 if (n < 0 || off < 0)
1433 return isl_aff_free(aff);
1434 len = aff->v->size;
1435 for (i = 0; i < n; ++i) {
1436 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1437 continue;
1438 ls = isl_local_space_copy(aff->ls);
1439 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1440 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1441 vec = isl_vec_copy(aff->v);
1442 vec = isl_vec_cow(vec);
1443 if (!ls || !vec)
1444 goto error;
1446 isl_int_init(v);
1448 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1449 len, len, v);
1451 isl_int_clear(v);
1453 isl_vec_free(aff->v);
1454 aff->v = vec;
1455 isl_local_space_free(aff->ls);
1456 aff->ls = ls;
1459 return aff;
1460 error:
1461 isl_vec_free(vec);
1462 isl_local_space_free(ls);
1463 return isl_aff_free(aff);
1466 /* Look for any divs j that appear with a unit coefficient inside
1467 * the definitions of other divs i and plug them into the definitions
1468 * of the divs i.
1470 * In particular, an expression of the form
1472 * floor((f(..) + floor(g(..)/n))/m)
1474 * is simplified to
1476 * floor((n * f(..) + g(..))/(n * m))
1478 * This simplification is correct because we can move the expression
1479 * f(..) into the inner floor in the original expression to obtain
1481 * floor(floor((n * f(..) + g(..))/n)/m)
1483 * from which we can derive the simplified expression.
1485 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1487 int i, j;
1488 isl_size n;
1489 isl_size off;
1491 n = isl_aff_domain_dim(aff, isl_dim_div);
1492 off = isl_aff_domain_offset(aff, isl_dim_div);
1493 if (n < 0 || off < 0)
1494 return isl_aff_free(aff);
1495 for (i = 1; i < n; ++i) {
1496 for (j = 0; j < i; ++j) {
1497 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1498 continue;
1499 aff->ls = isl_local_space_substitute_seq(aff->ls,
1500 isl_dim_div, j, aff->ls->div->row[j],
1501 aff->v->size, i, 1);
1502 if (!aff->ls)
1503 return isl_aff_free(aff);
1507 return aff;
1510 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1512 * Even though this function is only called on isl_affs with a single
1513 * reference, we are careful to only change aff->v and aff->ls together.
1515 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1517 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1518 isl_local_space *ls;
1519 isl_vec *v;
1521 if (off < 0)
1522 return isl_aff_free(aff);
1524 ls = isl_local_space_copy(aff->ls);
1525 ls = isl_local_space_swap_div(ls, a, b);
1526 v = isl_vec_copy(aff->v);
1527 v = isl_vec_cow(v);
1528 if (!ls || !v)
1529 goto error;
1531 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1532 isl_vec_free(aff->v);
1533 aff->v = v;
1534 isl_local_space_free(aff->ls);
1535 aff->ls = ls;
1537 return aff;
1538 error:
1539 isl_vec_free(v);
1540 isl_local_space_free(ls);
1541 return isl_aff_free(aff);
1544 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1546 * We currently do not actually remove div "b", but simply add its
1547 * coefficient to that of "a" and then zero it out.
1549 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1551 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1553 if (off < 0)
1554 return isl_aff_free(aff);
1556 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1557 return aff;
1559 aff->v = isl_vec_cow(aff->v);
1560 if (!aff->v)
1561 return isl_aff_free(aff);
1563 isl_int_add(aff->v->el[1 + off + a],
1564 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1565 isl_int_set_si(aff->v->el[1 + off + b], 0);
1567 return aff;
1570 /* Sort the divs in the local space of "aff" according to
1571 * the comparison function "cmp_row" in isl_local_space.c,
1572 * combining the coefficients of identical divs.
1574 * Reordering divs does not change the semantics of "aff",
1575 * so there is no need to call isl_aff_cow.
1576 * Moreover, this function is currently only called on isl_affs
1577 * with a single reference.
1579 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1581 isl_size n;
1582 int i, j;
1584 n = isl_aff_dim(aff, isl_dim_div);
1585 if (n < 0)
1586 return isl_aff_free(aff);
1587 for (i = 1; i < n; ++i) {
1588 for (j = i - 1; j >= 0; --j) {
1589 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1590 if (cmp < 0)
1591 break;
1592 if (cmp == 0)
1593 aff = merge_divs(aff, j, j + 1);
1594 else
1595 aff = swap_div(aff, j, j + 1);
1596 if (!aff)
1597 return NULL;
1601 return aff;
1604 /* Normalize the representation of "aff".
1606 * This function should only be called on "new" isl_affs, i.e.,
1607 * with only a single reference. We therefore do not need to
1608 * worry about affecting other instances.
1610 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1612 if (!aff)
1613 return NULL;
1614 aff->v = isl_vec_normalize(aff->v);
1615 if (!aff->v)
1616 return isl_aff_free(aff);
1617 aff = plug_in_integral_divs(aff);
1618 aff = plug_in_unit_divs(aff);
1619 aff = sort_divs(aff);
1620 aff = isl_aff_remove_unused_divs(aff);
1621 return aff;
1624 /* Given f, return floor(f).
1625 * If f is an integer expression, then just return f.
1626 * If f is a constant, then return the constant floor(f).
1627 * Otherwise, if f = g/m, write g = q m + r,
1628 * create a new div d = [r/m] and return the expression q + d.
1629 * The coefficients in r are taken to lie between -m/2 and m/2.
1631 * reduce_div_coefficients performs the same normalization.
1633 * As a special case, floor(NaN) = NaN.
1635 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1637 int i;
1638 int size;
1639 isl_ctx *ctx;
1640 isl_vec *div;
1642 if (!aff)
1643 return NULL;
1645 if (isl_aff_is_nan(aff))
1646 return aff;
1647 if (isl_int_is_one(aff->v->el[0]))
1648 return aff;
1650 aff = isl_aff_cow(aff);
1651 if (!aff)
1652 return NULL;
1654 aff->v = isl_vec_cow(aff->v);
1655 if (!aff->v)
1656 return isl_aff_free(aff);
1658 if (isl_aff_is_cst(aff)) {
1659 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1660 isl_int_set_si(aff->v->el[0], 1);
1661 return aff;
1664 div = isl_vec_copy(aff->v);
1665 div = isl_vec_cow(div);
1666 if (!div)
1667 return isl_aff_free(aff);
1669 ctx = isl_aff_get_ctx(aff);
1670 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1671 for (i = 1; i < aff->v->size; ++i) {
1672 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1673 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1674 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1675 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1676 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1680 aff->ls = isl_local_space_add_div(aff->ls, div);
1681 if (!aff->ls)
1682 return isl_aff_free(aff);
1684 size = aff->v->size;
1685 aff->v = isl_vec_extend(aff->v, size + 1);
1686 if (!aff->v)
1687 return isl_aff_free(aff);
1688 isl_int_set_si(aff->v->el[0], 1);
1689 isl_int_set_si(aff->v->el[size], 1);
1691 aff = isl_aff_normalize(aff);
1693 return aff;
1696 /* Compute
1698 * aff mod m = aff - m * floor(aff/m)
1700 * with m an integer value.
1702 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1703 __isl_take isl_val *m)
1705 isl_aff *res;
1707 if (!aff || !m)
1708 goto error;
1710 if (!isl_val_is_int(m))
1711 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1712 "expecting integer modulo", goto error);
1714 res = isl_aff_copy(aff);
1715 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1716 aff = isl_aff_floor(aff);
1717 aff = isl_aff_scale_val(aff, m);
1718 res = isl_aff_sub(res, aff);
1720 return res;
1721 error:
1722 isl_aff_free(aff);
1723 isl_val_free(m);
1724 return NULL;
1727 /* Compute
1729 * pwaff mod m = pwaff - m * floor(pwaff/m)
1731 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1733 isl_pw_aff *res;
1735 res = isl_pw_aff_copy(pwaff);
1736 pwaff = isl_pw_aff_scale_down(pwaff, m);
1737 pwaff = isl_pw_aff_floor(pwaff);
1738 pwaff = isl_pw_aff_scale(pwaff, m);
1739 res = isl_pw_aff_sub(res, pwaff);
1741 return res;
1744 /* Compute
1746 * pa mod m = pa - m * floor(pa/m)
1748 * with m an integer value.
1750 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1751 __isl_take isl_val *m)
1753 if (!pa || !m)
1754 goto error;
1755 if (!isl_val_is_int(m))
1756 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1757 "expecting integer modulo", goto error);
1758 pa = isl_pw_aff_mod(pa, m->n);
1759 isl_val_free(m);
1760 return pa;
1761 error:
1762 isl_pw_aff_free(pa);
1763 isl_val_free(m);
1764 return NULL;
1767 /* Given f, return ceil(f).
1768 * If f is an integer expression, then just return f.
1769 * Otherwise, let f be the expression
1771 * e/m
1773 * then return
1775 * floor((e + m - 1)/m)
1777 * As a special case, ceil(NaN) = NaN.
1779 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1781 if (!aff)
1782 return NULL;
1784 if (isl_aff_is_nan(aff))
1785 return aff;
1786 if (isl_int_is_one(aff->v->el[0]))
1787 return aff;
1789 aff = isl_aff_cow(aff);
1790 if (!aff)
1791 return NULL;
1792 aff->v = isl_vec_cow(aff->v);
1793 if (!aff->v)
1794 return isl_aff_free(aff);
1796 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1797 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1798 aff = isl_aff_floor(aff);
1800 return aff;
1803 /* Apply the expansion computed by isl_merge_divs.
1804 * The expansion itself is given by "exp" while the resulting
1805 * list of divs is given by "div".
1807 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1808 __isl_take isl_mat *div, int *exp)
1810 isl_size old_n_div;
1811 isl_size new_n_div;
1812 isl_size offset;
1814 aff = isl_aff_cow(aff);
1816 offset = isl_aff_domain_offset(aff, isl_dim_div);
1817 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1818 new_n_div = isl_mat_rows(div);
1819 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1820 goto error;
1822 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1823 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1824 if (!aff->v || !aff->ls)
1825 return isl_aff_free(aff);
1826 return aff;
1827 error:
1828 isl_aff_free(aff);
1829 isl_mat_free(div);
1830 return NULL;
1833 /* Add two affine expressions that live in the same local space.
1835 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1836 __isl_take isl_aff *aff2)
1838 isl_int gcd, f;
1840 aff1 = isl_aff_cow(aff1);
1841 if (!aff1 || !aff2)
1842 goto error;
1844 aff1->v = isl_vec_cow(aff1->v);
1845 if (!aff1->v)
1846 goto error;
1848 isl_int_init(gcd);
1849 isl_int_init(f);
1850 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1851 isl_int_divexact(f, aff2->v->el[0], gcd);
1852 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1853 isl_int_divexact(f, aff1->v->el[0], gcd);
1854 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1855 isl_int_divexact(f, aff2->v->el[0], gcd);
1856 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1857 isl_int_clear(f);
1858 isl_int_clear(gcd);
1860 isl_aff_free(aff2);
1861 aff1 = isl_aff_normalize(aff1);
1862 return aff1;
1863 error:
1864 isl_aff_free(aff1);
1865 isl_aff_free(aff2);
1866 return NULL;
1869 /* Replace one of the arguments by a NaN and free the other one.
1871 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1872 __isl_take isl_aff *aff2)
1874 isl_aff_free(aff2);
1875 return isl_aff_set_nan(aff1);
1878 /* Return the sum of "aff1" and "aff2".
1880 * If either of the two is NaN, then the result is NaN.
1882 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1883 __isl_take isl_aff *aff2)
1885 isl_ctx *ctx;
1886 int *exp1 = NULL;
1887 int *exp2 = NULL;
1888 isl_mat *div;
1889 isl_size n_div1, n_div2;
1891 if (!aff1 || !aff2)
1892 goto error;
1894 ctx = isl_aff_get_ctx(aff1);
1895 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1896 isl_die(ctx, isl_error_invalid,
1897 "spaces don't match", goto error);
1899 if (isl_aff_is_nan(aff1)) {
1900 isl_aff_free(aff2);
1901 return aff1;
1903 if (isl_aff_is_nan(aff2)) {
1904 isl_aff_free(aff1);
1905 return aff2;
1908 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1909 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1910 if (n_div1 < 0 || n_div2 < 0)
1911 goto error;
1912 if (n_div1 == 0 && n_div2 == 0)
1913 return add_expanded(aff1, aff2);
1915 exp1 = isl_alloc_array(ctx, int, n_div1);
1916 exp2 = isl_alloc_array(ctx, int, n_div2);
1917 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1918 goto error;
1920 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1921 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1922 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1923 free(exp1);
1924 free(exp2);
1926 return add_expanded(aff1, aff2);
1927 error:
1928 free(exp1);
1929 free(exp2);
1930 isl_aff_free(aff1);
1931 isl_aff_free(aff2);
1932 return NULL;
1935 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1936 __isl_take isl_aff *aff2)
1938 return isl_aff_add(aff1, isl_aff_neg(aff2));
1941 /* Return the result of scaling "aff" by a factor of "f".
1943 * As a special case, f * NaN = NaN.
1945 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1947 isl_int gcd;
1949 if (!aff)
1950 return NULL;
1951 if (isl_aff_is_nan(aff))
1952 return aff;
1954 if (isl_int_is_one(f))
1955 return aff;
1957 aff = isl_aff_cow(aff);
1958 if (!aff)
1959 return NULL;
1960 aff->v = isl_vec_cow(aff->v);
1961 if (!aff->v)
1962 return isl_aff_free(aff);
1964 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1965 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1966 return aff;
1969 isl_int_init(gcd);
1970 isl_int_gcd(gcd, aff->v->el[0], f);
1971 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1972 isl_int_divexact(gcd, f, gcd);
1973 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1974 isl_int_clear(gcd);
1976 return aff;
1979 /* Multiple "aff" by "v".
1981 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1982 __isl_take isl_val *v)
1984 if (!aff || !v)
1985 goto error;
1987 if (isl_val_is_one(v)) {
1988 isl_val_free(v);
1989 return aff;
1992 if (!isl_val_is_rat(v))
1993 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1994 "expecting rational factor", goto error);
1996 aff = isl_aff_scale(aff, v->n);
1997 aff = isl_aff_scale_down(aff, v->d);
1999 isl_val_free(v);
2000 return aff;
2001 error:
2002 isl_aff_free(aff);
2003 isl_val_free(v);
2004 return NULL;
2007 /* Return the result of scaling "aff" down by a factor of "f".
2009 * As a special case, NaN/f = NaN.
2011 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2013 isl_int gcd;
2015 if (!aff)
2016 return NULL;
2017 if (isl_aff_is_nan(aff))
2018 return aff;
2020 if (isl_int_is_one(f))
2021 return aff;
2023 aff = isl_aff_cow(aff);
2024 if (!aff)
2025 return NULL;
2027 if (isl_int_is_zero(f))
2028 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2029 "cannot scale down by zero", return isl_aff_free(aff));
2031 aff->v = isl_vec_cow(aff->v);
2032 if (!aff->v)
2033 return isl_aff_free(aff);
2035 isl_int_init(gcd);
2036 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2037 isl_int_gcd(gcd, gcd, f);
2038 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2039 isl_int_divexact(gcd, f, gcd);
2040 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2041 isl_int_clear(gcd);
2043 return aff;
2046 /* Divide "aff" by "v".
2048 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2049 __isl_take isl_val *v)
2051 if (!aff || !v)
2052 goto error;
2054 if (isl_val_is_one(v)) {
2055 isl_val_free(v);
2056 return aff;
2059 if (!isl_val_is_rat(v))
2060 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2061 "expecting rational factor", goto error);
2062 if (!isl_val_is_pos(v))
2063 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2064 "factor needs to be positive", goto error);
2066 aff = isl_aff_scale(aff, v->d);
2067 aff = isl_aff_scale_down(aff, v->n);
2069 isl_val_free(v);
2070 return aff;
2071 error:
2072 isl_aff_free(aff);
2073 isl_val_free(v);
2074 return NULL;
2077 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2079 isl_int v;
2081 if (f == 1)
2082 return aff;
2084 isl_int_init(v);
2085 isl_int_set_ui(v, f);
2086 aff = isl_aff_scale_down(aff, v);
2087 isl_int_clear(v);
2089 return aff;
2092 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2093 enum isl_dim_type type, unsigned pos, const char *s)
2095 aff = isl_aff_cow(aff);
2096 if (!aff)
2097 return NULL;
2098 if (type == isl_dim_out)
2099 isl_die(aff->v->ctx, isl_error_invalid,
2100 "cannot set name of output/set dimension",
2101 return isl_aff_free(aff));
2102 if (type == isl_dim_in)
2103 type = isl_dim_set;
2104 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2105 if (!aff->ls)
2106 return isl_aff_free(aff);
2108 return aff;
2111 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2112 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2114 aff = isl_aff_cow(aff);
2115 if (!aff)
2116 goto error;
2117 if (type == isl_dim_out)
2118 isl_die(aff->v->ctx, isl_error_invalid,
2119 "cannot set name of output/set dimension",
2120 goto error);
2121 if (type == isl_dim_in)
2122 type = isl_dim_set;
2123 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2124 if (!aff->ls)
2125 return isl_aff_free(aff);
2127 return aff;
2128 error:
2129 isl_id_free(id);
2130 isl_aff_free(aff);
2131 return NULL;
2134 /* Replace the identifier of the input tuple of "aff" by "id".
2135 * type is currently required to be equal to isl_dim_in
2137 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2138 enum isl_dim_type type, __isl_take isl_id *id)
2140 aff = isl_aff_cow(aff);
2141 if (!aff)
2142 goto error;
2143 if (type != isl_dim_in)
2144 isl_die(aff->v->ctx, isl_error_invalid,
2145 "cannot only set id of input tuple", goto error);
2146 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2147 if (!aff->ls)
2148 return isl_aff_free(aff);
2150 return aff;
2151 error:
2152 isl_id_free(id);
2153 isl_aff_free(aff);
2154 return NULL;
2157 /* Exploit the equalities in "eq" to simplify the affine expression
2158 * and the expressions of the integer divisions in the local space.
2159 * The integer divisions in this local space are assumed to appear
2160 * as regular dimensions in "eq".
2162 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2163 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2165 int i, j;
2166 unsigned o_div;
2167 unsigned n_div;
2169 if (!eq)
2170 goto error;
2171 if (eq->n_eq == 0) {
2172 isl_basic_set_free(eq);
2173 return aff;
2176 aff = isl_aff_cow(aff);
2177 if (!aff)
2178 goto error;
2180 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2181 isl_basic_set_copy(eq));
2182 aff->v = isl_vec_cow(aff->v);
2183 if (!aff->ls || !aff->v)
2184 goto error;
2186 o_div = isl_basic_set_offset(eq, isl_dim_div);
2187 n_div = eq->n_div;
2188 for (i = 0; i < eq->n_eq; ++i) {
2189 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2190 if (j < 0 || j == 0 || j >= o_div)
2191 continue;
2193 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2194 &aff->v->el[0]);
2197 isl_basic_set_free(eq);
2198 aff = isl_aff_normalize(aff);
2199 return aff;
2200 error:
2201 isl_basic_set_free(eq);
2202 isl_aff_free(aff);
2203 return NULL;
2206 /* Exploit the equalities in "eq" to simplify the affine expression
2207 * and the expressions of the integer divisions in the local space.
2209 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2210 __isl_take isl_basic_set *eq)
2212 isl_size n_div;
2214 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2215 if (n_div < 0)
2216 goto error;
2217 if (n_div > 0)
2218 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2219 return isl_aff_substitute_equalities_lifted(aff, eq);
2220 error:
2221 isl_basic_set_free(eq);
2222 isl_aff_free(aff);
2223 return NULL;
2226 /* Look for equalities among the variables shared by context and aff
2227 * and the integer divisions of aff, if any.
2228 * The equalities are then used to eliminate coefficients and/or integer
2229 * divisions from aff.
2231 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2232 __isl_take isl_set *context)
2234 isl_local_space *ls;
2235 isl_basic_set *hull;
2237 ls = isl_aff_get_domain_local_space(aff);
2238 context = isl_local_space_lift_set(ls, context);
2240 hull = isl_set_affine_hull(context);
2241 return isl_aff_substitute_equalities_lifted(aff, hull);
2244 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2245 __isl_take isl_set *context)
2247 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2248 dom_context = isl_set_intersect_params(dom_context, context);
2249 return isl_aff_gist(aff, dom_context);
2252 /* Return a basic set containing those elements in the space
2253 * of aff where it is positive. "rational" should not be set.
2255 * If "aff" is NaN, then it is not positive.
2257 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2258 int rational, void *user)
2260 isl_constraint *ineq;
2261 isl_basic_set *bset;
2262 isl_val *c;
2264 if (!aff)
2265 return NULL;
2266 if (isl_aff_is_nan(aff)) {
2267 isl_space *space = isl_aff_get_domain_space(aff);
2268 isl_aff_free(aff);
2269 return isl_basic_set_empty(space);
2271 if (rational)
2272 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2273 "rational sets not supported", goto error);
2275 ineq = isl_inequality_from_aff(aff);
2276 c = isl_constraint_get_constant_val(ineq);
2277 c = isl_val_sub_ui(c, 1);
2278 ineq = isl_constraint_set_constant_val(ineq, c);
2280 bset = isl_basic_set_from_constraint(ineq);
2281 bset = isl_basic_set_simplify(bset);
2282 return bset;
2283 error:
2284 isl_aff_free(aff);
2285 return NULL;
2288 /* Return a basic set containing those elements in the space
2289 * of aff where it is non-negative.
2290 * If "rational" is set, then return a rational basic set.
2292 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2294 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2295 __isl_take isl_aff *aff, 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_inequality_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 non-negative.
2320 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2322 return aff_nonneg_basic_set(aff, 0, NULL);
2325 /* Return a basic set containing those elements in the domain space
2326 * of "aff" where it is positive.
2328 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2330 aff = isl_aff_add_constant_num_si(aff, -1);
2331 return isl_aff_nonneg_basic_set(aff);
2334 /* Return a basic set containing those elements in the domain space
2335 * of aff where it is negative.
2337 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2339 aff = isl_aff_neg(aff);
2340 return isl_aff_pos_basic_set(aff);
2343 /* Return a basic set containing those elements in the space
2344 * of aff where it is zero.
2345 * If "rational" is set, then return a rational basic set.
2347 * If "aff" is NaN, then it is not zero.
2349 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2350 int rational, void *user)
2352 isl_constraint *ineq;
2353 isl_basic_set *bset;
2355 if (!aff)
2356 return NULL;
2357 if (isl_aff_is_nan(aff)) {
2358 isl_space *space = isl_aff_get_domain_space(aff);
2359 isl_aff_free(aff);
2360 return isl_basic_set_empty(space);
2363 ineq = isl_equality_from_aff(aff);
2365 bset = isl_basic_set_from_constraint(ineq);
2366 if (rational)
2367 bset = isl_basic_set_set_rational(bset);
2368 bset = isl_basic_set_simplify(bset);
2369 return bset;
2372 /* Return a basic set containing those elements in the space
2373 * of aff where it is zero.
2375 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2377 return aff_zero_basic_set(aff, 0, NULL);
2380 /* Return a basic set containing those elements in the shared space
2381 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2383 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2384 __isl_take isl_aff *aff2)
2386 aff1 = isl_aff_sub(aff1, aff2);
2388 return isl_aff_nonneg_basic_set(aff1);
2391 /* Return a basic set containing those elements in the shared domain space
2392 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2394 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2395 __isl_take isl_aff *aff2)
2397 aff1 = isl_aff_sub(aff1, aff2);
2399 return isl_aff_pos_basic_set(aff1);
2402 /* Return a set containing those elements in the shared space
2403 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2405 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2406 __isl_take isl_aff *aff2)
2408 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2411 /* Return a set containing those elements in the shared domain space
2412 * of aff1 and aff2 where aff1 is greater than aff2.
2414 * If either of the two inputs is NaN, then the result is empty,
2415 * as comparisons with NaN always return false.
2417 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2418 __isl_take isl_aff *aff2)
2420 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2423 /* Return a basic set containing those elements in the shared space
2424 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2426 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2427 __isl_take isl_aff *aff2)
2429 return isl_aff_ge_basic_set(aff2, aff1);
2432 /* Return a basic set containing those elements in the shared domain space
2433 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2435 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2436 __isl_take isl_aff *aff2)
2438 return isl_aff_gt_basic_set(aff2, aff1);
2441 /* Return a set containing those elements in the shared space
2442 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2444 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2445 __isl_take isl_aff *aff2)
2447 return isl_aff_ge_set(aff2, aff1);
2450 /* Return a set containing those elements in the shared domain space
2451 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2453 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2454 __isl_take isl_aff *aff2)
2456 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2459 /* Return a basic set containing those elements in the shared space
2460 * of aff1 and aff2 where aff1 and aff2 are equal.
2462 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2463 __isl_take isl_aff *aff2)
2465 aff1 = isl_aff_sub(aff1, aff2);
2467 return isl_aff_zero_basic_set(aff1);
2470 /* Return a set containing those elements in the shared space
2471 * of aff1 and aff2 where aff1 and aff2 are equal.
2473 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2474 __isl_take isl_aff *aff2)
2476 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2479 /* Return a set containing those elements in the shared domain space
2480 * of aff1 and aff2 where aff1 and aff2 are not equal.
2482 * If either of the two inputs is NaN, then the result is empty,
2483 * as comparisons with NaN always return false.
2485 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2486 __isl_take isl_aff *aff2)
2488 isl_set *set_lt, *set_gt;
2490 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2491 isl_aff_copy(aff2));
2492 set_gt = isl_aff_gt_set(aff1, aff2);
2493 return isl_set_union_disjoint(set_lt, set_gt);
2496 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2497 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2499 aff1 = isl_aff_add(aff1, aff2);
2500 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2501 return aff1;
2504 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2506 if (!aff)
2507 return isl_bool_error;
2509 return isl_bool_false;
2512 #undef TYPE
2513 #define TYPE isl_aff
2514 static
2515 #include "check_type_range_templ.c"
2517 /* Check whether the given affine expression has non-zero coefficient
2518 * for any dimension in the given range or if any of these dimensions
2519 * appear with non-zero coefficients in any of the integer divisions
2520 * involved in the affine expression.
2522 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2523 enum isl_dim_type type, unsigned first, unsigned n)
2525 int i;
2526 int *active = NULL;
2527 isl_bool involves = isl_bool_false;
2529 if (!aff)
2530 return isl_bool_error;
2531 if (n == 0)
2532 return isl_bool_false;
2533 if (isl_aff_check_range(aff, type, first, n) < 0)
2534 return isl_bool_error;
2536 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2537 if (!active)
2538 goto error;
2540 first += isl_local_space_offset(aff->ls, type) - 1;
2541 for (i = 0; i < n; ++i)
2542 if (active[first + i]) {
2543 involves = isl_bool_true;
2544 break;
2547 free(active);
2549 return involves;
2550 error:
2551 free(active);
2552 return isl_bool_error;
2555 /* Does "aff" involve any local variables, i.e., integer divisions?
2557 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2559 isl_size n;
2561 n = isl_aff_dim(aff, isl_dim_div);
2562 if (n < 0)
2563 return isl_bool_error;
2564 return isl_bool_ok(n > 0);
2567 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2568 enum isl_dim_type type, unsigned first, unsigned n)
2570 isl_ctx *ctx;
2572 if (!aff)
2573 return NULL;
2574 if (type == isl_dim_out)
2575 isl_die(aff->v->ctx, isl_error_invalid,
2576 "cannot drop output/set dimension",
2577 return isl_aff_free(aff));
2578 if (type == isl_dim_in)
2579 type = isl_dim_set;
2580 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2581 return aff;
2583 ctx = isl_aff_get_ctx(aff);
2584 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2585 return isl_aff_free(aff);
2587 aff = isl_aff_cow(aff);
2588 if (!aff)
2589 return NULL;
2591 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2592 if (!aff->ls)
2593 return isl_aff_free(aff);
2595 first += 1 + isl_local_space_offset(aff->ls, type);
2596 aff->v = isl_vec_drop_els(aff->v, first, n);
2597 if (!aff->v)
2598 return isl_aff_free(aff);
2600 return aff;
2603 /* Is the domain of "aff" a product?
2605 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2607 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2610 #undef TYPE
2611 #define TYPE isl_aff
2612 #include <isl_domain_factor_templ.c>
2614 /* Project the domain of the affine expression onto its parameter space.
2615 * The affine expression may not involve any of the domain dimensions.
2617 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2619 isl_space *space;
2620 isl_size n;
2622 n = isl_aff_dim(aff, isl_dim_in);
2623 if (n < 0)
2624 return isl_aff_free(aff);
2625 aff = isl_aff_drop_domain(aff, 0, n);
2626 space = isl_aff_get_domain_space(aff);
2627 space = isl_space_params(space);
2628 aff = isl_aff_reset_domain_space(aff, space);
2629 return aff;
2632 /* Convert an affine expression defined over a parameter domain
2633 * into one that is defined over a zero-dimensional set.
2635 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2637 isl_local_space *ls;
2639 ls = isl_aff_take_domain_local_space(aff);
2640 ls = isl_local_space_set_from_params(ls);
2641 aff = isl_aff_restore_domain_local_space(aff, ls);
2643 return aff;
2646 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2647 enum isl_dim_type type, unsigned first, unsigned n)
2649 isl_ctx *ctx;
2651 if (!aff)
2652 return NULL;
2653 if (type == isl_dim_out)
2654 isl_die(aff->v->ctx, isl_error_invalid,
2655 "cannot insert output/set dimensions",
2656 return isl_aff_free(aff));
2657 if (type == isl_dim_in)
2658 type = isl_dim_set;
2659 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2660 return aff;
2662 ctx = isl_aff_get_ctx(aff);
2663 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2664 return isl_aff_free(aff);
2666 aff = isl_aff_cow(aff);
2667 if (!aff)
2668 return NULL;
2670 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2671 if (!aff->ls)
2672 return isl_aff_free(aff);
2674 first += 1 + isl_local_space_offset(aff->ls, type);
2675 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2676 if (!aff->v)
2677 return isl_aff_free(aff);
2679 return aff;
2682 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2683 enum isl_dim_type type, unsigned n)
2685 isl_size pos;
2687 pos = isl_aff_dim(aff, type);
2688 if (pos < 0)
2689 return isl_aff_free(aff);
2691 return isl_aff_insert_dims(aff, type, pos, n);
2694 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2695 * to dimensions of "dst_type" at "dst_pos".
2697 * We only support moving input dimensions to parameters and vice versa.
2699 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2700 enum isl_dim_type dst_type, unsigned dst_pos,
2701 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2703 unsigned g_dst_pos;
2704 unsigned g_src_pos;
2705 isl_size src_off, dst_off;
2707 if (!aff)
2708 return NULL;
2709 if (n == 0 &&
2710 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2711 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2712 return aff;
2714 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2715 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2716 "cannot move output/set dimension",
2717 return isl_aff_free(aff));
2718 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2719 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2720 "cannot move divs", return isl_aff_free(aff));
2721 if (dst_type == isl_dim_in)
2722 dst_type = isl_dim_set;
2723 if (src_type == isl_dim_in)
2724 src_type = isl_dim_set;
2726 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2727 return isl_aff_free(aff);
2728 if (dst_type == src_type)
2729 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2730 "moving dims within the same type not supported",
2731 return isl_aff_free(aff));
2733 aff = isl_aff_cow(aff);
2734 src_off = isl_aff_domain_offset(aff, src_type);
2735 dst_off = isl_aff_domain_offset(aff, dst_type);
2736 if (src_off < 0 || dst_off < 0)
2737 return isl_aff_free(aff);
2739 g_src_pos = 1 + src_off + src_pos;
2740 g_dst_pos = 1 + dst_off + dst_pos;
2741 if (dst_type > src_type)
2742 g_dst_pos -= n;
2744 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2745 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2746 src_type, src_pos, n);
2747 if (!aff->v || !aff->ls)
2748 return isl_aff_free(aff);
2750 aff = sort_divs(aff);
2752 return aff;
2755 /* Return a zero isl_aff in the given space.
2757 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2758 * interface over all piecewise types.
2760 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2762 isl_local_space *ls;
2764 ls = isl_local_space_from_space(isl_space_domain(space));
2765 return isl_aff_zero_on_domain(ls);
2768 #define isl_aff_involves_nan isl_aff_is_nan
2770 #undef PW
2771 #define PW isl_pw_aff
2772 #undef BASE
2773 #define BASE aff
2774 #undef EL_IS_ZERO
2775 #define EL_IS_ZERO is_empty
2776 #undef ZERO
2777 #define ZERO empty
2778 #undef IS_ZERO
2779 #define IS_ZERO is_empty
2780 #undef FIELD
2781 #define FIELD aff
2782 #undef DEFAULT_IS_ZERO
2783 #define DEFAULT_IS_ZERO 0
2785 #include <isl_pw_templ.c>
2786 #include <isl_pw_add_constant_val_templ.c>
2787 #include <isl_pw_bind_domain_templ.c>
2788 #include <isl_pw_eval.c>
2789 #include <isl_pw_hash.c>
2790 #include <isl_pw_insert_dims_templ.c>
2791 #include <isl_pw_insert_domain_templ.c>
2792 #include <isl_pw_move_dims_templ.c>
2793 #include <isl_pw_neg_templ.c>
2794 #include <isl_pw_pullback_templ.c>
2795 #include <isl_pw_sub_templ.c>
2796 #include <isl_pw_union_opt.c>
2798 #undef BASE
2799 #define BASE pw_aff
2801 #include <isl_union_single.c>
2802 #include <isl_union_neg.c>
2804 #undef BASE
2805 #define BASE aff
2807 #include <isl_union_pw_templ.c>
2809 /* Compute a piecewise quasi-affine expression with a domain that
2810 * is the union of those of pwaff1 and pwaff2 and such that on each
2811 * cell, the quasi-affine expression is the maximum of those of pwaff1
2812 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2813 * cell, then the associated expression is the defined one.
2815 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2816 __isl_take isl_pw_aff *pwaff2)
2818 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2819 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2822 /* Compute a piecewise quasi-affine expression with a domain that
2823 * is the union of those of pwaff1 and pwaff2 and such that on each
2824 * cell, the quasi-affine expression is the minimum of those of pwaff1
2825 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2826 * cell, then the associated expression is the defined one.
2828 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2829 __isl_take isl_pw_aff *pwaff2)
2831 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2832 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2835 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2836 __isl_take isl_pw_aff *pwaff2, int max)
2838 if (max)
2839 return isl_pw_aff_union_max(pwaff1, pwaff2);
2840 else
2841 return isl_pw_aff_union_min(pwaff1, pwaff2);
2844 /* Is the domain of "pa" a product?
2846 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2848 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2851 #undef TYPE
2852 #define TYPE isl_pw_aff
2853 #include <isl_domain_factor_templ.c>
2855 /* Return a set containing those elements in the domain
2856 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2857 * does not satisfy "fn" (if complement is 1).
2859 * The pieces with a NaN never belong to the result since
2860 * NaN does not satisfy any property.
2862 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2863 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2864 void *user),
2865 int complement, void *user)
2867 int i;
2868 isl_set *set;
2870 if (!pwaff)
2871 return NULL;
2873 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2875 for (i = 0; i < pwaff->n; ++i) {
2876 isl_basic_set *bset;
2877 isl_set *set_i, *locus;
2878 isl_bool rational;
2880 if (isl_aff_is_nan(pwaff->p[i].aff))
2881 continue;
2883 rational = isl_set_has_rational(pwaff->p[i].set);
2884 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2885 locus = isl_set_from_basic_set(bset);
2886 set_i = isl_set_copy(pwaff->p[i].set);
2887 if (complement)
2888 set_i = isl_set_subtract(set_i, locus);
2889 else
2890 set_i = isl_set_intersect(set_i, locus);
2891 set = isl_set_union_disjoint(set, set_i);
2894 isl_pw_aff_free(pwaff);
2896 return set;
2899 /* Return a set containing those elements in the domain
2900 * of "pa" where it is positive.
2902 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2904 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2907 /* Return a set containing those elements in the domain
2908 * of pwaff where it is non-negative.
2910 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2912 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2915 /* Return a set containing those elements in the domain
2916 * of pwaff where it is zero.
2918 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2920 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2923 /* Return a set containing those elements in the domain
2924 * of pwaff where it is not zero.
2926 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2928 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2931 /* Bind the affine function "aff" to the parameter "id",
2932 * returning the elements in the domain where the affine expression
2933 * is equal to the parameter.
2935 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2936 __isl_take isl_id *id)
2938 isl_space *space;
2939 isl_aff *aff_id;
2941 space = isl_aff_get_domain_space(aff);
2942 space = isl_space_add_param_id(space, isl_id_copy(id));
2944 aff = isl_aff_align_params(aff, isl_space_copy(space));
2945 aff_id = isl_aff_param_on_domain_space_id(space, id);
2947 return isl_aff_eq_basic_set(aff, aff_id);
2950 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2951 * "rational" should not be set.
2953 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2954 int rational, void *user)
2956 isl_id *id = user;
2958 if (!aff)
2959 return NULL;
2960 if (rational)
2961 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2962 "rational binding not supported", goto error);
2963 return isl_aff_bind_id(aff, isl_id_copy(id));
2964 error:
2965 isl_aff_free(aff);
2966 return NULL;
2969 /* Bind the piecewise affine function "pa" to the parameter "id",
2970 * returning the elements in the domain where the expression
2971 * is equal to the parameter.
2973 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2974 __isl_take isl_id *id)
2976 isl_set *bound;
2978 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2979 isl_id_free(id);
2981 return bound;
2984 /* Return a set containing those elements in the shared domain
2985 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2987 * We compute the difference on the shared domain and then construct
2988 * the set of values where this difference is non-negative.
2989 * If strict is set, we first subtract 1 from the difference.
2990 * If equal is set, we only return the elements where pwaff1 and pwaff2
2991 * are equal.
2993 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2994 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2996 isl_set *set1, *set2;
2998 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2999 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3000 set1 = isl_set_intersect(set1, set2);
3001 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3002 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3003 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3005 if (strict) {
3006 isl_space *space = isl_set_get_space(set1);
3007 isl_aff *aff;
3008 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3009 aff = isl_aff_add_constant_si(aff, -1);
3010 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3011 } else
3012 isl_set_free(set1);
3014 if (equal)
3015 return isl_pw_aff_zero_set(pwaff1);
3016 return isl_pw_aff_nonneg_set(pwaff1);
3019 /* Return a set containing those elements in the shared domain
3020 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3022 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3023 __isl_take isl_pw_aff *pwaff2)
3025 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3026 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3029 /* Return a set containing those elements in the shared domain
3030 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3032 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3033 __isl_take isl_pw_aff *pwaff2)
3035 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3036 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3039 /* Return a set containing those elements in the shared domain
3040 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3042 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3043 __isl_take isl_pw_aff *pwaff2)
3045 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3046 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3049 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3050 __isl_take isl_pw_aff *pwaff2)
3052 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3055 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3056 __isl_take isl_pw_aff *pwaff2)
3058 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3061 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3062 * where the function values are ordered in the same way as "order",
3063 * which returns a set in the shared domain of its two arguments.
3065 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3066 * We first pull back the two functions such that they are defined on
3067 * the domain [A -> B]. Then we apply "order", resulting in a set
3068 * in the space [A -> B]. Finally, we unwrap this set to obtain
3069 * a map in the space A -> B.
3071 static __isl_give isl_map *isl_pw_aff_order_map(
3072 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3073 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3074 __isl_take isl_pw_aff *pa2))
3076 isl_space *space1, *space2;
3077 isl_multi_aff *ma;
3078 isl_set *set;
3080 isl_pw_aff_align_params_bin(&pa1, &pa2);
3081 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3082 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3083 space1 = isl_space_map_from_domain_and_range(space1, space2);
3084 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3085 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3086 ma = isl_multi_aff_range_map(space1);
3087 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3088 set = order(pa1, pa2);
3090 return isl_set_unwrap(set);
3093 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3094 * where the function values are equal.
3096 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3097 __isl_take isl_pw_aff *pa2)
3099 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3102 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3103 * where the function value of "pa1" is less than or equal to
3104 * the function value of "pa2".
3106 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3107 __isl_take isl_pw_aff *pa2)
3109 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3112 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3113 * where the function value of "pa1" is less than the function value of "pa2".
3115 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3116 __isl_take isl_pw_aff *pa2)
3118 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3121 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3122 * where the function value of "pa1" is greater than or equal to
3123 * the function value of "pa2".
3125 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3126 __isl_take isl_pw_aff *pa2)
3128 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3131 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3132 * where the function value of "pa1" is greater than the function value
3133 * of "pa2".
3135 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3136 __isl_take isl_pw_aff *pa2)
3138 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3141 /* Return a set containing those elements in the shared domain
3142 * of the elements of list1 and list2 where each element in list1
3143 * has the relation specified by "fn" with each element in list2.
3145 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3146 __isl_take isl_pw_aff_list *list2,
3147 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3148 __isl_take isl_pw_aff *pwaff2))
3150 int i, j;
3151 isl_ctx *ctx;
3152 isl_set *set;
3154 if (!list1 || !list2)
3155 goto error;
3157 ctx = isl_pw_aff_list_get_ctx(list1);
3158 if (list1->n < 1 || list2->n < 1)
3159 isl_die(ctx, isl_error_invalid,
3160 "list should contain at least one element", goto error);
3162 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3163 for (i = 0; i < list1->n; ++i)
3164 for (j = 0; j < list2->n; ++j) {
3165 isl_set *set_ij;
3167 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3168 isl_pw_aff_copy(list2->p[j]));
3169 set = isl_set_intersect(set, set_ij);
3172 isl_pw_aff_list_free(list1);
3173 isl_pw_aff_list_free(list2);
3174 return set;
3175 error:
3176 isl_pw_aff_list_free(list1);
3177 isl_pw_aff_list_free(list2);
3178 return NULL;
3181 /* Return a set containing those elements in the shared domain
3182 * of the elements of list1 and list2 where each element in list1
3183 * is equal to each element in list2.
3185 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3186 __isl_take isl_pw_aff_list *list2)
3188 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3191 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3192 __isl_take isl_pw_aff_list *list2)
3194 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3197 /* Return a set containing those elements in the shared domain
3198 * of the elements of list1 and list2 where each element in list1
3199 * is less than or equal to each element in list2.
3201 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3202 __isl_take isl_pw_aff_list *list2)
3204 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3207 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3208 __isl_take isl_pw_aff_list *list2)
3210 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3213 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3214 __isl_take isl_pw_aff_list *list2)
3216 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3219 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3220 __isl_take isl_pw_aff_list *list2)
3222 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3226 /* Return a set containing those elements in the shared domain
3227 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3229 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3230 __isl_take isl_pw_aff *pwaff2)
3232 isl_set *set_lt, *set_gt;
3234 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3235 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3236 isl_pw_aff_copy(pwaff2));
3237 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3238 return isl_set_union_disjoint(set_lt, set_gt);
3241 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3242 isl_int v)
3244 int i;
3246 if (isl_int_is_one(v))
3247 return pwaff;
3248 if (!isl_int_is_pos(v))
3249 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3250 "factor needs to be positive",
3251 return isl_pw_aff_free(pwaff));
3252 pwaff = isl_pw_aff_cow(pwaff);
3253 if (!pwaff)
3254 return NULL;
3255 if (pwaff->n == 0)
3256 return pwaff;
3258 for (i = 0; i < pwaff->n; ++i) {
3259 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3260 if (!pwaff->p[i].aff)
3261 return isl_pw_aff_free(pwaff);
3264 return pwaff;
3267 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3269 int i;
3271 pwaff = isl_pw_aff_cow(pwaff);
3272 if (!pwaff)
3273 return NULL;
3274 if (pwaff->n == 0)
3275 return pwaff;
3277 for (i = 0; i < pwaff->n; ++i) {
3278 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3279 if (!pwaff->p[i].aff)
3280 return isl_pw_aff_free(pwaff);
3283 return pwaff;
3286 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3288 int i;
3290 pwaff = isl_pw_aff_cow(pwaff);
3291 if (!pwaff)
3292 return NULL;
3293 if (pwaff->n == 0)
3294 return pwaff;
3296 for (i = 0; i < pwaff->n; ++i) {
3297 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3298 if (!pwaff->p[i].aff)
3299 return isl_pw_aff_free(pwaff);
3302 return pwaff;
3305 /* Assuming that "cond1" and "cond2" are disjoint,
3306 * return an affine expression that is equal to pwaff1 on cond1
3307 * and to pwaff2 on cond2.
3309 static __isl_give isl_pw_aff *isl_pw_aff_select(
3310 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3311 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3313 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3314 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3316 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3319 /* Return an affine expression that is equal to pwaff_true for elements
3320 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3321 * is zero.
3322 * That is, return cond ? pwaff_true : pwaff_false;
3324 * If "cond" involves and NaN, then we conservatively return a NaN
3325 * on its entire domain. In principle, we could consider the pieces
3326 * where it is NaN separately from those where it is not.
3328 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3329 * then only use the domain of "cond" to restrict the domain.
3331 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3332 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3334 isl_set *cond_true, *cond_false;
3335 isl_bool equal;
3337 if (!cond)
3338 goto error;
3339 if (isl_pw_aff_involves_nan(cond)) {
3340 isl_space *space = isl_pw_aff_get_domain_space(cond);
3341 isl_local_space *ls = isl_local_space_from_space(space);
3342 isl_pw_aff_free(cond);
3343 isl_pw_aff_free(pwaff_true);
3344 isl_pw_aff_free(pwaff_false);
3345 return isl_pw_aff_nan_on_domain(ls);
3348 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3349 isl_pw_aff_get_space(pwaff_false));
3350 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3351 isl_pw_aff_get_space(pwaff_true));
3352 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3353 if (equal < 0)
3354 goto error;
3355 if (equal) {
3356 isl_set *dom;
3358 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3359 isl_pw_aff_free(pwaff_false);
3360 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3363 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3364 cond_false = isl_pw_aff_zero_set(cond);
3365 return isl_pw_aff_select(cond_true, pwaff_true,
3366 cond_false, pwaff_false);
3367 error:
3368 isl_pw_aff_free(cond);
3369 isl_pw_aff_free(pwaff_true);
3370 isl_pw_aff_free(pwaff_false);
3371 return NULL;
3374 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3376 int pos;
3378 if (!aff)
3379 return isl_bool_error;
3381 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3382 return isl_bool_ok(pos == -1);
3385 /* Check whether pwaff is a piecewise constant.
3387 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3389 int i;
3391 if (!pwaff)
3392 return isl_bool_error;
3394 for (i = 0; i < pwaff->n; ++i) {
3395 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3396 if (is_cst < 0 || !is_cst)
3397 return is_cst;
3400 return isl_bool_true;
3403 /* Return the product of "aff1" and "aff2".
3405 * If either of the two is NaN, then the result is NaN.
3407 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3409 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3410 __isl_take isl_aff *aff2)
3412 if (!aff1 || !aff2)
3413 goto error;
3415 if (isl_aff_is_nan(aff1)) {
3416 isl_aff_free(aff2);
3417 return aff1;
3419 if (isl_aff_is_nan(aff2)) {
3420 isl_aff_free(aff1);
3421 return aff2;
3424 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3425 return isl_aff_mul(aff2, aff1);
3427 if (!isl_aff_is_cst(aff2))
3428 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3429 "at least one affine expression should be constant",
3430 goto error);
3432 aff1 = isl_aff_cow(aff1);
3433 if (!aff1 || !aff2)
3434 goto error;
3436 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3437 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3439 isl_aff_free(aff2);
3440 return aff1;
3441 error:
3442 isl_aff_free(aff1);
3443 isl_aff_free(aff2);
3444 return NULL;
3447 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3449 * If either of the two is NaN, then the result is NaN.
3450 * A division by zero also results in NaN.
3452 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3453 __isl_take isl_aff *aff2)
3455 isl_bool is_cst, is_zero;
3456 int neg;
3458 if (!aff1 || !aff2)
3459 goto error;
3461 if (isl_aff_is_nan(aff1)) {
3462 isl_aff_free(aff2);
3463 return aff1;
3465 if (isl_aff_is_nan(aff2)) {
3466 isl_aff_free(aff1);
3467 return aff2;
3470 is_cst = isl_aff_is_cst(aff2);
3471 if (is_cst < 0)
3472 goto error;
3473 if (!is_cst)
3474 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3475 "second argument should be a constant", goto error);
3476 is_zero = isl_aff_plain_is_zero(aff2);
3477 if (is_zero < 0)
3478 goto error;
3479 if (is_zero)
3480 return set_nan_free(aff1, aff2);
3482 neg = isl_int_is_neg(aff2->v->el[1]);
3483 if (neg) {
3484 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3485 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3488 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3489 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3491 if (neg) {
3492 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3493 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3496 isl_aff_free(aff2);
3497 return aff1;
3498 error:
3499 isl_aff_free(aff1);
3500 isl_aff_free(aff2);
3501 return NULL;
3504 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3505 __isl_take isl_pw_aff *pwaff2)
3507 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3508 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3511 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3512 __isl_take isl_pw_aff *pwaff2)
3514 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3517 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3518 __isl_take isl_pw_aff *pwaff2)
3520 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3521 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3524 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3526 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3527 __isl_take isl_pw_aff *pa2)
3529 int is_cst;
3531 is_cst = isl_pw_aff_is_cst(pa2);
3532 if (is_cst < 0)
3533 goto error;
3534 if (!is_cst)
3535 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3536 "second argument should be a piecewise constant",
3537 goto error);
3538 isl_pw_aff_align_params_bin(&pa1, &pa2);
3539 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3540 error:
3541 isl_pw_aff_free(pa1);
3542 isl_pw_aff_free(pa2);
3543 return NULL;
3546 /* Compute the quotient of the integer division of "pa1" by "pa2"
3547 * with rounding towards zero.
3548 * "pa2" is assumed to be a piecewise constant.
3550 * In particular, return
3552 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3555 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3556 __isl_take isl_pw_aff *pa2)
3558 int is_cst;
3559 isl_set *cond;
3560 isl_pw_aff *f, *c;
3562 is_cst = isl_pw_aff_is_cst(pa2);
3563 if (is_cst < 0)
3564 goto error;
3565 if (!is_cst)
3566 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3567 "second argument should be a piecewise constant",
3568 goto error);
3570 pa1 = isl_pw_aff_div(pa1, pa2);
3572 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3573 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3574 c = isl_pw_aff_ceil(pa1);
3575 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3576 error:
3577 isl_pw_aff_free(pa1);
3578 isl_pw_aff_free(pa2);
3579 return NULL;
3582 /* Compute the remainder of the integer division of "pa1" by "pa2"
3583 * with rounding towards zero.
3584 * "pa2" is assumed to be a piecewise constant.
3586 * In particular, return
3588 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3591 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3592 __isl_take isl_pw_aff *pa2)
3594 int is_cst;
3595 isl_pw_aff *res;
3597 is_cst = isl_pw_aff_is_cst(pa2);
3598 if (is_cst < 0)
3599 goto error;
3600 if (!is_cst)
3601 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3602 "second argument should be a piecewise constant",
3603 goto error);
3604 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3605 res = isl_pw_aff_mul(pa2, res);
3606 res = isl_pw_aff_sub(pa1, res);
3607 return res;
3608 error:
3609 isl_pw_aff_free(pa1);
3610 isl_pw_aff_free(pa2);
3611 return NULL;
3614 /* Does either of "pa1" or "pa2" involve any NaN2?
3616 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3617 __isl_keep isl_pw_aff *pa2)
3619 isl_bool has_nan;
3621 has_nan = isl_pw_aff_involves_nan(pa1);
3622 if (has_nan < 0 || has_nan)
3623 return has_nan;
3624 return isl_pw_aff_involves_nan(pa2);
3627 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3628 * by a NaN on their shared domain.
3630 * In principle, the result could be refined to only being NaN
3631 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3633 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3634 __isl_take isl_pw_aff *pa2)
3636 isl_local_space *ls;
3637 isl_set *dom;
3638 isl_pw_aff *pa;
3640 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3641 ls = isl_local_space_from_space(isl_set_get_space(dom));
3642 pa = isl_pw_aff_nan_on_domain(ls);
3643 pa = isl_pw_aff_intersect_domain(pa, dom);
3645 return pa;
3648 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3649 __isl_take isl_pw_aff *pwaff2)
3651 isl_set *le;
3652 isl_set *dom;
3654 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3655 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3656 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3657 isl_pw_aff_copy(pwaff2));
3658 dom = isl_set_subtract(dom, isl_set_copy(le));
3659 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3662 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3663 __isl_take isl_pw_aff *pwaff2)
3665 isl_set *ge;
3666 isl_set *dom;
3668 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3669 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3670 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3671 isl_pw_aff_copy(pwaff2));
3672 dom = isl_set_subtract(dom, isl_set_copy(ge));
3673 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3676 /* Return an expression for the minimum (if "max" is not set) or
3677 * the maximum (if "max" is set) of "pa1" and "pa2".
3678 * If either expression involves any NaN, then return a NaN
3679 * on the shared domain as result.
3681 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3682 __isl_take isl_pw_aff *pa2, int max)
3684 isl_bool has_nan;
3686 has_nan = either_involves_nan(pa1, pa2);
3687 if (has_nan < 0)
3688 pa1 = isl_pw_aff_free(pa1);
3689 else if (has_nan)
3690 return replace_by_nan(pa1, pa2);
3692 isl_pw_aff_align_params_bin(&pa1, &pa2);
3693 if (max)
3694 return pw_aff_max(pa1, pa2);
3695 else
3696 return pw_aff_min(pa1, pa2);
3699 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3701 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3702 __isl_take isl_pw_aff *pwaff2)
3704 return pw_aff_min_max(pwaff1, pwaff2, 0);
3707 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3709 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3710 __isl_take isl_pw_aff *pwaff2)
3712 return pw_aff_min_max(pwaff1, pwaff2, 1);
3715 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3716 __isl_take isl_pw_aff_list *list,
3717 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3718 __isl_take isl_pw_aff *pwaff2))
3720 int i;
3721 isl_ctx *ctx;
3722 isl_pw_aff *res;
3724 if (!list)
3725 return NULL;
3727 ctx = isl_pw_aff_list_get_ctx(list);
3728 if (list->n < 1)
3729 isl_die(ctx, isl_error_invalid,
3730 "list should contain at least one element", goto error);
3732 res = isl_pw_aff_copy(list->p[0]);
3733 for (i = 1; i < list->n; ++i)
3734 res = fn(res, isl_pw_aff_copy(list->p[i]));
3736 isl_pw_aff_list_free(list);
3737 return res;
3738 error:
3739 isl_pw_aff_list_free(list);
3740 return NULL;
3743 /* Return an isl_pw_aff that maps each element in the intersection of the
3744 * domains of the elements of list to the minimal corresponding affine
3745 * expression.
3747 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3749 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3752 /* Return an isl_pw_aff that maps each element in the intersection of the
3753 * domains of the elements of list to the maximal corresponding affine
3754 * expression.
3756 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3758 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3761 /* Mark the domains of "pwaff" as rational.
3763 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3765 int i;
3767 pwaff = isl_pw_aff_cow(pwaff);
3768 if (!pwaff)
3769 return NULL;
3770 if (pwaff->n == 0)
3771 return pwaff;
3773 for (i = 0; i < pwaff->n; ++i) {
3774 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3775 if (!pwaff->p[i].set)
3776 return isl_pw_aff_free(pwaff);
3779 return pwaff;
3782 /* Mark the domains of the elements of "list" as rational.
3784 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3785 __isl_take isl_pw_aff_list *list)
3787 int i, n;
3789 if (!list)
3790 return NULL;
3791 if (list->n == 0)
3792 return list;
3794 n = list->n;
3795 for (i = 0; i < n; ++i) {
3796 isl_pw_aff *pa;
3798 pa = isl_pw_aff_list_get_pw_aff(list, i);
3799 pa = isl_pw_aff_set_rational(pa);
3800 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3803 return list;
3806 /* Do the parameters of "aff" match those of "space"?
3808 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3809 __isl_keep isl_space *space)
3811 isl_space *aff_space;
3812 isl_bool match;
3814 if (!aff || !space)
3815 return isl_bool_error;
3817 aff_space = isl_aff_get_domain_space(aff);
3819 match = isl_space_has_equal_params(space, aff_space);
3821 isl_space_free(aff_space);
3822 return match;
3825 /* Check that the domain space of "aff" matches "space".
3827 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3828 __isl_keep isl_space *space)
3830 isl_space *aff_space;
3831 isl_bool match;
3833 if (!aff || !space)
3834 return isl_stat_error;
3836 aff_space = isl_aff_get_domain_space(aff);
3838 match = isl_space_has_equal_params(space, aff_space);
3839 if (match < 0)
3840 goto error;
3841 if (!match)
3842 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3843 "parameters don't match", goto error);
3844 match = isl_space_tuple_is_equal(space, isl_dim_in,
3845 aff_space, isl_dim_set);
3846 if (match < 0)
3847 goto error;
3848 if (!match)
3849 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3850 "domains don't match", goto error);
3851 isl_space_free(aff_space);
3852 return isl_stat_ok;
3853 error:
3854 isl_space_free(aff_space);
3855 return isl_stat_error;
3858 /* Return the shared (universe) domain of the elements of "ma".
3860 * Since an isl_multi_aff (and an isl_aff) is always total,
3861 * the domain is always the universe set in its domain space.
3862 * This is a helper function for use in the generic isl_multi_*_bind.
3864 static __isl_give isl_basic_set *isl_multi_aff_domain(
3865 __isl_take isl_multi_aff *ma)
3867 isl_space *space;
3869 space = isl_multi_aff_get_space(ma);
3870 isl_multi_aff_free(ma);
3872 return isl_basic_set_universe(isl_space_domain(space));
3875 #undef BASE
3876 #define BASE aff
3878 #include <isl_multi_no_explicit_domain.c>
3879 #include <isl_multi_templ.c>
3880 #include <isl_multi_add_constant_templ.c>
3881 #include <isl_multi_apply_set.c>
3882 #include <isl_multi_arith_templ.c>
3883 #include <isl_multi_bind_domain_templ.c>
3884 #include <isl_multi_cmp.c>
3885 #include <isl_multi_dim_id_templ.c>
3886 #include <isl_multi_dims.c>
3887 #include <isl_multi_floor.c>
3888 #include <isl_multi_from_base_templ.c>
3889 #include <isl_multi_identity_templ.c>
3890 #include <isl_multi_insert_domain_templ.c>
3891 #include <isl_multi_locals_templ.c>
3892 #include <isl_multi_move_dims_templ.c>
3893 #include <isl_multi_nan_templ.c>
3894 #include <isl_multi_product_templ.c>
3895 #include <isl_multi_splice_templ.c>
3896 #include <isl_multi_tuple_id_templ.c>
3897 #include <isl_multi_unbind_params_templ.c>
3898 #include <isl_multi_zero_templ.c>
3900 #undef DOMBASE
3901 #define DOMBASE set
3902 #include <isl_multi_gist.c>
3904 #undef DOMBASE
3905 #define DOMBASE basic_set
3906 #include <isl_multi_bind_templ.c>
3908 /* Construct an isl_multi_aff living in "space" that corresponds
3909 * to the affine transformation matrix "mat".
3911 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3912 __isl_take isl_space *space, __isl_take isl_mat *mat)
3914 isl_ctx *ctx;
3915 isl_local_space *ls = NULL;
3916 isl_multi_aff *ma = NULL;
3917 isl_size n_row, n_col, n_out, total;
3918 int i;
3920 if (!space || !mat)
3921 goto error;
3923 ctx = isl_mat_get_ctx(mat);
3925 n_row = isl_mat_rows(mat);
3926 n_col = isl_mat_cols(mat);
3927 n_out = isl_space_dim(space, isl_dim_out);
3928 total = isl_space_dim(space, isl_dim_all);
3929 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3930 goto error;
3931 if (n_row < 1)
3932 isl_die(ctx, isl_error_invalid,
3933 "insufficient number of rows", goto error);
3934 if (n_col < 1)
3935 isl_die(ctx, isl_error_invalid,
3936 "insufficient number of columns", goto error);
3937 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3938 isl_die(ctx, isl_error_invalid,
3939 "dimension mismatch", goto error);
3941 ma = isl_multi_aff_zero(isl_space_copy(space));
3942 space = isl_space_domain(space);
3943 ls = isl_local_space_from_space(isl_space_copy(space));
3945 for (i = 0; i < n_row - 1; ++i) {
3946 isl_vec *v;
3947 isl_aff *aff;
3949 v = isl_vec_alloc(ctx, 1 + n_col);
3950 if (!v)
3951 goto error;
3952 isl_int_set(v->el[0], mat->row[0][0]);
3953 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3954 v = isl_vec_normalize(v);
3955 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3956 ma = isl_multi_aff_set_aff(ma, i, aff);
3959 isl_space_free(space);
3960 isl_local_space_free(ls);
3961 isl_mat_free(mat);
3962 return ma;
3963 error:
3964 isl_space_free(space);
3965 isl_local_space_free(ls);
3966 isl_mat_free(mat);
3967 isl_multi_aff_free(ma);
3968 return NULL;
3971 /* Return the constant terms of the affine expressions of "ma".
3973 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3974 __isl_keep isl_multi_aff *ma)
3976 int i;
3977 isl_size n;
3978 isl_space *space;
3979 isl_multi_val *mv;
3981 n = isl_multi_aff_size(ma);
3982 if (n < 0)
3983 return NULL;
3984 space = isl_space_range(isl_multi_aff_get_space(ma));
3985 space = isl_space_drop_all_params(space);
3986 mv = isl_multi_val_zero(space);
3988 for (i = 0; i < n; ++i) {
3989 isl_aff *aff;
3990 isl_val *val;
3992 aff = isl_multi_aff_get_at(ma, i);
3993 val = isl_aff_get_constant_val(aff);
3994 isl_aff_free(aff);
3995 mv = isl_multi_val_set_at(mv, i, val);
3998 return mv;
4001 /* Remove any internal structure of the domain of "ma".
4002 * If there is any such internal structure in the input,
4003 * then the name of the corresponding space is also removed.
4005 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4006 __isl_take isl_multi_aff *ma)
4008 isl_space *space;
4010 if (!ma)
4011 return NULL;
4013 if (!ma->space->nested[0])
4014 return ma;
4016 space = isl_multi_aff_get_space(ma);
4017 space = isl_space_flatten_domain(space);
4018 ma = isl_multi_aff_reset_space(ma, space);
4020 return ma;
4023 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4024 * of the space to its domain.
4026 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4028 int i;
4029 isl_size n_in;
4030 isl_local_space *ls;
4031 isl_multi_aff *ma;
4033 if (!space)
4034 return NULL;
4035 if (!isl_space_is_map(space))
4036 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4037 "not a map space", goto error);
4039 n_in = isl_space_dim(space, isl_dim_in);
4040 if (n_in < 0)
4041 goto error;
4042 space = isl_space_domain_map(space);
4044 ma = isl_multi_aff_alloc(isl_space_copy(space));
4045 if (n_in == 0) {
4046 isl_space_free(space);
4047 return ma;
4050 space = isl_space_domain(space);
4051 ls = isl_local_space_from_space(space);
4052 for (i = 0; i < n_in; ++i) {
4053 isl_aff *aff;
4055 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4056 isl_dim_set, i);
4057 ma = isl_multi_aff_set_aff(ma, i, aff);
4059 isl_local_space_free(ls);
4060 return ma;
4061 error:
4062 isl_space_free(space);
4063 return NULL;
4066 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4067 * of the space to its range.
4069 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4071 int i;
4072 isl_size n_in, n_out;
4073 isl_local_space *ls;
4074 isl_multi_aff *ma;
4076 if (!space)
4077 return NULL;
4078 if (!isl_space_is_map(space))
4079 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4080 "not a map space", goto error);
4082 n_in = isl_space_dim(space, isl_dim_in);
4083 n_out = isl_space_dim(space, isl_dim_out);
4084 if (n_in < 0 || n_out < 0)
4085 goto error;
4086 space = isl_space_range_map(space);
4088 ma = isl_multi_aff_alloc(isl_space_copy(space));
4089 if (n_out == 0) {
4090 isl_space_free(space);
4091 return ma;
4094 space = isl_space_domain(space);
4095 ls = isl_local_space_from_space(space);
4096 for (i = 0; i < n_out; ++i) {
4097 isl_aff *aff;
4099 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4100 isl_dim_set, n_in + i);
4101 ma = isl_multi_aff_set_aff(ma, i, aff);
4103 isl_local_space_free(ls);
4104 return ma;
4105 error:
4106 isl_space_free(space);
4107 return NULL;
4110 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4111 * of the space to its domain.
4113 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4114 __isl_take isl_space *space)
4116 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4119 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4120 * of the space to its range.
4122 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4123 __isl_take isl_space *space)
4125 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4128 /* Given the space of a set and a range of set dimensions,
4129 * construct an isl_multi_aff that projects out those dimensions.
4131 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4132 __isl_take isl_space *space, enum isl_dim_type type,
4133 unsigned first, unsigned n)
4135 int i;
4136 isl_size dim;
4137 isl_local_space *ls;
4138 isl_multi_aff *ma;
4140 if (!space)
4141 return NULL;
4142 if (!isl_space_is_set(space))
4143 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4144 "expecting set space", goto error);
4145 if (type != isl_dim_set)
4146 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4147 "only set dimensions can be projected out", goto error);
4148 if (isl_space_check_range(space, type, first, n) < 0)
4149 goto error;
4151 dim = isl_space_dim(space, isl_dim_set);
4152 if (dim < 0)
4153 goto error;
4155 space = isl_space_from_domain(space);
4156 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4158 if (dim == n)
4159 return isl_multi_aff_alloc(space);
4161 ma = isl_multi_aff_alloc(isl_space_copy(space));
4162 space = isl_space_domain(space);
4163 ls = isl_local_space_from_space(space);
4165 for (i = 0; i < first; ++i) {
4166 isl_aff *aff;
4168 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4169 isl_dim_set, i);
4170 ma = isl_multi_aff_set_aff(ma, i, aff);
4173 for (i = 0; i < dim - (first + n); ++i) {
4174 isl_aff *aff;
4176 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4177 isl_dim_set, first + n + i);
4178 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4181 isl_local_space_free(ls);
4182 return ma;
4183 error:
4184 isl_space_free(space);
4185 return NULL;
4188 /* Given the space of a set and a range of set dimensions,
4189 * construct an isl_pw_multi_aff that projects out those dimensions.
4191 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4192 __isl_take isl_space *space, enum isl_dim_type type,
4193 unsigned first, unsigned n)
4195 isl_multi_aff *ma;
4197 ma = isl_multi_aff_project_out_map(space, type, first, n);
4198 return isl_pw_multi_aff_from_multi_aff(ma);
4201 /* Create a piecewise multi-affine expression in the given space that maps each
4202 * input dimension to the corresponding output dimension.
4204 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4205 __isl_take isl_space *space)
4207 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4210 /* Exploit the equalities in "eq" to simplify the affine expressions.
4212 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4213 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4215 int i;
4217 maff = isl_multi_aff_cow(maff);
4218 if (!maff || !eq)
4219 goto error;
4221 for (i = 0; i < maff->n; ++i) {
4222 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4223 isl_basic_set_copy(eq));
4224 if (!maff->u.p[i])
4225 goto error;
4228 isl_basic_set_free(eq);
4229 return maff;
4230 error:
4231 isl_basic_set_free(eq);
4232 isl_multi_aff_free(maff);
4233 return NULL;
4236 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4237 isl_int f)
4239 int i;
4241 maff = isl_multi_aff_cow(maff);
4242 if (!maff)
4243 return NULL;
4245 for (i = 0; i < maff->n; ++i) {
4246 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4247 if (!maff->u.p[i])
4248 return isl_multi_aff_free(maff);
4251 return maff;
4254 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4255 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4257 maff1 = isl_multi_aff_add(maff1, maff2);
4258 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4259 return maff1;
4262 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4264 if (!maff)
4265 return isl_bool_error;
4267 return isl_bool_false;
4270 /* Return the set of domain elements where "ma1" is lexicographically
4271 * smaller than or equal to "ma2".
4273 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4274 __isl_take isl_multi_aff *ma2)
4276 return isl_multi_aff_lex_ge_set(ma2, ma1);
4279 /* Return the set of domain elements where "ma1" is lexicographically
4280 * smaller than "ma2".
4282 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4283 __isl_take isl_multi_aff *ma2)
4285 return isl_multi_aff_lex_gt_set(ma2, ma1);
4288 /* Return the set of domain elements where "ma1" is lexicographically
4289 * greater than to "ma2". If "equal" is set, then include the domain
4290 * elements where they are equal.
4291 * Do this for the case where there are no entries.
4292 * In this case, "ma1" cannot be greater than "ma2",
4293 * but it is (greater than or) equal to "ma2".
4295 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4296 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4298 isl_space *space;
4300 space = isl_multi_aff_get_domain_space(ma1);
4302 isl_multi_aff_free(ma1);
4303 isl_multi_aff_free(ma2);
4305 if (equal)
4306 return isl_set_universe(space);
4307 else
4308 return isl_set_empty(space);
4311 /* Return the set where entry "i" of "ma1" and "ma2"
4312 * satisfy the relation prescribed by "cmp".
4314 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4315 __isl_keep isl_multi_aff *ma2, int i,
4316 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4317 __isl_take isl_aff *aff2))
4319 isl_aff *aff1, *aff2;
4321 aff1 = isl_multi_aff_get_at(ma1, i);
4322 aff2 = isl_multi_aff_get_at(ma2, i);
4323 return cmp(aff1, aff2);
4326 /* Return the set of domain elements where "ma1" is lexicographically
4327 * greater than to "ma2". If "equal" is set, then include the domain
4328 * elements where they are equal.
4330 * In particular, for all but the final entry,
4331 * include the set of elements where this entry is strictly greater in "ma1"
4332 * and all previous entries are equal.
4333 * The final entry is also allowed to be equal in the two functions
4334 * if "equal" is set.
4336 * The case where there are no entries is handled separately.
4338 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4339 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4341 int i;
4342 isl_size n;
4343 isl_space *space;
4344 isl_set *res;
4345 isl_set *equal_set;
4346 isl_set *gte;
4348 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4349 goto error;
4350 n = isl_multi_aff_size(ma1);
4351 if (n < 0)
4352 goto error;
4353 if (n == 0)
4354 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4356 space = isl_multi_aff_get_domain_space(ma1);
4357 res = isl_set_empty(isl_space_copy(space));
4358 equal_set = isl_set_universe(space);
4360 for (i = 0; i + 1 < n; ++i) {
4361 isl_bool empty;
4362 isl_set *gt, *eq;
4364 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4365 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4366 res = isl_set_union(res, gt);
4367 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4368 equal_set = isl_set_intersect(equal_set, eq);
4370 empty = isl_set_is_empty(equal_set);
4371 if (empty >= 0 && empty)
4372 break;
4375 if (equal)
4376 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4377 else
4378 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4379 isl_multi_aff_free(ma1);
4380 isl_multi_aff_free(ma2);
4382 gte = isl_set_intersect(gte, equal_set);
4383 return isl_set_union(res, gte);
4384 error:
4385 isl_multi_aff_free(ma1);
4386 isl_multi_aff_free(ma2);
4387 return NULL;
4390 /* Return the set of domain elements where "ma1" is lexicographically
4391 * greater than or equal to "ma2".
4393 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4394 __isl_take isl_multi_aff *ma2)
4396 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4399 /* Return the set of domain elements where "ma1" is lexicographically
4400 * greater than "ma2".
4402 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4403 __isl_take isl_multi_aff *ma2)
4405 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4408 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4410 #undef PW
4411 #define PW isl_pw_multi_aff
4412 #undef BASE
4413 #define BASE multi_aff
4414 #undef EL_IS_ZERO
4415 #define EL_IS_ZERO is_empty
4416 #undef ZERO
4417 #define ZERO empty
4418 #undef IS_ZERO
4419 #define IS_ZERO is_empty
4420 #undef FIELD
4421 #define FIELD maff
4422 #undef DEFAULT_IS_ZERO
4423 #define DEFAULT_IS_ZERO 0
4425 #include <isl_pw_templ.c>
4426 #include <isl_pw_add_constant_multi_val_templ.c>
4427 #include <isl_pw_add_constant_val_templ.c>
4428 #include <isl_pw_bind_domain_templ.c>
4429 #include <isl_pw_insert_dims_templ.c>
4430 #include <isl_pw_insert_domain_templ.c>
4431 #include <isl_pw_locals_templ.c>
4432 #include <isl_pw_move_dims_templ.c>
4433 #include <isl_pw_neg_templ.c>
4434 #include <isl_pw_pullback_templ.c>
4435 #include <isl_pw_union_opt.c>
4437 #undef BASE
4438 #define BASE pw_multi_aff
4440 #include <isl_union_multi.c>
4441 #include "isl_union_locals_templ.c"
4442 #include <isl_union_neg.c>
4444 #undef BASE
4445 #define BASE multi_aff
4447 #include <isl_union_pw_templ.c>
4449 /* Generic function for extracting a factor from a product "pma".
4450 * "check_space" checks that the space is that of the right kind of product.
4451 * "space_factor" extracts the factor from the space.
4452 * "multi_aff_factor" extracts the factor from the constituent functions.
4454 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4455 __isl_take isl_pw_multi_aff *pma,
4456 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4457 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4458 __isl_give isl_multi_aff *(*multi_aff_factor)(
4459 __isl_take isl_multi_aff *ma))
4461 int i;
4462 isl_space *space;
4464 if (check_space(pma) < 0)
4465 return isl_pw_multi_aff_free(pma);
4467 space = isl_pw_multi_aff_take_space(pma);
4468 space = space_factor(space);
4470 for (i = 0; pma && i < pma->n; ++i) {
4471 isl_multi_aff *ma;
4473 ma = isl_pw_multi_aff_take_base_at(pma, i);
4474 ma = multi_aff_factor(ma);
4475 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4478 pma = isl_pw_multi_aff_restore_space(pma, space);
4480 return pma;
4483 /* Is the range of "pma" a wrapped relation?
4485 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4486 __isl_keep isl_pw_multi_aff *pma)
4488 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4491 /* Check that the range of "pma" is a product.
4493 static isl_stat pw_multi_aff_check_range_product(
4494 __isl_keep isl_pw_multi_aff *pma)
4496 isl_bool wraps;
4498 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4499 if (wraps < 0)
4500 return isl_stat_error;
4501 if (!wraps)
4502 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4503 "range is not a product", return isl_stat_error);
4504 return isl_stat_ok;
4507 /* Given a function A -> [B -> C], extract the function A -> B.
4509 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4510 __isl_take isl_pw_multi_aff *pma)
4512 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4513 &isl_space_range_factor_domain,
4514 &isl_multi_aff_range_factor_domain);
4517 /* Given a function A -> [B -> C], extract the function A -> C.
4519 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4520 __isl_take isl_pw_multi_aff *pma)
4522 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4523 &isl_space_range_factor_range,
4524 &isl_multi_aff_range_factor_range);
4527 /* Given two piecewise multi affine expressions, return a piecewise
4528 * multi-affine expression defined on the union of the definition domains
4529 * of the inputs that is equal to the lexicographic maximum of the two
4530 * inputs on each cell. If only one of the two inputs is defined on
4531 * a given cell, then it is considered to be the maximum.
4533 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4534 __isl_take isl_pw_multi_aff *pma1,
4535 __isl_take isl_pw_multi_aff *pma2)
4537 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4538 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4539 &isl_multi_aff_lex_ge_set);
4542 /* Given two piecewise multi affine expressions, return a piecewise
4543 * multi-affine expression defined on the union of the definition domains
4544 * of the inputs that is equal to the lexicographic minimum of the two
4545 * inputs on each cell. If only one of the two inputs is defined on
4546 * a given cell, then it is considered to be the minimum.
4548 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4549 __isl_take isl_pw_multi_aff *pma1,
4550 __isl_take isl_pw_multi_aff *pma2)
4552 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4553 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4554 &isl_multi_aff_lex_le_set);
4557 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4558 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4560 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4561 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4562 &isl_multi_aff_add);
4565 /* Subtract "pma2" from "pma1" and return the result.
4567 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4568 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4570 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4571 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4572 &isl_multi_aff_sub);
4575 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4576 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4578 return isl_pw_multi_aff_union_add_(pma1, pma2);
4581 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4582 * with the actual sum on the shared domain and
4583 * the defined expression on the symmetric difference of the domains.
4585 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4586 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4588 return isl_union_pw_aff_union_add_(upa1, upa2);
4591 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4592 * with the actual sum on the shared domain and
4593 * the defined expression on the symmetric difference of the domains.
4595 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4596 __isl_take isl_union_pw_multi_aff *upma1,
4597 __isl_take isl_union_pw_multi_aff *upma2)
4599 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4602 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4603 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4605 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4606 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4608 int i, j, n;
4609 isl_space *space;
4610 isl_pw_multi_aff *res;
4612 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4613 goto error;
4615 n = pma1->n * pma2->n;
4616 space = isl_space_product(isl_space_copy(pma1->dim),
4617 isl_space_copy(pma2->dim));
4618 res = isl_pw_multi_aff_alloc_size(space, n);
4620 for (i = 0; i < pma1->n; ++i) {
4621 for (j = 0; j < pma2->n; ++j) {
4622 isl_set *domain;
4623 isl_multi_aff *ma;
4625 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4626 isl_set_copy(pma2->p[j].set));
4627 ma = isl_multi_aff_product(
4628 isl_multi_aff_copy(pma1->p[i].maff),
4629 isl_multi_aff_copy(pma2->p[j].maff));
4630 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4634 isl_pw_multi_aff_free(pma1);
4635 isl_pw_multi_aff_free(pma2);
4636 return res;
4637 error:
4638 isl_pw_multi_aff_free(pma1);
4639 isl_pw_multi_aff_free(pma2);
4640 return NULL;
4643 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4644 * denominator "denom".
4645 * "denom" is allowed to be negative, in which case the actual denominator
4646 * is -denom and the expressions are added instead.
4648 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4649 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4651 int i, first;
4652 int sign;
4653 isl_int d;
4655 first = isl_seq_first_non_zero(c, n);
4656 if (first == -1)
4657 return aff;
4659 sign = isl_int_sgn(denom);
4660 isl_int_init(d);
4661 isl_int_abs(d, denom);
4662 for (i = first; i < n; ++i) {
4663 isl_aff *aff_i;
4665 if (isl_int_is_zero(c[i]))
4666 continue;
4667 aff_i = isl_multi_aff_get_aff(ma, i);
4668 aff_i = isl_aff_scale(aff_i, c[i]);
4669 aff_i = isl_aff_scale_down(aff_i, d);
4670 if (sign >= 0)
4671 aff = isl_aff_sub(aff, aff_i);
4672 else
4673 aff = isl_aff_add(aff, aff_i);
4675 isl_int_clear(d);
4677 return aff;
4680 /* Extract an affine expression that expresses the output dimension "pos"
4681 * of "bmap" in terms of the parameters and input dimensions from
4682 * equality "eq".
4683 * Note that this expression may involve integer divisions defined
4684 * in terms of parameters and input dimensions.
4685 * The equality may also involve references to earlier (but not later)
4686 * output dimensions. These are replaced by the corresponding elements
4687 * in "ma".
4689 * If the equality is of the form
4691 * f(i) + h(j) + a x + g(i) = 0,
4693 * with f(i) a linear combinations of the parameters and input dimensions,
4694 * g(i) a linear combination of integer divisions defined in terms of the same
4695 * and h(j) a linear combinations of earlier output dimensions,
4696 * then the affine expression is
4698 * (-f(i) - g(i))/a - h(j)/a
4700 * If the equality is of the form
4702 * f(i) + h(j) - a x + g(i) = 0,
4704 * then the affine expression is
4706 * (f(i) + g(i))/a - h(j)/(-a)
4709 * If "div" refers to an integer division (i.e., it is smaller than
4710 * the number of integer divisions), then the equality constraint
4711 * does involve an integer division (the one at position "div") that
4712 * is defined in terms of output dimensions. However, this integer
4713 * division can be eliminated by exploiting a pair of constraints
4714 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4715 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4716 * -l + x >= 0.
4717 * In particular, let
4719 * x = e(i) + m floor(...)
4721 * with e(i) the expression derived above and floor(...) the integer
4722 * division involving output dimensions.
4723 * From
4725 * l <= x <= l + n,
4727 * we have
4729 * 0 <= x - l <= n
4731 * This means
4733 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4734 * = (e(i) - l) mod m
4736 * Therefore,
4738 * x - l = (e(i) - l) mod m
4740 * or
4742 * x = ((e(i) - l) mod m) + l
4744 * The variable "shift" below contains the expression -l, which may
4745 * also involve a linear combination of earlier output dimensions.
4747 static __isl_give isl_aff *extract_aff_from_equality(
4748 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4749 __isl_keep isl_multi_aff *ma)
4751 unsigned o_out;
4752 isl_size n_div, n_out;
4753 isl_ctx *ctx;
4754 isl_local_space *ls;
4755 isl_aff *aff, *shift;
4756 isl_val *mod;
4758 ctx = isl_basic_map_get_ctx(bmap);
4759 ls = isl_basic_map_get_local_space(bmap);
4760 ls = isl_local_space_domain(ls);
4761 aff = isl_aff_alloc(isl_local_space_copy(ls));
4762 if (!aff)
4763 goto error;
4764 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4765 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4766 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4767 if (n_out < 0 || n_div < 0)
4768 goto error;
4769 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4770 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4771 isl_seq_cpy(aff->v->el + 1 + o_out,
4772 bmap->eq[eq] + o_out + n_out, n_div);
4773 } else {
4774 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4775 isl_seq_neg(aff->v->el + 1 + o_out,
4776 bmap->eq[eq] + o_out + n_out, n_div);
4778 if (div < n_div)
4779 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4780 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4781 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4782 bmap->eq[eq][o_out + pos]);
4783 if (div < n_div) {
4784 shift = isl_aff_alloc(isl_local_space_copy(ls));
4785 if (!shift)
4786 goto error;
4787 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4788 isl_seq_cpy(shift->v->el + 1 + o_out,
4789 bmap->ineq[ineq] + o_out + n_out, n_div);
4790 isl_int_set_si(shift->v->el[0], 1);
4791 shift = subtract_initial(shift, ma, pos,
4792 bmap->ineq[ineq] + o_out, ctx->negone);
4793 aff = isl_aff_add(aff, isl_aff_copy(shift));
4794 mod = isl_val_int_from_isl_int(ctx,
4795 bmap->eq[eq][o_out + n_out + div]);
4796 mod = isl_val_abs(mod);
4797 aff = isl_aff_mod_val(aff, mod);
4798 aff = isl_aff_sub(aff, shift);
4801 isl_local_space_free(ls);
4802 return aff;
4803 error:
4804 isl_local_space_free(ls);
4805 isl_aff_free(aff);
4806 return NULL;
4809 /* Given a basic map with output dimensions defined
4810 * in terms of the parameters input dimensions and earlier
4811 * output dimensions using an equality (and possibly a pair on inequalities),
4812 * extract an isl_aff that expresses output dimension "pos" in terms
4813 * of the parameters and input dimensions.
4814 * Note that this expression may involve integer divisions defined
4815 * in terms of parameters and input dimensions.
4816 * "ma" contains the expressions corresponding to earlier output dimensions.
4818 * This function shares some similarities with
4819 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4821 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4822 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4824 int eq, div, ineq;
4825 isl_aff *aff;
4827 if (!bmap)
4828 return NULL;
4829 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4830 if (eq >= bmap->n_eq)
4831 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4832 "unable to find suitable equality", return NULL);
4833 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4835 aff = isl_aff_remove_unused_divs(aff);
4836 return aff;
4839 /* Given a basic map where each output dimension is defined
4840 * in terms of the parameters and input dimensions using an equality,
4841 * extract an isl_multi_aff that expresses the output dimensions in terms
4842 * of the parameters and input dimensions.
4844 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4845 __isl_take isl_basic_map *bmap)
4847 int i;
4848 isl_size n_out;
4849 isl_multi_aff *ma;
4851 if (!bmap)
4852 return NULL;
4854 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4855 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4856 if (n_out < 0)
4857 ma = isl_multi_aff_free(ma);
4859 for (i = 0; i < n_out; ++i) {
4860 isl_aff *aff;
4862 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4863 ma = isl_multi_aff_set_aff(ma, i, aff);
4866 isl_basic_map_free(bmap);
4868 return ma;
4871 /* Given a basic set where each set dimension is defined
4872 * in terms of the parameters using an equality,
4873 * extract an isl_multi_aff that expresses the set dimensions in terms
4874 * of the parameters.
4876 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4877 __isl_take isl_basic_set *bset)
4879 return extract_isl_multi_aff_from_basic_map(bset);
4882 /* Create an isl_pw_multi_aff that is equivalent to
4883 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4884 * The given basic map is such that each output dimension is defined
4885 * in terms of the parameters and input dimensions using an equality.
4887 * Since some applications expect the result of isl_pw_multi_aff_from_map
4888 * to only contain integer affine expressions, we compute the floor
4889 * of the expression before returning.
4891 * Remove all constraints involving local variables without
4892 * an explicit representation (resulting in the removal of those
4893 * local variables) prior to the actual extraction to ensure
4894 * that the local spaces in which the resulting affine expressions
4895 * are created do not contain any unknown local variables.
4896 * Removing such constraints is safe because constraints involving
4897 * unknown local variables are not used to determine whether
4898 * a basic map is obviously single-valued.
4900 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4901 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4903 isl_multi_aff *ma;
4905 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4906 ma = extract_isl_multi_aff_from_basic_map(bmap);
4907 ma = isl_multi_aff_floor(ma);
4908 return isl_pw_multi_aff_alloc(domain, ma);
4911 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4912 * This obviously only works if the input "map" is single-valued.
4913 * If so, we compute the lexicographic minimum of the image in the form
4914 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4915 * to its lexicographic minimum.
4916 * If the input is not single-valued, we produce an error.
4918 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4919 __isl_take isl_map *map)
4921 int i;
4922 int sv;
4923 isl_pw_multi_aff *pma;
4925 sv = isl_map_is_single_valued(map);
4926 if (sv < 0)
4927 goto error;
4928 if (!sv)
4929 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4930 "map is not single-valued", goto error);
4931 map = isl_map_make_disjoint(map);
4932 if (!map)
4933 return NULL;
4935 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4937 for (i = 0; i < map->n; ++i) {
4938 isl_pw_multi_aff *pma_i;
4939 isl_basic_map *bmap;
4940 bmap = isl_basic_map_copy(map->p[i]);
4941 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4942 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4945 isl_map_free(map);
4946 return pma;
4947 error:
4948 isl_map_free(map);
4949 return NULL;
4952 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4953 * taking into account that the output dimension at position "d"
4954 * can be represented as
4956 * x = floor((e(...) + c1) / m)
4958 * given that constraint "i" is of the form
4960 * e(...) + c1 - m x >= 0
4963 * Let "map" be of the form
4965 * A -> B
4967 * We construct a mapping
4969 * A -> [A -> x = floor(...)]
4971 * apply that to the map, obtaining
4973 * [A -> x = floor(...)] -> B
4975 * and equate dimension "d" to x.
4976 * We then compute a isl_pw_multi_aff representation of the resulting map
4977 * and plug in the mapping above.
4979 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4980 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4982 isl_ctx *ctx;
4983 isl_space *space = NULL;
4984 isl_local_space *ls;
4985 isl_multi_aff *ma;
4986 isl_aff *aff;
4987 isl_vec *v;
4988 isl_map *insert;
4989 int offset;
4990 isl_size n;
4991 isl_size n_in;
4992 isl_pw_multi_aff *pma;
4993 isl_bool is_set;
4995 is_set = isl_map_is_set(map);
4996 if (is_set < 0)
4997 goto error;
4999 offset = isl_basic_map_offset(hull, isl_dim_out);
5000 ctx = isl_map_get_ctx(map);
5001 space = isl_space_domain(isl_map_get_space(map));
5002 n_in = isl_space_dim(space, isl_dim_set);
5003 n = isl_space_dim(space, isl_dim_all);
5004 if (n_in < 0 || n < 0)
5005 goto error;
5007 v = isl_vec_alloc(ctx, 1 + 1 + n);
5008 if (v) {
5009 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5010 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5012 isl_basic_map_free(hull);
5014 ls = isl_local_space_from_space(isl_space_copy(space));
5015 aff = isl_aff_alloc_vec(ls, v);
5016 aff = isl_aff_floor(aff);
5017 if (is_set) {
5018 isl_space_free(space);
5019 ma = isl_multi_aff_from_aff(aff);
5020 } else {
5021 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5022 ma = isl_multi_aff_range_product(ma,
5023 isl_multi_aff_from_aff(aff));
5026 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5027 map = isl_map_apply_domain(map, insert);
5028 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5029 pma = isl_pw_multi_aff_from_map(map);
5030 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5032 return pma;
5033 error:
5034 isl_space_free(space);
5035 isl_map_free(map);
5036 isl_basic_map_free(hull);
5037 return NULL;
5040 /* Is constraint "c" of the form
5042 * e(...) + c1 - m x >= 0
5044 * or
5046 * -e(...) + c2 + m x >= 0
5048 * where m > 1 and e only depends on parameters and input dimemnsions?
5050 * "offset" is the offset of the output dimensions
5051 * "pos" is the position of output dimension x.
5053 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5055 if (isl_int_is_zero(c[offset + d]))
5056 return 0;
5057 if (isl_int_is_one(c[offset + d]))
5058 return 0;
5059 if (isl_int_is_negone(c[offset + d]))
5060 return 0;
5061 if (isl_seq_first_non_zero(c + offset, d) != -1)
5062 return 0;
5063 if (isl_seq_first_non_zero(c + offset + d + 1,
5064 total - (offset + d + 1)) != -1)
5065 return 0;
5066 return 1;
5069 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5071 * As a special case, we first check if there is any pair of constraints,
5072 * shared by all the basic maps in "map" that force a given dimension
5073 * to be equal to the floor of some affine combination of the input dimensions.
5075 * In particular, if we can find two constraints
5077 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5079 * and
5081 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5083 * where m > 1 and e only depends on parameters and input dimemnsions,
5084 * and such that
5086 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5088 * then we know that we can take
5090 * x = floor((e(...) + c1) / m)
5092 * without having to perform any computation.
5094 * Note that we know that
5096 * c1 + c2 >= 1
5098 * If c1 + c2 were 0, then we would have detected an equality during
5099 * simplification. If c1 + c2 were negative, then we would have detected
5100 * a contradiction.
5102 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5103 __isl_take isl_map *map)
5105 int d;
5106 isl_size dim;
5107 int i, j, n;
5108 int offset;
5109 isl_size total;
5110 isl_int sum;
5111 isl_basic_map *hull;
5113 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5114 dim = isl_map_dim(map, isl_dim_out);
5115 total = isl_basic_map_dim(hull, isl_dim_all);
5116 if (dim < 0 || total < 0)
5117 goto error;
5119 isl_int_init(sum);
5120 offset = isl_basic_map_offset(hull, isl_dim_out);
5121 n = hull->n_ineq;
5122 for (d = 0; d < dim; ++d) {
5123 for (i = 0; i < n; ++i) {
5124 if (!is_potential_div_constraint(hull->ineq[i],
5125 offset, d, 1 + total))
5126 continue;
5127 for (j = i + 1; j < n; ++j) {
5128 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5129 hull->ineq[j] + 1, total))
5130 continue;
5131 isl_int_add(sum, hull->ineq[i][0],
5132 hull->ineq[j][0]);
5133 if (isl_int_abs_lt(sum,
5134 hull->ineq[i][offset + d]))
5135 break;
5138 if (j >= n)
5139 continue;
5140 isl_int_clear(sum);
5141 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5142 j = i;
5143 return pw_multi_aff_from_map_div(map, hull, d, j);
5146 isl_int_clear(sum);
5147 isl_basic_map_free(hull);
5148 return pw_multi_aff_from_map_base(map);
5149 error:
5150 isl_map_free(map);
5151 isl_basic_map_free(hull);
5152 return NULL;
5155 /* Given an affine expression
5157 * [A -> B] -> f(A,B)
5159 * construct an isl_multi_aff
5161 * [A -> B] -> B'
5163 * such that dimension "d" in B' is set to "aff" and the remaining
5164 * dimensions are set equal to the corresponding dimensions in B.
5165 * "n_in" is the dimension of the space A.
5166 * "n_out" is the dimension of the space B.
5168 * If "is_set" is set, then the affine expression is of the form
5170 * [B] -> f(B)
5172 * and we construct an isl_multi_aff
5174 * B -> B'
5176 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5177 unsigned n_in, unsigned n_out, int is_set)
5179 int i;
5180 isl_multi_aff *ma;
5181 isl_space *space, *space2;
5182 isl_local_space *ls;
5184 space = isl_aff_get_domain_space(aff);
5185 ls = isl_local_space_from_space(isl_space_copy(space));
5186 space2 = isl_space_copy(space);
5187 if (!is_set)
5188 space2 = isl_space_range(isl_space_unwrap(space2));
5189 space = isl_space_map_from_domain_and_range(space, space2);
5190 ma = isl_multi_aff_alloc(space);
5191 ma = isl_multi_aff_set_aff(ma, d, aff);
5193 for (i = 0; i < n_out; ++i) {
5194 if (i == d)
5195 continue;
5196 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5197 isl_dim_set, n_in + i);
5198 ma = isl_multi_aff_set_aff(ma, i, aff);
5201 isl_local_space_free(ls);
5203 return ma;
5206 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5207 * taking into account that the dimension at position "d" can be written as
5209 * x = m a + f(..) (1)
5211 * where m is equal to "gcd".
5212 * "i" is the index of the equality in "hull" that defines f(..).
5213 * In particular, the equality is of the form
5215 * f(..) - x + m g(existentials) = 0
5217 * or
5219 * -f(..) + x + m g(existentials) = 0
5221 * We basically plug (1) into "map", resulting in a map with "a"
5222 * in the range instead of "x". The corresponding isl_pw_multi_aff
5223 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5225 * Specifically, given the input map
5227 * A -> B
5229 * We first wrap it into a set
5231 * [A -> B]
5233 * and define (1) on top of the corresponding space, resulting in "aff".
5234 * We use this to create an isl_multi_aff that maps the output position "d"
5235 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5236 * We plug this into the wrapped map, unwrap the result and compute the
5237 * corresponding isl_pw_multi_aff.
5238 * The result is an expression
5240 * A -> T(A)
5242 * We adjust that to
5244 * A -> [A -> T(A)]
5246 * so that we can plug that into "aff", after extending the latter to
5247 * a mapping
5249 * [A -> B] -> B'
5252 * If "map" is actually a set, then there is no "A" space, meaning
5253 * that we do not need to perform any wrapping, and that the result
5254 * of the recursive call is of the form
5256 * [T]
5258 * which is plugged into a mapping of the form
5260 * B -> B'
5262 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5263 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5264 isl_int gcd)
5266 isl_set *set;
5267 isl_space *space;
5268 isl_local_space *ls;
5269 isl_aff *aff;
5270 isl_multi_aff *ma;
5271 isl_pw_multi_aff *pma, *id;
5272 isl_size n_in;
5273 unsigned o_out;
5274 isl_size n_out;
5275 isl_bool is_set;
5277 is_set = isl_map_is_set(map);
5278 if (is_set < 0)
5279 goto error;
5281 n_in = isl_basic_map_dim(hull, isl_dim_in);
5282 n_out = isl_basic_map_dim(hull, isl_dim_out);
5283 if (n_in < 0 || n_out < 0)
5284 goto error;
5285 o_out = isl_basic_map_offset(hull, isl_dim_out);
5287 if (is_set)
5288 set = map;
5289 else
5290 set = isl_map_wrap(map);
5291 space = isl_space_map_from_set(isl_set_get_space(set));
5292 ma = isl_multi_aff_identity(space);
5293 ls = isl_local_space_from_space(isl_set_get_space(set));
5294 aff = isl_aff_alloc(ls);
5295 if (aff) {
5296 isl_int_set_si(aff->v->el[0], 1);
5297 if (isl_int_is_one(hull->eq[i][o_out + d]))
5298 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5299 aff->v->size - 1);
5300 else
5301 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5302 aff->v->size - 1);
5303 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5305 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5306 set = isl_set_preimage_multi_aff(set, ma);
5308 ma = range_map(aff, d, n_in, n_out, is_set);
5310 if (is_set)
5311 map = set;
5312 else
5313 map = isl_set_unwrap(set);
5314 pma = isl_pw_multi_aff_from_map(map);
5316 if (!is_set) {
5317 space = isl_pw_multi_aff_get_domain_space(pma);
5318 space = isl_space_map_from_set(space);
5319 id = isl_pw_multi_aff_identity(space);
5320 pma = isl_pw_multi_aff_range_product(id, pma);
5322 id = isl_pw_multi_aff_from_multi_aff(ma);
5323 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5325 isl_basic_map_free(hull);
5326 return pma;
5327 error:
5328 isl_map_free(map);
5329 isl_basic_map_free(hull);
5330 return NULL;
5333 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5334 * "hull" contains the equalities valid for "map".
5336 * Check if any of the output dimensions is "strided".
5337 * That is, we check if it can be written as
5339 * x = m a + f(..)
5341 * with m greater than 1, a some combination of existentially quantified
5342 * variables and f an expression in the parameters and input dimensions.
5343 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5345 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5346 * special case.
5348 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5349 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5351 int i, j;
5352 isl_size n_out;
5353 unsigned o_out;
5354 isl_size n_div;
5355 unsigned o_div;
5356 isl_int gcd;
5358 n_div = isl_basic_map_dim(hull, isl_dim_div);
5359 n_out = isl_basic_map_dim(hull, isl_dim_out);
5360 if (n_div < 0 || n_out < 0)
5361 goto error;
5363 if (n_div == 0) {
5364 isl_basic_map_free(hull);
5365 return pw_multi_aff_from_map_check_div(map);
5368 isl_int_init(gcd);
5370 o_div = isl_basic_map_offset(hull, isl_dim_div);
5371 o_out = isl_basic_map_offset(hull, isl_dim_out);
5373 for (i = 0; i < n_out; ++i) {
5374 for (j = 0; j < hull->n_eq; ++j) {
5375 isl_int *eq = hull->eq[j];
5376 isl_pw_multi_aff *res;
5378 if (!isl_int_is_one(eq[o_out + i]) &&
5379 !isl_int_is_negone(eq[o_out + i]))
5380 continue;
5381 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5382 continue;
5383 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5384 n_out - (i + 1)) != -1)
5385 continue;
5386 isl_seq_gcd(eq + o_div, n_div, &gcd);
5387 if (isl_int_is_zero(gcd))
5388 continue;
5389 if (isl_int_is_one(gcd))
5390 continue;
5392 res = pw_multi_aff_from_map_stride(map, hull,
5393 i, j, gcd);
5394 isl_int_clear(gcd);
5395 return res;
5399 isl_int_clear(gcd);
5400 isl_basic_map_free(hull);
5401 return pw_multi_aff_from_map_check_div(map);
5402 error:
5403 isl_map_free(map);
5404 isl_basic_map_free(hull);
5405 return NULL;
5408 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5410 * As a special case, we first check if all output dimensions are uniquely
5411 * defined in terms of the parameters and input dimensions over the entire
5412 * domain. If so, we extract the desired isl_pw_multi_aff directly
5413 * from the affine hull of "map" and its domain.
5415 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5416 * special cases.
5418 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5420 isl_bool sv;
5421 isl_size n;
5422 isl_basic_map *hull;
5424 n = isl_map_n_basic_map(map);
5425 if (n < 0)
5426 goto error;
5428 if (n == 1) {
5429 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5430 hull = isl_basic_map_plain_affine_hull(hull);
5431 sv = isl_basic_map_plain_is_single_valued(hull);
5432 if (sv >= 0 && sv)
5433 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5434 hull);
5435 isl_basic_map_free(hull);
5437 map = isl_map_detect_equalities(map);
5438 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5439 sv = isl_basic_map_plain_is_single_valued(hull);
5440 if (sv >= 0 && sv)
5441 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5442 if (sv >= 0)
5443 return pw_multi_aff_from_map_check_strides(map, hull);
5444 isl_basic_map_free(hull);
5445 error:
5446 isl_map_free(map);
5447 return NULL;
5450 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5452 return isl_pw_multi_aff_from_map(set);
5455 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5456 * add it to *user.
5458 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5460 isl_union_pw_multi_aff **upma = user;
5461 isl_pw_multi_aff *pma;
5463 pma = isl_pw_multi_aff_from_map(map);
5464 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5466 return *upma ? isl_stat_ok : isl_stat_error;
5469 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5470 * domain.
5472 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5473 __isl_take isl_aff *aff)
5475 isl_multi_aff *ma;
5476 isl_pw_multi_aff *pma;
5478 ma = isl_multi_aff_from_aff(aff);
5479 pma = isl_pw_multi_aff_from_multi_aff(ma);
5480 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5483 /* Try and create an isl_union_pw_multi_aff that is equivalent
5484 * to the given isl_union_map.
5485 * The isl_union_map is required to be single-valued in each space.
5486 * Otherwise, an error is produced.
5488 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5489 __isl_take isl_union_map *umap)
5491 isl_space *space;
5492 isl_union_pw_multi_aff *upma;
5494 space = isl_union_map_get_space(umap);
5495 upma = isl_union_pw_multi_aff_empty(space);
5496 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5497 upma = isl_union_pw_multi_aff_free(upma);
5498 isl_union_map_free(umap);
5500 return upma;
5503 /* Try and create an isl_union_pw_multi_aff that is equivalent
5504 * to the given isl_union_set.
5505 * The isl_union_set is required to be a singleton in each space.
5506 * Otherwise, an error is produced.
5508 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5509 __isl_take isl_union_set *uset)
5511 return isl_union_pw_multi_aff_from_union_map(uset);
5514 /* Return the piecewise affine expression "set ? 1 : 0".
5516 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5518 isl_pw_aff *pa;
5519 isl_space *space = isl_set_get_space(set);
5520 isl_local_space *ls = isl_local_space_from_space(space);
5521 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5522 isl_aff *one = isl_aff_zero_on_domain(ls);
5524 one = isl_aff_add_constant_si(one, 1);
5525 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5526 set = isl_set_complement(set);
5527 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5529 return pa;
5532 /* Plug in "subs" for dimension "type", "pos" of "aff".
5534 * Let i be the dimension to replace and let "subs" be of the form
5536 * f/d
5538 * and "aff" of the form
5540 * (a i + g)/m
5542 * The result is
5544 * (a f + d g')/(m d)
5546 * where g' is the result of plugging in "subs" in each of the integer
5547 * divisions in g.
5549 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5550 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5552 isl_ctx *ctx;
5553 isl_int v;
5554 isl_size n_div;
5556 aff = isl_aff_cow(aff);
5557 if (!aff || !subs)
5558 return isl_aff_free(aff);
5560 ctx = isl_aff_get_ctx(aff);
5561 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5562 isl_die(ctx, isl_error_invalid,
5563 "spaces don't match", return isl_aff_free(aff));
5564 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5565 if (n_div < 0)
5566 return isl_aff_free(aff);
5567 if (n_div != 0)
5568 isl_die(ctx, isl_error_unsupported,
5569 "cannot handle divs yet", return isl_aff_free(aff));
5571 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5572 if (!aff->ls)
5573 return isl_aff_free(aff);
5575 aff->v = isl_vec_cow(aff->v);
5576 if (!aff->v)
5577 return isl_aff_free(aff);
5579 pos += isl_local_space_offset(aff->ls, type);
5581 isl_int_init(v);
5582 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5583 aff->v->size, subs->v->size, v);
5584 isl_int_clear(v);
5586 return aff;
5589 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5590 * expressions in "maff".
5592 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5593 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5594 __isl_keep isl_aff *subs)
5596 int i;
5598 maff = isl_multi_aff_cow(maff);
5599 if (!maff || !subs)
5600 return isl_multi_aff_free(maff);
5602 if (type == isl_dim_in)
5603 type = isl_dim_set;
5605 for (i = 0; i < maff->n; ++i) {
5606 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5607 type, pos, subs);
5608 if (!maff->u.p[i])
5609 return isl_multi_aff_free(maff);
5612 return maff;
5615 /* Plug in "subs" for dimension "type", "pos" of "pma".
5617 * pma is of the form
5619 * A_i(v) -> M_i(v)
5621 * while subs is of the form
5623 * v' = B_j(v) -> S_j
5625 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5626 * has a contribution in the result, in particular
5628 * C_ij(S_j) -> M_i(S_j)
5630 * Note that plugging in S_j in C_ij may also result in an empty set
5631 * and this contribution should simply be discarded.
5633 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5634 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5635 __isl_keep isl_pw_aff *subs)
5637 int i, j, n;
5638 isl_pw_multi_aff *res;
5640 if (!pma || !subs)
5641 return isl_pw_multi_aff_free(pma);
5643 n = pma->n * subs->n;
5644 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5646 for (i = 0; i < pma->n; ++i) {
5647 for (j = 0; j < subs->n; ++j) {
5648 isl_set *common;
5649 isl_multi_aff *res_ij;
5650 int empty;
5652 common = isl_set_intersect(
5653 isl_set_copy(pma->p[i].set),
5654 isl_set_copy(subs->p[j].set));
5655 common = isl_set_substitute(common,
5656 type, pos, subs->p[j].aff);
5657 empty = isl_set_plain_is_empty(common);
5658 if (empty < 0 || empty) {
5659 isl_set_free(common);
5660 if (empty < 0)
5661 goto error;
5662 continue;
5665 res_ij = isl_multi_aff_substitute(
5666 isl_multi_aff_copy(pma->p[i].maff),
5667 type, pos, subs->p[j].aff);
5669 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5673 isl_pw_multi_aff_free(pma);
5674 return res;
5675 error:
5676 isl_pw_multi_aff_free(pma);
5677 isl_pw_multi_aff_free(res);
5678 return NULL;
5681 /* Compute the preimage of a range of dimensions in the affine expression "src"
5682 * under "ma" and put the result in "dst". The number of dimensions in "src"
5683 * that precede the range is given by "n_before". The number of dimensions
5684 * in the range is given by the number of output dimensions of "ma".
5685 * The number of dimensions that follow the range is given by "n_after".
5686 * If "has_denom" is set (to one),
5687 * then "src" and "dst" have an extra initial denominator.
5688 * "n_div_ma" is the number of existentials in "ma"
5689 * "n_div_bset" is the number of existentials in "src"
5690 * The resulting "dst" (which is assumed to have been allocated by
5691 * the caller) contains coefficients for both sets of existentials,
5692 * first those in "ma" and then those in "src".
5693 * f, c1, c2 and g are temporary objects that have been initialized
5694 * by the caller.
5696 * Let src represent the expression
5698 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5700 * and let ma represent the expressions
5702 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5704 * We start out with the following expression for dst:
5706 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5708 * with the multiplication factor f initially equal to 1
5709 * and f \sum_i b_i v_i kept separately.
5710 * For each x_i that we substitute, we multiply the numerator
5711 * (and denominator) of dst by c_1 = m_i and add the numerator
5712 * of the x_i expression multiplied by c_2 = f b_i,
5713 * after removing the common factors of c_1 and c_2.
5714 * The multiplication factor f also needs to be multiplied by c_1
5715 * for the next x_j, j > i.
5717 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5718 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5719 int n_div_ma, int n_div_bmap,
5720 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5722 int i;
5723 isl_size n_param, n_in, n_out;
5724 int o_dst, o_src;
5726 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5727 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5728 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5729 if (n_param < 0 || n_in < 0 || n_out < 0)
5730 return isl_stat_error;
5732 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5733 o_dst = o_src = has_denom + 1 + n_param + n_before;
5734 isl_seq_clr(dst + o_dst, n_in);
5735 o_dst += n_in;
5736 o_src += n_out;
5737 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5738 o_dst += n_after;
5739 o_src += n_after;
5740 isl_seq_clr(dst + o_dst, n_div_ma);
5741 o_dst += n_div_ma;
5742 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5744 isl_int_set_si(f, 1);
5746 for (i = 0; i < n_out; ++i) {
5747 int offset = has_denom + 1 + n_param + n_before + i;
5749 if (isl_int_is_zero(src[offset]))
5750 continue;
5751 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5752 isl_int_mul(c2, f, src[offset]);
5753 isl_int_gcd(g, c1, c2);
5754 isl_int_divexact(c1, c1, g);
5755 isl_int_divexact(c2, c2, g);
5757 isl_int_mul(f, f, c1);
5758 o_dst = has_denom;
5759 o_src = 1;
5760 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5761 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5762 o_dst += 1 + n_param;
5763 o_src += 1 + n_param;
5764 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5765 o_dst += n_before;
5766 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5767 c2, ma->u.p[i]->v->el + o_src, n_in);
5768 o_dst += n_in;
5769 o_src += n_in;
5770 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5771 o_dst += n_after;
5772 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5773 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5774 o_dst += n_div_ma;
5775 o_src += n_div_ma;
5776 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5777 if (has_denom)
5778 isl_int_mul(dst[0], dst[0], c1);
5781 return isl_stat_ok;
5784 /* Compute the pullback of "aff" by the function represented by "ma".
5785 * In other words, plug in "ma" in "aff". The result is an affine expression
5786 * defined over the domain space of "ma".
5788 * If "aff" is represented by
5790 * (a(p) + b x + c(divs))/d
5792 * and ma is represented by
5794 * x = D(p) + F(y) + G(divs')
5796 * then the result is
5798 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5800 * The divs in the local space of the input are similarly adjusted
5801 * through a call to isl_local_space_preimage_multi_aff.
5803 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5804 __isl_take isl_multi_aff *ma)
5806 isl_aff *res = NULL;
5807 isl_local_space *ls;
5808 isl_size n_div_aff, n_div_ma;
5809 isl_int f, c1, c2, g;
5811 ma = isl_multi_aff_align_divs(ma);
5812 if (!aff || !ma)
5813 goto error;
5815 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5816 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5817 if (n_div_aff < 0 || n_div_ma < 0)
5818 goto error;
5820 ls = isl_aff_get_domain_local_space(aff);
5821 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5822 res = isl_aff_alloc(ls);
5823 if (!res)
5824 goto error;
5826 isl_int_init(f);
5827 isl_int_init(c1);
5828 isl_int_init(c2);
5829 isl_int_init(g);
5831 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5832 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5833 res = isl_aff_free(res);
5835 isl_int_clear(f);
5836 isl_int_clear(c1);
5837 isl_int_clear(c2);
5838 isl_int_clear(g);
5840 isl_aff_free(aff);
5841 isl_multi_aff_free(ma);
5842 res = isl_aff_normalize(res);
5843 return res;
5844 error:
5845 isl_aff_free(aff);
5846 isl_multi_aff_free(ma);
5847 isl_aff_free(res);
5848 return NULL;
5851 /* Compute the pullback of "aff1" by the function represented by "aff2".
5852 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5853 * defined over the domain space of "aff1".
5855 * The domain of "aff1" should match the range of "aff2", which means
5856 * that it should be single-dimensional.
5858 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5859 __isl_take isl_aff *aff2)
5861 isl_multi_aff *ma;
5863 ma = isl_multi_aff_from_aff(aff2);
5864 return isl_aff_pullback_multi_aff(aff1, ma);
5867 /* Compute the pullback of "ma1" by the function represented by "ma2".
5868 * In other words, plug in "ma2" in "ma1".
5870 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5871 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5873 int i;
5874 isl_space *space = NULL;
5876 isl_multi_aff_align_params_bin(&ma1, &ma2);
5877 ma2 = isl_multi_aff_align_divs(ma2);
5878 ma1 = isl_multi_aff_cow(ma1);
5879 if (!ma1 || !ma2)
5880 goto error;
5882 space = isl_space_join(isl_multi_aff_get_space(ma2),
5883 isl_multi_aff_get_space(ma1));
5885 for (i = 0; i < ma1->n; ++i) {
5886 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5887 isl_multi_aff_copy(ma2));
5888 if (!ma1->u.p[i])
5889 goto error;
5892 ma1 = isl_multi_aff_reset_space(ma1, space);
5893 isl_multi_aff_free(ma2);
5894 return ma1;
5895 error:
5896 isl_space_free(space);
5897 isl_multi_aff_free(ma2);
5898 isl_multi_aff_free(ma1);
5899 return NULL;
5902 /* Extend the local space of "dst" to include the divs
5903 * in the local space of "src".
5905 * If "src" does not have any divs or if the local spaces of "dst" and
5906 * "src" are the same, then no extension is required.
5908 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5909 __isl_keep isl_aff *src)
5911 isl_ctx *ctx;
5912 isl_size src_n_div, dst_n_div;
5913 int *exp1 = NULL;
5914 int *exp2 = NULL;
5915 isl_bool equal;
5916 isl_mat *div;
5918 if (!src || !dst)
5919 return isl_aff_free(dst);
5921 ctx = isl_aff_get_ctx(src);
5922 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5923 if (equal < 0)
5924 return isl_aff_free(dst);
5925 if (!equal)
5926 isl_die(ctx, isl_error_invalid,
5927 "spaces don't match", goto error);
5929 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
5930 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
5931 if (src_n_div == 0)
5932 return dst;
5933 equal = isl_local_space_is_equal(src->ls, dst->ls);
5934 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5935 return isl_aff_free(dst);
5936 if (equal)
5937 return dst;
5939 exp1 = isl_alloc_array(ctx, int, src_n_div);
5940 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5941 if (!exp1 || (dst_n_div && !exp2))
5942 goto error;
5944 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5945 dst = isl_aff_expand_divs(dst, div, exp2);
5946 free(exp1);
5947 free(exp2);
5949 return dst;
5950 error:
5951 free(exp1);
5952 free(exp2);
5953 return isl_aff_free(dst);
5956 /* Adjust the local spaces of the affine expressions in "maff"
5957 * such that they all have the save divs.
5959 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5960 __isl_take isl_multi_aff *maff)
5962 int i;
5964 if (!maff)
5965 return NULL;
5966 if (maff->n == 0)
5967 return maff;
5968 maff = isl_multi_aff_cow(maff);
5969 if (!maff)
5970 return NULL;
5972 for (i = 1; i < maff->n; ++i)
5973 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5974 for (i = 1; i < maff->n; ++i) {
5975 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5976 if (!maff->u.p[i])
5977 return isl_multi_aff_free(maff);
5980 return maff;
5983 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5985 aff = isl_aff_cow(aff);
5986 if (!aff)
5987 return NULL;
5989 aff->ls = isl_local_space_lift(aff->ls);
5990 if (!aff->ls)
5991 return isl_aff_free(aff);
5993 return aff;
5996 /* Lift "maff" to a space with extra dimensions such that the result
5997 * has no more existentially quantified variables.
5998 * If "ls" is not NULL, then *ls is assigned the local space that lies
5999 * at the basis of the lifting applied to "maff".
6001 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6002 __isl_give isl_local_space **ls)
6004 int i;
6005 isl_space *space;
6006 isl_size n_div;
6008 if (ls)
6009 *ls = NULL;
6011 if (!maff)
6012 return NULL;
6014 if (maff->n == 0) {
6015 if (ls) {
6016 isl_space *space = isl_multi_aff_get_domain_space(maff);
6017 *ls = isl_local_space_from_space(space);
6018 if (!*ls)
6019 return isl_multi_aff_free(maff);
6021 return maff;
6024 maff = isl_multi_aff_cow(maff);
6025 maff = isl_multi_aff_align_divs(maff);
6026 if (!maff)
6027 return NULL;
6029 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
6030 if (n_div < 0)
6031 return isl_multi_aff_free(maff);
6032 space = isl_multi_aff_get_space(maff);
6033 space = isl_space_lift(isl_space_domain(space), n_div);
6034 space = isl_space_extend_domain_with_range(space,
6035 isl_multi_aff_get_space(maff));
6036 if (!space)
6037 return isl_multi_aff_free(maff);
6038 isl_space_free(maff->space);
6039 maff->space = space;
6041 if (ls) {
6042 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
6043 if (!*ls)
6044 return isl_multi_aff_free(maff);
6047 for (i = 0; i < maff->n; ++i) {
6048 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
6049 if (!maff->u.p[i])
6050 goto error;
6053 return maff;
6054 error:
6055 if (ls)
6056 isl_local_space_free(*ls);
6057 return isl_multi_aff_free(maff);
6060 #undef TYPE
6061 #define TYPE isl_pw_multi_aff
6062 static
6063 #include "check_type_range_templ.c"
6065 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6067 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6068 __isl_keep isl_pw_multi_aff *pma, int pos)
6070 int i;
6071 isl_size n_out;
6072 isl_space *space;
6073 isl_pw_aff *pa;
6075 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6076 return NULL;
6078 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6079 if (n_out < 0)
6080 return NULL;
6082 space = isl_pw_multi_aff_get_space(pma);
6083 space = isl_space_drop_dims(space, isl_dim_out,
6084 pos + 1, n_out - pos - 1);
6085 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6087 pa = isl_pw_aff_alloc_size(space, pma->n);
6088 for (i = 0; i < pma->n; ++i) {
6089 isl_aff *aff;
6090 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6091 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6094 return pa;
6097 /* Return an isl_pw_multi_aff with the given "set" as domain and
6098 * an unnamed zero-dimensional range.
6100 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6101 __isl_take isl_set *set)
6103 isl_multi_aff *ma;
6104 isl_space *space;
6106 space = isl_set_get_space(set);
6107 space = isl_space_from_domain(space);
6108 ma = isl_multi_aff_zero(space);
6109 return isl_pw_multi_aff_alloc(set, ma);
6112 /* Add an isl_pw_multi_aff with the given "set" as domain and
6113 * an unnamed zero-dimensional range to *user.
6115 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6116 void *user)
6118 isl_union_pw_multi_aff **upma = user;
6119 isl_pw_multi_aff *pma;
6121 pma = isl_pw_multi_aff_from_domain(set);
6122 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6124 return isl_stat_ok;
6127 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6128 * an unnamed zero-dimensional range.
6130 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6131 __isl_take isl_union_set *uset)
6133 isl_space *space;
6134 isl_union_pw_multi_aff *upma;
6136 if (!uset)
6137 return NULL;
6139 space = isl_union_set_get_space(uset);
6140 upma = isl_union_pw_multi_aff_empty(space);
6142 if (isl_union_set_foreach_set(uset,
6143 &add_pw_multi_aff_from_domain, &upma) < 0)
6144 goto error;
6146 isl_union_set_free(uset);
6147 return upma;
6148 error:
6149 isl_union_set_free(uset);
6150 isl_union_pw_multi_aff_free(upma);
6151 return NULL;
6154 /* Local data for bin_entry and the callback "fn".
6156 struct isl_union_pw_multi_aff_bin_data {
6157 isl_union_pw_multi_aff *upma2;
6158 isl_union_pw_multi_aff *res;
6159 isl_pw_multi_aff *pma;
6160 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6163 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6164 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6166 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6168 struct isl_union_pw_multi_aff_bin_data *data = user;
6169 isl_stat r;
6171 data->pma = pma;
6172 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6173 data->fn, data);
6174 isl_pw_multi_aff_free(pma);
6176 return r;
6179 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6180 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6181 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6182 * as *entry. The callback should adjust data->res if desired.
6184 static __isl_give isl_union_pw_multi_aff *bin_op(
6185 __isl_take isl_union_pw_multi_aff *upma1,
6186 __isl_take isl_union_pw_multi_aff *upma2,
6187 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6189 isl_space *space;
6190 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6192 space = isl_union_pw_multi_aff_get_space(upma2);
6193 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6194 space = isl_union_pw_multi_aff_get_space(upma1);
6195 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6197 if (!upma1 || !upma2)
6198 goto error;
6200 data.upma2 = upma2;
6201 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6202 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6203 &bin_entry, &data) < 0)
6204 goto error;
6206 isl_union_pw_multi_aff_free(upma1);
6207 isl_union_pw_multi_aff_free(upma2);
6208 return data.res;
6209 error:
6210 isl_union_pw_multi_aff_free(upma1);
6211 isl_union_pw_multi_aff_free(upma2);
6212 isl_union_pw_multi_aff_free(data.res);
6213 return NULL;
6216 /* Given two isl_pw_multi_affs A -> B and C -> D,
6217 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6219 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6220 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6222 isl_space *space;
6224 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6225 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6226 isl_pw_multi_aff_get_space(pma2));
6227 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6228 &isl_multi_aff_range_product);
6231 /* Given two isl_pw_multi_affs A -> B and C -> D,
6232 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6234 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6235 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6237 isl_space *space;
6239 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6240 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6241 isl_pw_multi_aff_get_space(pma2));
6242 space = isl_space_flatten_range(space);
6243 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6244 &isl_multi_aff_flat_range_product);
6247 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6248 * to compute some form of range product and add the result to data->res.
6250 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6251 __isl_give isl_pw_multi_aff *(*range_product)(
6252 __isl_take isl_pw_multi_aff *pma1,
6253 __isl_take isl_pw_multi_aff *pma2),
6254 void *user)
6256 struct isl_union_pw_multi_aff_bin_data *data = user;
6257 isl_bool match;
6258 isl_space *space1, *space2;
6260 space1 = isl_pw_multi_aff_peek_space(data->pma);
6261 space2 = isl_pw_multi_aff_peek_space(pma2);
6262 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6263 space2, isl_dim_in);
6264 if (match < 0 || !match) {
6265 isl_pw_multi_aff_free(pma2);
6266 return match < 0 ? isl_stat_error : isl_stat_ok;
6269 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6271 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6273 return isl_stat_ok;
6276 /* If data->pma and "pma2" have the same domain space, then compute
6277 * their flat range product and add the result to data->res.
6279 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6280 void *user)
6282 return gen_range_product_entry(pma2,
6283 &isl_pw_multi_aff_flat_range_product, user);
6286 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6287 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6289 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6290 __isl_take isl_union_pw_multi_aff *upma1,
6291 __isl_take isl_union_pw_multi_aff *upma2)
6293 return bin_op(upma1, upma2, &flat_range_product_entry);
6296 /* If data->pma and "pma2" have the same domain space, then compute
6297 * their range product and add the result to data->res.
6299 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6300 void *user)
6302 return gen_range_product_entry(pma2,
6303 &isl_pw_multi_aff_range_product, user);
6306 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6307 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6309 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6310 __isl_take isl_union_pw_multi_aff *upma1,
6311 __isl_take isl_union_pw_multi_aff *upma2)
6313 return bin_op(upma1, upma2, &range_product_entry);
6316 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6317 * The parameters are assumed to have been aligned.
6319 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6320 * except that it works on two different isl_pw_* types.
6322 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6323 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6324 __isl_take isl_pw_aff *pa)
6326 int i, j, n;
6327 isl_pw_multi_aff *res = NULL;
6329 if (!pma || !pa)
6330 goto error;
6332 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6333 pa->dim, isl_dim_in))
6334 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6335 "domains don't match", goto error);
6336 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6337 goto error;
6339 n = pma->n * pa->n;
6340 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6342 for (i = 0; i < pma->n; ++i) {
6343 for (j = 0; j < pa->n; ++j) {
6344 isl_set *common;
6345 isl_multi_aff *res_ij;
6346 int empty;
6348 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6349 isl_set_copy(pa->p[j].set));
6350 empty = isl_set_plain_is_empty(common);
6351 if (empty < 0 || empty) {
6352 isl_set_free(common);
6353 if (empty < 0)
6354 goto error;
6355 continue;
6358 res_ij = isl_multi_aff_set_aff(
6359 isl_multi_aff_copy(pma->p[i].maff), pos,
6360 isl_aff_copy(pa->p[j].aff));
6361 res_ij = isl_multi_aff_gist(res_ij,
6362 isl_set_copy(common));
6364 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6368 isl_pw_multi_aff_free(pma);
6369 isl_pw_aff_free(pa);
6370 return res;
6371 error:
6372 isl_pw_multi_aff_free(pma);
6373 isl_pw_aff_free(pa);
6374 return isl_pw_multi_aff_free(res);
6377 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6379 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6380 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6381 __isl_take isl_pw_aff *pa)
6383 isl_bool equal_params;
6385 if (!pma || !pa)
6386 goto error;
6387 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6388 if (equal_params < 0)
6389 goto error;
6390 if (equal_params)
6391 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6392 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6393 isl_pw_aff_check_named_params(pa) < 0)
6394 goto error;
6395 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6396 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6397 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6398 error:
6399 isl_pw_multi_aff_free(pma);
6400 isl_pw_aff_free(pa);
6401 return NULL;
6404 /* Do the parameters of "pa" match those of "space"?
6406 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6407 __isl_keep isl_space *space)
6409 isl_space *pa_space;
6410 isl_bool match;
6412 if (!pa || !space)
6413 return isl_bool_error;
6415 pa_space = isl_pw_aff_get_space(pa);
6417 match = isl_space_has_equal_params(space, pa_space);
6419 isl_space_free(pa_space);
6420 return match;
6423 /* Check that the domain space of "pa" matches "space".
6425 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6426 __isl_keep isl_space *space)
6428 isl_space *pa_space;
6429 isl_bool match;
6431 if (!pa || !space)
6432 return isl_stat_error;
6434 pa_space = isl_pw_aff_get_space(pa);
6436 match = isl_space_has_equal_params(space, pa_space);
6437 if (match < 0)
6438 goto error;
6439 if (!match)
6440 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6441 "parameters don't match", goto error);
6442 match = isl_space_tuple_is_equal(space, isl_dim_in,
6443 pa_space, isl_dim_in);
6444 if (match < 0)
6445 goto error;
6446 if (!match)
6447 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6448 "domains don't match", goto error);
6449 isl_space_free(pa_space);
6450 return isl_stat_ok;
6451 error:
6452 isl_space_free(pa_space);
6453 return isl_stat_error;
6456 #undef BASE
6457 #define BASE pw_aff
6458 #undef DOMBASE
6459 #define DOMBASE set
6461 #include <isl_multi_explicit_domain.c>
6462 #include <isl_multi_pw_aff_explicit_domain.c>
6463 #include <isl_multi_templ.c>
6464 #include <isl_multi_add_constant_templ.c>
6465 #include <isl_multi_apply_set.c>
6466 #include <isl_multi_arith_templ.c>
6467 #include <isl_multi_bind_templ.c>
6468 #include <isl_multi_bind_domain_templ.c>
6469 #include <isl_multi_coalesce.c>
6470 #include <isl_multi_domain_templ.c>
6471 #include <isl_multi_dim_id_templ.c>
6472 #include <isl_multi_dims.c>
6473 #include <isl_multi_from_base_templ.c>
6474 #include <isl_multi_gist.c>
6475 #include <isl_multi_hash.c>
6476 #include <isl_multi_identity_templ.c>
6477 #include <isl_multi_align_set.c>
6478 #include <isl_multi_insert_domain_templ.c>
6479 #include <isl_multi_intersect.c>
6480 #include <isl_multi_min_max_templ.c>
6481 #include <isl_multi_move_dims_templ.c>
6482 #include <isl_multi_nan_templ.c>
6483 #include <isl_multi_param_templ.c>
6484 #include <isl_multi_product_templ.c>
6485 #include <isl_multi_splice_templ.c>
6486 #include <isl_multi_tuple_id_templ.c>
6487 #include <isl_multi_union_add_templ.c>
6488 #include <isl_multi_zero_templ.c>
6489 #include <isl_multi_unbind_params_templ.c>
6491 /* Are all elements of "mpa" piecewise constants?
6493 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6495 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6498 /* Does "mpa" have a non-trivial explicit domain?
6500 * The explicit domain, if present, is trivial if it represents
6501 * an (obviously) universe set.
6503 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6504 __isl_keep isl_multi_pw_aff *mpa)
6506 if (!mpa)
6507 return isl_bool_error;
6508 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6509 return isl_bool_false;
6510 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6513 #undef BASE
6514 #define BASE set
6516 #include "isl_opt_mpa_templ.c"
6518 /* Compute the minima of the set dimensions as a function of the
6519 * parameters, but independently of the other set dimensions.
6521 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6523 return set_opt_mpa(set, &isl_set_dim_min);
6526 /* Compute the maxima of the set dimensions as a function of the
6527 * parameters, but independently of the other set dimensions.
6529 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6531 return set_opt_mpa(set, &isl_set_dim_max);
6534 #undef BASE
6535 #define BASE map
6537 #include "isl_opt_mpa_templ.c"
6539 /* Compute the minima of the output dimensions as a function of the
6540 * parameters and input dimensions, but independently of
6541 * the other output dimensions.
6543 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6545 return map_opt_mpa(map, &isl_map_dim_min);
6548 /* Compute the maxima of the output dimensions as a function of the
6549 * parameters and input dimensions, but independently of
6550 * the other output dimensions.
6552 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6554 return map_opt_mpa(map, &isl_map_dim_max);
6557 /* Scale the elements of "pma" by the corresponding elements of "mv".
6559 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6560 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6562 int i;
6563 isl_bool equal_params;
6565 pma = isl_pw_multi_aff_cow(pma);
6566 if (!pma || !mv)
6567 goto error;
6568 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6569 mv->space, isl_dim_set))
6570 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6571 "spaces don't match", goto error);
6572 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6573 if (equal_params < 0)
6574 goto error;
6575 if (!equal_params) {
6576 pma = isl_pw_multi_aff_align_params(pma,
6577 isl_multi_val_get_space(mv));
6578 mv = isl_multi_val_align_params(mv,
6579 isl_pw_multi_aff_get_space(pma));
6580 if (!pma || !mv)
6581 goto error;
6584 for (i = 0; i < pma->n; ++i) {
6585 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6586 isl_multi_val_copy(mv));
6587 if (!pma->p[i].maff)
6588 goto error;
6591 isl_multi_val_free(mv);
6592 return pma;
6593 error:
6594 isl_multi_val_free(mv);
6595 isl_pw_multi_aff_free(pma);
6596 return NULL;
6599 /* This function is called for each entry of an isl_union_pw_multi_aff.
6600 * If the space of the entry matches that of data->mv,
6601 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6602 * Otherwise, return an empty isl_pw_multi_aff.
6604 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6605 __isl_take isl_pw_multi_aff *pma, void *user)
6607 isl_multi_val *mv = user;
6609 if (!pma)
6610 return NULL;
6611 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6612 mv->space, isl_dim_set)) {
6613 isl_space *space = isl_pw_multi_aff_get_space(pma);
6614 isl_pw_multi_aff_free(pma);
6615 return isl_pw_multi_aff_empty(space);
6618 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6621 /* Scale the elements of "upma" by the corresponding elements of "mv",
6622 * for those entries that match the space of "mv".
6624 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6625 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6627 struct isl_union_pw_multi_aff_transform_control control = {
6628 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6629 .fn_user = mv,
6632 upma = isl_union_pw_multi_aff_align_params(upma,
6633 isl_multi_val_get_space(mv));
6634 mv = isl_multi_val_align_params(mv,
6635 isl_union_pw_multi_aff_get_space(upma));
6636 if (!upma || !mv)
6637 goto error;
6639 return isl_union_pw_multi_aff_transform(upma, &control);
6641 isl_multi_val_free(mv);
6642 return upma;
6643 error:
6644 isl_multi_val_free(mv);
6645 isl_union_pw_multi_aff_free(upma);
6646 return NULL;
6649 /* Construct and return a piecewise multi affine expression
6650 * in the given space with value zero in each of the output dimensions and
6651 * a universe domain.
6653 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6655 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6658 /* Construct and return a piecewise multi affine expression
6659 * that is equal to the given piecewise affine expression.
6661 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6662 __isl_take isl_pw_aff *pa)
6664 int i;
6665 isl_space *space;
6666 isl_pw_multi_aff *pma;
6668 if (!pa)
6669 return NULL;
6671 space = isl_pw_aff_get_space(pa);
6672 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6674 for (i = 0; i < pa->n; ++i) {
6675 isl_set *set;
6676 isl_multi_aff *ma;
6678 set = isl_set_copy(pa->p[i].set);
6679 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6680 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6683 isl_pw_aff_free(pa);
6684 return pma;
6687 /* Construct and return a piecewise multi affine expression
6688 * that is equal to the given multi piecewise affine expression
6689 * on the shared domain of the piecewise affine expressions,
6690 * in the special case of a 0D multi piecewise affine expression.
6692 * Create a piecewise multi affine expression with the explicit domain of
6693 * the 0D multi piecewise affine expression as domain.
6695 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6696 __isl_take isl_multi_pw_aff *mpa)
6698 isl_space *space;
6699 isl_set *dom;
6700 isl_multi_aff *ma;
6702 space = isl_multi_pw_aff_get_space(mpa);
6703 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6704 isl_multi_pw_aff_free(mpa);
6706 ma = isl_multi_aff_zero(space);
6707 return isl_pw_multi_aff_alloc(dom, ma);
6710 /* Construct and return a piecewise multi affine expression
6711 * that is equal to the given multi piecewise affine expression
6712 * on the shared domain of the piecewise affine expressions.
6714 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6715 __isl_take isl_multi_pw_aff *mpa)
6717 int i;
6718 isl_space *space;
6719 isl_pw_aff *pa;
6720 isl_pw_multi_aff *pma;
6722 if (!mpa)
6723 return NULL;
6725 if (mpa->n == 0)
6726 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6728 space = isl_multi_pw_aff_get_space(mpa);
6729 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6730 pma = isl_pw_multi_aff_from_pw_aff(pa);
6732 for (i = 1; i < mpa->n; ++i) {
6733 isl_pw_multi_aff *pma_i;
6735 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6736 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6737 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6740 pma = isl_pw_multi_aff_reset_space(pma, space);
6742 isl_multi_pw_aff_free(mpa);
6743 return pma;
6746 /* Convenience function that constructs an isl_multi_pw_aff
6747 * directly from an isl_aff.
6749 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6751 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6754 /* Construct and return a multi piecewise affine expression
6755 * that is equal to the given multi affine expression.
6757 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6758 __isl_take isl_multi_aff *ma)
6760 int i;
6761 isl_size n;
6762 isl_multi_pw_aff *mpa;
6764 n = isl_multi_aff_dim(ma, isl_dim_out);
6765 if (n < 0)
6766 ma = isl_multi_aff_free(ma);
6767 if (!ma)
6768 return NULL;
6770 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6772 for (i = 0; i < n; ++i) {
6773 isl_pw_aff *pa;
6775 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6776 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6779 isl_multi_aff_free(ma);
6780 return mpa;
6783 /* Construct and return a multi piecewise affine expression
6784 * that is equal to the given piecewise multi affine expression.
6786 * If the resulting multi piecewise affine expression has
6787 * an explicit domain, then assign it the domain of the input.
6788 * In other cases, the domain is stored in the individual elements.
6790 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6791 __isl_take isl_pw_multi_aff *pma)
6793 int i;
6794 isl_size n;
6795 isl_space *space;
6796 isl_multi_pw_aff *mpa;
6798 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6799 if (n < 0)
6800 pma = isl_pw_multi_aff_free(pma);
6801 space = isl_pw_multi_aff_get_space(pma);
6802 mpa = isl_multi_pw_aff_alloc(space);
6804 for (i = 0; i < n; ++i) {
6805 isl_pw_aff *pa;
6807 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6808 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6810 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6811 isl_set *dom;
6813 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6814 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6817 isl_pw_multi_aff_free(pma);
6818 return mpa;
6821 /* Do "pa1" and "pa2" represent the same function?
6823 * We first check if they are obviously equal.
6824 * If not, we convert them to maps and check if those are equal.
6826 * If "pa1" or "pa2" contain any NaNs, then they are considered
6827 * not to be the same. A NaN is not equal to anything, not even
6828 * to another NaN.
6830 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6831 __isl_keep isl_pw_aff *pa2)
6833 isl_bool equal;
6834 isl_bool has_nan;
6835 isl_map *map1, *map2;
6837 if (!pa1 || !pa2)
6838 return isl_bool_error;
6840 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6841 if (equal < 0 || equal)
6842 return equal;
6843 has_nan = either_involves_nan(pa1, pa2);
6844 if (has_nan < 0)
6845 return isl_bool_error;
6846 if (has_nan)
6847 return isl_bool_false;
6849 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6850 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6851 equal = isl_map_is_equal(map1, map2);
6852 isl_map_free(map1);
6853 isl_map_free(map2);
6855 return equal;
6858 /* Do "mpa1" and "mpa2" represent the same function?
6860 * Note that we cannot convert the entire isl_multi_pw_aff
6861 * to a map because the domains of the piecewise affine expressions
6862 * may not be the same.
6864 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6865 __isl_keep isl_multi_pw_aff *mpa2)
6867 int i;
6868 isl_bool equal, equal_params;
6870 if (!mpa1 || !mpa2)
6871 return isl_bool_error;
6873 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6874 if (equal_params < 0)
6875 return isl_bool_error;
6876 if (!equal_params) {
6877 if (!isl_space_has_named_params(mpa1->space))
6878 return isl_bool_false;
6879 if (!isl_space_has_named_params(mpa2->space))
6880 return isl_bool_false;
6881 mpa1 = isl_multi_pw_aff_copy(mpa1);
6882 mpa2 = isl_multi_pw_aff_copy(mpa2);
6883 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6884 isl_multi_pw_aff_get_space(mpa2));
6885 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6886 isl_multi_pw_aff_get_space(mpa1));
6887 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6888 isl_multi_pw_aff_free(mpa1);
6889 isl_multi_pw_aff_free(mpa2);
6890 return equal;
6893 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6894 if (equal < 0 || !equal)
6895 return equal;
6897 for (i = 0; i < mpa1->n; ++i) {
6898 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6899 if (equal < 0 || !equal)
6900 return equal;
6903 return isl_bool_true;
6906 /* Do "pma1" and "pma2" represent the same function?
6908 * First check if they are obviously equal.
6909 * If not, then convert them to maps and check if those are equal.
6911 * If "pa1" or "pa2" contain any NaNs, then they are considered
6912 * not to be the same. A NaN is not equal to anything, not even
6913 * to another NaN.
6915 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6916 __isl_keep isl_pw_multi_aff *pma2)
6918 isl_bool equal;
6919 isl_bool has_nan;
6920 isl_map *map1, *map2;
6922 if (!pma1 || !pma2)
6923 return isl_bool_error;
6925 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6926 if (equal < 0 || equal)
6927 return equal;
6928 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6929 if (has_nan >= 0 && !has_nan)
6930 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6931 if (has_nan < 0 || has_nan)
6932 return isl_bool_not(has_nan);
6934 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
6935 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
6936 equal = isl_map_is_equal(map1, map2);
6937 isl_map_free(map1);
6938 isl_map_free(map2);
6940 return equal;
6943 /* Compute the pullback of "mpa" by the function represented by "ma".
6944 * In other words, plug in "ma" in "mpa".
6946 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6948 * If "mpa" has an explicit domain, then it is this domain
6949 * that needs to undergo a pullback, i.e., a preimage.
6951 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6952 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6954 int i;
6955 isl_space *space = NULL;
6957 mpa = isl_multi_pw_aff_cow(mpa);
6958 if (!mpa || !ma)
6959 goto error;
6961 space = isl_space_join(isl_multi_aff_get_space(ma),
6962 isl_multi_pw_aff_get_space(mpa));
6963 if (!space)
6964 goto error;
6966 for (i = 0; i < mpa->n; ++i) {
6967 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6968 isl_multi_aff_copy(ma));
6969 if (!mpa->u.p[i])
6970 goto error;
6972 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6973 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6974 isl_multi_aff_copy(ma));
6975 if (!mpa->u.dom)
6976 goto error;
6979 isl_multi_aff_free(ma);
6980 isl_space_free(mpa->space);
6981 mpa->space = space;
6982 return mpa;
6983 error:
6984 isl_space_free(space);
6985 isl_multi_pw_aff_free(mpa);
6986 isl_multi_aff_free(ma);
6987 return NULL;
6990 /* Compute the pullback of "mpa" by the function represented by "ma".
6991 * In other words, plug in "ma" in "mpa".
6993 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6994 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6996 isl_bool equal_params;
6998 if (!mpa || !ma)
6999 goto error;
7000 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
7001 if (equal_params < 0)
7002 goto error;
7003 if (equal_params)
7004 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7005 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
7006 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
7007 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7008 error:
7009 isl_multi_pw_aff_free(mpa);
7010 isl_multi_aff_free(ma);
7011 return NULL;
7014 /* Compute the pullback of "mpa" by the function represented by "pma".
7015 * In other words, plug in "pma" in "mpa".
7017 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7019 * If "mpa" has an explicit domain, then it is this domain
7020 * that needs to undergo a pullback, i.e., a preimage.
7022 static __isl_give isl_multi_pw_aff *
7023 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7024 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7026 int i;
7027 isl_space *space = NULL;
7029 mpa = isl_multi_pw_aff_cow(mpa);
7030 if (!mpa || !pma)
7031 goto error;
7033 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
7034 isl_multi_pw_aff_get_space(mpa));
7036 for (i = 0; i < mpa->n; ++i) {
7037 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7038 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
7039 if (!mpa->u.p[i])
7040 goto error;
7042 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7043 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
7044 isl_pw_multi_aff_copy(pma));
7045 if (!mpa->u.dom)
7046 goto error;
7049 isl_pw_multi_aff_free(pma);
7050 isl_space_free(mpa->space);
7051 mpa->space = space;
7052 return mpa;
7053 error:
7054 isl_space_free(space);
7055 isl_multi_pw_aff_free(mpa);
7056 isl_pw_multi_aff_free(pma);
7057 return NULL;
7060 /* Compute the pullback of "mpa" by the function represented by "pma".
7061 * In other words, plug in "pma" in "mpa".
7063 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
7064 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7066 isl_bool equal_params;
7068 if (!mpa || !pma)
7069 goto error;
7070 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
7071 if (equal_params < 0)
7072 goto error;
7073 if (equal_params)
7074 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7075 mpa = isl_multi_pw_aff_align_params(mpa,
7076 isl_pw_multi_aff_get_space(pma));
7077 pma = isl_pw_multi_aff_align_params(pma,
7078 isl_multi_pw_aff_get_space(mpa));
7079 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7080 error:
7081 isl_multi_pw_aff_free(mpa);
7082 isl_pw_multi_aff_free(pma);
7083 return NULL;
7086 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7087 * with the domain of "aff". The domain of the result is the same
7088 * as that of "mpa".
7089 * "mpa" and "aff" are assumed to have been aligned.
7091 * We first extract the parametric constant from "aff", defined
7092 * over the correct domain.
7093 * Then we add the appropriate combinations of the members of "mpa".
7094 * Finally, we add the integer divisions through recursive calls.
7096 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7097 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7099 int i;
7100 isl_size n_in, n_div, n_mpa_in;
7101 isl_space *space;
7102 isl_val *v;
7103 isl_pw_aff *pa;
7104 isl_aff *tmp;
7106 n_in = isl_aff_dim(aff, isl_dim_in);
7107 n_div = isl_aff_dim(aff, isl_dim_div);
7108 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7109 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7110 goto error;
7112 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7113 tmp = isl_aff_copy(aff);
7114 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7115 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7116 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7117 tmp = isl_aff_reset_domain_space(tmp, space);
7118 pa = isl_pw_aff_from_aff(tmp);
7120 for (i = 0; i < n_in; ++i) {
7121 isl_pw_aff *pa_i;
7123 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7124 continue;
7125 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7126 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7127 pa_i = isl_pw_aff_scale_val(pa_i, v);
7128 pa = isl_pw_aff_add(pa, pa_i);
7131 for (i = 0; i < n_div; ++i) {
7132 isl_aff *div;
7133 isl_pw_aff *pa_i;
7135 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7136 continue;
7137 div = isl_aff_get_div(aff, i);
7138 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7139 isl_multi_pw_aff_copy(mpa), div);
7140 pa_i = isl_pw_aff_floor(pa_i);
7141 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7142 pa_i = isl_pw_aff_scale_val(pa_i, v);
7143 pa = isl_pw_aff_add(pa, pa_i);
7146 isl_multi_pw_aff_free(mpa);
7147 isl_aff_free(aff);
7149 return pa;
7150 error:
7151 isl_multi_pw_aff_free(mpa);
7152 isl_aff_free(aff);
7153 return NULL;
7156 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7157 * with the domain of "aff". The domain of the result is the same
7158 * as that of "mpa".
7160 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7161 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7163 isl_bool equal_params;
7165 if (!aff || !mpa)
7166 goto error;
7167 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7168 if (equal_params < 0)
7169 goto error;
7170 if (equal_params)
7171 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7173 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7174 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7176 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7177 error:
7178 isl_aff_free(aff);
7179 isl_multi_pw_aff_free(mpa);
7180 return NULL;
7183 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7184 * with the domain of "pa". The domain of the result is the same
7185 * as that of "mpa".
7186 * "mpa" and "pa" are assumed to have been aligned.
7188 * We consider each piece in turn. Note that the domains of the
7189 * pieces are assumed to be disjoint and they remain disjoint
7190 * after taking the preimage (over the same function).
7192 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7193 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7195 isl_space *space;
7196 isl_pw_aff *res;
7197 int i;
7199 if (!mpa || !pa)
7200 goto error;
7202 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7203 isl_pw_aff_get_space(pa));
7204 res = isl_pw_aff_empty(space);
7206 for (i = 0; i < pa->n; ++i) {
7207 isl_pw_aff *pa_i;
7208 isl_set *domain;
7210 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7211 isl_multi_pw_aff_copy(mpa),
7212 isl_aff_copy(pa->p[i].aff));
7213 domain = isl_set_copy(pa->p[i].set);
7214 domain = isl_set_preimage_multi_pw_aff(domain,
7215 isl_multi_pw_aff_copy(mpa));
7216 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7217 res = isl_pw_aff_add_disjoint(res, pa_i);
7220 isl_pw_aff_free(pa);
7221 isl_multi_pw_aff_free(mpa);
7222 return res;
7223 error:
7224 isl_pw_aff_free(pa);
7225 isl_multi_pw_aff_free(mpa);
7226 return NULL;
7229 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7230 * with the domain of "pa". The domain of the result is the same
7231 * as that of "mpa".
7233 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7234 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7236 isl_bool equal_params;
7238 if (!pa || !mpa)
7239 goto error;
7240 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7241 if (equal_params < 0)
7242 goto error;
7243 if (equal_params)
7244 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7246 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7247 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7249 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7250 error:
7251 isl_pw_aff_free(pa);
7252 isl_multi_pw_aff_free(mpa);
7253 return NULL;
7256 /* Compute the pullback of "pa" by the function represented by "mpa".
7257 * In other words, plug in "mpa" in "pa".
7258 * "pa" and "mpa" are assumed to have been aligned.
7260 * The pullback is computed by applying "pa" to "mpa".
7262 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7263 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7265 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7268 /* Compute the pullback of "pa" by the function represented by "mpa".
7269 * In other words, plug in "mpa" in "pa".
7271 * The pullback is computed by applying "pa" to "mpa".
7273 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7274 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7276 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7279 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7280 * In other words, plug in "mpa2" in "mpa1".
7282 * We pullback each member of "mpa1" in turn.
7284 * If "mpa1" has an explicit domain, then it is this domain
7285 * that needs to undergo a pullback instead, i.e., a preimage.
7287 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7288 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7290 int i;
7291 isl_space *space = NULL;
7293 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7294 mpa1 = isl_multi_pw_aff_cow(mpa1);
7295 if (!mpa1 || !mpa2)
7296 goto error;
7298 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7299 isl_multi_pw_aff_get_space(mpa1));
7301 for (i = 0; i < mpa1->n; ++i) {
7302 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7303 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7304 if (!mpa1->u.p[i])
7305 goto error;
7308 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7309 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7310 isl_multi_pw_aff_copy(mpa2));
7311 if (!mpa1->u.dom)
7312 goto error;
7314 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7316 isl_multi_pw_aff_free(mpa2);
7317 return mpa1;
7318 error:
7319 isl_space_free(space);
7320 isl_multi_pw_aff_free(mpa1);
7321 isl_multi_pw_aff_free(mpa2);
7322 return NULL;
7325 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7326 * of "mpa1" and "mpa2" live in the same space, construct map space
7327 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7328 * with this map space as extract argument.
7330 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7331 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7332 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7333 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7335 int match;
7336 isl_space *space1, *space2;
7337 isl_map *res;
7339 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7340 isl_multi_pw_aff_get_space(mpa2));
7341 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7342 isl_multi_pw_aff_get_space(mpa1));
7343 if (!mpa1 || !mpa2)
7344 goto error;
7345 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7346 mpa2->space, isl_dim_out);
7347 if (match < 0)
7348 goto error;
7349 if (!match)
7350 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7351 "range spaces don't match", goto error);
7352 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7353 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7354 space1 = isl_space_map_from_domain_and_range(space1, space2);
7356 res = order(mpa1, mpa2, space1);
7357 isl_multi_pw_aff_free(mpa1);
7358 isl_multi_pw_aff_free(mpa2);
7359 return res;
7360 error:
7361 isl_multi_pw_aff_free(mpa1);
7362 isl_multi_pw_aff_free(mpa2);
7363 return NULL;
7366 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7367 * where the function values are equal. "space" is the space of the result.
7368 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7370 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7371 * in the sequences are equal.
7373 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7374 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7375 __isl_take isl_space *space)
7377 int i;
7378 isl_size n;
7379 isl_map *res;
7381 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7382 if (n < 0)
7383 space = isl_space_free(space);
7384 res = isl_map_universe(space);
7386 for (i = 0; i < n; ++i) {
7387 isl_pw_aff *pa1, *pa2;
7388 isl_map *map;
7390 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7391 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7392 map = isl_pw_aff_eq_map(pa1, pa2);
7393 res = isl_map_intersect(res, map);
7396 return res;
7399 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7400 * where the function values are equal.
7402 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7403 __isl_take isl_multi_pw_aff *mpa2)
7405 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7406 &isl_multi_pw_aff_eq_map_on_space);
7409 /* Intersect "map" with the result of applying "order"
7410 * on two copies of "mpa".
7412 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7413 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7414 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7415 __isl_take isl_multi_pw_aff *mpa2))
7417 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7420 /* Return the subset of "map" where the domain and the range
7421 * have equal "mpa" values.
7423 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7424 __isl_take isl_multi_pw_aff *mpa)
7426 return isl_map_order_at_multi_pw_aff(map, mpa,
7427 &isl_multi_pw_aff_eq_map);
7430 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7431 * where the function values of "mpa1" lexicographically satisfies "base"
7432 * compared to that of "mpa2". "space" is the space of the result.
7433 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7435 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7436 * if its i-th element satisfies "base" when compared to
7437 * the i-th element of "mpa2" while all previous elements are
7438 * pairwise equal.
7440 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7441 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7442 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7443 __isl_take isl_pw_aff *pa2),
7444 __isl_take isl_space *space)
7446 int i;
7447 isl_size n;
7448 isl_map *res, *rest;
7450 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7451 if (n < 0)
7452 space = isl_space_free(space);
7453 res = isl_map_empty(isl_space_copy(space));
7454 rest = isl_map_universe(space);
7456 for (i = 0; i < n; ++i) {
7457 isl_pw_aff *pa1, *pa2;
7458 isl_map *map;
7460 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7461 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7462 map = base(pa1, pa2);
7463 map = isl_map_intersect(map, isl_map_copy(rest));
7464 res = isl_map_union(res, map);
7466 if (i == n - 1)
7467 continue;
7469 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7470 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7471 map = isl_pw_aff_eq_map(pa1, pa2);
7472 rest = isl_map_intersect(rest, map);
7475 isl_map_free(rest);
7476 return res;
7479 #undef ORDER
7480 #define ORDER le
7481 #include "isl_aff_lex_templ.c"
7483 #undef ORDER
7484 #define ORDER lt
7485 #include "isl_aff_lex_templ.c"
7487 #undef ORDER
7488 #define ORDER ge
7489 #include "isl_aff_lex_templ.c"
7491 #undef ORDER
7492 #define ORDER gt
7493 #include "isl_aff_lex_templ.c"
7495 /* Compare two isl_affs.
7497 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7498 * than "aff2" and 0 if they are equal.
7500 * The order is fairly arbitrary. We do consider expressions that only involve
7501 * earlier dimensions as "smaller".
7503 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7505 int cmp;
7506 int last1, last2;
7508 if (aff1 == aff2)
7509 return 0;
7511 if (!aff1)
7512 return -1;
7513 if (!aff2)
7514 return 1;
7516 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7517 if (cmp != 0)
7518 return cmp;
7520 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7521 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7522 if (last1 != last2)
7523 return last1 - last2;
7525 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7528 /* Compare two isl_pw_affs.
7530 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7531 * than "pa2" and 0 if they are equal.
7533 * The order is fairly arbitrary. We do consider expressions that only involve
7534 * earlier dimensions as "smaller".
7536 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7537 __isl_keep isl_pw_aff *pa2)
7539 int i;
7540 int cmp;
7542 if (pa1 == pa2)
7543 return 0;
7545 if (!pa1)
7546 return -1;
7547 if (!pa2)
7548 return 1;
7550 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7551 if (cmp != 0)
7552 return cmp;
7554 if (pa1->n != pa2->n)
7555 return pa1->n - pa2->n;
7557 for (i = 0; i < pa1->n; ++i) {
7558 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7559 if (cmp != 0)
7560 return cmp;
7561 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7562 if (cmp != 0)
7563 return cmp;
7566 return 0;
7569 /* Return a piecewise affine expression that is equal to "v" on "domain".
7571 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7572 __isl_take isl_val *v)
7574 isl_space *space;
7575 isl_local_space *ls;
7576 isl_aff *aff;
7578 space = isl_set_get_space(domain);
7579 ls = isl_local_space_from_space(space);
7580 aff = isl_aff_val_on_domain(ls, v);
7582 return isl_pw_aff_alloc(domain, aff);
7585 /* Return a piecewise affine expression that is equal to the parameter
7586 * with identifier "id" on "domain".
7588 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7589 __isl_take isl_set *domain, __isl_take isl_id *id)
7591 isl_space *space;
7592 isl_aff *aff;
7594 space = isl_set_get_space(domain);
7595 space = isl_space_add_param_id(space, isl_id_copy(id));
7596 domain = isl_set_align_params(domain, isl_space_copy(space));
7597 aff = isl_aff_param_on_domain_space_id(space, id);
7599 return isl_pw_aff_alloc(domain, aff);
7602 /* Return a multi affine expression that is equal to "mv" on domain
7603 * space "space".
7605 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7606 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7608 int i;
7609 isl_size n;
7610 isl_space *space2;
7611 isl_local_space *ls;
7612 isl_multi_aff *ma;
7614 n = isl_multi_val_dim(mv, isl_dim_set);
7615 if (!space || n < 0)
7616 goto error;
7618 space2 = isl_multi_val_get_space(mv);
7619 space2 = isl_space_align_params(space2, isl_space_copy(space));
7620 space = isl_space_align_params(space, isl_space_copy(space2));
7621 space = isl_space_map_from_domain_and_range(space, space2);
7622 ma = isl_multi_aff_alloc(isl_space_copy(space));
7623 ls = isl_local_space_from_space(isl_space_domain(space));
7624 for (i = 0; i < n; ++i) {
7625 isl_val *v;
7626 isl_aff *aff;
7628 v = isl_multi_val_get_val(mv, i);
7629 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7630 ma = isl_multi_aff_set_aff(ma, i, aff);
7632 isl_local_space_free(ls);
7634 isl_multi_val_free(mv);
7635 return ma;
7636 error:
7637 isl_space_free(space);
7638 isl_multi_val_free(mv);
7639 return NULL;
7642 /* Return a piecewise multi-affine expression
7643 * that is equal to "mv" on "domain".
7645 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7646 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7648 isl_space *space;
7649 isl_multi_aff *ma;
7651 space = isl_set_get_space(domain);
7652 ma = isl_multi_aff_multi_val_on_space(space, mv);
7654 return isl_pw_multi_aff_alloc(domain, ma);
7657 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7658 * mv is the value that should be attained on each domain set
7659 * res collects the results
7661 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7662 isl_multi_val *mv;
7663 isl_union_pw_multi_aff *res;
7666 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7667 * and add it to data->res.
7669 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7670 void *user)
7672 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7673 isl_pw_multi_aff *pma;
7674 isl_multi_val *mv;
7676 mv = isl_multi_val_copy(data->mv);
7677 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7678 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7680 return data->res ? isl_stat_ok : isl_stat_error;
7683 /* Return a union piecewise multi-affine expression
7684 * that is equal to "mv" on "domain".
7686 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7687 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7689 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7690 isl_space *space;
7692 space = isl_union_set_get_space(domain);
7693 data.res = isl_union_pw_multi_aff_empty(space);
7694 data.mv = mv;
7695 if (isl_union_set_foreach_set(domain,
7696 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7697 data.res = isl_union_pw_multi_aff_free(data.res);
7698 isl_union_set_free(domain);
7699 isl_multi_val_free(mv);
7700 return data.res;
7703 /* Compute the pullback of data->pma by the function represented by "pma2",
7704 * provided the spaces match, and add the results to data->res.
7706 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7708 struct isl_union_pw_multi_aff_bin_data *data = user;
7710 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7711 pma2->dim, isl_dim_out)) {
7712 isl_pw_multi_aff_free(pma2);
7713 return isl_stat_ok;
7716 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7717 isl_pw_multi_aff_copy(data->pma), pma2);
7719 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7720 if (!data->res)
7721 return isl_stat_error;
7723 return isl_stat_ok;
7726 /* Compute the pullback of "upma1" by the function represented by "upma2".
7728 __isl_give isl_union_pw_multi_aff *
7729 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7730 __isl_take isl_union_pw_multi_aff *upma1,
7731 __isl_take isl_union_pw_multi_aff *upma2)
7733 return bin_op(upma1, upma2, &pullback_entry);
7736 /* Apply "upma2" to "upma1".
7738 * That is, compute the pullback of "upma2" by "upma1".
7740 __isl_give isl_union_pw_multi_aff *
7741 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7742 __isl_take isl_union_pw_multi_aff *upma1,
7743 __isl_take isl_union_pw_multi_aff *upma2)
7745 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7748 /* Check that the domain space of "upa" matches "space".
7750 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7751 * can in principle never fail since the space "space" is that
7752 * of the isl_multi_union_pw_aff and is a set space such that
7753 * there is no domain space to match.
7755 * We check the parameters and double-check that "space" is
7756 * indeed that of a set.
7758 static isl_stat isl_union_pw_aff_check_match_domain_space(
7759 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7761 isl_space *upa_space;
7762 isl_bool match;
7764 if (!upa || !space)
7765 return isl_stat_error;
7767 match = isl_space_is_set(space);
7768 if (match < 0)
7769 return isl_stat_error;
7770 if (!match)
7771 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7772 "expecting set space", return isl_stat_error);
7774 upa_space = isl_union_pw_aff_get_space(upa);
7775 match = isl_space_has_equal_params(space, upa_space);
7776 if (match < 0)
7777 goto error;
7778 if (!match)
7779 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7780 "parameters don't match", goto error);
7782 isl_space_free(upa_space);
7783 return isl_stat_ok;
7784 error:
7785 isl_space_free(upa_space);
7786 return isl_stat_error;
7789 /* Do the parameters of "upa" match those of "space"?
7791 static isl_bool isl_union_pw_aff_matching_params(
7792 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7794 isl_space *upa_space;
7795 isl_bool match;
7797 if (!upa || !space)
7798 return isl_bool_error;
7800 upa_space = isl_union_pw_aff_get_space(upa);
7802 match = isl_space_has_equal_params(space, upa_space);
7804 isl_space_free(upa_space);
7805 return match;
7808 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7809 * space represents the new parameters.
7810 * res collects the results.
7812 struct isl_union_pw_aff_reset_params_data {
7813 isl_space *space;
7814 isl_union_pw_aff *res;
7817 /* Replace the parameters of "pa" by data->space and
7818 * add the result to data->res.
7820 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7822 struct isl_union_pw_aff_reset_params_data *data = user;
7823 isl_space *space;
7825 space = isl_pw_aff_get_space(pa);
7826 space = isl_space_replace_params(space, data->space);
7827 pa = isl_pw_aff_reset_space(pa, space);
7828 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7830 return data->res ? isl_stat_ok : isl_stat_error;
7833 /* Replace the domain space of "upa" by "space".
7834 * Since a union expression does not have a (single) domain space,
7835 * "space" is necessarily a parameter space.
7837 * Since the order and the names of the parameters determine
7838 * the hash value, we need to create a new hash table.
7840 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7841 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7843 struct isl_union_pw_aff_reset_params_data data = { space };
7844 isl_bool match;
7846 match = isl_union_pw_aff_matching_params(upa, space);
7847 if (match < 0)
7848 upa = isl_union_pw_aff_free(upa);
7849 else if (match) {
7850 isl_space_free(space);
7851 return upa;
7854 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7855 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7856 data.res = isl_union_pw_aff_free(data.res);
7858 isl_union_pw_aff_free(upa);
7859 isl_space_free(space);
7860 return data.res;
7863 /* Return the floor of "pa".
7865 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7867 return isl_pw_aff_floor(pa);
7870 /* Given f, return floor(f).
7872 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7873 __isl_take isl_union_pw_aff *upa)
7875 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7878 /* Compute
7880 * upa mod m = upa - m * floor(upa/m)
7882 * with m an integer value.
7884 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7885 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7887 isl_union_pw_aff *res;
7889 if (!upa || !m)
7890 goto error;
7892 if (!isl_val_is_int(m))
7893 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7894 "expecting integer modulo", goto error);
7895 if (!isl_val_is_pos(m))
7896 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7897 "expecting positive modulo", goto error);
7899 res = isl_union_pw_aff_copy(upa);
7900 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7901 upa = isl_union_pw_aff_floor(upa);
7902 upa = isl_union_pw_aff_scale_val(upa, m);
7903 res = isl_union_pw_aff_sub(res, upa);
7905 return res;
7906 error:
7907 isl_val_free(m);
7908 isl_union_pw_aff_free(upa);
7909 return NULL;
7912 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7913 * pos is the output position that needs to be extracted.
7914 * res collects the results.
7916 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7917 int pos;
7918 isl_union_pw_aff *res;
7921 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7922 * (assuming it has such a dimension) and add it to data->res.
7924 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7926 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7927 isl_size n_out;
7928 isl_pw_aff *pa;
7930 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7931 if (n_out < 0)
7932 return isl_stat_error;
7933 if (data->pos >= n_out) {
7934 isl_pw_multi_aff_free(pma);
7935 return isl_stat_ok;
7938 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7939 isl_pw_multi_aff_free(pma);
7941 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7943 return data->res ? isl_stat_ok : isl_stat_error;
7946 /* Extract an isl_union_pw_aff corresponding to
7947 * output dimension "pos" of "upma".
7949 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7950 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7952 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7953 isl_space *space;
7955 if (!upma)
7956 return NULL;
7958 if (pos < 0)
7959 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7960 "cannot extract at negative position", return NULL);
7962 space = isl_union_pw_multi_aff_get_space(upma);
7963 data.res = isl_union_pw_aff_empty(space);
7964 data.pos = pos;
7965 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7966 &get_union_pw_aff, &data) < 0)
7967 data.res = isl_union_pw_aff_free(data.res);
7969 return data.res;
7972 /* Return a union piecewise affine expression
7973 * that is equal to "aff" on "domain".
7975 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7976 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7978 isl_pw_aff *pa;
7980 pa = isl_pw_aff_from_aff(aff);
7981 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7984 /* Return a union piecewise affine expression
7985 * that is equal to the parameter identified by "id" on "domain".
7987 * Make sure the parameter appears in the space passed to
7988 * isl_aff_param_on_domain_space_id.
7990 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7991 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7993 isl_space *space;
7994 isl_aff *aff;
7996 space = isl_union_set_get_space(domain);
7997 space = isl_space_add_param_id(space, isl_id_copy(id));
7998 aff = isl_aff_param_on_domain_space_id(space, id);
7999 return isl_union_pw_aff_aff_on_domain(domain, aff);
8002 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8003 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8004 * needs to attain.
8005 * "res" collects the results.
8007 struct isl_union_pw_aff_pw_aff_on_domain_data {
8008 isl_pw_aff *pa;
8009 isl_union_pw_aff *res;
8012 /* Construct a piecewise affine expression that is equal to data->pa
8013 * on "domain" and add the result to data->res.
8015 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8017 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8018 isl_pw_aff *pa;
8019 isl_size dim;
8021 pa = isl_pw_aff_copy(data->pa);
8022 dim = isl_set_dim(domain, isl_dim_set);
8023 if (dim < 0)
8024 pa = isl_pw_aff_free(pa);
8025 pa = isl_pw_aff_from_range(pa);
8026 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8027 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8028 pa = isl_pw_aff_intersect_domain(pa, domain);
8029 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8031 return data->res ? isl_stat_ok : isl_stat_error;
8034 /* Return a union piecewise affine expression
8035 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8036 * have been aligned.
8038 * Construct an isl_pw_aff on each of the sets in "domain" and
8039 * collect the results.
8041 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8042 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8044 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8045 isl_space *space;
8047 space = isl_union_set_get_space(domain);
8048 data.res = isl_union_pw_aff_empty(space);
8049 data.pa = pa;
8050 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8051 data.res = isl_union_pw_aff_free(data.res);
8052 isl_union_set_free(domain);
8053 isl_pw_aff_free(pa);
8054 return data.res;
8057 /* Return a union piecewise affine expression
8058 * that is equal to "pa" on "domain".
8060 * Check that "pa" is a parametric expression,
8061 * align the parameters if needed and call
8062 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8064 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8065 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8067 isl_bool is_set;
8068 isl_bool equal_params;
8069 isl_space *domain_space, *pa_space;
8071 pa_space = isl_pw_aff_peek_space(pa);
8072 is_set = isl_space_is_set(pa_space);
8073 if (is_set < 0)
8074 goto error;
8075 if (!is_set)
8076 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8077 "expecting parametric expression", goto error);
8079 domain_space = isl_union_set_get_space(domain);
8080 pa_space = isl_pw_aff_get_space(pa);
8081 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8082 if (equal_params >= 0 && !equal_params) {
8083 isl_space *space;
8085 space = isl_space_align_params(domain_space, pa_space);
8086 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8087 domain = isl_union_set_align_params(domain, space);
8088 } else {
8089 isl_space_free(domain_space);
8090 isl_space_free(pa_space);
8093 if (equal_params < 0)
8094 goto error;
8095 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8096 error:
8097 isl_union_set_free(domain);
8098 isl_pw_aff_free(pa);
8099 return NULL;
8102 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8103 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8104 * "res" collects the results.
8106 struct isl_union_pw_aff_val_on_domain_data {
8107 isl_val *v;
8108 isl_union_pw_aff *res;
8111 /* Construct a piecewise affine expression that is equal to data->v
8112 * on "domain" and add the result to data->res.
8114 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8116 struct isl_union_pw_aff_val_on_domain_data *data = user;
8117 isl_pw_aff *pa;
8118 isl_val *v;
8120 v = isl_val_copy(data->v);
8121 pa = isl_pw_aff_val_on_domain(domain, v);
8122 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8124 return data->res ? isl_stat_ok : isl_stat_error;
8127 /* Return a union piecewise affine expression
8128 * that is equal to "v" on "domain".
8130 * Construct an isl_pw_aff on each of the sets in "domain" and
8131 * collect the results.
8133 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8134 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8136 struct isl_union_pw_aff_val_on_domain_data data;
8137 isl_space *space;
8139 space = isl_union_set_get_space(domain);
8140 data.res = isl_union_pw_aff_empty(space);
8141 data.v = v;
8142 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8143 data.res = isl_union_pw_aff_free(data.res);
8144 isl_union_set_free(domain);
8145 isl_val_free(v);
8146 return data.res;
8149 /* Construct a piecewise multi affine expression
8150 * that is equal to "pa" and add it to upma.
8152 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8153 void *user)
8155 isl_union_pw_multi_aff **upma = user;
8156 isl_pw_multi_aff *pma;
8158 pma = isl_pw_multi_aff_from_pw_aff(pa);
8159 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8161 return *upma ? isl_stat_ok : isl_stat_error;
8164 /* Construct and return a union piecewise multi affine expression
8165 * that is equal to the given union piecewise affine expression.
8167 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8168 __isl_take isl_union_pw_aff *upa)
8170 isl_space *space;
8171 isl_union_pw_multi_aff *upma;
8173 if (!upa)
8174 return NULL;
8176 space = isl_union_pw_aff_get_space(upa);
8177 upma = isl_union_pw_multi_aff_empty(space);
8179 if (isl_union_pw_aff_foreach_pw_aff(upa,
8180 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8181 upma = isl_union_pw_multi_aff_free(upma);
8183 isl_union_pw_aff_free(upa);
8184 return upma;
8187 /* Compute the set of elements in the domain of "pa" where it is zero and
8188 * add this set to "uset".
8190 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8192 isl_union_set **uset = (isl_union_set **)user;
8194 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8196 return *uset ? isl_stat_ok : isl_stat_error;
8199 /* Return a union set containing those elements in the domain
8200 * of "upa" where it is zero.
8202 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8203 __isl_take isl_union_pw_aff *upa)
8205 isl_union_set *zero;
8207 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8208 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8209 zero = isl_union_set_free(zero);
8211 isl_union_pw_aff_free(upa);
8212 return zero;
8215 /* Internal data structure for isl_union_pw_aff_bind_id,
8216 * storing the parameter that needs to be bound and
8217 * the accumulated results.
8219 struct isl_bind_id_data {
8220 isl_id *id;
8221 isl_union_set *bound;
8224 /* Bind the piecewise affine function "pa" to the parameter data->id,
8225 * adding the resulting elements in the domain where the expression
8226 * is equal to the parameter to data->bound.
8228 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8230 struct isl_bind_id_data *data = user;
8231 isl_set *bound;
8233 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8234 data->bound = isl_union_set_add_set(data->bound, bound);
8236 return data->bound ? isl_stat_ok : isl_stat_error;
8239 /* Bind the union piecewise affine function "upa" to the parameter "id",
8240 * returning the elements in the domain where the expression
8241 * is equal to the parameter.
8243 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8244 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8246 struct isl_bind_id_data data = { id };
8248 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8249 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8250 data.bound = isl_union_set_free(data.bound);
8252 isl_union_pw_aff_free(upa);
8253 isl_id_free(id);
8254 return data.bound;
8257 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8258 * upma is the function that is plugged in.
8259 * pa is the current part of the function in which upma is plugged in.
8260 * res collects the results.
8262 struct isl_union_pw_aff_pullback_upma_data {
8263 isl_union_pw_multi_aff *upma;
8264 isl_pw_aff *pa;
8265 isl_union_pw_aff *res;
8268 /* Check if "pma" can be plugged into data->pa.
8269 * If so, perform the pullback and add the result to data->res.
8271 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8273 struct isl_union_pw_aff_pullback_upma_data *data = user;
8274 isl_pw_aff *pa;
8276 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8277 pma->dim, isl_dim_out)) {
8278 isl_pw_multi_aff_free(pma);
8279 return isl_stat_ok;
8282 pa = isl_pw_aff_copy(data->pa);
8283 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8285 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8287 return data->res ? isl_stat_ok : isl_stat_error;
8290 /* Check if any of the elements of data->upma can be plugged into pa,
8291 * add if so add the result to data->res.
8293 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8295 struct isl_union_pw_aff_pullback_upma_data *data = user;
8296 isl_stat r;
8298 data->pa = pa;
8299 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8300 &pa_pb_pma, data);
8301 isl_pw_aff_free(pa);
8303 return r;
8306 /* Compute the pullback of "upa" by the function represented by "upma".
8307 * In other words, plug in "upma" in "upa". The result contains
8308 * expressions defined over the domain space of "upma".
8310 * Run over all pairs of elements in "upa" and "upma", perform
8311 * the pullback when appropriate and collect the results.
8312 * If the hash value were based on the domain space rather than
8313 * the function space, then we could run through all elements
8314 * of "upma" and directly pick out the corresponding element of "upa".
8316 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8317 __isl_take isl_union_pw_aff *upa,
8318 __isl_take isl_union_pw_multi_aff *upma)
8320 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8321 isl_space *space;
8323 space = isl_union_pw_multi_aff_get_space(upma);
8324 upa = isl_union_pw_aff_align_params(upa, space);
8325 space = isl_union_pw_aff_get_space(upa);
8326 upma = isl_union_pw_multi_aff_align_params(upma, space);
8328 if (!upa || !upma)
8329 goto error;
8331 data.upma = upma;
8332 data.res = isl_union_pw_aff_alloc_same_size(upa);
8333 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8334 data.res = isl_union_pw_aff_free(data.res);
8336 isl_union_pw_aff_free(upa);
8337 isl_union_pw_multi_aff_free(upma);
8338 return data.res;
8339 error:
8340 isl_union_pw_aff_free(upa);
8341 isl_union_pw_multi_aff_free(upma);
8342 return NULL;
8345 #undef BASE
8346 #define BASE union_pw_aff
8347 #undef DOMBASE
8348 #define DOMBASE union_set
8350 #include <isl_multi_explicit_domain.c>
8351 #include <isl_multi_union_pw_aff_explicit_domain.c>
8352 #include <isl_multi_templ.c>
8353 #include <isl_multi_apply_set.c>
8354 #include <isl_multi_apply_union_set.c>
8355 #include <isl_multi_arith_templ.c>
8356 #include <isl_multi_bind_templ.c>
8357 #include <isl_multi_coalesce.c>
8358 #include <isl_multi_dim_id_templ.c>
8359 #include <isl_multi_floor.c>
8360 #include <isl_multi_from_base_templ.c>
8361 #include <isl_multi_gist.c>
8362 #include <isl_multi_align_set.c>
8363 #include <isl_multi_align_union_set.c>
8364 #include <isl_multi_intersect.c>
8365 #include <isl_multi_nan_templ.c>
8366 #include <isl_multi_tuple_id_templ.c>
8367 #include <isl_multi_union_add_templ.c>
8369 /* Does "mupa" have a non-trivial explicit domain?
8371 * The explicit domain, if present, is trivial if it represents
8372 * an (obviously) universe parameter set.
8374 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8375 __isl_keep isl_multi_union_pw_aff *mupa)
8377 isl_bool is_params, trivial;
8378 isl_set *set;
8380 if (!mupa)
8381 return isl_bool_error;
8382 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8383 return isl_bool_false;
8384 is_params = isl_union_set_is_params(mupa->u.dom);
8385 if (is_params < 0 || !is_params)
8386 return isl_bool_not(is_params);
8387 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8388 trivial = isl_set_plain_is_universe(set);
8389 isl_set_free(set);
8390 return isl_bool_not(trivial);
8393 /* Construct a multiple union piecewise affine expression
8394 * in the given space with value zero in each of the output dimensions.
8396 * Since there is no canonical zero value for
8397 * a union piecewise affine expression, we can only construct
8398 * a zero-dimensional "zero" value.
8400 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8401 __isl_take isl_space *space)
8403 isl_bool params;
8404 isl_size dim;
8406 if (!space)
8407 return NULL;
8409 params = isl_space_is_params(space);
8410 if (params < 0)
8411 goto error;
8412 if (params)
8413 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8414 "expecting proper set space", goto error);
8415 if (!isl_space_is_set(space))
8416 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8417 "expecting set space", goto error);
8418 dim = isl_space_dim(space, isl_dim_out);
8419 if (dim < 0)
8420 goto error;
8421 if (dim != 0)
8422 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8423 "expecting 0D space", goto error);
8425 return isl_multi_union_pw_aff_alloc(space);
8426 error:
8427 isl_space_free(space);
8428 return NULL;
8431 /* Construct and return a multi union piecewise affine expression
8432 * that is equal to the given multi affine expression.
8434 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8435 __isl_take isl_multi_aff *ma)
8437 isl_multi_pw_aff *mpa;
8439 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8440 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8443 /* Construct and return a multi union piecewise affine expression
8444 * that is equal to the given multi piecewise affine expression.
8446 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8447 __isl_take isl_multi_pw_aff *mpa)
8449 int i;
8450 isl_size n;
8451 isl_space *space;
8452 isl_multi_union_pw_aff *mupa;
8454 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8455 if (n < 0)
8456 mpa = isl_multi_pw_aff_free(mpa);
8457 if (!mpa)
8458 return NULL;
8460 space = isl_multi_pw_aff_get_space(mpa);
8461 space = isl_space_range(space);
8462 mupa = isl_multi_union_pw_aff_alloc(space);
8464 for (i = 0; i < n; ++i) {
8465 isl_pw_aff *pa;
8466 isl_union_pw_aff *upa;
8468 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8469 upa = isl_union_pw_aff_from_pw_aff(pa);
8470 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8473 isl_multi_pw_aff_free(mpa);
8475 return mupa;
8478 /* Extract the range space of "pma" and assign it to *space.
8479 * If *space has already been set (through a previous call to this function),
8480 * then check that the range space is the same.
8482 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8484 isl_space **space = user;
8485 isl_space *pma_space;
8486 isl_bool equal;
8488 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8489 isl_pw_multi_aff_free(pma);
8491 if (!pma_space)
8492 return isl_stat_error;
8493 if (!*space) {
8494 *space = pma_space;
8495 return isl_stat_ok;
8498 equal = isl_space_is_equal(pma_space, *space);
8499 isl_space_free(pma_space);
8501 if (equal < 0)
8502 return isl_stat_error;
8503 if (!equal)
8504 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8505 "range spaces not the same", return isl_stat_error);
8506 return isl_stat_ok;
8509 /* Construct and return a multi union piecewise affine expression
8510 * that is equal to the given union piecewise multi affine expression.
8512 * In order to be able to perform the conversion, the input
8513 * needs to be non-empty and may only involve a single range space.
8515 * If the resulting multi union piecewise affine expression has
8516 * an explicit domain, then assign it the domain of the input.
8517 * In other cases, the domain is stored in the individual elements.
8519 __isl_give isl_multi_union_pw_aff *
8520 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8521 __isl_take isl_union_pw_multi_aff *upma)
8523 isl_space *space = NULL;
8524 isl_multi_union_pw_aff *mupa;
8525 int i;
8526 isl_size n;
8528 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8529 if (n < 0)
8530 goto error;
8531 if (n == 0)
8532 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8533 "cannot extract range space from empty input",
8534 goto error);
8535 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8536 &space) < 0)
8537 goto error;
8539 if (!space)
8540 goto error;
8542 n = isl_space_dim(space, isl_dim_set);
8543 if (n < 0)
8544 space = isl_space_free(space);
8545 mupa = isl_multi_union_pw_aff_alloc(space);
8547 for (i = 0; i < n; ++i) {
8548 isl_union_pw_aff *upa;
8550 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8551 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8553 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8554 isl_union_set *dom;
8555 isl_union_pw_multi_aff *copy;
8557 copy = isl_union_pw_multi_aff_copy(upma);
8558 dom = isl_union_pw_multi_aff_domain(copy);
8559 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8562 isl_union_pw_multi_aff_free(upma);
8563 return mupa;
8564 error:
8565 isl_space_free(space);
8566 isl_union_pw_multi_aff_free(upma);
8567 return NULL;
8570 /* Try and create an isl_multi_union_pw_aff that is equivalent
8571 * to the given isl_union_map.
8572 * The isl_union_map is required to be single-valued in each space.
8573 * Moreover, it cannot be empty and all range spaces need to be the same.
8574 * Otherwise, an error is produced.
8576 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8577 __isl_take isl_union_map *umap)
8579 isl_union_pw_multi_aff *upma;
8581 upma = isl_union_pw_multi_aff_from_union_map(umap);
8582 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8585 /* Return a multiple union piecewise affine expression
8586 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8587 * have been aligned.
8589 * If the resulting multi union piecewise affine expression has
8590 * an explicit domain, then assign it the input domain.
8591 * In other cases, the domain is stored in the individual elements.
8593 static __isl_give isl_multi_union_pw_aff *
8594 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8595 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8597 int i;
8598 isl_size n;
8599 isl_space *space;
8600 isl_multi_union_pw_aff *mupa;
8602 n = isl_multi_val_dim(mv, isl_dim_set);
8603 if (!domain || n < 0)
8604 goto error;
8606 space = isl_multi_val_get_space(mv);
8607 mupa = isl_multi_union_pw_aff_alloc(space);
8608 for (i = 0; i < n; ++i) {
8609 isl_val *v;
8610 isl_union_pw_aff *upa;
8612 v = isl_multi_val_get_val(mv, i);
8613 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8615 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8617 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8618 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8619 isl_union_set_copy(domain));
8621 isl_union_set_free(domain);
8622 isl_multi_val_free(mv);
8623 return mupa;
8624 error:
8625 isl_union_set_free(domain);
8626 isl_multi_val_free(mv);
8627 return NULL;
8630 /* Return a multiple union piecewise affine expression
8631 * that is equal to "mv" on "domain".
8633 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8634 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8636 isl_bool equal_params;
8638 if (!domain || !mv)
8639 goto error;
8640 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8641 if (equal_params < 0)
8642 goto error;
8643 if (equal_params)
8644 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8645 domain, mv);
8646 domain = isl_union_set_align_params(domain,
8647 isl_multi_val_get_space(mv));
8648 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8649 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8650 error:
8651 isl_union_set_free(domain);
8652 isl_multi_val_free(mv);
8653 return NULL;
8656 /* Return a multiple union piecewise affine expression
8657 * that is equal to "ma" on "domain".
8659 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8660 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8662 isl_pw_multi_aff *pma;
8664 pma = isl_pw_multi_aff_from_multi_aff(ma);
8665 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8668 /* Return a multiple union piecewise affine expression
8669 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8670 * have been aligned.
8672 * If the resulting multi union piecewise affine expression has
8673 * an explicit domain, then assign it the input domain.
8674 * In other cases, the domain is stored in the individual elements.
8676 static __isl_give isl_multi_union_pw_aff *
8677 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8678 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8680 int i;
8681 isl_size n;
8682 isl_space *space;
8683 isl_multi_union_pw_aff *mupa;
8685 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8686 if (!domain || n < 0)
8687 goto error;
8688 space = isl_pw_multi_aff_get_space(pma);
8689 mupa = isl_multi_union_pw_aff_alloc(space);
8690 for (i = 0; i < n; ++i) {
8691 isl_pw_aff *pa;
8692 isl_union_pw_aff *upa;
8694 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8695 upa = isl_union_pw_aff_pw_aff_on_domain(
8696 isl_union_set_copy(domain), pa);
8697 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8699 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8700 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8701 isl_union_set_copy(domain));
8703 isl_union_set_free(domain);
8704 isl_pw_multi_aff_free(pma);
8705 return mupa;
8706 error:
8707 isl_union_set_free(domain);
8708 isl_pw_multi_aff_free(pma);
8709 return NULL;
8712 /* Return a multiple union piecewise affine expression
8713 * that is equal to "pma" on "domain".
8715 __isl_give isl_multi_union_pw_aff *
8716 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8717 __isl_take isl_pw_multi_aff *pma)
8719 isl_bool equal_params;
8720 isl_space *space;
8722 space = isl_pw_multi_aff_peek_space(pma);
8723 equal_params = isl_union_set_space_has_equal_params(domain, space);
8724 if (equal_params < 0)
8725 goto error;
8726 if (equal_params)
8727 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8728 domain, pma);
8729 domain = isl_union_set_align_params(domain,
8730 isl_pw_multi_aff_get_space(pma));
8731 pma = isl_pw_multi_aff_align_params(pma,
8732 isl_union_set_get_space(domain));
8733 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8734 pma);
8735 error:
8736 isl_union_set_free(domain);
8737 isl_pw_multi_aff_free(pma);
8738 return NULL;
8741 /* Return a union set containing those elements in the domains
8742 * of the elements of "mupa" where they are all zero.
8744 * If there are no elements, then simply return the entire domain.
8746 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8747 __isl_take isl_multi_union_pw_aff *mupa)
8749 int i;
8750 isl_size n;
8751 isl_union_pw_aff *upa;
8752 isl_union_set *zero;
8754 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8755 if (n < 0)
8756 mupa = isl_multi_union_pw_aff_free(mupa);
8757 if (!mupa)
8758 return NULL;
8760 if (n == 0)
8761 return isl_multi_union_pw_aff_domain(mupa);
8763 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8764 zero = isl_union_pw_aff_zero_union_set(upa);
8766 for (i = 1; i < n; ++i) {
8767 isl_union_set *zero_i;
8769 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8770 zero_i = isl_union_pw_aff_zero_union_set(upa);
8772 zero = isl_union_set_intersect(zero, zero_i);
8775 isl_multi_union_pw_aff_free(mupa);
8776 return zero;
8779 /* Construct a union map mapping the shared domain
8780 * of the union piecewise affine expressions to the range of "mupa"
8781 * in the special case of a 0D multi union piecewise affine expression.
8783 * Construct a map between the explicit domain of "mupa" and
8784 * the range space.
8785 * Note that this assumes that the domain consists of explicit elements.
8787 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8788 __isl_take isl_multi_union_pw_aff *mupa)
8790 isl_bool is_params;
8791 isl_space *space;
8792 isl_union_set *dom, *ran;
8794 space = isl_multi_union_pw_aff_get_space(mupa);
8795 dom = isl_multi_union_pw_aff_domain(mupa);
8796 ran = isl_union_set_from_set(isl_set_universe(space));
8798 is_params = isl_union_set_is_params(dom);
8799 if (is_params < 0)
8800 dom = isl_union_set_free(dom);
8801 else if (is_params)
8802 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8803 "cannot create union map from expression without "
8804 "explicit domain elements",
8805 dom = isl_union_set_free(dom));
8807 return isl_union_map_from_domain_and_range(dom, ran);
8810 /* Construct a union map mapping the shared domain
8811 * of the union piecewise affine expressions to the range of "mupa"
8812 * with each dimension in the range equated to the
8813 * corresponding union piecewise affine expression.
8815 * If the input is zero-dimensional, then construct a mapping
8816 * from its explicit domain.
8818 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8819 __isl_take isl_multi_union_pw_aff *mupa)
8821 int i;
8822 isl_size n;
8823 isl_space *space;
8824 isl_union_map *umap;
8825 isl_union_pw_aff *upa;
8827 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8828 if (n < 0)
8829 mupa = isl_multi_union_pw_aff_free(mupa);
8830 if (!mupa)
8831 return NULL;
8833 if (n == 0)
8834 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8836 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8837 umap = isl_union_map_from_union_pw_aff(upa);
8839 for (i = 1; i < n; ++i) {
8840 isl_union_map *umap_i;
8842 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8843 umap_i = isl_union_map_from_union_pw_aff(upa);
8844 umap = isl_union_map_flat_range_product(umap, umap_i);
8847 space = isl_multi_union_pw_aff_get_space(mupa);
8848 umap = isl_union_map_reset_range_space(umap, space);
8850 isl_multi_union_pw_aff_free(mupa);
8851 return umap;
8854 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8855 * "range" is the space from which to set the range space.
8856 * "res" collects the results.
8858 struct isl_union_pw_multi_aff_reset_range_space_data {
8859 isl_space *range;
8860 isl_union_pw_multi_aff *res;
8863 /* Replace the range space of "pma" by the range space of data->range and
8864 * add the result to data->res.
8866 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8868 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8869 isl_space *space;
8871 space = isl_pw_multi_aff_get_space(pma);
8872 space = isl_space_domain(space);
8873 space = isl_space_extend_domain_with_range(space,
8874 isl_space_copy(data->range));
8875 pma = isl_pw_multi_aff_reset_space(pma, space);
8876 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8878 return data->res ? isl_stat_ok : isl_stat_error;
8881 /* Replace the range space of all the piecewise affine expressions in "upma" by
8882 * the range space of "space".
8884 * This assumes that all these expressions have the same output dimension.
8886 * Since the spaces of the expressions change, so do their hash values.
8887 * We therefore need to create a new isl_union_pw_multi_aff.
8888 * Note that the hash value is currently computed based on the entire
8889 * space even though there can only be a single expression with a given
8890 * domain space.
8892 static __isl_give isl_union_pw_multi_aff *
8893 isl_union_pw_multi_aff_reset_range_space(
8894 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8896 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8897 isl_space *space_upma;
8899 space_upma = isl_union_pw_multi_aff_get_space(upma);
8900 data.res = isl_union_pw_multi_aff_empty(space_upma);
8901 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8902 &reset_range_space, &data) < 0)
8903 data.res = isl_union_pw_multi_aff_free(data.res);
8905 isl_space_free(space);
8906 isl_union_pw_multi_aff_free(upma);
8907 return data.res;
8910 /* Construct and return a union piecewise multi affine expression
8911 * that is equal to the given multi union piecewise affine expression,
8912 * in the special case of a 0D multi union piecewise affine expression.
8914 * Construct a union piecewise multi affine expression
8915 * on top of the explicit domain of the input.
8917 __isl_give isl_union_pw_multi_aff *
8918 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8919 __isl_take isl_multi_union_pw_aff *mupa)
8921 isl_space *space;
8922 isl_multi_val *mv;
8923 isl_union_set *domain;
8925 space = isl_multi_union_pw_aff_get_space(mupa);
8926 mv = isl_multi_val_zero(space);
8927 domain = isl_multi_union_pw_aff_domain(mupa);
8928 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8931 /* Construct and return a union piecewise multi affine expression
8932 * that is equal to the given multi union piecewise affine expression.
8934 * If the input is zero-dimensional, then
8935 * construct a union piecewise multi affine expression
8936 * on top of the explicit domain of the input.
8938 __isl_give isl_union_pw_multi_aff *
8939 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8940 __isl_take isl_multi_union_pw_aff *mupa)
8942 int i;
8943 isl_size n;
8944 isl_space *space;
8945 isl_union_pw_multi_aff *upma;
8946 isl_union_pw_aff *upa;
8948 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8949 if (n < 0)
8950 mupa = isl_multi_union_pw_aff_free(mupa);
8951 if (!mupa)
8952 return NULL;
8954 if (n == 0)
8955 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8957 space = isl_multi_union_pw_aff_get_space(mupa);
8958 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8959 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8961 for (i = 1; i < n; ++i) {
8962 isl_union_pw_multi_aff *upma_i;
8964 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8965 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8966 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8969 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8971 isl_multi_union_pw_aff_free(mupa);
8972 return upma;
8975 /* Intersect the range of "mupa" with "range",
8976 * in the special case where "mupa" is 0D.
8978 * Intersect the domain of "mupa" with the constraints on the parameters
8979 * of "range".
8981 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8982 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8984 range = isl_set_params(range);
8985 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8986 return mupa;
8989 /* Intersect the range of "mupa" with "range".
8990 * That is, keep only those domain elements that have a function value
8991 * in "range".
8993 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8994 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8996 isl_union_pw_multi_aff *upma;
8997 isl_union_set *domain;
8998 isl_space *space;
8999 isl_size n;
9000 int match;
9002 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9003 if (n < 0 || !range)
9004 goto error;
9006 space = isl_set_get_space(range);
9007 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9008 space, isl_dim_set);
9009 isl_space_free(space);
9010 if (match < 0)
9011 goto error;
9012 if (!match)
9013 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9014 "space don't match", goto error);
9015 if (n == 0)
9016 return mupa_intersect_range_0D(mupa, range);
9018 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9019 isl_multi_union_pw_aff_copy(mupa));
9020 domain = isl_union_set_from_set(range);
9021 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9022 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9024 return mupa;
9025 error:
9026 isl_multi_union_pw_aff_free(mupa);
9027 isl_set_free(range);
9028 return NULL;
9031 /* Return the shared domain of the elements of "mupa",
9032 * in the special case where "mupa" is zero-dimensional.
9034 * Return the explicit domain of "mupa".
9035 * Note that this domain may be a parameter set, either
9036 * because "mupa" is meant to live in a set space or
9037 * because no explicit domain has been set.
9039 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9040 __isl_take isl_multi_union_pw_aff *mupa)
9042 isl_union_set *dom;
9044 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9045 isl_multi_union_pw_aff_free(mupa);
9047 return dom;
9050 /* Return the shared domain of the elements of "mupa".
9052 * If "mupa" is zero-dimensional, then return its explicit domain.
9054 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9055 __isl_take isl_multi_union_pw_aff *mupa)
9057 int i;
9058 isl_size n;
9059 isl_union_pw_aff *upa;
9060 isl_union_set *dom;
9062 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9063 if (n < 0)
9064 mupa = isl_multi_union_pw_aff_free(mupa);
9065 if (!mupa)
9066 return NULL;
9068 if (n == 0)
9069 return isl_multi_union_pw_aff_domain_0D(mupa);
9071 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9072 dom = isl_union_pw_aff_domain(upa);
9073 for (i = 1; i < n; ++i) {
9074 isl_union_set *dom_i;
9076 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9077 dom_i = isl_union_pw_aff_domain(upa);
9078 dom = isl_union_set_intersect(dom, dom_i);
9081 isl_multi_union_pw_aff_free(mupa);
9082 return dom;
9085 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9086 * In particular, the spaces have been aligned.
9087 * The result is defined over the shared domain of the elements of "mupa"
9089 * We first extract the parametric constant part of "aff" and
9090 * define that over the shared domain.
9091 * Then we iterate over all input dimensions of "aff" and add the corresponding
9092 * multiples of the elements of "mupa".
9093 * Finally, we consider the integer divisions, calling the function
9094 * recursively to obtain an isl_union_pw_aff corresponding to the
9095 * integer division argument.
9097 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9098 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9100 int i;
9101 isl_size n_in, n_div;
9102 isl_union_pw_aff *upa;
9103 isl_union_set *uset;
9104 isl_val *v;
9105 isl_aff *cst;
9107 n_in = isl_aff_dim(aff, isl_dim_in);
9108 n_div = isl_aff_dim(aff, isl_dim_div);
9109 if (n_in < 0 || n_div < 0)
9110 goto error;
9112 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9113 cst = isl_aff_copy(aff);
9114 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9115 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9116 cst = isl_aff_project_domain_on_params(cst);
9117 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9119 for (i = 0; i < n_in; ++i) {
9120 isl_union_pw_aff *upa_i;
9122 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9123 continue;
9124 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9125 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9126 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9127 upa = isl_union_pw_aff_add(upa, upa_i);
9130 for (i = 0; i < n_div; ++i) {
9131 isl_aff *div;
9132 isl_union_pw_aff *upa_i;
9134 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9135 continue;
9136 div = isl_aff_get_div(aff, i);
9137 upa_i = multi_union_pw_aff_apply_aff(
9138 isl_multi_union_pw_aff_copy(mupa), div);
9139 upa_i = isl_union_pw_aff_floor(upa_i);
9140 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9141 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9142 upa = isl_union_pw_aff_add(upa, upa_i);
9145 isl_multi_union_pw_aff_free(mupa);
9146 isl_aff_free(aff);
9148 return upa;
9149 error:
9150 isl_multi_union_pw_aff_free(mupa);
9151 isl_aff_free(aff);
9152 return NULL;
9155 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9156 * with the domain of "aff".
9157 * Furthermore, the dimension of this space needs to be greater than zero.
9158 * The result is defined over the shared domain of the elements of "mupa"
9160 * We perform these checks and then hand over control to
9161 * multi_union_pw_aff_apply_aff.
9163 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9164 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9166 isl_size dim;
9167 isl_space *space1, *space2;
9168 isl_bool equal;
9170 mupa = isl_multi_union_pw_aff_align_params(mupa,
9171 isl_aff_get_space(aff));
9172 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9173 if (!mupa || !aff)
9174 goto error;
9176 space1 = isl_multi_union_pw_aff_get_space(mupa);
9177 space2 = isl_aff_get_domain_space(aff);
9178 equal = isl_space_is_equal(space1, space2);
9179 isl_space_free(space1);
9180 isl_space_free(space2);
9181 if (equal < 0)
9182 goto error;
9183 if (!equal)
9184 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9185 "spaces don't match", goto error);
9186 dim = isl_aff_dim(aff, isl_dim_in);
9187 if (dim < 0)
9188 goto error;
9189 if (dim == 0)
9190 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9191 "cannot determine domains", goto error);
9193 return multi_union_pw_aff_apply_aff(mupa, aff);
9194 error:
9195 isl_multi_union_pw_aff_free(mupa);
9196 isl_aff_free(aff);
9197 return NULL;
9200 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9201 * The space of "mupa" is known to be compatible with the domain of "ma".
9203 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9204 * on the domain of "mupa".
9206 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9207 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9209 isl_union_set *dom;
9211 dom = isl_multi_union_pw_aff_domain(mupa);
9212 ma = isl_multi_aff_project_domain_on_params(ma);
9214 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9217 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9218 * with the domain of "ma".
9219 * The result is defined over the shared domain of the elements of "mupa"
9221 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9222 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9224 isl_space *space1, *space2;
9225 isl_multi_union_pw_aff *res;
9226 isl_bool equal;
9227 int i;
9228 isl_size n_in, n_out;
9230 mupa = isl_multi_union_pw_aff_align_params(mupa,
9231 isl_multi_aff_get_space(ma));
9232 ma = isl_multi_aff_align_params(ma,
9233 isl_multi_union_pw_aff_get_space(mupa));
9234 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9235 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9236 if (!mupa || n_in < 0 || n_out < 0)
9237 goto error;
9239 space1 = isl_multi_union_pw_aff_get_space(mupa);
9240 space2 = isl_multi_aff_get_domain_space(ma);
9241 equal = isl_space_is_equal(space1, space2);
9242 isl_space_free(space1);
9243 isl_space_free(space2);
9244 if (equal < 0)
9245 goto error;
9246 if (!equal)
9247 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9248 "spaces don't match", goto error);
9249 if (n_in == 0)
9250 return mupa_apply_multi_aff_0D(mupa, ma);
9252 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9253 res = isl_multi_union_pw_aff_alloc(space1);
9255 for (i = 0; i < n_out; ++i) {
9256 isl_aff *aff;
9257 isl_union_pw_aff *upa;
9259 aff = isl_multi_aff_get_aff(ma, i);
9260 upa = multi_union_pw_aff_apply_aff(
9261 isl_multi_union_pw_aff_copy(mupa), aff);
9262 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9265 isl_multi_aff_free(ma);
9266 isl_multi_union_pw_aff_free(mupa);
9267 return res;
9268 error:
9269 isl_multi_union_pw_aff_free(mupa);
9270 isl_multi_aff_free(ma);
9271 return NULL;
9274 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9275 * The space of "mupa" is known to be compatible with the domain of "pa".
9277 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9278 * on the domain of "mupa".
9280 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9281 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9283 isl_union_set *dom;
9285 dom = isl_multi_union_pw_aff_domain(mupa);
9286 pa = isl_pw_aff_project_domain_on_params(pa);
9288 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9291 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9292 * with the domain of "pa".
9293 * Furthermore, the dimension of this space needs to be greater than zero.
9294 * The result is defined over the shared domain of the elements of "mupa"
9296 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9297 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9299 int i;
9300 isl_bool equal;
9301 isl_size n_in;
9302 isl_space *space, *space2;
9303 isl_union_pw_aff *upa;
9305 mupa = isl_multi_union_pw_aff_align_params(mupa,
9306 isl_pw_aff_get_space(pa));
9307 pa = isl_pw_aff_align_params(pa,
9308 isl_multi_union_pw_aff_get_space(mupa));
9309 if (!mupa || !pa)
9310 goto error;
9312 space = isl_multi_union_pw_aff_get_space(mupa);
9313 space2 = isl_pw_aff_get_domain_space(pa);
9314 equal = isl_space_is_equal(space, space2);
9315 isl_space_free(space);
9316 isl_space_free(space2);
9317 if (equal < 0)
9318 goto error;
9319 if (!equal)
9320 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9321 "spaces don't match", goto error);
9322 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9323 if (n_in < 0)
9324 goto error;
9325 if (n_in == 0)
9326 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9328 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9329 upa = isl_union_pw_aff_empty(space);
9331 for (i = 0; i < pa->n; ++i) {
9332 isl_aff *aff;
9333 isl_set *domain;
9334 isl_multi_union_pw_aff *mupa_i;
9335 isl_union_pw_aff *upa_i;
9337 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9338 domain = isl_set_copy(pa->p[i].set);
9339 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9340 aff = isl_aff_copy(pa->p[i].aff);
9341 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9342 upa = isl_union_pw_aff_union_add(upa, upa_i);
9345 isl_multi_union_pw_aff_free(mupa);
9346 isl_pw_aff_free(pa);
9347 return upa;
9348 error:
9349 isl_multi_union_pw_aff_free(mupa);
9350 isl_pw_aff_free(pa);
9351 return NULL;
9354 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9355 * The space of "mupa" is known to be compatible with the domain of "pma".
9357 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9358 * on the domain of "mupa".
9360 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9361 __isl_take isl_multi_union_pw_aff *mupa,
9362 __isl_take isl_pw_multi_aff *pma)
9364 isl_union_set *dom;
9366 dom = isl_multi_union_pw_aff_domain(mupa);
9367 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9369 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9372 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9373 * with the domain of "pma".
9374 * The result is defined over the shared domain of the elements of "mupa"
9376 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9377 __isl_take isl_multi_union_pw_aff *mupa,
9378 __isl_take isl_pw_multi_aff *pma)
9380 isl_space *space1, *space2;
9381 isl_multi_union_pw_aff *res;
9382 isl_bool equal;
9383 int i;
9384 isl_size n_in, n_out;
9386 mupa = isl_multi_union_pw_aff_align_params(mupa,
9387 isl_pw_multi_aff_get_space(pma));
9388 pma = isl_pw_multi_aff_align_params(pma,
9389 isl_multi_union_pw_aff_get_space(mupa));
9390 if (!mupa || !pma)
9391 goto error;
9393 space1 = isl_multi_union_pw_aff_get_space(mupa);
9394 space2 = isl_pw_multi_aff_get_domain_space(pma);
9395 equal = isl_space_is_equal(space1, space2);
9396 isl_space_free(space1);
9397 isl_space_free(space2);
9398 if (equal < 0)
9399 goto error;
9400 if (!equal)
9401 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9402 "spaces don't match", goto error);
9403 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9404 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9405 if (n_in < 0 || n_out < 0)
9406 goto error;
9407 if (n_in == 0)
9408 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9410 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9411 res = isl_multi_union_pw_aff_alloc(space1);
9413 for (i = 0; i < n_out; ++i) {
9414 isl_pw_aff *pa;
9415 isl_union_pw_aff *upa;
9417 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9418 upa = isl_multi_union_pw_aff_apply_pw_aff(
9419 isl_multi_union_pw_aff_copy(mupa), pa);
9420 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9423 isl_pw_multi_aff_free(pma);
9424 isl_multi_union_pw_aff_free(mupa);
9425 return res;
9426 error:
9427 isl_multi_union_pw_aff_free(mupa);
9428 isl_pw_multi_aff_free(pma);
9429 return NULL;
9432 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9433 * If the explicit domain only keeps track of constraints on the parameters,
9434 * then only update those constraints.
9436 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9437 __isl_take isl_multi_union_pw_aff *mupa,
9438 __isl_keep isl_union_pw_multi_aff *upma)
9440 isl_bool is_params;
9442 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9443 return isl_multi_union_pw_aff_free(mupa);
9445 mupa = isl_multi_union_pw_aff_cow(mupa);
9446 if (!mupa)
9447 return NULL;
9449 is_params = isl_union_set_is_params(mupa->u.dom);
9450 if (is_params < 0)
9451 return isl_multi_union_pw_aff_free(mupa);
9453 upma = isl_union_pw_multi_aff_copy(upma);
9454 if (is_params)
9455 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9456 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9457 else
9458 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9459 mupa->u.dom, upma);
9460 if (!mupa->u.dom)
9461 return isl_multi_union_pw_aff_free(mupa);
9462 return mupa;
9465 /* Compute the pullback of "mupa" by the function represented by "upma".
9466 * In other words, plug in "upma" in "mupa". The result contains
9467 * expressions defined over the domain space of "upma".
9469 * Run over all elements of "mupa" and plug in "upma" in each of them.
9471 * If "mupa" has an explicit domain, then it is this domain
9472 * that needs to undergo a pullback instead, i.e., a preimage.
9474 __isl_give isl_multi_union_pw_aff *
9475 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9476 __isl_take isl_multi_union_pw_aff *mupa,
9477 __isl_take isl_union_pw_multi_aff *upma)
9479 int i;
9480 isl_size n;
9482 mupa = isl_multi_union_pw_aff_align_params(mupa,
9483 isl_union_pw_multi_aff_get_space(upma));
9484 upma = isl_union_pw_multi_aff_align_params(upma,
9485 isl_multi_union_pw_aff_get_space(mupa));
9486 mupa = isl_multi_union_pw_aff_cow(mupa);
9487 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9488 if (n < 0 || !upma)
9489 goto error;
9491 for (i = 0; i < n; ++i) {
9492 isl_union_pw_aff *upa;
9494 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9495 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9496 isl_union_pw_multi_aff_copy(upma));
9497 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9500 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9501 mupa = preimage_explicit_domain(mupa, upma);
9503 isl_union_pw_multi_aff_free(upma);
9504 return mupa;
9505 error:
9506 isl_multi_union_pw_aff_free(mupa);
9507 isl_union_pw_multi_aff_free(upma);
9508 return NULL;
9511 /* Extract the sequence of elements in "mupa" with domain space "space"
9512 * (ignoring parameters).
9514 * For the elements of "mupa" that are not defined on the specified space,
9515 * the corresponding element in the result is empty.
9517 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9518 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9520 int i;
9521 isl_size n;
9522 isl_space *space_mpa;
9523 isl_multi_pw_aff *mpa;
9525 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9526 if (n < 0 || !space)
9527 goto error;
9529 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9530 space = isl_space_replace_params(space, space_mpa);
9531 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9532 space_mpa);
9533 mpa = isl_multi_pw_aff_alloc(space_mpa);
9535 space = isl_space_from_domain(space);
9536 space = isl_space_add_dims(space, isl_dim_out, 1);
9537 for (i = 0; i < n; ++i) {
9538 isl_union_pw_aff *upa;
9539 isl_pw_aff *pa;
9541 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9542 pa = isl_union_pw_aff_extract_pw_aff(upa,
9543 isl_space_copy(space));
9544 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9545 isl_union_pw_aff_free(upa);
9548 isl_space_free(space);
9549 return mpa;
9550 error:
9551 isl_space_free(space);
9552 return NULL;
9555 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9556 * should modify the base expressions in the input.
9558 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9559 * are taken into account.
9560 * "fn" is applied to each entry in the input.
9562 struct isl_union_pw_multi_aff_un_op_control {
9563 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9564 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9567 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9568 * a second argument) for use as an isl_union_pw_multi_aff_transform
9569 * filter function (which does take a second argument).
9570 * Simply call control->filter without the second argument.
9572 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9573 __isl_take isl_pw_multi_aff *pma, void *user)
9575 struct isl_union_pw_multi_aff_un_op_control *control = user;
9577 return control->filter(pma);
9580 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9581 * a second argument) for use as an isl_union_pw_multi_aff_transform
9582 * base function (which does take a second argument).
9583 * Simply call control->fn without the second argument.
9585 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9586 __isl_take isl_pw_multi_aff *pma, void *user)
9588 struct isl_union_pw_multi_aff_un_op_control *control = user;
9590 return control->fn(pma);
9593 /* Construct an isl_union_pw_multi_aff that is obtained by
9594 * modifying "upma" according to "control".
9596 * isl_union_pw_multi_aff_transform performs essentially
9597 * the same operation, but takes a filter and a callback function
9598 * of a different form (with an extra argument).
9599 * Call isl_union_pw_multi_aff_transform with wrappers
9600 * that remove this extra argument.
9602 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9603 __isl_take isl_union_pw_multi_aff *upma,
9604 struct isl_union_pw_multi_aff_un_op_control *control)
9606 struct isl_union_pw_multi_aff_transform_control t_control = {
9607 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9608 .filter_user = control,
9609 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9610 .fn_user = control,
9613 return isl_union_pw_multi_aff_transform(upma, &t_control);
9616 /* For each function in "upma" of the form A -> [B -> C],
9617 * extract the function A -> B and collect the results.
9619 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9620 __isl_take isl_union_pw_multi_aff *upma)
9622 struct isl_union_pw_multi_aff_un_op_control control = {
9623 .filter = &isl_pw_multi_aff_range_is_wrapping,
9624 .fn = &isl_pw_multi_aff_range_factor_domain,
9626 return isl_union_pw_multi_aff_un_op(upma, &control);
9629 /* For each function in "upma" of the form A -> [B -> C],
9630 * extract the function A -> C and collect the results.
9632 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9633 __isl_take isl_union_pw_multi_aff *upma)
9635 struct isl_union_pw_multi_aff_un_op_control control = {
9636 .filter = &isl_pw_multi_aff_range_is_wrapping,
9637 .fn = &isl_pw_multi_aff_range_factor_range,
9639 return isl_union_pw_multi_aff_un_op(upma, &control);
9642 /* Evaluate the affine function "aff" in the void point "pnt".
9643 * In particular, return the value NaN.
9645 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9646 __isl_take isl_point *pnt)
9648 isl_ctx *ctx;
9650 ctx = isl_point_get_ctx(pnt);
9651 isl_aff_free(aff);
9652 isl_point_free(pnt);
9653 return isl_val_nan(ctx);
9656 /* Evaluate the affine expression "aff"
9657 * in the coordinates (with denominator) "pnt".
9659 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9660 __isl_keep isl_vec *pnt)
9662 isl_int n, d;
9663 isl_ctx *ctx;
9664 isl_val *v;
9666 if (!aff || !pnt)
9667 return NULL;
9669 ctx = isl_vec_get_ctx(aff);
9670 isl_int_init(n);
9671 isl_int_init(d);
9672 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9673 isl_int_mul(d, aff->el[0], pnt->el[0]);
9674 v = isl_val_rat_from_isl_int(ctx, n, d);
9675 v = isl_val_normalize(v);
9676 isl_int_clear(n);
9677 isl_int_clear(d);
9679 return v;
9682 /* Check that the domain space of "aff" is equal to "space".
9684 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9685 __isl_keep isl_space *space)
9687 isl_bool ok;
9689 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9690 if (ok < 0)
9691 return isl_stat_error;
9692 if (!ok)
9693 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9694 "incompatible spaces", return isl_stat_error);
9695 return isl_stat_ok;
9698 /* Evaluate the affine function "aff" in "pnt".
9700 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9701 __isl_take isl_point *pnt)
9703 isl_bool is_void;
9704 isl_val *v;
9705 isl_local_space *ls;
9707 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9708 goto error;
9709 is_void = isl_point_is_void(pnt);
9710 if (is_void < 0)
9711 goto error;
9712 if (is_void)
9713 return eval_void(aff, pnt);
9715 ls = isl_aff_get_domain_local_space(aff);
9716 pnt = isl_local_space_lift_point(ls, pnt);
9718 v = eval(aff->v, isl_point_peek_vec(pnt));
9720 isl_aff_free(aff);
9721 isl_point_free(pnt);
9723 return v;
9724 error:
9725 isl_aff_free(aff);
9726 isl_point_free(pnt);
9727 return NULL;