isl_space_range_reverse: extract out isl_space_reverse_wrapped
[isl.git] / isl_aff.c
blob2886aa7d73ad1544323af0a2fb4960930320356f
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,2020 Cerebras Systems
8 * Copyright 2021 Sven Verdoolaege
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
14 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
15 * 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_union_map_private.h>
26 #include <isl_aff_private.h>
27 #include <isl_space_private.h>
28 #include <isl_local_space_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_mat_private.h>
31 #include <isl_id_private.h>
32 #include <isl/constraint.h>
33 #include <isl_seq.h>
34 #include <isl/set.h>
35 #include <isl_val_private.h>
36 #include <isl_point_private.h>
37 #include <isl_config.h>
39 #undef EL_BASE
40 #define EL_BASE aff
42 #include <isl_list_templ.c>
43 #include <isl_list_read_templ.c>
45 #undef EL_BASE
46 #define EL_BASE pw_aff
48 #include <isl_list_templ.c>
49 #include <isl_list_read_templ.c>
51 #undef EL_BASE
52 #define EL_BASE pw_multi_aff
54 #include <isl_list_templ.c>
55 #include <isl_list_read_templ.c>
57 #undef EL_BASE
58 #define EL_BASE union_pw_aff
60 #include <isl_list_templ.c>
61 #include <isl_list_read_templ.c>
63 #undef EL_BASE
64 #define EL_BASE union_pw_multi_aff
66 #include <isl_list_templ.c>
68 /* Construct an isl_aff from the given domain local space "ls" and
69 * coefficients "v", where the local space is known to be valid
70 * for an affine expression.
72 static __isl_give isl_aff *isl_aff_alloc_vec_validated(
73 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
75 isl_aff *aff;
77 if (!ls || !v)
78 goto error;
80 aff = isl_calloc_type(v->ctx, struct isl_aff);
81 if (!aff)
82 goto error;
84 aff->ref = 1;
85 aff->ls = ls;
86 aff->v = v;
88 return aff;
89 error:
90 isl_local_space_free(ls);
91 isl_vec_free(v);
92 return NULL;
95 /* Construct an isl_aff from the given domain local space "ls" and
96 * coefficients "v".
98 * First check that "ls" is a valid domain local space
99 * for an affine expression.
101 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
102 __isl_take isl_vec *v)
104 isl_ctx *ctx;
106 if (!ls)
107 return NULL;
109 ctx = isl_local_space_get_ctx(ls);
110 if (!isl_local_space_divs_known(ls))
111 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
112 goto error);
113 if (!isl_local_space_is_set(ls))
114 isl_die(ctx, isl_error_invalid,
115 "domain of affine expression should be a set",
116 goto error);
117 return isl_aff_alloc_vec_validated(ls, v);
118 error:
119 isl_local_space_free(ls);
120 isl_vec_free(v);
121 return NULL;
124 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
126 isl_ctx *ctx;
127 isl_vec *v;
128 isl_size total;
130 if (!ls)
131 return NULL;
133 ctx = isl_local_space_get_ctx(ls);
135 total = isl_local_space_dim(ls, isl_dim_all);
136 if (total < 0)
137 goto error;
138 v = isl_vec_alloc(ctx, 1 + 1 + total);
139 return isl_aff_alloc_vec(ls, v);
140 error:
141 isl_local_space_free(ls);
142 return NULL;
145 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
147 if (!aff)
148 return NULL;
150 aff->ref++;
151 return aff;
154 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
156 if (!aff)
157 return NULL;
159 return isl_aff_alloc_vec_validated(isl_local_space_copy(aff->ls),
160 isl_vec_copy(aff->v));
163 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
165 if (!aff)
166 return NULL;
168 if (aff->ref == 1)
169 return aff;
170 aff->ref--;
171 return isl_aff_dup(aff);
174 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
176 isl_aff *aff;
178 aff = isl_aff_alloc(ls);
179 if (!aff)
180 return NULL;
182 isl_int_set_si(aff->v->el[0], 1);
183 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
185 return aff;
188 /* Return an affine expression that is equal to zero on domain space "space".
190 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
192 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
195 /* This function performs the same operation as isl_aff_zero_on_domain_space,
196 * but is considered as a function on an isl_space when exported.
198 __isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
200 return isl_aff_zero_on_domain_space(space);
203 /* Return a piecewise affine expression defined on the specified domain
204 * that is equal to zero.
206 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
208 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
211 /* Change "aff" into a NaN.
213 * Note that this function gets called from isl_aff_nan_on_domain,
214 * so "aff" may not have been initialized yet.
216 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
218 aff = isl_aff_cow(aff);
219 if (!aff)
220 return NULL;
222 aff->v = isl_vec_clr(aff->v);
223 if (!aff->v)
224 return isl_aff_free(aff);
226 return aff;
229 /* Return an affine expression defined on the specified domain
230 * that represents NaN.
232 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
234 isl_aff *aff;
236 aff = isl_aff_alloc(ls);
237 return isl_aff_set_nan(aff);
240 /* Return an affine expression defined on the specified domain space
241 * that represents NaN.
243 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
245 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
248 /* Return a piecewise affine expression defined on the specified domain space
249 * that represents NaN.
251 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
252 __isl_take isl_space *space)
254 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
257 /* Return a piecewise affine expression defined on the specified domain
258 * that represents NaN.
260 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
262 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
265 /* Return an affine expression that is equal to "val" on
266 * domain local space "ls".
268 * Note that the encoding for the special value NaN
269 * is the same in isl_val and isl_aff, so this does not need
270 * to be treated in any special way.
272 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
273 __isl_take isl_val *val)
275 isl_aff *aff;
277 if (!ls || !val)
278 goto error;
279 if (!isl_val_is_rat(val) && !isl_val_is_nan(val))
280 isl_die(isl_val_get_ctx(val), isl_error_invalid,
281 "expecting rational value or NaN", goto error);
283 aff = isl_aff_alloc(isl_local_space_copy(ls));
284 if (!aff)
285 goto error;
287 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
288 isl_int_set(aff->v->el[1], val->n);
289 isl_int_set(aff->v->el[0], val->d);
291 isl_local_space_free(ls);
292 isl_val_free(val);
293 return aff;
294 error:
295 isl_local_space_free(ls);
296 isl_val_free(val);
297 return NULL;
300 /* Return an affine expression that is equal to "val" on domain space "space".
302 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
303 __isl_take isl_val *val)
305 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
308 /* Return an affine expression that is equal to the specified dimension
309 * in "ls".
311 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
312 enum isl_dim_type type, unsigned pos)
314 isl_space *space;
315 isl_aff *aff;
317 if (!ls)
318 return NULL;
320 space = isl_local_space_get_space(ls);
321 if (!space)
322 goto error;
323 if (isl_space_is_map(space))
324 isl_die(isl_space_get_ctx(space), isl_error_invalid,
325 "expecting (parameter) set space", goto error);
326 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
327 goto error;
329 isl_space_free(space);
330 aff = isl_aff_alloc(ls);
331 if (!aff)
332 return NULL;
334 pos += isl_local_space_offset(aff->ls, type);
336 isl_int_set_si(aff->v->el[0], 1);
337 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
338 isl_int_set_si(aff->v->el[1 + pos], 1);
340 return aff;
341 error:
342 isl_local_space_free(ls);
343 isl_space_free(space);
344 return NULL;
347 /* Return a piecewise affine expression that is equal to
348 * the specified dimension in "ls".
350 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
351 enum isl_dim_type type, unsigned pos)
353 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
356 /* Return an affine expression that is equal to the parameter
357 * in the domain space "space" with identifier "id".
359 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
360 __isl_take isl_space *space, __isl_take isl_id *id)
362 int pos;
363 isl_local_space *ls;
365 if (!space || !id)
366 goto error;
367 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
368 if (pos < 0)
369 isl_die(isl_space_get_ctx(space), isl_error_invalid,
370 "parameter not found in space", goto error);
371 isl_id_free(id);
372 ls = isl_local_space_from_space(space);
373 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
374 error:
375 isl_space_free(space);
376 isl_id_free(id);
377 return NULL;
380 /* This function performs the same operation as
381 * isl_aff_param_on_domain_space_id,
382 * but is considered as a function on an isl_space when exported.
384 __isl_give isl_aff *isl_space_param_aff_on_domain_id(
385 __isl_take isl_space *space, __isl_take isl_id *id)
387 return isl_aff_param_on_domain_space_id(space, id);
390 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
392 if (!aff)
393 return NULL;
395 if (--aff->ref > 0)
396 return NULL;
398 isl_local_space_free(aff->ls);
399 isl_vec_free(aff->v);
401 free(aff);
403 return NULL;
406 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
408 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
411 /* Return a hash value that digests "aff".
413 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
415 uint32_t hash, ls_hash, v_hash;
417 if (!aff)
418 return 0;
420 hash = isl_hash_init();
421 ls_hash = isl_local_space_get_hash(aff->ls);
422 isl_hash_hash(hash, ls_hash);
423 v_hash = isl_vec_get_hash(aff->v);
424 isl_hash_hash(hash, v_hash);
426 return hash;
429 /* Return the domain local space of "aff".
431 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
432 __isl_keep isl_aff *aff)
434 return aff ? aff->ls : NULL;
437 /* Return the number of variables of the given type in the domain of "aff".
439 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
441 isl_local_space *ls;
443 ls = isl_aff_peek_domain_local_space(aff);
444 return isl_local_space_dim(ls, type);
447 /* Externally, an isl_aff has a map space, but internally, the
448 * ls field corresponds to the domain of that space.
450 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
452 if (!aff)
453 return isl_size_error;
454 if (type == isl_dim_out)
455 return 1;
456 if (type == isl_dim_in)
457 type = isl_dim_set;
458 return isl_aff_domain_dim(aff, type);
461 /* Return the offset of the first coefficient of type "type" in
462 * the domain of "aff".
464 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
466 isl_local_space *ls;
468 ls = isl_aff_peek_domain_local_space(aff);
469 return isl_local_space_offset(ls, type);
472 /* Return the position of the dimension of the given type and name
473 * in "aff".
474 * Return -1 if no such dimension can be found.
476 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
477 const char *name)
479 if (!aff)
480 return -1;
481 if (type == isl_dim_out)
482 return -1;
483 if (type == isl_dim_in)
484 type = isl_dim_set;
485 return isl_local_space_find_dim_by_name(aff->ls, type, name);
488 /* Return the domain space of "aff".
490 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
492 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
495 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
497 return isl_space_copy(isl_aff_peek_domain_space(aff));
500 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
502 isl_space *space;
503 if (!aff)
504 return NULL;
505 space = isl_local_space_get_space(aff->ls);
506 space = isl_space_from_domain(space);
507 space = isl_space_add_dims(space, isl_dim_out, 1);
508 return space;
511 /* Return a copy of the domain space of "aff".
513 __isl_give isl_local_space *isl_aff_get_domain_local_space(
514 __isl_keep isl_aff *aff)
516 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
519 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
521 isl_local_space *ls;
522 if (!aff)
523 return NULL;
524 ls = isl_local_space_copy(aff->ls);
525 ls = isl_local_space_from_domain(ls);
526 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
527 return ls;
530 /* Return the local space of the domain of "aff".
531 * This may be either a copy or the local space itself
532 * if there is only one reference to "aff".
533 * This allows the local space to be modified inplace
534 * if both the expression and its local space have only a single reference.
535 * The caller is not allowed to modify "aff" between this call and
536 * a subsequent call to isl_aff_restore_domain_local_space.
537 * The only exception is that isl_aff_free can be called instead.
539 __isl_give isl_local_space *isl_aff_take_domain_local_space(
540 __isl_keep isl_aff *aff)
542 isl_local_space *ls;
544 if (!aff)
545 return NULL;
546 if (aff->ref != 1)
547 return isl_aff_get_domain_local_space(aff);
548 ls = aff->ls;
549 aff->ls = NULL;
550 return ls;
553 /* Set the local space of the domain of "aff" to "ls",
554 * where the local space of "aff" may be missing
555 * due to a preceding call to isl_aff_take_domain_local_space.
556 * However, in this case, "aff" only has a single reference and
557 * then the call to isl_aff_cow has no effect.
559 __isl_give isl_aff *isl_aff_restore_domain_local_space(
560 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
562 if (!aff || !ls)
563 goto error;
565 if (aff->ls == ls) {
566 isl_local_space_free(ls);
567 return aff;
570 aff = isl_aff_cow(aff);
571 if (!aff)
572 goto error;
573 isl_local_space_free(aff->ls);
574 aff->ls = ls;
576 return aff;
577 error:
578 isl_aff_free(aff);
579 isl_local_space_free(ls);
580 return NULL;
583 /* Externally, an isl_aff has a map space, but internally, the
584 * ls field corresponds to the domain of that space.
586 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
587 enum isl_dim_type type, unsigned pos)
589 if (!aff)
590 return NULL;
591 if (type == isl_dim_out)
592 return NULL;
593 if (type == isl_dim_in)
594 type = isl_dim_set;
595 return isl_local_space_get_dim_name(aff->ls, type, pos);
598 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
599 __isl_take isl_space *space)
601 aff = isl_aff_cow(aff);
602 if (!aff || !space)
603 goto error;
605 aff->ls = isl_local_space_reset_space(aff->ls, space);
606 if (!aff->ls)
607 return isl_aff_free(aff);
609 return aff;
610 error:
611 isl_aff_free(aff);
612 isl_space_free(space);
613 return NULL;
616 /* Reset the space of "aff". This function is called from isl_pw_templ.c
617 * and doesn't know if the space of an element object is represented
618 * directly or through its domain. It therefore passes along both.
620 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
621 __isl_take isl_space *space, __isl_take isl_space *domain)
623 isl_space_free(space);
624 return isl_aff_reset_domain_space(aff, domain);
627 /* Reorder the dimensions of the domain of "aff" according
628 * to the given reordering.
630 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
631 __isl_take isl_reordering *r)
633 aff = isl_aff_cow(aff);
634 if (!aff)
635 goto error;
637 r = isl_reordering_extend(r, aff->ls->div->n_row);
638 aff->v = isl_vec_reorder(aff->v, 2, isl_reordering_copy(r));
639 aff->ls = isl_local_space_realign(aff->ls, r);
641 if (!aff->v || !aff->ls)
642 return isl_aff_free(aff);
644 return aff;
645 error:
646 isl_aff_free(aff);
647 isl_reordering_free(r);
648 return NULL;
651 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
652 __isl_take isl_space *model)
654 isl_space *domain_space;
655 isl_bool equal_params;
657 domain_space = isl_aff_peek_domain_space(aff);
658 equal_params = isl_space_has_equal_params(domain_space, model);
659 if (equal_params < 0)
660 goto error;
661 if (!equal_params) {
662 isl_reordering *exp;
664 exp = isl_parameter_alignment_reordering(domain_space, model);
665 aff = isl_aff_realign_domain(aff, exp);
668 isl_space_free(model);
669 return aff;
670 error:
671 isl_space_free(model);
672 isl_aff_free(aff);
673 return NULL;
676 #undef TYPE
677 #define TYPE isl_aff
678 #include "isl_unbind_params_templ.c"
680 /* Is "aff" obviously equal to zero?
682 * If the denominator is zero, then "aff" is not equal to zero.
684 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
686 int pos;
688 if (!aff)
689 return isl_bool_error;
691 if (isl_int_is_zero(aff->v->el[0]))
692 return isl_bool_false;
693 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
694 return isl_bool_ok(pos < 0);
697 /* Does "aff" represent NaN?
699 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
701 if (!aff)
702 return isl_bool_error;
704 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
707 /* Are "aff1" and "aff2" obviously equal?
709 * NaN is not equal to anything, not even to another NaN.
711 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
712 __isl_keep isl_aff *aff2)
714 isl_bool equal;
716 if (!aff1 || !aff2)
717 return isl_bool_error;
719 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
720 return isl_bool_false;
722 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
723 if (equal < 0 || !equal)
724 return equal;
726 return isl_vec_is_equal(aff1->v, aff2->v);
729 /* Return the common denominator of "aff" in "v".
731 * We cannot return anything meaningful in case of a NaN.
733 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
735 if (!aff)
736 return isl_stat_error;
737 if (isl_aff_is_nan(aff))
738 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
739 "cannot get denominator of NaN", return isl_stat_error);
740 isl_int_set(*v, aff->v->el[0]);
741 return isl_stat_ok;
744 /* Return the common denominator of "aff".
746 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
748 isl_ctx *ctx;
750 if (!aff)
751 return NULL;
753 ctx = isl_aff_get_ctx(aff);
754 if (isl_aff_is_nan(aff))
755 return isl_val_nan(ctx);
756 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
759 /* Return the constant term of "aff".
761 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
763 isl_ctx *ctx;
764 isl_val *v;
766 if (!aff)
767 return NULL;
769 ctx = isl_aff_get_ctx(aff);
770 if (isl_aff_is_nan(aff))
771 return isl_val_nan(ctx);
772 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
773 return isl_val_normalize(v);
776 /* Return the coefficient of the variable of type "type" at position "pos"
777 * of "aff".
779 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
780 enum isl_dim_type type, int pos)
782 isl_ctx *ctx;
783 isl_val *v;
785 if (!aff)
786 return NULL;
788 ctx = isl_aff_get_ctx(aff);
789 if (type == isl_dim_out)
790 isl_die(ctx, isl_error_invalid,
791 "output/set dimension does not have a coefficient",
792 return NULL);
793 if (type == isl_dim_in)
794 type = isl_dim_set;
796 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
797 return NULL;
799 if (isl_aff_is_nan(aff))
800 return isl_val_nan(ctx);
801 pos += isl_local_space_offset(aff->ls, type);
802 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
803 return isl_val_normalize(v);
806 /* Return the sign of the coefficient of the variable of type "type"
807 * at position "pos" of "aff".
809 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
810 int pos)
812 isl_ctx *ctx;
814 if (!aff)
815 return 0;
817 ctx = isl_aff_get_ctx(aff);
818 if (type == isl_dim_out)
819 isl_die(ctx, isl_error_invalid,
820 "output/set dimension does not have a coefficient",
821 return 0);
822 if (type == isl_dim_in)
823 type = isl_dim_set;
825 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
826 return 0;
828 pos += isl_local_space_offset(aff->ls, type);
829 return isl_int_sgn(aff->v->el[1 + pos]);
832 /* Replace the numerator of the constant term of "aff" by "v".
834 * A NaN is unaffected by this operation.
836 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
838 if (!aff)
839 return NULL;
840 if (isl_aff_is_nan(aff))
841 return aff;
842 aff = isl_aff_cow(aff);
843 if (!aff)
844 return NULL;
846 aff->v = isl_vec_cow(aff->v);
847 if (!aff->v)
848 return isl_aff_free(aff);
850 isl_int_set(aff->v->el[1], v);
852 return aff;
855 /* Replace the constant term of "aff" by "v".
857 * A NaN is unaffected by this operation.
859 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
860 __isl_take isl_val *v)
862 if (!aff || !v)
863 goto error;
865 if (isl_aff_is_nan(aff)) {
866 isl_val_free(v);
867 return aff;
870 if (!isl_val_is_rat(v))
871 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
872 "expecting rational value", goto error);
874 if (isl_int_eq(aff->v->el[1], v->n) &&
875 isl_int_eq(aff->v->el[0], v->d)) {
876 isl_val_free(v);
877 return aff;
880 aff = isl_aff_cow(aff);
881 if (!aff)
882 goto error;
883 aff->v = isl_vec_cow(aff->v);
884 if (!aff->v)
885 goto error;
887 if (isl_int_eq(aff->v->el[0], v->d)) {
888 isl_int_set(aff->v->el[1], v->n);
889 } else if (isl_int_is_one(v->d)) {
890 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
891 } else {
892 isl_seq_scale(aff->v->el + 1,
893 aff->v->el + 1, v->d, aff->v->size - 1);
894 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
895 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
896 aff->v = isl_vec_normalize(aff->v);
897 if (!aff->v)
898 goto error;
901 isl_val_free(v);
902 return aff;
903 error:
904 isl_aff_free(aff);
905 isl_val_free(v);
906 return NULL;
909 /* Add "v" to the constant term of "aff".
911 * A NaN is unaffected by this operation.
913 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
915 if (isl_int_is_zero(v))
916 return aff;
918 if (!aff)
919 return NULL;
920 if (isl_aff_is_nan(aff))
921 return aff;
922 aff = isl_aff_cow(aff);
923 if (!aff)
924 return NULL;
926 aff->v = isl_vec_cow(aff->v);
927 if (!aff->v)
928 return isl_aff_free(aff);
930 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
932 return aff;
935 /* Add "v" to the constant term of "aff",
936 * in case "aff" is a rational expression.
938 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
939 __isl_take isl_val *v)
941 aff = isl_aff_cow(aff);
942 if (!aff)
943 goto error;
945 aff->v = isl_vec_cow(aff->v);
946 if (!aff->v)
947 goto error;
949 if (isl_int_is_one(v->d)) {
950 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
951 } else if (isl_int_eq(aff->v->el[0], v->d)) {
952 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
953 aff->v = isl_vec_normalize(aff->v);
954 if (!aff->v)
955 goto error;
956 } else {
957 isl_seq_scale(aff->v->el + 1,
958 aff->v->el + 1, v->d, aff->v->size - 1);
959 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
960 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
961 aff->v = isl_vec_normalize(aff->v);
962 if (!aff->v)
963 goto error;
966 isl_val_free(v);
967 return aff;
968 error:
969 isl_aff_free(aff);
970 isl_val_free(v);
971 return NULL;
974 /* Return the first argument and free the second.
976 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
977 __isl_take isl_val *v)
979 isl_val_free(v);
980 return aff;
983 /* Replace the first argument by NaN and free the second argument.
985 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
986 __isl_take isl_val *v)
988 isl_val_free(v);
989 return isl_aff_set_nan(aff);
992 /* Add "v" to the constant term of "aff".
994 * A NaN is unaffected by this operation.
995 * Conversely, adding a NaN turns "aff" into a NaN.
997 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
998 __isl_take isl_val *v)
1000 isl_bool is_nan, is_zero, is_rat;
1002 is_nan = isl_aff_is_nan(aff);
1003 is_zero = isl_val_is_zero(v);
1004 if (is_nan < 0 || is_zero < 0)
1005 goto error;
1006 if (is_nan || is_zero)
1007 return pick_free(aff, v);
1009 is_nan = isl_val_is_nan(v);
1010 is_rat = isl_val_is_rat(v);
1011 if (is_nan < 0 || is_rat < 0)
1012 goto error;
1013 if (is_nan)
1014 return set_nan_free_val(aff, v);
1015 if (!is_rat)
1016 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1017 "expecting rational value or NaN", goto error);
1019 return isl_aff_add_rat_constant_val(aff, v);
1020 error:
1021 isl_aff_free(aff);
1022 isl_val_free(v);
1023 return NULL;
1026 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1028 isl_int t;
1030 isl_int_init(t);
1031 isl_int_set_si(t, v);
1032 aff = isl_aff_add_constant(aff, t);
1033 isl_int_clear(t);
1035 return aff;
1038 /* Add "v" to the numerator of the constant term of "aff".
1040 * A NaN is unaffected by this operation.
1042 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1044 if (isl_int_is_zero(v))
1045 return aff;
1047 if (!aff)
1048 return NULL;
1049 if (isl_aff_is_nan(aff))
1050 return aff;
1051 aff = isl_aff_cow(aff);
1052 if (!aff)
1053 return NULL;
1055 aff->v = isl_vec_cow(aff->v);
1056 if (!aff->v)
1057 return isl_aff_free(aff);
1059 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1061 return aff;
1064 /* Add "v" to the numerator of the constant term of "aff".
1066 * A NaN is unaffected by this operation.
1068 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1070 isl_int t;
1072 if (v == 0)
1073 return aff;
1075 isl_int_init(t);
1076 isl_int_set_si(t, v);
1077 aff = isl_aff_add_constant_num(aff, t);
1078 isl_int_clear(t);
1080 return aff;
1083 /* Replace the numerator of the constant term of "aff" by "v".
1085 * A NaN is unaffected by this operation.
1087 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1089 if (!aff)
1090 return NULL;
1091 if (isl_aff_is_nan(aff))
1092 return aff;
1093 aff = isl_aff_cow(aff);
1094 if (!aff)
1095 return NULL;
1097 aff->v = isl_vec_cow(aff->v);
1098 if (!aff->v)
1099 return isl_aff_free(aff);
1101 isl_int_set_si(aff->v->el[1], v);
1103 return aff;
1106 /* Replace the numerator of the coefficient of the variable of type "type"
1107 * at position "pos" of "aff" by "v".
1109 * A NaN is unaffected by this operation.
1111 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1112 enum isl_dim_type type, int pos, isl_int v)
1114 if (!aff)
1115 return NULL;
1117 if (type == isl_dim_out)
1118 isl_die(aff->v->ctx, isl_error_invalid,
1119 "output/set dimension does not have a coefficient",
1120 return isl_aff_free(aff));
1121 if (type == isl_dim_in)
1122 type = isl_dim_set;
1124 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1125 return isl_aff_free(aff);
1127 if (isl_aff_is_nan(aff))
1128 return aff;
1129 aff = isl_aff_cow(aff);
1130 if (!aff)
1131 return NULL;
1133 aff->v = isl_vec_cow(aff->v);
1134 if (!aff->v)
1135 return isl_aff_free(aff);
1137 pos += isl_local_space_offset(aff->ls, type);
1138 isl_int_set(aff->v->el[1 + pos], v);
1140 return aff;
1143 /* Replace the numerator of the coefficient of the variable of type "type"
1144 * at position "pos" of "aff" by "v".
1146 * A NaN is unaffected by this operation.
1148 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1149 enum isl_dim_type type, int pos, int v)
1151 if (!aff)
1152 return NULL;
1154 if (type == isl_dim_out)
1155 isl_die(aff->v->ctx, isl_error_invalid,
1156 "output/set dimension does not have a coefficient",
1157 return isl_aff_free(aff));
1158 if (type == isl_dim_in)
1159 type = isl_dim_set;
1161 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1162 return isl_aff_free(aff);
1164 if (isl_aff_is_nan(aff))
1165 return aff;
1166 pos += isl_local_space_offset(aff->ls, type);
1167 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1168 return aff;
1170 aff = isl_aff_cow(aff);
1171 if (!aff)
1172 return NULL;
1174 aff->v = isl_vec_cow(aff->v);
1175 if (!aff->v)
1176 return isl_aff_free(aff);
1178 isl_int_set_si(aff->v->el[1 + pos], v);
1180 return aff;
1183 /* Replace the coefficient of the variable of type "type" at position "pos"
1184 * of "aff" by "v".
1186 * A NaN is unaffected by this operation.
1188 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1189 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1191 if (!aff || !v)
1192 goto error;
1194 if (type == isl_dim_out)
1195 isl_die(aff->v->ctx, isl_error_invalid,
1196 "output/set dimension does not have a coefficient",
1197 goto error);
1198 if (type == isl_dim_in)
1199 type = isl_dim_set;
1201 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1202 return isl_aff_free(aff);
1204 if (isl_aff_is_nan(aff)) {
1205 isl_val_free(v);
1206 return aff;
1208 if (!isl_val_is_rat(v))
1209 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1210 "expecting rational value", goto error);
1212 pos += isl_local_space_offset(aff->ls, type);
1213 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1214 isl_int_eq(aff->v->el[0], v->d)) {
1215 isl_val_free(v);
1216 return aff;
1219 aff = isl_aff_cow(aff);
1220 if (!aff)
1221 goto error;
1222 aff->v = isl_vec_cow(aff->v);
1223 if (!aff->v)
1224 goto error;
1226 if (isl_int_eq(aff->v->el[0], v->d)) {
1227 isl_int_set(aff->v->el[1 + pos], v->n);
1228 } else if (isl_int_is_one(v->d)) {
1229 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1230 } else {
1231 isl_seq_scale(aff->v->el + 1,
1232 aff->v->el + 1, v->d, aff->v->size - 1);
1233 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1234 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1235 aff->v = isl_vec_normalize(aff->v);
1236 if (!aff->v)
1237 goto error;
1240 isl_val_free(v);
1241 return aff;
1242 error:
1243 isl_aff_free(aff);
1244 isl_val_free(v);
1245 return NULL;
1248 /* Add "v" to the coefficient of the variable of type "type"
1249 * at position "pos" of "aff".
1251 * A NaN is unaffected by this operation.
1253 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1254 enum isl_dim_type type, int pos, isl_int v)
1256 if (!aff)
1257 return NULL;
1259 if (type == isl_dim_out)
1260 isl_die(aff->v->ctx, isl_error_invalid,
1261 "output/set dimension does not have a coefficient",
1262 return isl_aff_free(aff));
1263 if (type == isl_dim_in)
1264 type = isl_dim_set;
1266 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1267 return isl_aff_free(aff);
1269 if (isl_aff_is_nan(aff))
1270 return aff;
1271 aff = isl_aff_cow(aff);
1272 if (!aff)
1273 return NULL;
1275 aff->v = isl_vec_cow(aff->v);
1276 if (!aff->v)
1277 return isl_aff_free(aff);
1279 pos += isl_local_space_offset(aff->ls, type);
1280 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1282 return aff;
1285 /* Add "v" to the coefficient of the variable of type "type"
1286 * at position "pos" of "aff".
1288 * A NaN is unaffected by this operation.
1290 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1291 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1293 if (!aff || !v)
1294 goto error;
1296 if (isl_val_is_zero(v)) {
1297 isl_val_free(v);
1298 return aff;
1301 if (type == isl_dim_out)
1302 isl_die(aff->v->ctx, isl_error_invalid,
1303 "output/set dimension does not have a coefficient",
1304 goto error);
1305 if (type == isl_dim_in)
1306 type = isl_dim_set;
1308 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1309 goto error;
1311 if (isl_aff_is_nan(aff)) {
1312 isl_val_free(v);
1313 return aff;
1315 if (!isl_val_is_rat(v))
1316 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1317 "expecting rational value", goto error);
1319 aff = isl_aff_cow(aff);
1320 if (!aff)
1321 goto error;
1323 aff->v = isl_vec_cow(aff->v);
1324 if (!aff->v)
1325 goto error;
1327 pos += isl_local_space_offset(aff->ls, type);
1328 if (isl_int_is_one(v->d)) {
1329 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1330 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1331 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1332 aff->v = isl_vec_normalize(aff->v);
1333 if (!aff->v)
1334 goto error;
1335 } else {
1336 isl_seq_scale(aff->v->el + 1,
1337 aff->v->el + 1, v->d, aff->v->size - 1);
1338 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1339 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1340 aff->v = isl_vec_normalize(aff->v);
1341 if (!aff->v)
1342 goto error;
1345 isl_val_free(v);
1346 return aff;
1347 error:
1348 isl_aff_free(aff);
1349 isl_val_free(v);
1350 return NULL;
1353 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1354 enum isl_dim_type type, int pos, int v)
1356 isl_int t;
1358 isl_int_init(t);
1359 isl_int_set_si(t, v);
1360 aff = isl_aff_add_coefficient(aff, type, pos, t);
1361 isl_int_clear(t);
1363 return aff;
1366 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1368 if (!aff)
1369 return NULL;
1371 return isl_local_space_get_div(aff->ls, pos);
1374 /* Return the negation of "aff".
1376 * As a special case, -NaN = NaN.
1378 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1380 if (!aff)
1381 return NULL;
1382 if (isl_aff_is_nan(aff))
1383 return aff;
1384 aff = isl_aff_cow(aff);
1385 if (!aff)
1386 return NULL;
1387 aff->v = isl_vec_cow(aff->v);
1388 if (!aff->v)
1389 return isl_aff_free(aff);
1391 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1393 return aff;
1396 /* Remove divs from the local space that do not appear in the affine
1397 * expression.
1398 * We currently only remove divs at the end.
1399 * Some intermediate divs may also not appear directly in the affine
1400 * expression, but we would also need to check that no other divs are
1401 * defined in terms of them.
1403 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1405 int pos;
1406 isl_size off;
1407 isl_size n;
1409 n = isl_aff_domain_dim(aff, isl_dim_div);
1410 off = isl_aff_domain_offset(aff, isl_dim_div);
1411 if (n < 0 || off < 0)
1412 return isl_aff_free(aff);
1414 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1415 if (pos == n)
1416 return aff;
1418 aff = isl_aff_cow(aff);
1419 if (!aff)
1420 return NULL;
1422 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1423 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1424 if (!aff->ls || !aff->v)
1425 return isl_aff_free(aff);
1427 return aff;
1430 /* Look for any divs in the aff->ls with a denominator equal to one
1431 * and plug them into the affine expression and any subsequent divs
1432 * that may reference the div.
1434 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1436 int i;
1437 isl_size n;
1438 int len;
1439 isl_int v;
1440 isl_vec *vec;
1441 isl_local_space *ls;
1442 isl_size off;
1444 n = isl_aff_domain_dim(aff, isl_dim_div);
1445 off = isl_aff_domain_offset(aff, isl_dim_div);
1446 if (n < 0 || off < 0)
1447 return isl_aff_free(aff);
1448 len = aff->v->size;
1449 for (i = 0; i < n; ++i) {
1450 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1451 continue;
1452 ls = isl_local_space_copy(aff->ls);
1453 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1454 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1455 vec = isl_vec_copy(aff->v);
1456 vec = isl_vec_cow(vec);
1457 if (!ls || !vec)
1458 goto error;
1460 isl_int_init(v);
1462 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1463 len, len, v);
1465 isl_int_clear(v);
1467 isl_vec_free(aff->v);
1468 aff->v = vec;
1469 isl_local_space_free(aff->ls);
1470 aff->ls = ls;
1473 return aff;
1474 error:
1475 isl_vec_free(vec);
1476 isl_local_space_free(ls);
1477 return isl_aff_free(aff);
1480 /* Look for any divs j that appear with a unit coefficient inside
1481 * the definitions of other divs i and plug them into the definitions
1482 * of the divs i.
1484 * In particular, an expression of the form
1486 * floor((f(..) + floor(g(..)/n))/m)
1488 * is simplified to
1490 * floor((n * f(..) + g(..))/(n * m))
1492 * This simplification is correct because we can move the expression
1493 * f(..) into the inner floor in the original expression to obtain
1495 * floor(floor((n * f(..) + g(..))/n)/m)
1497 * from which we can derive the simplified expression.
1499 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1501 int i, j;
1502 isl_size n;
1503 isl_size off;
1505 n = isl_aff_domain_dim(aff, isl_dim_div);
1506 off = isl_aff_domain_offset(aff, isl_dim_div);
1507 if (n < 0 || off < 0)
1508 return isl_aff_free(aff);
1509 for (i = 1; i < n; ++i) {
1510 for (j = 0; j < i; ++j) {
1511 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1512 continue;
1513 aff->ls = isl_local_space_substitute_seq(aff->ls,
1514 isl_dim_div, j, aff->ls->div->row[j],
1515 aff->v->size, i, 1);
1516 if (!aff->ls)
1517 return isl_aff_free(aff);
1521 return aff;
1524 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1526 * Even though this function is only called on isl_affs with a single
1527 * reference, we are careful to only change aff->v and aff->ls together.
1529 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1531 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1532 isl_local_space *ls;
1533 isl_vec *v;
1535 if (off < 0)
1536 return isl_aff_free(aff);
1538 ls = isl_local_space_copy(aff->ls);
1539 ls = isl_local_space_swap_div(ls, a, b);
1540 v = isl_vec_copy(aff->v);
1541 v = isl_vec_cow(v);
1542 if (!ls || !v)
1543 goto error;
1545 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1546 isl_vec_free(aff->v);
1547 aff->v = v;
1548 isl_local_space_free(aff->ls);
1549 aff->ls = ls;
1551 return aff;
1552 error:
1553 isl_vec_free(v);
1554 isl_local_space_free(ls);
1555 return isl_aff_free(aff);
1558 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1560 * We currently do not actually remove div "b", but simply add its
1561 * coefficient to that of "a" and then zero it out.
1563 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1565 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1567 if (off < 0)
1568 return isl_aff_free(aff);
1570 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1571 return aff;
1573 aff->v = isl_vec_cow(aff->v);
1574 if (!aff->v)
1575 return isl_aff_free(aff);
1577 isl_int_add(aff->v->el[1 + off + a],
1578 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1579 isl_int_set_si(aff->v->el[1 + off + b], 0);
1581 return aff;
1584 /* Sort the divs in the local space of "aff" according to
1585 * the comparison function "cmp_row" in isl_local_space.c,
1586 * combining the coefficients of identical divs.
1588 * Reordering divs does not change the semantics of "aff",
1589 * so there is no need to call isl_aff_cow.
1590 * Moreover, this function is currently only called on isl_affs
1591 * with a single reference.
1593 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1595 isl_size n;
1596 int i, j;
1598 n = isl_aff_dim(aff, isl_dim_div);
1599 if (n < 0)
1600 return isl_aff_free(aff);
1601 for (i = 1; i < n; ++i) {
1602 for (j = i - 1; j >= 0; --j) {
1603 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1604 if (cmp < 0)
1605 break;
1606 if (cmp == 0)
1607 aff = merge_divs(aff, j, j + 1);
1608 else
1609 aff = swap_div(aff, j, j + 1);
1610 if (!aff)
1611 return NULL;
1615 return aff;
1618 /* Normalize the representation of "aff".
1620 * This function should only be called on "new" isl_affs, i.e.,
1621 * with only a single reference. We therefore do not need to
1622 * worry about affecting other instances.
1624 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1626 if (!aff)
1627 return NULL;
1628 aff->v = isl_vec_normalize(aff->v);
1629 if (!aff->v)
1630 return isl_aff_free(aff);
1631 aff = plug_in_integral_divs(aff);
1632 aff = plug_in_unit_divs(aff);
1633 aff = sort_divs(aff);
1634 aff = isl_aff_remove_unused_divs(aff);
1635 return aff;
1638 /* Given f, return floor(f).
1639 * If f is an integer expression, then just return f.
1640 * If f is a constant, then return the constant floor(f).
1641 * Otherwise, if f = g/m, write g = q m + r,
1642 * create a new div d = [r/m] and return the expression q + d.
1643 * The coefficients in r are taken to lie between -m/2 and m/2.
1645 * reduce_div_coefficients performs the same normalization.
1647 * As a special case, floor(NaN) = NaN.
1649 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1651 int i;
1652 int size;
1653 isl_ctx *ctx;
1654 isl_vec *div;
1656 if (!aff)
1657 return NULL;
1659 if (isl_aff_is_nan(aff))
1660 return aff;
1661 if (isl_int_is_one(aff->v->el[0]))
1662 return aff;
1664 aff = isl_aff_cow(aff);
1665 if (!aff)
1666 return NULL;
1668 aff->v = isl_vec_cow(aff->v);
1669 if (!aff->v)
1670 return isl_aff_free(aff);
1672 if (isl_aff_is_cst(aff)) {
1673 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1674 isl_int_set_si(aff->v->el[0], 1);
1675 return aff;
1678 div = isl_vec_copy(aff->v);
1679 div = isl_vec_cow(div);
1680 if (!div)
1681 return isl_aff_free(aff);
1683 ctx = isl_aff_get_ctx(aff);
1684 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1685 for (i = 1; i < aff->v->size; ++i) {
1686 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1687 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1688 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1689 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1690 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1694 aff->ls = isl_local_space_add_div(aff->ls, div);
1695 if (!aff->ls)
1696 return isl_aff_free(aff);
1698 size = aff->v->size;
1699 aff->v = isl_vec_extend(aff->v, size + 1);
1700 if (!aff->v)
1701 return isl_aff_free(aff);
1702 isl_int_set_si(aff->v->el[0], 1);
1703 isl_int_set_si(aff->v->el[size], 1);
1705 aff = isl_aff_normalize(aff);
1707 return aff;
1710 /* Compute
1712 * aff mod m = aff - m * floor(aff/m)
1714 * with m an integer value.
1716 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1717 __isl_take isl_val *m)
1719 isl_aff *res;
1721 if (!aff || !m)
1722 goto error;
1724 if (!isl_val_is_int(m))
1725 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1726 "expecting integer modulo", goto error);
1728 res = isl_aff_copy(aff);
1729 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1730 aff = isl_aff_floor(aff);
1731 aff = isl_aff_scale_val(aff, m);
1732 res = isl_aff_sub(res, aff);
1734 return res;
1735 error:
1736 isl_aff_free(aff);
1737 isl_val_free(m);
1738 return NULL;
1741 /* Compute
1743 * pwaff mod m = pwaff - m * floor(pwaff/m)
1745 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1747 isl_pw_aff *res;
1749 res = isl_pw_aff_copy(pwaff);
1750 pwaff = isl_pw_aff_scale_down(pwaff, m);
1751 pwaff = isl_pw_aff_floor(pwaff);
1752 pwaff = isl_pw_aff_scale(pwaff, m);
1753 res = isl_pw_aff_sub(res, pwaff);
1755 return res;
1758 /* Compute
1760 * pa mod m = pa - m * floor(pa/m)
1762 * with m an integer value.
1764 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1765 __isl_take isl_val *m)
1767 if (!pa || !m)
1768 goto error;
1769 if (!isl_val_is_int(m))
1770 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1771 "expecting integer modulo", goto error);
1772 pa = isl_pw_aff_mod(pa, m->n);
1773 isl_val_free(m);
1774 return pa;
1775 error:
1776 isl_pw_aff_free(pa);
1777 isl_val_free(m);
1778 return NULL;
1781 /* Given f, return ceil(f).
1782 * If f is an integer expression, then just return f.
1783 * Otherwise, let f be the expression
1785 * e/m
1787 * then return
1789 * floor((e + m - 1)/m)
1791 * As a special case, ceil(NaN) = NaN.
1793 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1795 if (!aff)
1796 return NULL;
1798 if (isl_aff_is_nan(aff))
1799 return aff;
1800 if (isl_int_is_one(aff->v->el[0]))
1801 return aff;
1803 aff = isl_aff_cow(aff);
1804 if (!aff)
1805 return NULL;
1806 aff->v = isl_vec_cow(aff->v);
1807 if (!aff->v)
1808 return isl_aff_free(aff);
1810 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1811 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1812 aff = isl_aff_floor(aff);
1814 return aff;
1817 /* Apply the expansion computed by isl_merge_divs.
1818 * The expansion itself is given by "exp" while the resulting
1819 * list of divs is given by "div".
1821 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1822 __isl_take isl_mat *div, int *exp)
1824 isl_size old_n_div;
1825 isl_size new_n_div;
1826 isl_size offset;
1828 aff = isl_aff_cow(aff);
1830 offset = isl_aff_domain_offset(aff, isl_dim_div);
1831 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1832 new_n_div = isl_mat_rows(div);
1833 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1834 goto error;
1836 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1837 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1838 if (!aff->v || !aff->ls)
1839 return isl_aff_free(aff);
1840 return aff;
1841 error:
1842 isl_aff_free(aff);
1843 isl_mat_free(div);
1844 return NULL;
1847 /* Add two affine expressions that live in the same local space.
1849 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1850 __isl_take isl_aff *aff2)
1852 isl_int gcd, f;
1854 aff1 = isl_aff_cow(aff1);
1855 if (!aff1 || !aff2)
1856 goto error;
1858 aff1->v = isl_vec_cow(aff1->v);
1859 if (!aff1->v)
1860 goto error;
1862 isl_int_init(gcd);
1863 isl_int_init(f);
1864 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1865 isl_int_divexact(f, aff2->v->el[0], gcd);
1866 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1867 isl_int_divexact(f, aff1->v->el[0], gcd);
1868 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1869 isl_int_divexact(f, aff2->v->el[0], gcd);
1870 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1871 isl_int_clear(f);
1872 isl_int_clear(gcd);
1874 isl_aff_free(aff2);
1875 aff1 = isl_aff_normalize(aff1);
1876 return aff1;
1877 error:
1878 isl_aff_free(aff1);
1879 isl_aff_free(aff2);
1880 return NULL;
1883 /* Replace one of the arguments by a NaN and free the other one.
1885 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1886 __isl_take isl_aff *aff2)
1888 isl_aff_free(aff2);
1889 return isl_aff_set_nan(aff1);
1892 /* Return the sum of "aff1" and "aff2".
1894 * If either of the two is NaN, then the result is NaN.
1896 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1897 __isl_take isl_aff *aff2)
1899 isl_ctx *ctx;
1900 int *exp1 = NULL;
1901 int *exp2 = NULL;
1902 isl_mat *div;
1903 isl_size n_div1, n_div2;
1905 if (!aff1 || !aff2)
1906 goto error;
1908 ctx = isl_aff_get_ctx(aff1);
1909 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1910 isl_die(ctx, isl_error_invalid,
1911 "spaces don't match", goto error);
1913 if (isl_aff_is_nan(aff1)) {
1914 isl_aff_free(aff2);
1915 return aff1;
1917 if (isl_aff_is_nan(aff2)) {
1918 isl_aff_free(aff1);
1919 return aff2;
1922 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1923 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1924 if (n_div1 < 0 || n_div2 < 0)
1925 goto error;
1926 if (n_div1 == 0 && n_div2 == 0)
1927 return add_expanded(aff1, aff2);
1929 exp1 = isl_alloc_array(ctx, int, n_div1);
1930 exp2 = isl_alloc_array(ctx, int, n_div2);
1931 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1932 goto error;
1934 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1935 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1936 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1937 free(exp1);
1938 free(exp2);
1940 return add_expanded(aff1, aff2);
1941 error:
1942 free(exp1);
1943 free(exp2);
1944 isl_aff_free(aff1);
1945 isl_aff_free(aff2);
1946 return NULL;
1949 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1950 __isl_take isl_aff *aff2)
1952 return isl_aff_add(aff1, isl_aff_neg(aff2));
1955 /* Return the result of scaling "aff" by a factor of "f".
1957 * As a special case, f * NaN = NaN.
1959 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1961 isl_int gcd;
1963 if (!aff)
1964 return NULL;
1965 if (isl_aff_is_nan(aff))
1966 return aff;
1968 if (isl_int_is_one(f))
1969 return aff;
1971 aff = isl_aff_cow(aff);
1972 if (!aff)
1973 return NULL;
1974 aff->v = isl_vec_cow(aff->v);
1975 if (!aff->v)
1976 return isl_aff_free(aff);
1978 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1979 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1980 return aff;
1983 isl_int_init(gcd);
1984 isl_int_gcd(gcd, aff->v->el[0], f);
1985 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1986 isl_int_divexact(gcd, f, gcd);
1987 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1988 isl_int_clear(gcd);
1990 return aff;
1993 /* Multiple "aff" by "v".
1995 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1996 __isl_take isl_val *v)
1998 if (!aff || !v)
1999 goto error;
2001 if (isl_val_is_one(v)) {
2002 isl_val_free(v);
2003 return aff;
2006 if (!isl_val_is_rat(v))
2007 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2008 "expecting rational factor", goto error);
2010 aff = isl_aff_scale(aff, v->n);
2011 aff = isl_aff_scale_down(aff, v->d);
2013 isl_val_free(v);
2014 return aff;
2015 error:
2016 isl_aff_free(aff);
2017 isl_val_free(v);
2018 return NULL;
2021 /* Return the result of scaling "aff" down by a factor of "f".
2023 * As a special case, NaN/f = NaN.
2025 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2027 isl_int gcd;
2029 if (!aff)
2030 return NULL;
2031 if (isl_aff_is_nan(aff))
2032 return aff;
2034 if (isl_int_is_one(f))
2035 return aff;
2037 aff = isl_aff_cow(aff);
2038 if (!aff)
2039 return NULL;
2041 if (isl_int_is_zero(f))
2042 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2043 "cannot scale down by zero", return isl_aff_free(aff));
2045 aff->v = isl_vec_cow(aff->v);
2046 if (!aff->v)
2047 return isl_aff_free(aff);
2049 isl_int_init(gcd);
2050 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2051 isl_int_gcd(gcd, gcd, f);
2052 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2053 isl_int_divexact(gcd, f, gcd);
2054 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2055 isl_int_clear(gcd);
2057 return aff;
2060 /* Divide "aff" by "v".
2062 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2063 __isl_take isl_val *v)
2065 if (!aff || !v)
2066 goto error;
2068 if (isl_val_is_one(v)) {
2069 isl_val_free(v);
2070 return aff;
2073 if (!isl_val_is_rat(v))
2074 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2075 "expecting rational factor", goto error);
2076 if (!isl_val_is_pos(v))
2077 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2078 "factor needs to be positive", goto error);
2080 aff = isl_aff_scale(aff, v->d);
2081 aff = isl_aff_scale_down(aff, v->n);
2083 isl_val_free(v);
2084 return aff;
2085 error:
2086 isl_aff_free(aff);
2087 isl_val_free(v);
2088 return NULL;
2091 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2093 isl_int v;
2095 if (f == 1)
2096 return aff;
2098 isl_int_init(v);
2099 isl_int_set_ui(v, f);
2100 aff = isl_aff_scale_down(aff, v);
2101 isl_int_clear(v);
2103 return aff;
2106 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2107 enum isl_dim_type type, unsigned pos, const char *s)
2109 aff = isl_aff_cow(aff);
2110 if (!aff)
2111 return NULL;
2112 if (type == isl_dim_out)
2113 isl_die(aff->v->ctx, isl_error_invalid,
2114 "cannot set name of output/set dimension",
2115 return isl_aff_free(aff));
2116 if (type == isl_dim_in)
2117 type = isl_dim_set;
2118 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2119 if (!aff->ls)
2120 return isl_aff_free(aff);
2122 return aff;
2125 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2126 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2128 aff = isl_aff_cow(aff);
2129 if (!aff)
2130 goto error;
2131 if (type == isl_dim_out)
2132 isl_die(aff->v->ctx, isl_error_invalid,
2133 "cannot set name of output/set dimension",
2134 goto error);
2135 if (type == isl_dim_in)
2136 type = isl_dim_set;
2137 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2138 if (!aff->ls)
2139 return isl_aff_free(aff);
2141 return aff;
2142 error:
2143 isl_id_free(id);
2144 isl_aff_free(aff);
2145 return NULL;
2148 /* Replace the identifier of the input tuple of "aff" by "id".
2149 * type is currently required to be equal to isl_dim_in
2151 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2152 enum isl_dim_type type, __isl_take isl_id *id)
2154 aff = isl_aff_cow(aff);
2155 if (!aff)
2156 goto error;
2157 if (type != isl_dim_in)
2158 isl_die(aff->v->ctx, isl_error_invalid,
2159 "cannot only set id of input tuple", goto error);
2160 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2161 if (!aff->ls)
2162 return isl_aff_free(aff);
2164 return aff;
2165 error:
2166 isl_id_free(id);
2167 isl_aff_free(aff);
2168 return NULL;
2171 /* Exploit the equalities in "eq" to simplify the affine expression
2172 * and the expressions of the integer divisions in the local space.
2173 * The integer divisions in this local space are assumed to appear
2174 * as regular dimensions in "eq".
2176 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2177 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2179 int i, j;
2180 unsigned o_div;
2181 unsigned n_div;
2183 if (!eq)
2184 goto error;
2185 if (eq->n_eq == 0) {
2186 isl_basic_set_free(eq);
2187 return aff;
2190 aff = isl_aff_cow(aff);
2191 if (!aff)
2192 goto error;
2194 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2195 isl_basic_set_copy(eq));
2196 aff->v = isl_vec_cow(aff->v);
2197 if (!aff->ls || !aff->v)
2198 goto error;
2200 o_div = isl_basic_set_offset(eq, isl_dim_div);
2201 n_div = eq->n_div;
2202 for (i = 0; i < eq->n_eq; ++i) {
2203 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2204 if (j < 0 || j == 0 || j >= o_div)
2205 continue;
2207 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2208 &aff->v->el[0]);
2211 isl_basic_set_free(eq);
2212 aff = isl_aff_normalize(aff);
2213 return aff;
2214 error:
2215 isl_basic_set_free(eq);
2216 isl_aff_free(aff);
2217 return NULL;
2220 /* Exploit the equalities in "eq" to simplify the affine expression
2221 * and the expressions of the integer divisions in the local space.
2223 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2224 __isl_take isl_basic_set *eq)
2226 isl_size n_div;
2228 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2229 if (n_div < 0)
2230 goto error;
2231 if (n_div > 0)
2232 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2233 return isl_aff_substitute_equalities_lifted(aff, eq);
2234 error:
2235 isl_basic_set_free(eq);
2236 isl_aff_free(aff);
2237 return NULL;
2240 /* Look for equalities among the variables shared by context and aff
2241 * and the integer divisions of aff, if any.
2242 * The equalities are then used to eliminate coefficients and/or integer
2243 * divisions from aff.
2245 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2246 __isl_take isl_set *context)
2248 isl_local_space *ls;
2249 isl_basic_set *hull;
2251 ls = isl_aff_get_domain_local_space(aff);
2252 context = isl_local_space_lift_set(ls, context);
2254 hull = isl_set_affine_hull(context);
2255 return isl_aff_substitute_equalities_lifted(aff, hull);
2258 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2259 __isl_take isl_set *context)
2261 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2262 dom_context = isl_set_intersect_params(dom_context, context);
2263 return isl_aff_gist(aff, dom_context);
2266 /* Return a basic set containing those elements in the space
2267 * of aff where it is positive. "rational" should not be set.
2269 * If "aff" is NaN, then it is not positive.
2271 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2272 int rational, void *user)
2274 isl_constraint *ineq;
2275 isl_basic_set *bset;
2276 isl_val *c;
2278 if (!aff)
2279 return NULL;
2280 if (isl_aff_is_nan(aff)) {
2281 isl_space *space = isl_aff_get_domain_space(aff);
2282 isl_aff_free(aff);
2283 return isl_basic_set_empty(space);
2285 if (rational)
2286 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2287 "rational sets not supported", goto error);
2289 ineq = isl_inequality_from_aff(aff);
2290 c = isl_constraint_get_constant_val(ineq);
2291 c = isl_val_sub_ui(c, 1);
2292 ineq = isl_constraint_set_constant_val(ineq, c);
2294 bset = isl_basic_set_from_constraint(ineq);
2295 bset = isl_basic_set_simplify(bset);
2296 return bset;
2297 error:
2298 isl_aff_free(aff);
2299 return NULL;
2302 /* Return a basic set containing those elements in the space
2303 * of aff where it is non-negative.
2304 * If "rational" is set, then return a rational basic set.
2306 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2308 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2309 __isl_take isl_aff *aff, int rational, void *user)
2311 isl_constraint *ineq;
2312 isl_basic_set *bset;
2314 if (!aff)
2315 return NULL;
2316 if (isl_aff_is_nan(aff)) {
2317 isl_space *space = isl_aff_get_domain_space(aff);
2318 isl_aff_free(aff);
2319 return isl_basic_set_empty(space);
2322 ineq = isl_inequality_from_aff(aff);
2324 bset = isl_basic_set_from_constraint(ineq);
2325 if (rational)
2326 bset = isl_basic_set_set_rational(bset);
2327 bset = isl_basic_set_simplify(bset);
2328 return bset;
2331 /* Return a basic set containing those elements in the space
2332 * of aff where it is non-negative.
2334 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2336 return aff_nonneg_basic_set(aff, 0, NULL);
2339 /* Return a basic set containing those elements in the domain space
2340 * of "aff" where it is positive.
2342 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2344 aff = isl_aff_add_constant_num_si(aff, -1);
2345 return isl_aff_nonneg_basic_set(aff);
2348 /* Return a basic set containing those elements in the domain space
2349 * of aff where it is negative.
2351 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2353 aff = isl_aff_neg(aff);
2354 return isl_aff_pos_basic_set(aff);
2357 /* Return a basic set containing those elements in the space
2358 * of aff where it is zero.
2359 * If "rational" is set, then return a rational basic set.
2361 * If "aff" is NaN, then it is not zero.
2363 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2364 int rational, void *user)
2366 isl_constraint *ineq;
2367 isl_basic_set *bset;
2369 if (!aff)
2370 return NULL;
2371 if (isl_aff_is_nan(aff)) {
2372 isl_space *space = isl_aff_get_domain_space(aff);
2373 isl_aff_free(aff);
2374 return isl_basic_set_empty(space);
2377 ineq = isl_equality_from_aff(aff);
2379 bset = isl_basic_set_from_constraint(ineq);
2380 if (rational)
2381 bset = isl_basic_set_set_rational(bset);
2382 bset = isl_basic_set_simplify(bset);
2383 return bset;
2386 /* Return a basic set containing those elements in the space
2387 * of aff where it is zero.
2389 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2391 return aff_zero_basic_set(aff, 0, NULL);
2394 /* Return a basic set containing those elements in the shared space
2395 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2397 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2398 __isl_take isl_aff *aff2)
2400 aff1 = isl_aff_sub(aff1, aff2);
2402 return isl_aff_nonneg_basic_set(aff1);
2405 /* Return a basic set containing those elements in the shared domain space
2406 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2408 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2409 __isl_take isl_aff *aff2)
2411 aff1 = isl_aff_sub(aff1, aff2);
2413 return isl_aff_pos_basic_set(aff1);
2416 /* Return a set containing those elements in the shared space
2417 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2419 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2420 __isl_take isl_aff *aff2)
2422 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2425 /* Return a set containing those elements in the shared domain space
2426 * of aff1 and aff2 where aff1 is greater than aff2.
2428 * If either of the two inputs is NaN, then the result is empty,
2429 * as comparisons with NaN always return false.
2431 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2432 __isl_take isl_aff *aff2)
2434 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2437 /* Return a basic set containing those elements in the shared space
2438 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2440 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2441 __isl_take isl_aff *aff2)
2443 return isl_aff_ge_basic_set(aff2, aff1);
2446 /* Return a basic set containing those elements in the shared domain space
2447 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2449 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2450 __isl_take isl_aff *aff2)
2452 return isl_aff_gt_basic_set(aff2, aff1);
2455 /* Return a set containing those elements in the shared space
2456 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2458 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2459 __isl_take isl_aff *aff2)
2461 return isl_aff_ge_set(aff2, aff1);
2464 /* Return a set containing those elements in the shared domain space
2465 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2467 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2468 __isl_take isl_aff *aff2)
2470 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2473 /* Return a basic set containing those elements in the shared space
2474 * of aff1 and aff2 where aff1 and aff2 are equal.
2476 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2477 __isl_take isl_aff *aff2)
2479 aff1 = isl_aff_sub(aff1, aff2);
2481 return isl_aff_zero_basic_set(aff1);
2484 /* Return a set containing those elements in the shared space
2485 * of aff1 and aff2 where aff1 and aff2 are equal.
2487 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2488 __isl_take isl_aff *aff2)
2490 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2493 /* Return a set containing those elements in the shared domain space
2494 * of aff1 and aff2 where aff1 and aff2 are not equal.
2496 * If either of the two inputs is NaN, then the result is empty,
2497 * as comparisons with NaN always return false.
2499 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2500 __isl_take isl_aff *aff2)
2502 isl_set *set_lt, *set_gt;
2504 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2505 isl_aff_copy(aff2));
2506 set_gt = isl_aff_gt_set(aff1, aff2);
2507 return isl_set_union_disjoint(set_lt, set_gt);
2510 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2511 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2513 aff1 = isl_aff_add(aff1, aff2);
2514 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2515 return aff1;
2518 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2520 if (!aff)
2521 return isl_bool_error;
2523 return isl_bool_false;
2526 #undef TYPE
2527 #define TYPE isl_aff
2528 static
2529 #include "check_type_range_templ.c"
2531 /* Check whether the given affine expression has non-zero coefficient
2532 * for any dimension in the given range or if any of these dimensions
2533 * appear with non-zero coefficients in any of the integer divisions
2534 * involved in the affine expression.
2536 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2537 enum isl_dim_type type, unsigned first, unsigned n)
2539 int i;
2540 int *active = NULL;
2541 isl_bool involves = isl_bool_false;
2543 if (!aff)
2544 return isl_bool_error;
2545 if (n == 0)
2546 return isl_bool_false;
2547 if (isl_aff_check_range(aff, type, first, n) < 0)
2548 return isl_bool_error;
2550 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2551 if (!active)
2552 goto error;
2554 first += isl_local_space_offset(aff->ls, type) - 1;
2555 for (i = 0; i < n; ++i)
2556 if (active[first + i]) {
2557 involves = isl_bool_true;
2558 break;
2561 free(active);
2563 return involves;
2564 error:
2565 free(active);
2566 return isl_bool_error;
2569 /* Does "aff" involve any local variables, i.e., integer divisions?
2571 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2573 isl_size n;
2575 n = isl_aff_dim(aff, isl_dim_div);
2576 if (n < 0)
2577 return isl_bool_error;
2578 return isl_bool_ok(n > 0);
2581 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2582 enum isl_dim_type type, unsigned first, unsigned n)
2584 if (!aff)
2585 return NULL;
2586 if (type == isl_dim_out)
2587 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2588 "cannot drop output/set dimension",
2589 return isl_aff_free(aff));
2590 if (type == isl_dim_in)
2591 type = isl_dim_set;
2592 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2593 return aff;
2595 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2596 return isl_aff_free(aff);
2598 aff = isl_aff_cow(aff);
2599 if (!aff)
2600 return NULL;
2602 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2603 if (!aff->ls)
2604 return isl_aff_free(aff);
2606 first += 1 + isl_local_space_offset(aff->ls, type);
2607 aff->v = isl_vec_drop_els(aff->v, first, n);
2608 if (!aff->v)
2609 return isl_aff_free(aff);
2611 return aff;
2614 /* Is the domain of "aff" a product?
2616 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2618 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2621 #undef TYPE
2622 #define TYPE isl_aff
2623 #include <isl_domain_factor_templ.c>
2625 /* Project the domain of the affine expression onto its parameter space.
2626 * The affine expression may not involve any of the domain dimensions.
2628 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2630 isl_space *space;
2631 isl_size n;
2633 n = isl_aff_dim(aff, isl_dim_in);
2634 if (n < 0)
2635 return isl_aff_free(aff);
2636 aff = isl_aff_drop_domain(aff, 0, n);
2637 space = isl_aff_get_domain_space(aff);
2638 space = isl_space_params(space);
2639 aff = isl_aff_reset_domain_space(aff, space);
2640 return aff;
2643 /* Convert an affine expression defined over a parameter domain
2644 * into one that is defined over a zero-dimensional set.
2646 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2648 isl_local_space *ls;
2650 ls = isl_aff_take_domain_local_space(aff);
2651 ls = isl_local_space_set_from_params(ls);
2652 aff = isl_aff_restore_domain_local_space(aff, ls);
2654 return aff;
2657 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2658 enum isl_dim_type type, unsigned first, unsigned n)
2660 if (!aff)
2661 return NULL;
2662 if (type == isl_dim_out)
2663 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2664 "cannot insert output/set dimensions",
2665 return isl_aff_free(aff));
2666 if (type == isl_dim_in)
2667 type = isl_dim_set;
2668 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2669 return aff;
2671 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2672 return isl_aff_free(aff);
2674 aff = isl_aff_cow(aff);
2675 if (!aff)
2676 return NULL;
2678 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2679 if (!aff->ls)
2680 return isl_aff_free(aff);
2682 first += 1 + isl_local_space_offset(aff->ls, type);
2683 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2684 if (!aff->v)
2685 return isl_aff_free(aff);
2687 return aff;
2690 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2691 enum isl_dim_type type, unsigned n)
2693 isl_size pos;
2695 pos = isl_aff_dim(aff, type);
2696 if (pos < 0)
2697 return isl_aff_free(aff);
2699 return isl_aff_insert_dims(aff, type, pos, n);
2702 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2703 * to dimensions of "dst_type" at "dst_pos".
2705 * We only support moving input dimensions to parameters and vice versa.
2707 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2708 enum isl_dim_type dst_type, unsigned dst_pos,
2709 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2711 unsigned g_dst_pos;
2712 unsigned g_src_pos;
2713 isl_size src_off, dst_off;
2715 if (!aff)
2716 return NULL;
2717 if (n == 0 &&
2718 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2719 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2720 return aff;
2722 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2723 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2724 "cannot move output/set dimension",
2725 return isl_aff_free(aff));
2726 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2727 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2728 "cannot move divs", return isl_aff_free(aff));
2729 if (dst_type == isl_dim_in)
2730 dst_type = isl_dim_set;
2731 if (src_type == isl_dim_in)
2732 src_type = isl_dim_set;
2734 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2735 return isl_aff_free(aff);
2736 if (dst_type == src_type)
2737 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2738 "moving dims within the same type not supported",
2739 return isl_aff_free(aff));
2741 aff = isl_aff_cow(aff);
2742 src_off = isl_aff_domain_offset(aff, src_type);
2743 dst_off = isl_aff_domain_offset(aff, dst_type);
2744 if (src_off < 0 || dst_off < 0)
2745 return isl_aff_free(aff);
2747 g_src_pos = 1 + src_off + src_pos;
2748 g_dst_pos = 1 + dst_off + dst_pos;
2749 if (dst_type > src_type)
2750 g_dst_pos -= n;
2752 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2753 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2754 src_type, src_pos, n);
2755 if (!aff->v || !aff->ls)
2756 return isl_aff_free(aff);
2758 aff = sort_divs(aff);
2760 return aff;
2763 /* Return a zero isl_aff in the given space.
2765 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2766 * interface over all piecewise types.
2768 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2770 isl_local_space *ls;
2772 ls = isl_local_space_from_space(isl_space_domain(space));
2773 return isl_aff_zero_on_domain(ls);
2776 #define isl_aff_involves_nan isl_aff_is_nan
2778 #undef PW
2779 #define PW isl_pw_aff
2780 #undef BASE
2781 #define BASE aff
2782 #undef EL_IS_ZERO
2783 #define EL_IS_ZERO is_empty
2784 #undef ZERO
2785 #define ZERO empty
2786 #undef IS_ZERO
2787 #define IS_ZERO is_empty
2788 #undef FIELD
2789 #define FIELD aff
2790 #undef DEFAULT_IS_ZERO
2791 #define DEFAULT_IS_ZERO 0
2793 #include <isl_pw_templ.c>
2794 #include <isl_pw_un_op_templ.c>
2795 #include <isl_pw_add_constant_val_templ.c>
2796 #include <isl_pw_add_disjoint_templ.c>
2797 #include <isl_pw_bind_domain_templ.c>
2798 #include <isl_pw_eval.c>
2799 #include <isl_pw_hash.c>
2800 #include <isl_pw_fix_templ.c>
2801 #include <isl_pw_from_range_templ.c>
2802 #include <isl_pw_insert_dims_templ.c>
2803 #include <isl_pw_insert_domain_templ.c>
2804 #include <isl_pw_move_dims_templ.c>
2805 #include <isl_pw_neg_templ.c>
2806 #include <isl_pw_pullback_templ.c>
2807 #include <isl_pw_scale_templ.c>
2808 #include <isl_pw_sub_templ.c>
2809 #include <isl_pw_union_opt.c>
2811 #undef BASE
2812 #define BASE pw_aff
2814 #include <isl_union_single.c>
2815 #include <isl_union_neg.c>
2816 #include <isl_union_sub_templ.c>
2818 #undef BASE
2819 #define BASE aff
2821 #include <isl_union_pw_templ.c>
2823 /* Compute a piecewise quasi-affine expression with a domain that
2824 * is the union of those of pwaff1 and pwaff2 and such that on each
2825 * cell, the quasi-affine expression is the maximum of those of pwaff1
2826 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2827 * cell, then the associated expression is the defined one.
2829 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2830 __isl_take isl_pw_aff *pwaff2)
2832 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2833 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2836 /* Compute a piecewise quasi-affine expression with a domain that
2837 * is the union of those of pwaff1 and pwaff2 and such that on each
2838 * cell, the quasi-affine expression is the minimum of those of pwaff1
2839 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2840 * cell, then the associated expression is the defined one.
2842 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2843 __isl_take isl_pw_aff *pwaff2)
2845 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2846 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2849 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2850 __isl_take isl_pw_aff *pwaff2, int max)
2852 if (max)
2853 return isl_pw_aff_union_max(pwaff1, pwaff2);
2854 else
2855 return isl_pw_aff_union_min(pwaff1, pwaff2);
2858 /* Is the domain of "pa" a product?
2860 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2862 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2865 #undef TYPE
2866 #define TYPE isl_pw_aff
2867 #include <isl_domain_factor_templ.c>
2869 /* Return a set containing those elements in the domain
2870 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2871 * does not satisfy "fn" (if complement is 1).
2873 * The pieces with a NaN never belong to the result since
2874 * NaN does not satisfy any property.
2876 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2877 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2878 void *user),
2879 int complement, void *user)
2881 int i;
2882 isl_set *set;
2884 if (!pwaff)
2885 return NULL;
2887 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2889 for (i = 0; i < pwaff->n; ++i) {
2890 isl_basic_set *bset;
2891 isl_set *set_i, *locus;
2892 isl_bool rational;
2894 if (isl_aff_is_nan(pwaff->p[i].aff))
2895 continue;
2897 rational = isl_set_has_rational(pwaff->p[i].set);
2898 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2899 locus = isl_set_from_basic_set(bset);
2900 set_i = isl_set_copy(pwaff->p[i].set);
2901 if (complement)
2902 set_i = isl_set_subtract(set_i, locus);
2903 else
2904 set_i = isl_set_intersect(set_i, locus);
2905 set = isl_set_union_disjoint(set, set_i);
2908 isl_pw_aff_free(pwaff);
2910 return set;
2913 /* Return a set containing those elements in the domain
2914 * of "pa" where it is positive.
2916 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2918 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2921 /* Return a set containing those elements in the domain
2922 * of pwaff where it is non-negative.
2924 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2926 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2929 /* Return a set containing those elements in the domain
2930 * of pwaff where it is zero.
2932 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2934 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2937 /* Return a set containing those elements in the domain
2938 * of pwaff where it is not zero.
2940 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2942 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2945 /* Bind the affine function "aff" to the parameter "id",
2946 * returning the elements in the domain where the affine expression
2947 * is equal to the parameter.
2949 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2950 __isl_take isl_id *id)
2952 isl_space *space;
2953 isl_aff *aff_id;
2955 space = isl_aff_get_domain_space(aff);
2956 space = isl_space_add_param_id(space, isl_id_copy(id));
2958 aff = isl_aff_align_params(aff, isl_space_copy(space));
2959 aff_id = isl_aff_param_on_domain_space_id(space, id);
2961 return isl_aff_eq_basic_set(aff, aff_id);
2964 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2965 * "rational" should not be set.
2967 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2968 int rational, void *user)
2970 isl_id *id = user;
2972 if (!aff)
2973 return NULL;
2974 if (rational)
2975 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2976 "rational binding not supported", goto error);
2977 return isl_aff_bind_id(aff, isl_id_copy(id));
2978 error:
2979 isl_aff_free(aff);
2980 return NULL;
2983 /* Bind the piecewise affine function "pa" to the parameter "id",
2984 * returning the elements in the domain where the expression
2985 * is equal to the parameter.
2987 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2988 __isl_take isl_id *id)
2990 isl_set *bound;
2992 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2993 isl_id_free(id);
2995 return bound;
2998 /* Return a set containing those elements in the shared domain
2999 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3001 * We compute the difference on the shared domain and then construct
3002 * the set of values where this difference is non-negative.
3003 * If strict is set, we first subtract 1 from the difference.
3004 * If equal is set, we only return the elements where pwaff1 and pwaff2
3005 * are equal.
3007 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3008 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3010 isl_set *set1, *set2;
3012 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3013 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3014 set1 = isl_set_intersect(set1, set2);
3015 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3016 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3017 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3019 if (strict) {
3020 isl_space *space = isl_set_get_space(set1);
3021 isl_aff *aff;
3022 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3023 aff = isl_aff_add_constant_si(aff, -1);
3024 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3025 } else
3026 isl_set_free(set1);
3028 if (equal)
3029 return isl_pw_aff_zero_set(pwaff1);
3030 return isl_pw_aff_nonneg_set(pwaff1);
3033 /* Return a set containing those elements in the shared domain
3034 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3036 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3037 __isl_take isl_pw_aff *pwaff2)
3039 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3040 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3043 /* Return a set containing those elements in the shared domain
3044 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3046 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3047 __isl_take isl_pw_aff *pwaff2)
3049 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3050 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3053 /* Return a set containing those elements in the shared domain
3054 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3056 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3057 __isl_take isl_pw_aff *pwaff2)
3059 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3060 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3063 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3064 __isl_take isl_pw_aff *pwaff2)
3066 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3069 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3070 __isl_take isl_pw_aff *pwaff2)
3072 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3075 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3076 * where the function values are ordered in the same way as "order",
3077 * which returns a set in the shared domain of its two arguments.
3079 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3080 * We first pull back the two functions such that they are defined on
3081 * the domain [A -> B]. Then we apply "order", resulting in a set
3082 * in the space [A -> B]. Finally, we unwrap this set to obtain
3083 * a map in the space A -> B.
3085 static __isl_give isl_map *isl_pw_aff_order_map(
3086 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3087 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3088 __isl_take isl_pw_aff *pa2))
3090 isl_space *space1, *space2;
3091 isl_multi_aff *ma;
3092 isl_set *set;
3094 isl_pw_aff_align_params_bin(&pa1, &pa2);
3095 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3096 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3097 space1 = isl_space_map_from_domain_and_range(space1, space2);
3098 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3099 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3100 ma = isl_multi_aff_range_map(space1);
3101 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3102 set = order(pa1, pa2);
3104 return isl_set_unwrap(set);
3107 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3108 * where the function values are equal.
3110 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3111 __isl_take isl_pw_aff *pa2)
3113 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3116 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3117 * where the function value of "pa1" is less than or equal to
3118 * the function value of "pa2".
3120 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3121 __isl_take isl_pw_aff *pa2)
3123 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3126 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3127 * where the function value of "pa1" is less than the function value of "pa2".
3129 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3130 __isl_take isl_pw_aff *pa2)
3132 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3135 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3136 * where the function value of "pa1" is greater than or equal to
3137 * the function value of "pa2".
3139 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3140 __isl_take isl_pw_aff *pa2)
3142 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3145 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3146 * where the function value of "pa1" is greater than the function value
3147 * of "pa2".
3149 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3150 __isl_take isl_pw_aff *pa2)
3152 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3155 /* Return a set containing those elements in the shared domain
3156 * of the elements of list1 and list2 where each element in list1
3157 * has the relation specified by "fn" with each element in list2.
3159 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3160 __isl_take isl_pw_aff_list *list2,
3161 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3162 __isl_take isl_pw_aff *pwaff2))
3164 int i, j;
3165 isl_ctx *ctx;
3166 isl_set *set;
3168 if (!list1 || !list2)
3169 goto error;
3171 ctx = isl_pw_aff_list_get_ctx(list1);
3172 if (list1->n < 1 || list2->n < 1)
3173 isl_die(ctx, isl_error_invalid,
3174 "list should contain at least one element", goto error);
3176 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3177 for (i = 0; i < list1->n; ++i)
3178 for (j = 0; j < list2->n; ++j) {
3179 isl_set *set_ij;
3181 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3182 isl_pw_aff_copy(list2->p[j]));
3183 set = isl_set_intersect(set, set_ij);
3186 isl_pw_aff_list_free(list1);
3187 isl_pw_aff_list_free(list2);
3188 return set;
3189 error:
3190 isl_pw_aff_list_free(list1);
3191 isl_pw_aff_list_free(list2);
3192 return NULL;
3195 /* Return a set containing those elements in the shared domain
3196 * of the elements of list1 and list2 where each element in list1
3197 * is equal to each element in list2.
3199 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3200 __isl_take isl_pw_aff_list *list2)
3202 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3205 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3206 __isl_take isl_pw_aff_list *list2)
3208 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3211 /* Return a set containing those elements in the shared domain
3212 * of the elements of list1 and list2 where each element in list1
3213 * is less than or equal to each element in list2.
3215 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3216 __isl_take isl_pw_aff_list *list2)
3218 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3221 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3222 __isl_take isl_pw_aff_list *list2)
3224 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3227 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3228 __isl_take isl_pw_aff_list *list2)
3230 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3233 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3234 __isl_take isl_pw_aff_list *list2)
3236 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3240 /* Return a set containing those elements in the shared domain
3241 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3243 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3244 __isl_take isl_pw_aff *pwaff2)
3246 isl_set *set_lt, *set_gt;
3248 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3249 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3250 isl_pw_aff_copy(pwaff2));
3251 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3252 return isl_set_union_disjoint(set_lt, set_gt);
3255 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3256 isl_int v)
3258 int i;
3260 if (isl_int_is_one(v))
3261 return pwaff;
3262 if (!isl_int_is_pos(v))
3263 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3264 "factor needs to be positive",
3265 return isl_pw_aff_free(pwaff));
3266 pwaff = isl_pw_aff_cow(pwaff);
3267 if (!pwaff)
3268 return NULL;
3269 if (pwaff->n == 0)
3270 return pwaff;
3272 for (i = 0; i < pwaff->n; ++i) {
3273 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3274 if (!pwaff->p[i].aff)
3275 return isl_pw_aff_free(pwaff);
3278 return pwaff;
3281 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3283 return isl_pw_aff_un_op(pwaff, &isl_aff_floor);
3286 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3288 return isl_pw_aff_un_op(pwaff, &isl_aff_ceil);
3291 /* Assuming that "cond1" and "cond2" are disjoint,
3292 * return an affine expression that is equal to pwaff1 on cond1
3293 * and to pwaff2 on cond2.
3295 static __isl_give isl_pw_aff *isl_pw_aff_select(
3296 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3297 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3299 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3300 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3302 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3305 /* Return an affine expression that is equal to pwaff_true for elements
3306 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3307 * is zero.
3308 * That is, return cond ? pwaff_true : pwaff_false;
3310 * If "cond" involves and NaN, then we conservatively return a NaN
3311 * on its entire domain. In principle, we could consider the pieces
3312 * where it is NaN separately from those where it is not.
3314 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3315 * then only use the domain of "cond" to restrict the domain.
3317 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3318 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3320 isl_set *cond_true, *cond_false;
3321 isl_bool equal;
3323 if (!cond)
3324 goto error;
3325 if (isl_pw_aff_involves_nan(cond)) {
3326 isl_space *space = isl_pw_aff_get_domain_space(cond);
3327 isl_local_space *ls = isl_local_space_from_space(space);
3328 isl_pw_aff_free(cond);
3329 isl_pw_aff_free(pwaff_true);
3330 isl_pw_aff_free(pwaff_false);
3331 return isl_pw_aff_nan_on_domain(ls);
3334 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3335 isl_pw_aff_get_space(pwaff_false));
3336 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3337 isl_pw_aff_get_space(pwaff_true));
3338 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3339 if (equal < 0)
3340 goto error;
3341 if (equal) {
3342 isl_set *dom;
3344 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3345 isl_pw_aff_free(pwaff_false);
3346 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3349 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3350 cond_false = isl_pw_aff_zero_set(cond);
3351 return isl_pw_aff_select(cond_true, pwaff_true,
3352 cond_false, pwaff_false);
3353 error:
3354 isl_pw_aff_free(cond);
3355 isl_pw_aff_free(pwaff_true);
3356 isl_pw_aff_free(pwaff_false);
3357 return NULL;
3360 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3362 int pos;
3364 if (!aff)
3365 return isl_bool_error;
3367 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3368 return isl_bool_ok(pos == -1);
3371 /* Check whether pwaff is a piecewise constant.
3373 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3375 int i;
3377 if (!pwaff)
3378 return isl_bool_error;
3380 for (i = 0; i < pwaff->n; ++i) {
3381 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3382 if (is_cst < 0 || !is_cst)
3383 return is_cst;
3386 return isl_bool_true;
3389 /* Return the product of "aff1" and "aff2".
3391 * If either of the two is NaN, then the result is NaN.
3393 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3395 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3396 __isl_take isl_aff *aff2)
3398 if (!aff1 || !aff2)
3399 goto error;
3401 if (isl_aff_is_nan(aff1)) {
3402 isl_aff_free(aff2);
3403 return aff1;
3405 if (isl_aff_is_nan(aff2)) {
3406 isl_aff_free(aff1);
3407 return aff2;
3410 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3411 return isl_aff_mul(aff2, aff1);
3413 if (!isl_aff_is_cst(aff2))
3414 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3415 "at least one affine expression should be constant",
3416 goto error);
3418 aff1 = isl_aff_cow(aff1);
3419 if (!aff1 || !aff2)
3420 goto error;
3422 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3423 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3425 isl_aff_free(aff2);
3426 return aff1;
3427 error:
3428 isl_aff_free(aff1);
3429 isl_aff_free(aff2);
3430 return NULL;
3433 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3435 * If either of the two is NaN, then the result is NaN.
3436 * A division by zero also results in NaN.
3438 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3439 __isl_take isl_aff *aff2)
3441 isl_bool is_cst, is_zero;
3442 int neg;
3444 if (!aff1 || !aff2)
3445 goto error;
3447 if (isl_aff_is_nan(aff1)) {
3448 isl_aff_free(aff2);
3449 return aff1;
3451 if (isl_aff_is_nan(aff2)) {
3452 isl_aff_free(aff1);
3453 return aff2;
3456 is_cst = isl_aff_is_cst(aff2);
3457 if (is_cst < 0)
3458 goto error;
3459 if (!is_cst)
3460 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3461 "second argument should be a constant", goto error);
3462 is_zero = isl_aff_plain_is_zero(aff2);
3463 if (is_zero < 0)
3464 goto error;
3465 if (is_zero)
3466 return set_nan_free(aff1, aff2);
3468 neg = isl_int_is_neg(aff2->v->el[1]);
3469 if (neg) {
3470 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3471 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3474 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3475 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3477 if (neg) {
3478 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3479 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3482 isl_aff_free(aff2);
3483 return aff1;
3484 error:
3485 isl_aff_free(aff1);
3486 isl_aff_free(aff2);
3487 return NULL;
3490 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3491 __isl_take isl_pw_aff *pwaff2)
3493 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3494 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3497 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3498 __isl_take isl_pw_aff *pwaff2)
3500 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3501 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3504 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3506 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3507 __isl_take isl_pw_aff *pa2)
3509 int is_cst;
3511 is_cst = isl_pw_aff_is_cst(pa2);
3512 if (is_cst < 0)
3513 goto error;
3514 if (!is_cst)
3515 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3516 "second argument should be a piecewise constant",
3517 goto error);
3518 isl_pw_aff_align_params_bin(&pa1, &pa2);
3519 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3520 error:
3521 isl_pw_aff_free(pa1);
3522 isl_pw_aff_free(pa2);
3523 return NULL;
3526 /* Compute the quotient of the integer division of "pa1" by "pa2"
3527 * with rounding towards zero.
3528 * "pa2" is assumed to be a piecewise constant.
3530 * In particular, return
3532 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3535 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3536 __isl_take isl_pw_aff *pa2)
3538 int is_cst;
3539 isl_set *cond;
3540 isl_pw_aff *f, *c;
3542 is_cst = isl_pw_aff_is_cst(pa2);
3543 if (is_cst < 0)
3544 goto error;
3545 if (!is_cst)
3546 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3547 "second argument should be a piecewise constant",
3548 goto error);
3550 pa1 = isl_pw_aff_div(pa1, pa2);
3552 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3553 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3554 c = isl_pw_aff_ceil(pa1);
3555 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3556 error:
3557 isl_pw_aff_free(pa1);
3558 isl_pw_aff_free(pa2);
3559 return NULL;
3562 /* Compute the remainder of the integer division of "pa1" by "pa2"
3563 * with rounding towards zero.
3564 * "pa2" is assumed to be a piecewise constant.
3566 * In particular, return
3568 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3571 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3572 __isl_take isl_pw_aff *pa2)
3574 int is_cst;
3575 isl_pw_aff *res;
3577 is_cst = isl_pw_aff_is_cst(pa2);
3578 if (is_cst < 0)
3579 goto error;
3580 if (!is_cst)
3581 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3582 "second argument should be a piecewise constant",
3583 goto error);
3584 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3585 res = isl_pw_aff_mul(pa2, res);
3586 res = isl_pw_aff_sub(pa1, res);
3587 return res;
3588 error:
3589 isl_pw_aff_free(pa1);
3590 isl_pw_aff_free(pa2);
3591 return NULL;
3594 /* Does either of "pa1" or "pa2" involve any NaN?
3596 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3597 __isl_keep isl_pw_aff *pa2)
3599 isl_bool has_nan;
3601 has_nan = isl_pw_aff_involves_nan(pa1);
3602 if (has_nan < 0 || has_nan)
3603 return has_nan;
3604 return isl_pw_aff_involves_nan(pa2);
3607 /* Return a piecewise affine expression defined on the specified domain
3608 * that represents NaN.
3610 static __isl_give isl_pw_aff *nan_on_domain_set(__isl_take isl_set *dom)
3612 isl_local_space *ls;
3613 isl_pw_aff *pa;
3615 ls = isl_local_space_from_space(isl_set_get_space(dom));
3616 pa = isl_pw_aff_nan_on_domain(ls);
3617 pa = isl_pw_aff_intersect_domain(pa, dom);
3619 return pa;
3622 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3623 * by a NaN on their shared domain.
3625 * In principle, the result could be refined to only being NaN
3626 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3628 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3629 __isl_take isl_pw_aff *pa2)
3631 isl_set *dom;
3633 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3634 return nan_on_domain_set(dom);
3637 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3638 __isl_take isl_pw_aff *pwaff2)
3640 isl_set *le;
3641 isl_set *dom;
3643 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3644 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3645 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3646 isl_pw_aff_copy(pwaff2));
3647 dom = isl_set_subtract(dom, isl_set_copy(le));
3648 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3651 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3652 __isl_take isl_pw_aff *pwaff2)
3654 isl_set *ge;
3655 isl_set *dom;
3657 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3658 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3659 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3660 isl_pw_aff_copy(pwaff2));
3661 dom = isl_set_subtract(dom, isl_set_copy(ge));
3662 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3665 /* Return an expression for the minimum (if "max" is not set) or
3666 * the maximum (if "max" is set) of "pa1" and "pa2".
3667 * If either expression involves any NaN, then return a NaN
3668 * on the shared domain as result.
3670 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3671 __isl_take isl_pw_aff *pa2, int max)
3673 isl_bool has_nan;
3675 has_nan = either_involves_nan(pa1, pa2);
3676 if (has_nan < 0)
3677 pa1 = isl_pw_aff_free(pa1);
3678 else if (has_nan)
3679 return replace_by_nan(pa1, pa2);
3681 isl_pw_aff_align_params_bin(&pa1, &pa2);
3682 if (max)
3683 return pw_aff_max(pa1, pa2);
3684 else
3685 return pw_aff_min(pa1, pa2);
3688 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3690 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3691 __isl_take isl_pw_aff *pwaff2)
3693 return pw_aff_min_max(pwaff1, pwaff2, 0);
3696 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3698 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3699 __isl_take isl_pw_aff *pwaff2)
3701 return pw_aff_min_max(pwaff1, pwaff2, 1);
3704 /* Does "pa" not involve any NaN?
3706 static isl_bool pw_aff_no_nan(__isl_keep isl_pw_aff *pa, void *user)
3708 return isl_bool_not(isl_pw_aff_involves_nan(pa));
3711 /* Does any element of "list" involve any NaN?
3713 * That is, is it not the case that every element does not involve any NaN?
3715 static isl_bool isl_pw_aff_list_involves_nan(__isl_keep isl_pw_aff_list *list)
3717 return isl_bool_not(isl_pw_aff_list_every(list, &pw_aff_no_nan, NULL));
3720 /* Replace "list" (consisting of "n" elements, of which
3721 * at least one element involves a NaN)
3722 * by a NaN on the shared domain of the elements.
3724 * In principle, the result could be refined to only being NaN
3725 * on the parts of this domain where at least one of the elements is NaN.
3727 static __isl_give isl_pw_aff *replace_list_by_nan(
3728 __isl_take isl_pw_aff_list *list, int n)
3730 int i;
3731 isl_set *dom;
3733 dom = isl_pw_aff_domain(isl_pw_aff_list_get_at(list, 0));
3734 for (i = 1; i < n; ++i) {
3735 isl_set *dom_i;
3737 dom_i = isl_pw_aff_domain(isl_pw_aff_list_get_at(list, i));
3738 dom = isl_set_intersect(dom, dom_i);
3741 isl_pw_aff_list_free(list);
3742 return nan_on_domain_set(dom);
3745 /* Return the set where the element at "pos1" of "list" is less than or
3746 * equal to the element at "pos2".
3747 * Equality is only allowed if "pos1" is smaller than "pos2".
3749 static __isl_give isl_set *less(__isl_keep isl_pw_aff_list *list,
3750 int pos1, int pos2)
3752 isl_pw_aff *pa1, *pa2;
3754 pa1 = isl_pw_aff_list_get_at(list, pos1);
3755 pa2 = isl_pw_aff_list_get_at(list, pos2);
3757 if (pos1 < pos2)
3758 return isl_pw_aff_le_set(pa1, pa2);
3759 else
3760 return isl_pw_aff_lt_set(pa1, pa2);
3763 /* Return an isl_pw_aff that maps each element in the intersection of the
3764 * domains of the piecewise affine expressions in "list"
3765 * to the maximal (if "max" is set) or minimal (if "max" is not set)
3766 * expression in "list" at that element.
3767 * If any expression involves any NaN, then return a NaN
3768 * on the shared domain as result.
3770 * If "list" has n elements, then the result consists of n pieces,
3771 * where, in the case of a minimum, each piece has as value expression
3772 * the value expression of one of the elements and as domain
3773 * the set of elements where that value expression
3774 * is less than (or equal) to the other value expressions.
3775 * In the case of a maximum, the condition is
3776 * that all the other value expressions are less than (or equal)
3777 * to the given value expression.
3779 * In order to produce disjoint pieces, a pair of elements
3780 * in the original domain is only allowed to be equal to each other
3781 * on exactly one of the two pieces corresponding to the two elements.
3782 * The position in the list is used to break ties.
3783 * In particular, in the case of a minimum,
3784 * in the piece corresponding to a given element,
3785 * this element is allowed to be equal to any later element in the list,
3786 * but not to any earlier element in the list.
3788 static __isl_give isl_pw_aff *isl_pw_aff_list_opt(
3789 __isl_take isl_pw_aff_list *list, int max)
3791 int i, j;
3792 isl_bool has_nan;
3793 isl_size n;
3794 isl_space *space;
3795 isl_pw_aff *pa, *res;
3797 n = isl_pw_aff_list_size(list);
3798 if (n < 0)
3799 goto error;
3800 if (n < 1)
3801 isl_die(isl_pw_aff_list_get_ctx(list), isl_error_invalid,
3802 "list should contain at least one element", goto error);
3804 has_nan = isl_pw_aff_list_involves_nan(list);
3805 if (has_nan < 0)
3806 goto error;
3807 if (has_nan)
3808 return replace_list_by_nan(list, n);
3810 pa = isl_pw_aff_list_get_at(list, 0);
3811 space = isl_pw_aff_get_space(pa);
3812 isl_pw_aff_free(pa);
3813 res = isl_pw_aff_empty(space);
3815 for (i = 0; i < n; ++i) {
3816 pa = isl_pw_aff_list_get_at(list, i);
3817 for (j = 0; j < n; ++j) {
3818 isl_set *dom;
3820 if (j == i)
3821 continue;
3822 if (max)
3823 dom = less(list, j, i);
3824 else
3825 dom = less(list, i, j);
3827 pa = isl_pw_aff_intersect_domain(pa, dom);
3829 res = isl_pw_aff_add_disjoint(res, pa);
3832 isl_pw_aff_list_free(list);
3833 return res;
3834 error:
3835 isl_pw_aff_list_free(list);
3836 return NULL;
3839 /* Return an isl_pw_aff that maps each element in the intersection of the
3840 * domains of the elements of list to the minimal corresponding affine
3841 * expression.
3843 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3845 return isl_pw_aff_list_opt(list, 0);
3848 /* Return an isl_pw_aff that maps each element in the intersection of the
3849 * domains of the elements of list to the maximal corresponding affine
3850 * expression.
3852 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3854 return isl_pw_aff_list_opt(list, 1);
3857 /* Mark the domains of "pwaff" as rational.
3859 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3861 int i;
3863 pwaff = isl_pw_aff_cow(pwaff);
3864 if (!pwaff)
3865 return NULL;
3866 if (pwaff->n == 0)
3867 return pwaff;
3869 for (i = 0; i < pwaff->n; ++i) {
3870 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3871 if (!pwaff->p[i].set)
3872 return isl_pw_aff_free(pwaff);
3875 return pwaff;
3878 /* Mark the domains of the elements of "list" as rational.
3880 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3881 __isl_take isl_pw_aff_list *list)
3883 int i, n;
3885 if (!list)
3886 return NULL;
3887 if (list->n == 0)
3888 return list;
3890 n = list->n;
3891 for (i = 0; i < n; ++i) {
3892 isl_pw_aff *pa;
3894 pa = isl_pw_aff_list_get_pw_aff(list, i);
3895 pa = isl_pw_aff_set_rational(pa);
3896 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3899 return list;
3902 /* Do the parameters of "aff" match those of "space"?
3904 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3905 __isl_keep isl_space *space)
3907 isl_space *aff_space;
3908 isl_bool match;
3910 if (!aff || !space)
3911 return isl_bool_error;
3913 aff_space = isl_aff_get_domain_space(aff);
3915 match = isl_space_has_equal_params(space, aff_space);
3917 isl_space_free(aff_space);
3918 return match;
3921 /* Check that the domain space of "aff" matches "space".
3923 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3924 __isl_keep isl_space *space)
3926 isl_space *aff_space;
3927 isl_bool match;
3929 if (!aff || !space)
3930 return isl_stat_error;
3932 aff_space = isl_aff_get_domain_space(aff);
3934 match = isl_space_has_equal_params(space, aff_space);
3935 if (match < 0)
3936 goto error;
3937 if (!match)
3938 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3939 "parameters don't match", goto error);
3940 match = isl_space_tuple_is_equal(space, isl_dim_in,
3941 aff_space, isl_dim_set);
3942 if (match < 0)
3943 goto error;
3944 if (!match)
3945 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3946 "domains don't match", goto error);
3947 isl_space_free(aff_space);
3948 return isl_stat_ok;
3949 error:
3950 isl_space_free(aff_space);
3951 return isl_stat_error;
3954 /* Return the shared (universe) domain of the elements of "ma".
3956 * Since an isl_multi_aff (and an isl_aff) is always total,
3957 * the domain is always the universe set in its domain space.
3958 * This is a helper function for use in the generic isl_multi_*_bind.
3960 static __isl_give isl_basic_set *isl_multi_aff_domain(
3961 __isl_take isl_multi_aff *ma)
3963 isl_space *space;
3965 space = isl_multi_aff_get_space(ma);
3966 isl_multi_aff_free(ma);
3968 return isl_basic_set_universe(isl_space_domain(space));
3971 #undef BASE
3972 #define BASE aff
3974 #include <isl_multi_no_explicit_domain.c>
3975 #include <isl_multi_templ.c>
3976 #include <isl_multi_un_op_templ.c>
3977 #include <isl_multi_bin_val_templ.c>
3978 #include <isl_multi_add_constant_templ.c>
3979 #include <isl_multi_align_set.c>
3980 #include <isl_multi_arith_templ.c>
3981 #include <isl_multi_bind_domain_templ.c>
3982 #include <isl_multi_cmp.c>
3983 #include <isl_multi_dim_id_templ.c>
3984 #include <isl_multi_dims.c>
3985 #include <isl_multi_floor.c>
3986 #include <isl_multi_from_base_templ.c>
3987 #include <isl_multi_identity_templ.c>
3988 #include <isl_multi_insert_domain_templ.c>
3989 #include <isl_multi_locals_templ.c>
3990 #include <isl_multi_move_dims_templ.c>
3991 #include <isl_multi_nan_templ.c>
3992 #include <isl_multi_product_templ.c>
3993 #include <isl_multi_splice_templ.c>
3994 #include <isl_multi_tuple_id_templ.c>
3995 #include <isl_multi_unbind_params_templ.c>
3996 #include <isl_multi_zero_templ.c>
3998 #undef DOMBASE
3999 #define DOMBASE set
4000 #include <isl_multi_check_domain_templ.c>
4001 #include <isl_multi_apply_set_no_explicit_domain_templ.c>
4002 #include <isl_multi_gist.c>
4004 #undef DOMBASE
4005 #define DOMBASE basic_set
4006 #include <isl_multi_bind_templ.c>
4008 /* Construct an isl_multi_aff living in "space" that corresponds
4009 * to the affine transformation matrix "mat".
4011 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
4012 __isl_take isl_space *space, __isl_take isl_mat *mat)
4014 isl_ctx *ctx;
4015 isl_local_space *ls = NULL;
4016 isl_multi_aff *ma = NULL;
4017 isl_size n_row, n_col, n_out, total;
4018 int i;
4020 if (!space || !mat)
4021 goto error;
4023 ctx = isl_mat_get_ctx(mat);
4025 n_row = isl_mat_rows(mat);
4026 n_col = isl_mat_cols(mat);
4027 n_out = isl_space_dim(space, isl_dim_out);
4028 total = isl_space_dim(space, isl_dim_all);
4029 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
4030 goto error;
4031 if (n_row < 1)
4032 isl_die(ctx, isl_error_invalid,
4033 "insufficient number of rows", goto error);
4034 if (n_col < 1)
4035 isl_die(ctx, isl_error_invalid,
4036 "insufficient number of columns", goto error);
4037 if (1 + n_out != n_row || 2 + total != n_row + n_col)
4038 isl_die(ctx, isl_error_invalid,
4039 "dimension mismatch", goto error);
4041 ma = isl_multi_aff_zero(isl_space_copy(space));
4042 space = isl_space_domain(space);
4043 ls = isl_local_space_from_space(isl_space_copy(space));
4045 for (i = 0; i < n_row - 1; ++i) {
4046 isl_vec *v;
4047 isl_aff *aff;
4049 v = isl_vec_alloc(ctx, 1 + n_col);
4050 if (!v)
4051 goto error;
4052 isl_int_set(v->el[0], mat->row[0][0]);
4053 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
4054 v = isl_vec_normalize(v);
4055 aff = isl_aff_alloc_vec_validated(isl_local_space_copy(ls), v);
4056 ma = isl_multi_aff_set_aff(ma, i, aff);
4059 isl_space_free(space);
4060 isl_local_space_free(ls);
4061 isl_mat_free(mat);
4062 return ma;
4063 error:
4064 isl_space_free(space);
4065 isl_local_space_free(ls);
4066 isl_mat_free(mat);
4067 isl_multi_aff_free(ma);
4068 return NULL;
4071 /* Return the constant terms of the affine expressions of "ma".
4073 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
4074 __isl_keep isl_multi_aff *ma)
4076 int i;
4077 isl_size n;
4078 isl_space *space;
4079 isl_multi_val *mv;
4081 n = isl_multi_aff_size(ma);
4082 if (n < 0)
4083 return NULL;
4084 space = isl_space_range(isl_multi_aff_get_space(ma));
4085 space = isl_space_drop_all_params(space);
4086 mv = isl_multi_val_zero(space);
4088 for (i = 0; i < n; ++i) {
4089 isl_aff *aff;
4090 isl_val *val;
4092 aff = isl_multi_aff_get_at(ma, i);
4093 val = isl_aff_get_constant_val(aff);
4094 isl_aff_free(aff);
4095 mv = isl_multi_val_set_at(mv, i, val);
4098 return mv;
4101 /* Remove any internal structure of the domain of "ma".
4102 * If there is any such internal structure in the input,
4103 * then the name of the corresponding space is also removed.
4105 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4106 __isl_take isl_multi_aff *ma)
4108 isl_space *space;
4110 if (!ma)
4111 return NULL;
4113 if (!ma->space->nested[0])
4114 return ma;
4116 space = isl_multi_aff_get_space(ma);
4117 space = isl_space_flatten_domain(space);
4118 ma = isl_multi_aff_reset_space(ma, space);
4120 return ma;
4123 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4124 * of the space to its domain.
4126 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4128 int i;
4129 isl_size n_in;
4130 isl_local_space *ls;
4131 isl_multi_aff *ma;
4133 if (!space)
4134 return NULL;
4135 if (!isl_space_is_map(space))
4136 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4137 "not a map space", goto error);
4139 n_in = isl_space_dim(space, isl_dim_in);
4140 if (n_in < 0)
4141 goto error;
4142 space = isl_space_domain_map(space);
4144 ma = isl_multi_aff_alloc(isl_space_copy(space));
4145 if (n_in == 0) {
4146 isl_space_free(space);
4147 return ma;
4150 space = isl_space_domain(space);
4151 ls = isl_local_space_from_space(space);
4152 for (i = 0; i < n_in; ++i) {
4153 isl_aff *aff;
4155 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4156 isl_dim_set, i);
4157 ma = isl_multi_aff_set_aff(ma, i, aff);
4159 isl_local_space_free(ls);
4160 return ma;
4161 error:
4162 isl_space_free(space);
4163 return NULL;
4166 /* This function performs the same operation as isl_multi_aff_domain_map,
4167 * but is considered as a function on an isl_space when exported.
4169 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4170 __isl_take isl_space *space)
4172 return isl_multi_aff_domain_map(space);
4175 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4176 * of the space to its range.
4178 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4180 int i;
4181 isl_size n_in, n_out;
4182 isl_local_space *ls;
4183 isl_multi_aff *ma;
4185 if (!space)
4186 return NULL;
4187 if (!isl_space_is_map(space))
4188 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4189 "not a map space", goto error);
4191 n_in = isl_space_dim(space, isl_dim_in);
4192 n_out = isl_space_dim(space, isl_dim_out);
4193 if (n_in < 0 || n_out < 0)
4194 goto error;
4195 space = isl_space_range_map(space);
4197 ma = isl_multi_aff_alloc(isl_space_copy(space));
4198 if (n_out == 0) {
4199 isl_space_free(space);
4200 return ma;
4203 space = isl_space_domain(space);
4204 ls = isl_local_space_from_space(space);
4205 for (i = 0; i < n_out; ++i) {
4206 isl_aff *aff;
4208 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4209 isl_dim_set, n_in + i);
4210 ma = isl_multi_aff_set_aff(ma, i, aff);
4212 isl_local_space_free(ls);
4213 return ma;
4214 error:
4215 isl_space_free(space);
4216 return NULL;
4219 /* This function performs the same operation as isl_multi_aff_range_map,
4220 * but is considered as a function on an isl_space when exported.
4222 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4223 __isl_take isl_space *space)
4225 return isl_multi_aff_range_map(space);
4228 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4229 * of the space to its domain.
4231 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4232 __isl_take isl_space *space)
4234 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4237 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4238 * but is considered as a function on an isl_space when exported.
4240 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4241 __isl_take isl_space *space)
4243 return isl_pw_multi_aff_domain_map(space);
4246 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4247 * of the space to its range.
4249 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4250 __isl_take isl_space *space)
4252 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4255 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4256 * but is considered as a function on an isl_space when exported.
4258 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4259 __isl_take isl_space *space)
4261 return isl_pw_multi_aff_range_map(space);
4264 /* Given the space of a set and a range of set dimensions,
4265 * construct an isl_multi_aff that projects out those dimensions.
4267 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4268 __isl_take isl_space *space, enum isl_dim_type type,
4269 unsigned first, unsigned n)
4271 int i;
4272 isl_size dim;
4273 isl_local_space *ls;
4274 isl_multi_aff *ma;
4276 if (!space)
4277 return NULL;
4278 if (!isl_space_is_set(space))
4279 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4280 "expecting set space", goto error);
4281 if (type != isl_dim_set)
4282 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4283 "only set dimensions can be projected out", goto error);
4284 if (isl_space_check_range(space, type, first, n) < 0)
4285 goto error;
4287 dim = isl_space_dim(space, isl_dim_set);
4288 if (dim < 0)
4289 goto error;
4291 space = isl_space_from_domain(space);
4292 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4294 if (dim == n)
4295 return isl_multi_aff_alloc(space);
4297 ma = isl_multi_aff_alloc(isl_space_copy(space));
4298 space = isl_space_domain(space);
4299 ls = isl_local_space_from_space(space);
4301 for (i = 0; i < first; ++i) {
4302 isl_aff *aff;
4304 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4305 isl_dim_set, i);
4306 ma = isl_multi_aff_set_aff(ma, i, aff);
4309 for (i = 0; i < dim - (first + n); ++i) {
4310 isl_aff *aff;
4312 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4313 isl_dim_set, first + n + i);
4314 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4317 isl_local_space_free(ls);
4318 return ma;
4319 error:
4320 isl_space_free(space);
4321 return NULL;
4324 /* Given the space of a set and a range of set dimensions,
4325 * construct an isl_pw_multi_aff that projects out those dimensions.
4327 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4328 __isl_take isl_space *space, enum isl_dim_type type,
4329 unsigned first, unsigned n)
4331 isl_multi_aff *ma;
4333 ma = isl_multi_aff_project_out_map(space, type, first, n);
4334 return isl_pw_multi_aff_from_multi_aff(ma);
4337 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4338 * but is considered as a function on an isl_multi_aff when exported.
4340 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4341 __isl_take isl_multi_aff *ma)
4343 return isl_pw_multi_aff_from_multi_aff(ma);
4346 /* Create a piecewise multi-affine expression in the given space that maps each
4347 * input dimension to the corresponding output dimension.
4349 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4350 __isl_take isl_space *space)
4352 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4355 /* Create a piecewise multi expression that maps elements in the given space
4356 * to themselves.
4358 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4359 __isl_take isl_space *space)
4361 isl_multi_aff *ma;
4363 ma = isl_multi_aff_identity_on_domain_space(space);
4364 return isl_pw_multi_aff_from_multi_aff(ma);
4367 /* This function performs the same operation as
4368 * isl_pw_multi_aff_identity_on_domain_space,
4369 * but is considered as a function on an isl_space when exported.
4371 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4372 __isl_take isl_space *space)
4374 return isl_pw_multi_aff_identity_on_domain_space(space);
4377 /* Exploit the equalities in "eq" to simplify the affine expressions.
4379 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4380 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4382 isl_size n;
4383 int i;
4385 n = isl_multi_aff_size(maff);
4386 if (n < 0 || !eq)
4387 goto error;
4389 for (i = 0; i < n; ++i) {
4390 isl_aff *aff;
4392 aff = isl_multi_aff_take_at(maff, i);
4393 aff = isl_aff_substitute_equalities(aff,
4394 isl_basic_set_copy(eq));
4395 maff = isl_multi_aff_restore_at(maff, i, aff);
4398 isl_basic_set_free(eq);
4399 return maff;
4400 error:
4401 isl_basic_set_free(eq);
4402 isl_multi_aff_free(maff);
4403 return NULL;
4406 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4407 isl_int f)
4409 isl_size n;
4410 int i;
4412 n = isl_multi_aff_size(maff);
4413 if (n < 0)
4414 return isl_multi_aff_free(maff);
4416 for (i = 0; i < n; ++i) {
4417 isl_aff *aff;
4419 aff = isl_multi_aff_take_at(maff, i);
4420 aff = isl_aff_scale(aff, f);
4421 maff = isl_multi_aff_restore_at(maff, i, aff);
4424 return maff;
4427 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4428 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4430 maff1 = isl_multi_aff_add(maff1, maff2);
4431 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4432 return maff1;
4435 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4437 if (!maff)
4438 return isl_bool_error;
4440 return isl_bool_false;
4443 /* Return the set of domain elements where "ma1" is lexicographically
4444 * smaller than or equal to "ma2".
4446 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4447 __isl_take isl_multi_aff *ma2)
4449 return isl_multi_aff_lex_ge_set(ma2, ma1);
4452 /* Return the set of domain elements where "ma1" is lexicographically
4453 * smaller than "ma2".
4455 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4456 __isl_take isl_multi_aff *ma2)
4458 return isl_multi_aff_lex_gt_set(ma2, ma1);
4461 /* Return the set of domain elements where "ma1" is lexicographically
4462 * greater than to "ma2". If "equal" is set, then include the domain
4463 * elements where they are equal.
4464 * Do this for the case where there are no entries.
4465 * In this case, "ma1" cannot be greater than "ma2",
4466 * but it is (greater than or) equal to "ma2".
4468 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4469 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4471 isl_space *space;
4473 space = isl_multi_aff_get_domain_space(ma1);
4475 isl_multi_aff_free(ma1);
4476 isl_multi_aff_free(ma2);
4478 if (equal)
4479 return isl_set_universe(space);
4480 else
4481 return isl_set_empty(space);
4484 /* Return the set where entry "i" of "ma1" and "ma2"
4485 * satisfy the relation prescribed by "cmp".
4487 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4488 __isl_keep isl_multi_aff *ma2, int i,
4489 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4490 __isl_take isl_aff *aff2))
4492 isl_aff *aff1, *aff2;
4494 aff1 = isl_multi_aff_get_at(ma1, i);
4495 aff2 = isl_multi_aff_get_at(ma2, i);
4496 return cmp(aff1, aff2);
4499 /* Return the set of domain elements where "ma1" is lexicographically
4500 * greater than to "ma2". If "equal" is set, then include the domain
4501 * elements where they are equal.
4503 * In particular, for all but the final entry,
4504 * include the set of elements where this entry is strictly greater in "ma1"
4505 * and all previous entries are equal.
4506 * The final entry is also allowed to be equal in the two functions
4507 * if "equal" is set.
4509 * The case where there are no entries is handled separately.
4511 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4512 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4514 int i;
4515 isl_size n;
4516 isl_space *space;
4517 isl_set *res;
4518 isl_set *equal_set;
4519 isl_set *gte;
4521 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4522 goto error;
4523 n = isl_multi_aff_size(ma1);
4524 if (n < 0)
4525 goto error;
4526 if (n == 0)
4527 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4529 space = isl_multi_aff_get_domain_space(ma1);
4530 res = isl_set_empty(isl_space_copy(space));
4531 equal_set = isl_set_universe(space);
4533 for (i = 0; i + 1 < n; ++i) {
4534 isl_bool empty;
4535 isl_set *gt, *eq;
4537 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4538 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4539 res = isl_set_union(res, gt);
4540 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4541 equal_set = isl_set_intersect(equal_set, eq);
4543 empty = isl_set_is_empty(equal_set);
4544 if (empty >= 0 && empty)
4545 break;
4548 if (equal)
4549 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4550 else
4551 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4552 isl_multi_aff_free(ma1);
4553 isl_multi_aff_free(ma2);
4555 gte = isl_set_intersect(gte, equal_set);
4556 return isl_set_union(res, gte);
4557 error:
4558 isl_multi_aff_free(ma1);
4559 isl_multi_aff_free(ma2);
4560 return NULL;
4563 /* Return the set of domain elements where "ma1" is lexicographically
4564 * greater than or equal to "ma2".
4566 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4567 __isl_take isl_multi_aff *ma2)
4569 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4572 /* Return the set of domain elements where "ma1" is lexicographically
4573 * greater than "ma2".
4575 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4576 __isl_take isl_multi_aff *ma2)
4578 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4581 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4583 #undef PW
4584 #define PW isl_pw_multi_aff
4585 #undef BASE
4586 #define BASE multi_aff
4587 #undef EL_IS_ZERO
4588 #define EL_IS_ZERO is_empty
4589 #undef ZERO
4590 #define ZERO empty
4591 #undef IS_ZERO
4592 #define IS_ZERO is_empty
4593 #undef FIELD
4594 #define FIELD maff
4595 #undef DEFAULT_IS_ZERO
4596 #define DEFAULT_IS_ZERO 0
4598 #include <isl_pw_templ.c>
4599 #include <isl_pw_un_op_templ.c>
4600 #include <isl_pw_add_constant_multi_val_templ.c>
4601 #include <isl_pw_add_constant_val_templ.c>
4602 #include <isl_pw_add_disjoint_templ.c>
4603 #include <isl_pw_bind_domain_templ.c>
4604 #include <isl_pw_fix_templ.c>
4605 #include <isl_pw_from_range_templ.c>
4606 #include <isl_pw_insert_dims_templ.c>
4607 #include <isl_pw_insert_domain_templ.c>
4608 #include <isl_pw_locals_templ.c>
4609 #include <isl_pw_move_dims_templ.c>
4610 #include <isl_pw_neg_templ.c>
4611 #include <isl_pw_pullback_templ.c>
4612 #include <isl_pw_range_tuple_id_templ.c>
4613 #include <isl_pw_union_opt.c>
4615 #undef BASE
4616 #define BASE pw_multi_aff
4618 #include <isl_union_multi.c>
4619 #include "isl_union_locals_templ.c"
4620 #include <isl_union_neg.c>
4621 #include <isl_union_sub_templ.c>
4623 #undef BASE
4624 #define BASE multi_aff
4626 #include <isl_union_pw_templ.c>
4628 /* Generic function for extracting a factor from a product "pma".
4629 * "check_space" checks that the space is that of the right kind of product.
4630 * "space_factor" extracts the factor from the space.
4631 * "multi_aff_factor" extracts the factor from the constituent functions.
4633 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4634 __isl_take isl_pw_multi_aff *pma,
4635 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4636 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4637 __isl_give isl_multi_aff *(*multi_aff_factor)(
4638 __isl_take isl_multi_aff *ma))
4640 int i;
4641 isl_space *space;
4643 if (check_space(pma) < 0)
4644 return isl_pw_multi_aff_free(pma);
4646 space = isl_pw_multi_aff_take_space(pma);
4647 space = space_factor(space);
4649 for (i = 0; pma && i < pma->n; ++i) {
4650 isl_multi_aff *ma;
4652 ma = isl_pw_multi_aff_take_base_at(pma, i);
4653 ma = multi_aff_factor(ma);
4654 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4657 pma = isl_pw_multi_aff_restore_space(pma, space);
4659 return pma;
4662 /* Is the range of "pma" a wrapped relation?
4664 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4665 __isl_keep isl_pw_multi_aff *pma)
4667 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4670 /* Check that the range of "pma" is a product.
4672 static isl_stat pw_multi_aff_check_range_product(
4673 __isl_keep isl_pw_multi_aff *pma)
4675 isl_bool wraps;
4677 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4678 if (wraps < 0)
4679 return isl_stat_error;
4680 if (!wraps)
4681 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4682 "range is not a product", return isl_stat_error);
4683 return isl_stat_ok;
4686 /* Given a function A -> [B -> C], extract the function A -> B.
4688 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4689 __isl_take isl_pw_multi_aff *pma)
4691 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4692 &isl_space_range_factor_domain,
4693 &isl_multi_aff_range_factor_domain);
4696 /* Given a function A -> [B -> C], extract the function A -> C.
4698 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4699 __isl_take isl_pw_multi_aff *pma)
4701 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4702 &isl_space_range_factor_range,
4703 &isl_multi_aff_range_factor_range);
4706 /* Given two piecewise multi affine expressions, return a piecewise
4707 * multi-affine expression defined on the union of the definition domains
4708 * of the inputs that is equal to the lexicographic maximum of the two
4709 * inputs on each cell. If only one of the two inputs is defined on
4710 * a given cell, then it is considered to be the maximum.
4712 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4713 __isl_take isl_pw_multi_aff *pma1,
4714 __isl_take isl_pw_multi_aff *pma2)
4716 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4717 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4718 &isl_multi_aff_lex_ge_set);
4721 /* Given two piecewise multi affine expressions, return a piecewise
4722 * multi-affine expression defined on the union of the definition domains
4723 * of the inputs that is equal to the lexicographic minimum of the two
4724 * inputs on each cell. If only one of the two inputs is defined on
4725 * a given cell, then it is considered to be the minimum.
4727 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4728 __isl_take isl_pw_multi_aff *pma1,
4729 __isl_take isl_pw_multi_aff *pma2)
4731 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4732 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4733 &isl_multi_aff_lex_le_set);
4736 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4737 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4739 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4740 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4741 &isl_multi_aff_add);
4744 /* Subtract "pma2" from "pma1" and return the result.
4746 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4747 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4749 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4750 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4751 &isl_multi_aff_sub);
4754 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4755 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4757 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4758 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4760 int i, j, n;
4761 isl_space *space;
4762 isl_pw_multi_aff *res;
4764 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4765 goto error;
4767 n = pma1->n * pma2->n;
4768 space = isl_space_product(isl_space_copy(pma1->dim),
4769 isl_space_copy(pma2->dim));
4770 res = isl_pw_multi_aff_alloc_size(space, n);
4772 for (i = 0; i < pma1->n; ++i) {
4773 for (j = 0; j < pma2->n; ++j) {
4774 isl_set *domain;
4775 isl_multi_aff *ma;
4777 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4778 isl_set_copy(pma2->p[j].set));
4779 ma = isl_multi_aff_product(
4780 isl_multi_aff_copy(pma1->p[i].maff),
4781 isl_multi_aff_copy(pma2->p[j].maff));
4782 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4786 isl_pw_multi_aff_free(pma1);
4787 isl_pw_multi_aff_free(pma2);
4788 return res;
4789 error:
4790 isl_pw_multi_aff_free(pma1);
4791 isl_pw_multi_aff_free(pma2);
4792 return NULL;
4795 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4796 * denominator "denom".
4797 * "denom" is allowed to be negative, in which case the actual denominator
4798 * is -denom and the expressions are added instead.
4800 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4801 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4803 int i, first;
4804 int sign;
4805 isl_int d;
4807 first = isl_seq_first_non_zero(c, n);
4808 if (first == -1)
4809 return aff;
4811 sign = isl_int_sgn(denom);
4812 isl_int_init(d);
4813 isl_int_abs(d, denom);
4814 for (i = first; i < n; ++i) {
4815 isl_aff *aff_i;
4817 if (isl_int_is_zero(c[i]))
4818 continue;
4819 aff_i = isl_multi_aff_get_aff(ma, i);
4820 aff_i = isl_aff_scale(aff_i, c[i]);
4821 aff_i = isl_aff_scale_down(aff_i, d);
4822 if (sign >= 0)
4823 aff = isl_aff_sub(aff, aff_i);
4824 else
4825 aff = isl_aff_add(aff, aff_i);
4827 isl_int_clear(d);
4829 return aff;
4832 /* Extract an affine expression that expresses the output dimension "pos"
4833 * of "bmap" in terms of the parameters and input dimensions from
4834 * equality "eq".
4835 * Note that this expression may involve integer divisions defined
4836 * in terms of parameters and input dimensions.
4837 * The equality may also involve references to earlier (but not later)
4838 * output dimensions. These are replaced by the corresponding elements
4839 * in "ma".
4841 * If the equality is of the form
4843 * f(i) + h(j) + a x + g(i) = 0,
4845 * with f(i) a linear combinations of the parameters and input dimensions,
4846 * g(i) a linear combination of integer divisions defined in terms of the same
4847 * and h(j) a linear combinations of earlier output dimensions,
4848 * then the affine expression is
4850 * (-f(i) - g(i))/a - h(j)/a
4852 * If the equality is of the form
4854 * f(i) + h(j) - a x + g(i) = 0,
4856 * then the affine expression is
4858 * (f(i) + g(i))/a - h(j)/(-a)
4861 * If "div" refers to an integer division (i.e., it is smaller than
4862 * the number of integer divisions), then the equality constraint
4863 * does involve an integer division (the one at position "div") that
4864 * is defined in terms of output dimensions. However, this integer
4865 * division can be eliminated by exploiting a pair of constraints
4866 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4867 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4868 * -l + x >= 0.
4869 * In particular, let
4871 * x = e(i) + m floor(...)
4873 * with e(i) the expression derived above and floor(...) the integer
4874 * division involving output dimensions.
4875 * From
4877 * l <= x <= l + n,
4879 * we have
4881 * 0 <= x - l <= n
4883 * This means
4885 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4886 * = (e(i) - l) mod m
4888 * Therefore,
4890 * x - l = (e(i) - l) mod m
4892 * or
4894 * x = ((e(i) - l) mod m) + l
4896 * The variable "shift" below contains the expression -l, which may
4897 * also involve a linear combination of earlier output dimensions.
4899 static __isl_give isl_aff *extract_aff_from_equality(
4900 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4901 __isl_keep isl_multi_aff *ma)
4903 unsigned o_out;
4904 isl_size n_div, n_out;
4905 isl_ctx *ctx;
4906 isl_local_space *ls;
4907 isl_aff *aff, *shift;
4908 isl_val *mod;
4910 ctx = isl_basic_map_get_ctx(bmap);
4911 ls = isl_basic_map_get_local_space(bmap);
4912 ls = isl_local_space_domain(ls);
4913 aff = isl_aff_alloc(isl_local_space_copy(ls));
4914 if (!aff)
4915 goto error;
4916 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4917 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4918 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4919 if (n_out < 0 || n_div < 0)
4920 goto error;
4921 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4922 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4923 isl_seq_cpy(aff->v->el + 1 + o_out,
4924 bmap->eq[eq] + o_out + n_out, n_div);
4925 } else {
4926 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4927 isl_seq_neg(aff->v->el + 1 + o_out,
4928 bmap->eq[eq] + o_out + n_out, n_div);
4930 if (div < n_div)
4931 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4932 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4933 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4934 bmap->eq[eq][o_out + pos]);
4935 if (div < n_div) {
4936 shift = isl_aff_alloc(isl_local_space_copy(ls));
4937 if (!shift)
4938 goto error;
4939 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4940 isl_seq_cpy(shift->v->el + 1 + o_out,
4941 bmap->ineq[ineq] + o_out + n_out, n_div);
4942 isl_int_set_si(shift->v->el[0], 1);
4943 shift = subtract_initial(shift, ma, pos,
4944 bmap->ineq[ineq] + o_out, ctx->negone);
4945 aff = isl_aff_add(aff, isl_aff_copy(shift));
4946 mod = isl_val_int_from_isl_int(ctx,
4947 bmap->eq[eq][o_out + n_out + div]);
4948 mod = isl_val_abs(mod);
4949 aff = isl_aff_mod_val(aff, mod);
4950 aff = isl_aff_sub(aff, shift);
4953 isl_local_space_free(ls);
4954 return aff;
4955 error:
4956 isl_local_space_free(ls);
4957 isl_aff_free(aff);
4958 return NULL;
4961 /* Given a basic map with output dimensions defined
4962 * in terms of the parameters input dimensions and earlier
4963 * output dimensions using an equality (and possibly a pair on inequalities),
4964 * extract an isl_aff that expresses output dimension "pos" in terms
4965 * of the parameters and input dimensions.
4966 * Note that this expression may involve integer divisions defined
4967 * in terms of parameters and input dimensions.
4968 * "ma" contains the expressions corresponding to earlier output dimensions.
4970 * This function shares some similarities with
4971 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4973 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4974 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4976 int eq, div, ineq;
4977 isl_aff *aff;
4979 if (!bmap)
4980 return NULL;
4981 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4982 if (eq >= bmap->n_eq)
4983 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4984 "unable to find suitable equality", return NULL);
4985 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4987 aff = isl_aff_remove_unused_divs(aff);
4988 return aff;
4991 /* Given a basic map where each output dimension is defined
4992 * in terms of the parameters and input dimensions using an equality,
4993 * extract an isl_multi_aff that expresses the output dimensions in terms
4994 * of the parameters and input dimensions.
4996 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4997 __isl_take isl_basic_map *bmap)
4999 int i;
5000 isl_size n_out;
5001 isl_multi_aff *ma;
5003 if (!bmap)
5004 return NULL;
5006 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
5007 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5008 if (n_out < 0)
5009 ma = isl_multi_aff_free(ma);
5011 for (i = 0; i < n_out; ++i) {
5012 isl_aff *aff;
5014 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
5015 ma = isl_multi_aff_set_aff(ma, i, aff);
5018 isl_basic_map_free(bmap);
5020 return ma;
5023 /* Given a basic set where each set dimension is defined
5024 * in terms of the parameters using an equality,
5025 * extract an isl_multi_aff that expresses the set dimensions in terms
5026 * of the parameters.
5028 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
5029 __isl_take isl_basic_set *bset)
5031 return extract_isl_multi_aff_from_basic_map(bset);
5034 /* Create an isl_pw_multi_aff that is equivalent to
5035 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
5036 * The given basic map is such that each output dimension is defined
5037 * in terms of the parameters and input dimensions using an equality.
5039 * Since some applications expect the result of isl_pw_multi_aff_from_map
5040 * to only contain integer affine expressions, we compute the floor
5041 * of the expression before returning.
5043 * Remove all constraints involving local variables without
5044 * an explicit representation (resulting in the removal of those
5045 * local variables) prior to the actual extraction to ensure
5046 * that the local spaces in which the resulting affine expressions
5047 * are created do not contain any unknown local variables.
5048 * Removing such constraints is safe because constraints involving
5049 * unknown local variables are not used to determine whether
5050 * a basic map is obviously single-valued.
5052 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
5053 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
5055 isl_multi_aff *ma;
5057 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
5058 ma = extract_isl_multi_aff_from_basic_map(bmap);
5059 ma = isl_multi_aff_floor(ma);
5060 return isl_pw_multi_aff_alloc(domain, ma);
5063 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5064 * This obviously only works if the input "map" is single-valued.
5065 * If so, we compute the lexicographic minimum of the image in the form
5066 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5067 * to its lexicographic minimum.
5068 * If the input is not single-valued, we produce an error.
5070 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
5071 __isl_take isl_map *map)
5073 int i;
5074 int sv;
5075 isl_pw_multi_aff *pma;
5077 sv = isl_map_is_single_valued(map);
5078 if (sv < 0)
5079 goto error;
5080 if (!sv)
5081 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5082 "map is not single-valued", goto error);
5083 map = isl_map_make_disjoint(map);
5084 if (!map)
5085 return NULL;
5087 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5089 for (i = 0; i < map->n; ++i) {
5090 isl_pw_multi_aff *pma_i;
5091 isl_basic_map *bmap;
5092 bmap = isl_basic_map_copy(map->p[i]);
5093 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5094 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5097 isl_map_free(map);
5098 return pma;
5099 error:
5100 isl_map_free(map);
5101 return NULL;
5104 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5105 * taking into account that the output dimension at position "d"
5106 * can be represented as
5108 * x = floor((e(...) + c1) / m)
5110 * given that constraint "i" is of the form
5112 * e(...) + c1 - m x >= 0
5115 * Let "map" be of the form
5117 * A -> B
5119 * We construct a mapping
5121 * A -> [A -> x = floor(...)]
5123 * apply that to the map, obtaining
5125 * [A -> x = floor(...)] -> B
5127 * and equate dimension "d" to x.
5128 * We then compute a isl_pw_multi_aff representation of the resulting map
5129 * and plug in the mapping above.
5131 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5132 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5134 isl_ctx *ctx;
5135 isl_space *space = NULL;
5136 isl_local_space *ls;
5137 isl_multi_aff *ma;
5138 isl_aff *aff;
5139 isl_vec *v;
5140 isl_map *insert;
5141 int offset;
5142 isl_size n;
5143 isl_size n_in;
5144 isl_pw_multi_aff *pma;
5145 isl_bool is_set;
5147 is_set = isl_map_is_set(map);
5148 if (is_set < 0)
5149 goto error;
5151 offset = isl_basic_map_offset(hull, isl_dim_out);
5152 ctx = isl_map_get_ctx(map);
5153 space = isl_space_domain(isl_map_get_space(map));
5154 n_in = isl_space_dim(space, isl_dim_set);
5155 n = isl_space_dim(space, isl_dim_all);
5156 if (n_in < 0 || n < 0)
5157 goto error;
5159 v = isl_vec_alloc(ctx, 1 + 1 + n);
5160 if (v) {
5161 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5162 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5164 isl_basic_map_free(hull);
5166 ls = isl_local_space_from_space(isl_space_copy(space));
5167 aff = isl_aff_alloc_vec_validated(ls, v);
5168 aff = isl_aff_floor(aff);
5169 if (is_set) {
5170 isl_space_free(space);
5171 ma = isl_multi_aff_from_aff(aff);
5172 } else {
5173 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5174 ma = isl_multi_aff_range_product(ma,
5175 isl_multi_aff_from_aff(aff));
5178 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5179 map = isl_map_apply_domain(map, insert);
5180 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5181 pma = isl_pw_multi_aff_from_map(map);
5182 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5184 return pma;
5185 error:
5186 isl_space_free(space);
5187 isl_map_free(map);
5188 isl_basic_map_free(hull);
5189 return NULL;
5192 /* Is constraint "c" of the form
5194 * e(...) + c1 - m x >= 0
5196 * or
5198 * -e(...) + c2 + m x >= 0
5200 * where m > 1 and e only depends on parameters and input dimensions?
5202 * "offset" is the offset of the output dimensions
5203 * "pos" is the position of output dimension x.
5205 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5207 if (isl_int_is_zero(c[offset + d]))
5208 return 0;
5209 if (isl_int_is_one(c[offset + d]))
5210 return 0;
5211 if (isl_int_is_negone(c[offset + d]))
5212 return 0;
5213 if (isl_seq_first_non_zero(c + offset, d) != -1)
5214 return 0;
5215 if (isl_seq_first_non_zero(c + offset + d + 1,
5216 total - (offset + d + 1)) != -1)
5217 return 0;
5218 return 1;
5221 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5223 * As a special case, we first check if there is any pair of constraints,
5224 * shared by all the basic maps in "map" that force a given dimension
5225 * to be equal to the floor of some affine combination of the input dimensions.
5227 * In particular, if we can find two constraints
5229 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5231 * and
5233 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5235 * where m > 1 and e only depends on parameters and input dimensions,
5236 * and such that
5238 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5240 * then we know that we can take
5242 * x = floor((e(...) + c1) / m)
5244 * without having to perform any computation.
5246 * Note that we know that
5248 * c1 + c2 >= 1
5250 * If c1 + c2 were 0, then we would have detected an equality during
5251 * simplification. If c1 + c2 were negative, then we would have detected
5252 * a contradiction.
5254 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5255 __isl_take isl_map *map)
5257 int d;
5258 isl_size dim;
5259 int i, j, n;
5260 int offset;
5261 isl_size total;
5262 isl_int sum;
5263 isl_basic_map *hull;
5265 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5266 dim = isl_map_dim(map, isl_dim_out);
5267 total = isl_basic_map_dim(hull, isl_dim_all);
5268 if (dim < 0 || total < 0)
5269 goto error;
5271 isl_int_init(sum);
5272 offset = isl_basic_map_offset(hull, isl_dim_out);
5273 n = hull->n_ineq;
5274 for (d = 0; d < dim; ++d) {
5275 for (i = 0; i < n; ++i) {
5276 if (!is_potential_div_constraint(hull->ineq[i],
5277 offset, d, 1 + total))
5278 continue;
5279 for (j = i + 1; j < n; ++j) {
5280 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5281 hull->ineq[j] + 1, total))
5282 continue;
5283 isl_int_add(sum, hull->ineq[i][0],
5284 hull->ineq[j][0]);
5285 if (isl_int_abs_lt(sum,
5286 hull->ineq[i][offset + d]))
5287 break;
5290 if (j >= n)
5291 continue;
5292 isl_int_clear(sum);
5293 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5294 j = i;
5295 return pw_multi_aff_from_map_div(map, hull, d, j);
5298 isl_int_clear(sum);
5299 isl_basic_map_free(hull);
5300 return pw_multi_aff_from_map_base(map);
5301 error:
5302 isl_map_free(map);
5303 isl_basic_map_free(hull);
5304 return NULL;
5307 /* Given an affine expression
5309 * [A -> B] -> f(A,B)
5311 * construct an isl_multi_aff
5313 * [A -> B] -> B'
5315 * such that dimension "d" in B' is set to "aff" and the remaining
5316 * dimensions are set equal to the corresponding dimensions in B.
5317 * "n_in" is the dimension of the space A.
5318 * "n_out" is the dimension of the space B.
5320 * If "is_set" is set, then the affine expression is of the form
5322 * [B] -> f(B)
5324 * and we construct an isl_multi_aff
5326 * B -> B'
5328 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5329 unsigned n_in, unsigned n_out, int is_set)
5331 int i;
5332 isl_multi_aff *ma;
5333 isl_space *space, *space2;
5334 isl_local_space *ls;
5336 space = isl_aff_get_domain_space(aff);
5337 ls = isl_local_space_from_space(isl_space_copy(space));
5338 space2 = isl_space_copy(space);
5339 if (!is_set)
5340 space2 = isl_space_range(isl_space_unwrap(space2));
5341 space = isl_space_map_from_domain_and_range(space, space2);
5342 ma = isl_multi_aff_alloc(space);
5343 ma = isl_multi_aff_set_aff(ma, d, aff);
5345 for (i = 0; i < n_out; ++i) {
5346 if (i == d)
5347 continue;
5348 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5349 isl_dim_set, n_in + i);
5350 ma = isl_multi_aff_set_aff(ma, i, aff);
5353 isl_local_space_free(ls);
5355 return ma;
5358 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5359 * taking into account that the dimension at position "d" can be written as
5361 * x = m a + f(..) (1)
5363 * where m is equal to "gcd".
5364 * "i" is the index of the equality in "hull" that defines f(..).
5365 * In particular, the equality is of the form
5367 * f(..) - x + m g(existentials) = 0
5369 * or
5371 * -f(..) + x + m g(existentials) = 0
5373 * We basically plug (1) into "map", resulting in a map with "a"
5374 * in the range instead of "x". The corresponding isl_pw_multi_aff
5375 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5377 * Specifically, given the input map
5379 * A -> B
5381 * We first wrap it into a set
5383 * [A -> B]
5385 * and define (1) on top of the corresponding space, resulting in "aff".
5386 * We use this to create an isl_multi_aff that maps the output position "d"
5387 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5388 * We plug this into the wrapped map, unwrap the result and compute the
5389 * corresponding isl_pw_multi_aff.
5390 * The result is an expression
5392 * A -> T(A)
5394 * We adjust that to
5396 * A -> [A -> T(A)]
5398 * so that we can plug that into "aff", after extending the latter to
5399 * a mapping
5401 * [A -> B] -> B'
5404 * If "map" is actually a set, then there is no "A" space, meaning
5405 * that we do not need to perform any wrapping, and that the result
5406 * of the recursive call is of the form
5408 * [T]
5410 * which is plugged into a mapping of the form
5412 * B -> B'
5414 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5415 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5416 isl_int gcd)
5418 isl_set *set;
5419 isl_space *space;
5420 isl_local_space *ls;
5421 isl_aff *aff;
5422 isl_multi_aff *ma;
5423 isl_pw_multi_aff *pma, *id;
5424 isl_size n_in;
5425 unsigned o_out;
5426 isl_size n_out;
5427 isl_bool is_set;
5429 is_set = isl_map_is_set(map);
5430 if (is_set < 0)
5431 goto error;
5433 n_in = isl_basic_map_dim(hull, isl_dim_in);
5434 n_out = isl_basic_map_dim(hull, isl_dim_out);
5435 if (n_in < 0 || n_out < 0)
5436 goto error;
5437 o_out = isl_basic_map_offset(hull, isl_dim_out);
5439 if (is_set)
5440 set = map;
5441 else
5442 set = isl_map_wrap(map);
5443 space = isl_space_map_from_set(isl_set_get_space(set));
5444 ma = isl_multi_aff_identity(space);
5445 ls = isl_local_space_from_space(isl_set_get_space(set));
5446 aff = isl_aff_alloc(ls);
5447 if (aff) {
5448 isl_int_set_si(aff->v->el[0], 1);
5449 if (isl_int_is_one(hull->eq[i][o_out + d]))
5450 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5451 aff->v->size - 1);
5452 else
5453 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5454 aff->v->size - 1);
5455 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5457 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5458 set = isl_set_preimage_multi_aff(set, ma);
5460 ma = range_map(aff, d, n_in, n_out, is_set);
5462 if (is_set)
5463 map = set;
5464 else
5465 map = isl_set_unwrap(set);
5466 pma = isl_pw_multi_aff_from_map(map);
5468 if (!is_set) {
5469 space = isl_pw_multi_aff_get_domain_space(pma);
5470 space = isl_space_map_from_set(space);
5471 id = isl_pw_multi_aff_identity(space);
5472 pma = isl_pw_multi_aff_range_product(id, pma);
5474 id = isl_pw_multi_aff_from_multi_aff(ma);
5475 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5477 isl_basic_map_free(hull);
5478 return pma;
5479 error:
5480 isl_map_free(map);
5481 isl_basic_map_free(hull);
5482 return NULL;
5485 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5486 * "hull" contains the equalities valid for "map".
5488 * Check if any of the output dimensions is "strided".
5489 * That is, we check if it can be written as
5491 * x = m a + f(..)
5493 * with m greater than 1, a some combination of existentially quantified
5494 * variables and f an expression in the parameters and input dimensions.
5495 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5497 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5498 * special case.
5500 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5501 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5503 int i, j;
5504 isl_size n_out;
5505 unsigned o_out;
5506 isl_size n_div;
5507 unsigned o_div;
5508 isl_int gcd;
5510 n_div = isl_basic_map_dim(hull, isl_dim_div);
5511 n_out = isl_basic_map_dim(hull, isl_dim_out);
5512 if (n_div < 0 || n_out < 0)
5513 goto error;
5515 if (n_div == 0) {
5516 isl_basic_map_free(hull);
5517 return pw_multi_aff_from_map_check_div(map);
5520 isl_int_init(gcd);
5522 o_div = isl_basic_map_offset(hull, isl_dim_div);
5523 o_out = isl_basic_map_offset(hull, isl_dim_out);
5525 for (i = 0; i < n_out; ++i) {
5526 for (j = 0; j < hull->n_eq; ++j) {
5527 isl_int *eq = hull->eq[j];
5528 isl_pw_multi_aff *res;
5530 if (!isl_int_is_one(eq[o_out + i]) &&
5531 !isl_int_is_negone(eq[o_out + i]))
5532 continue;
5533 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5534 continue;
5535 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5536 n_out - (i + 1)) != -1)
5537 continue;
5538 isl_seq_gcd(eq + o_div, n_div, &gcd);
5539 if (isl_int_is_zero(gcd))
5540 continue;
5541 if (isl_int_is_one(gcd))
5542 continue;
5544 res = pw_multi_aff_from_map_stride(map, hull,
5545 i, j, gcd);
5546 isl_int_clear(gcd);
5547 return res;
5551 isl_int_clear(gcd);
5552 isl_basic_map_free(hull);
5553 return pw_multi_aff_from_map_check_div(map);
5554 error:
5555 isl_map_free(map);
5556 isl_basic_map_free(hull);
5557 return NULL;
5560 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5562 * As a special case, we first check if all output dimensions are uniquely
5563 * defined in terms of the parameters and input dimensions over the entire
5564 * domain. If so, we extract the desired isl_pw_multi_aff directly
5565 * from the affine hull of "map" and its domain.
5567 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5568 * special cases.
5570 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5572 isl_bool sv;
5573 isl_size n;
5574 isl_basic_map *hull;
5576 n = isl_map_n_basic_map(map);
5577 if (n < 0)
5578 goto error;
5580 if (n == 1) {
5581 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5582 hull = isl_basic_map_plain_affine_hull(hull);
5583 sv = isl_basic_map_plain_is_single_valued(hull);
5584 if (sv >= 0 && sv)
5585 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5586 hull);
5587 isl_basic_map_free(hull);
5589 map = isl_map_detect_equalities(map);
5590 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5591 sv = isl_basic_map_plain_is_single_valued(hull);
5592 if (sv >= 0 && sv)
5593 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5594 if (sv >= 0)
5595 return pw_multi_aff_from_map_check_strides(map, hull);
5596 isl_basic_map_free(hull);
5597 error:
5598 isl_map_free(map);
5599 return NULL;
5602 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5603 * but is considered as a function on an isl_map when exported.
5605 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5607 return isl_pw_multi_aff_from_map(map);
5610 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5612 return isl_pw_multi_aff_from_map(set);
5615 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5616 * but is considered as a function on an isl_set when exported.
5618 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5620 return isl_pw_multi_aff_from_set(set);
5623 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5624 * add it to *user.
5626 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5628 isl_union_pw_multi_aff **upma = user;
5629 isl_pw_multi_aff *pma;
5631 pma = isl_pw_multi_aff_from_map(map);
5632 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5634 return *upma ? isl_stat_ok : isl_stat_error;
5637 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5638 * domain.
5640 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5641 __isl_take isl_aff *aff)
5643 isl_multi_aff *ma;
5644 isl_pw_multi_aff *pma;
5646 ma = isl_multi_aff_from_aff(aff);
5647 pma = isl_pw_multi_aff_from_multi_aff(ma);
5648 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5651 /* Try and create an isl_union_pw_multi_aff that is equivalent
5652 * to the given isl_union_map.
5653 * The isl_union_map is required to be single-valued in each space.
5654 * Otherwise, an error is produced.
5656 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5657 __isl_take isl_union_map *umap)
5659 isl_space *space;
5660 isl_union_pw_multi_aff *upma;
5662 space = isl_union_map_get_space(umap);
5663 upma = isl_union_pw_multi_aff_empty(space);
5664 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5665 upma = isl_union_pw_multi_aff_free(upma);
5666 isl_union_map_free(umap);
5668 return upma;
5671 /* This function performs the same operation as
5672 * isl_union_pw_multi_aff_from_union_map,
5673 * but is considered as a function on an isl_union_map when exported.
5675 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5676 __isl_take isl_union_map *umap)
5678 return isl_union_pw_multi_aff_from_union_map(umap);
5681 /* Try and create an isl_union_pw_multi_aff that is equivalent
5682 * to the given isl_union_set.
5683 * The isl_union_set is required to be a singleton in each space.
5684 * Otherwise, an error is produced.
5686 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5687 __isl_take isl_union_set *uset)
5689 return isl_union_pw_multi_aff_from_union_map(uset);
5692 /* Return the piecewise affine expression "set ? 1 : 0".
5694 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5696 isl_pw_aff *pa;
5697 isl_space *space = isl_set_get_space(set);
5698 isl_local_space *ls = isl_local_space_from_space(space);
5699 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5700 isl_aff *one = isl_aff_zero_on_domain(ls);
5702 one = isl_aff_add_constant_si(one, 1);
5703 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5704 set = isl_set_complement(set);
5705 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5707 return pa;
5710 /* Plug in "subs" for dimension "type", "pos" of "aff".
5712 * Let i be the dimension to replace and let "subs" be of the form
5714 * f/d
5716 * and "aff" of the form
5718 * (a i + g)/m
5720 * The result is
5722 * (a f + d g')/(m d)
5724 * where g' is the result of plugging in "subs" in each of the integer
5725 * divisions in g.
5727 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5728 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5730 isl_ctx *ctx;
5731 isl_int v;
5732 isl_size n_div;
5734 aff = isl_aff_cow(aff);
5735 if (!aff || !subs)
5736 return isl_aff_free(aff);
5738 ctx = isl_aff_get_ctx(aff);
5739 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5740 isl_die(ctx, isl_error_invalid,
5741 "spaces don't match", return isl_aff_free(aff));
5742 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5743 if (n_div < 0)
5744 return isl_aff_free(aff);
5745 if (n_div != 0)
5746 isl_die(ctx, isl_error_unsupported,
5747 "cannot handle divs yet", return isl_aff_free(aff));
5749 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5750 if (!aff->ls)
5751 return isl_aff_free(aff);
5753 aff->v = isl_vec_cow(aff->v);
5754 if (!aff->v)
5755 return isl_aff_free(aff);
5757 pos += isl_local_space_offset(aff->ls, type);
5759 isl_int_init(v);
5760 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5761 aff->v->size, subs->v->size, v);
5762 isl_int_clear(v);
5764 return aff;
5767 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5768 * expressions in "maff".
5770 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5771 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5772 __isl_keep isl_aff *subs)
5774 isl_size n;
5775 int i;
5777 n = isl_multi_aff_size(maff);
5778 if (n < 0 || !subs)
5779 return isl_multi_aff_free(maff);
5781 if (type == isl_dim_in)
5782 type = isl_dim_set;
5784 for (i = 0; i < n; ++i) {
5785 isl_aff *aff;
5787 aff = isl_multi_aff_take_at(maff, i);
5788 aff = isl_aff_substitute(aff, type, pos, subs);
5789 maff = isl_multi_aff_restore_at(maff, i, aff);
5792 return maff;
5795 /* Plug in "subs" for input dimension "pos" of "pma".
5797 * pma is of the form
5799 * A_i(v) -> M_i(v)
5801 * while subs is of the form
5803 * v' = B_j(v) -> S_j
5805 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5806 * has a contribution in the result, in particular
5808 * C_ij(S_j) -> M_i(S_j)
5810 * Note that plugging in S_j in C_ij may also result in an empty set
5811 * and this contribution should simply be discarded.
5813 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5814 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5815 __isl_keep isl_pw_aff *subs)
5817 int i, j, n;
5818 isl_pw_multi_aff *res;
5820 if (!pma || !subs)
5821 return isl_pw_multi_aff_free(pma);
5823 n = pma->n * subs->n;
5824 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5826 for (i = 0; i < pma->n; ++i) {
5827 for (j = 0; j < subs->n; ++j) {
5828 isl_set *common;
5829 isl_multi_aff *res_ij;
5830 int empty;
5832 common = isl_set_intersect(
5833 isl_set_copy(pma->p[i].set),
5834 isl_set_copy(subs->p[j].set));
5835 common = isl_set_substitute(common,
5836 pos, subs->p[j].aff);
5837 empty = isl_set_plain_is_empty(common);
5838 if (empty < 0 || empty) {
5839 isl_set_free(common);
5840 if (empty < 0)
5841 goto error;
5842 continue;
5845 res_ij = isl_multi_aff_substitute(
5846 isl_multi_aff_copy(pma->p[i].maff),
5847 isl_dim_in, pos, subs->p[j].aff);
5849 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5853 isl_pw_multi_aff_free(pma);
5854 return res;
5855 error:
5856 isl_pw_multi_aff_free(pma);
5857 isl_pw_multi_aff_free(res);
5858 return NULL;
5861 /* Compute the preimage of a range of dimensions in the affine expression "src"
5862 * under "ma" and put the result in "dst". The number of dimensions in "src"
5863 * that precede the range is given by "n_before". The number of dimensions
5864 * in the range is given by the number of output dimensions of "ma".
5865 * The number of dimensions that follow the range is given by "n_after".
5866 * If "has_denom" is set (to one),
5867 * then "src" and "dst" have an extra initial denominator.
5868 * "n_div_ma" is the number of existentials in "ma"
5869 * "n_div_bset" is the number of existentials in "src"
5870 * The resulting "dst" (which is assumed to have been allocated by
5871 * the caller) contains coefficients for both sets of existentials,
5872 * first those in "ma" and then those in "src".
5873 * f, c1, c2 and g are temporary objects that have been initialized
5874 * by the caller.
5876 * Let src represent the expression
5878 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5880 * and let ma represent the expressions
5882 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5884 * We start out with the following expression for dst:
5886 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5888 * with the multiplication factor f initially equal to 1
5889 * and f \sum_i b_i v_i kept separately.
5890 * For each x_i that we substitute, we multiply the numerator
5891 * (and denominator) of dst by c_1 = m_i and add the numerator
5892 * of the x_i expression multiplied by c_2 = f b_i,
5893 * after removing the common factors of c_1 and c_2.
5894 * The multiplication factor f also needs to be multiplied by c_1
5895 * for the next x_j, j > i.
5897 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5898 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5899 int n_div_ma, int n_div_bmap,
5900 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5902 int i;
5903 isl_size n_param, n_in, n_out;
5904 int o_dst, o_src;
5906 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5907 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5908 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5909 if (n_param < 0 || n_in < 0 || n_out < 0)
5910 return isl_stat_error;
5912 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5913 o_dst = o_src = has_denom + 1 + n_param + n_before;
5914 isl_seq_clr(dst + o_dst, n_in);
5915 o_dst += n_in;
5916 o_src += n_out;
5917 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5918 o_dst += n_after;
5919 o_src += n_after;
5920 isl_seq_clr(dst + o_dst, n_div_ma);
5921 o_dst += n_div_ma;
5922 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5924 isl_int_set_si(f, 1);
5926 for (i = 0; i < n_out; ++i) {
5927 int offset = has_denom + 1 + n_param + n_before + i;
5929 if (isl_int_is_zero(src[offset]))
5930 continue;
5931 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5932 isl_int_mul(c2, f, src[offset]);
5933 isl_int_gcd(g, c1, c2);
5934 isl_int_divexact(c1, c1, g);
5935 isl_int_divexact(c2, c2, g);
5937 isl_int_mul(f, f, c1);
5938 o_dst = has_denom;
5939 o_src = 1;
5940 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5941 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5942 o_dst += 1 + n_param;
5943 o_src += 1 + n_param;
5944 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5945 o_dst += n_before;
5946 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5947 c2, ma->u.p[i]->v->el + o_src, n_in);
5948 o_dst += n_in;
5949 o_src += n_in;
5950 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5951 o_dst += n_after;
5952 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5953 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5954 o_dst += n_div_ma;
5955 o_src += n_div_ma;
5956 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5957 if (has_denom)
5958 isl_int_mul(dst[0], dst[0], c1);
5961 return isl_stat_ok;
5964 /* Compute the pullback of "aff" by the function represented by "ma".
5965 * In other words, plug in "ma" in "aff". The result is an affine expression
5966 * defined over the domain space of "ma".
5968 * If "aff" is represented by
5970 * (a(p) + b x + c(divs))/d
5972 * and ma is represented by
5974 * x = D(p) + F(y) + G(divs')
5976 * then the result is
5978 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5980 * The divs in the local space of the input are similarly adjusted
5981 * through a call to isl_local_space_preimage_multi_aff.
5983 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5984 __isl_take isl_multi_aff *ma)
5986 isl_aff *res = NULL;
5987 isl_local_space *ls;
5988 isl_size n_div_aff, n_div_ma;
5989 isl_int f, c1, c2, g;
5991 ma = isl_multi_aff_align_divs(ma);
5992 if (!aff || !ma)
5993 goto error;
5995 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5996 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5997 if (n_div_aff < 0 || n_div_ma < 0)
5998 goto error;
6000 ls = isl_aff_get_domain_local_space(aff);
6001 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
6002 res = isl_aff_alloc(ls);
6003 if (!res)
6004 goto error;
6006 isl_int_init(f);
6007 isl_int_init(c1);
6008 isl_int_init(c2);
6009 isl_int_init(g);
6011 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
6012 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
6013 res = isl_aff_free(res);
6015 isl_int_clear(f);
6016 isl_int_clear(c1);
6017 isl_int_clear(c2);
6018 isl_int_clear(g);
6020 isl_aff_free(aff);
6021 isl_multi_aff_free(ma);
6022 res = isl_aff_normalize(res);
6023 return res;
6024 error:
6025 isl_aff_free(aff);
6026 isl_multi_aff_free(ma);
6027 isl_aff_free(res);
6028 return NULL;
6031 /* Compute the pullback of "aff1" by the function represented by "aff2".
6032 * In other words, plug in "aff2" in "aff1". The result is an affine expression
6033 * defined over the domain space of "aff1".
6035 * The domain of "aff1" should match the range of "aff2", which means
6036 * that it should be single-dimensional.
6038 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
6039 __isl_take isl_aff *aff2)
6041 isl_multi_aff *ma;
6043 ma = isl_multi_aff_from_aff(aff2);
6044 return isl_aff_pullback_multi_aff(aff1, ma);
6047 /* Compute the pullback of "ma1" by the function represented by "ma2".
6048 * In other words, plug in "ma2" in "ma1".
6050 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
6051 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
6053 int i;
6054 isl_size n;
6055 isl_space *space = NULL;
6057 isl_multi_aff_align_params_bin(&ma1, &ma2);
6058 ma2 = isl_multi_aff_align_divs(ma2);
6059 n = isl_multi_aff_size(ma1);
6060 if (n < 0 || !ma2)
6061 goto error;
6063 space = isl_space_join(isl_multi_aff_get_space(ma2),
6064 isl_multi_aff_get_space(ma1));
6066 for (i = 0; i < n; ++i) {
6067 isl_aff *aff;
6069 aff = isl_multi_aff_take_at(ma1, i);
6070 aff = isl_aff_pullback_multi_aff(aff, isl_multi_aff_copy(ma2));
6071 ma1 = isl_multi_aff_restore_at(ma1, i, aff);
6074 ma1 = isl_multi_aff_reset_space(ma1, space);
6075 isl_multi_aff_free(ma2);
6076 return ma1;
6077 error:
6078 isl_space_free(space);
6079 isl_multi_aff_free(ma2);
6080 isl_multi_aff_free(ma1);
6081 return NULL;
6084 /* Extend the local space of "dst" to include the divs
6085 * in the local space of "src".
6087 * If "src" does not have any divs or if the local spaces of "dst" and
6088 * "src" are the same, then no extension is required.
6090 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6091 __isl_keep isl_aff *src)
6093 isl_ctx *ctx;
6094 isl_size src_n_div, dst_n_div;
6095 int *exp1 = NULL;
6096 int *exp2 = NULL;
6097 isl_bool equal;
6098 isl_mat *div;
6100 if (!src || !dst)
6101 return isl_aff_free(dst);
6103 ctx = isl_aff_get_ctx(src);
6104 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6105 if (equal < 0)
6106 return isl_aff_free(dst);
6107 if (!equal)
6108 isl_die(ctx, isl_error_invalid,
6109 "spaces don't match", goto error);
6111 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6112 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6113 if (src_n_div == 0)
6114 return dst;
6115 equal = isl_local_space_is_equal(src->ls, dst->ls);
6116 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6117 return isl_aff_free(dst);
6118 if (equal)
6119 return dst;
6121 exp1 = isl_alloc_array(ctx, int, src_n_div);
6122 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6123 if (!exp1 || (dst_n_div && !exp2))
6124 goto error;
6126 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6127 dst = isl_aff_expand_divs(dst, div, exp2);
6128 free(exp1);
6129 free(exp2);
6131 return dst;
6132 error:
6133 free(exp1);
6134 free(exp2);
6135 return isl_aff_free(dst);
6138 /* Adjust the local spaces of the affine expressions in "maff"
6139 * such that they all have the save divs.
6141 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6142 __isl_take isl_multi_aff *maff)
6144 isl_aff *aff_0;
6145 isl_size n;
6146 int i;
6148 n = isl_multi_aff_size(maff);
6149 if (n < 0)
6150 return isl_multi_aff_free(maff);
6151 if (n <= 1)
6152 return maff;
6154 aff_0 = isl_multi_aff_take_at(maff, 0);
6155 for (i = 1; i < n; ++i) {
6156 isl_aff *aff_i;
6158 aff_i = isl_multi_aff_peek_at(maff, i);
6159 aff_0 = isl_aff_align_divs(aff_0, aff_i);
6161 maff = isl_multi_aff_restore_at(maff, 0, aff_0);
6163 aff_0 = isl_multi_aff_peek_at(maff, 0);
6164 for (i = 1; i < n; ++i) {
6165 isl_aff *aff_i;
6167 aff_i = isl_multi_aff_take_at(maff, i);
6168 aff_i = isl_aff_align_divs(aff_i, aff_0);
6169 maff = isl_multi_aff_restore_at(maff, i, aff_i);
6172 return maff;
6175 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6177 aff = isl_aff_cow(aff);
6178 if (!aff)
6179 return NULL;
6181 aff->ls = isl_local_space_lift(aff->ls);
6182 if (!aff->ls)
6183 return isl_aff_free(aff);
6185 return aff;
6188 /* Lift "maff" to a space with extra dimensions such that the result
6189 * has no more existentially quantified variables.
6190 * If "ls" is not NULL, then *ls is assigned the local space that lies
6191 * at the basis of the lifting applied to "maff".
6193 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6194 __isl_give isl_local_space **ls)
6196 int i;
6197 isl_space *space;
6198 isl_aff *aff;
6199 isl_size n, n_div;
6201 if (ls)
6202 *ls = NULL;
6204 n = isl_multi_aff_size(maff);
6205 if (n < 0)
6206 return isl_multi_aff_free(maff);
6208 if (n == 0) {
6209 if (ls) {
6210 isl_space *space = isl_multi_aff_get_domain_space(maff);
6211 *ls = isl_local_space_from_space(space);
6212 if (!*ls)
6213 return isl_multi_aff_free(maff);
6215 return maff;
6218 maff = isl_multi_aff_align_divs(maff);
6220 aff = isl_multi_aff_peek_at(maff, 0);
6221 n_div = isl_aff_dim(aff, isl_dim_div);
6222 if (n_div < 0)
6223 return isl_multi_aff_free(maff);
6224 space = isl_multi_aff_get_space(maff);
6225 space = isl_space_lift(isl_space_domain(space), n_div);
6226 space = isl_space_extend_domain_with_range(space,
6227 isl_multi_aff_get_space(maff));
6228 maff = isl_multi_aff_restore_space(maff, space);
6230 if (ls) {
6231 aff = isl_multi_aff_peek_at(maff, 0);
6232 *ls = isl_aff_get_domain_local_space(aff);
6233 if (!*ls)
6234 return isl_multi_aff_free(maff);
6237 for (i = 0; i < n; ++i) {
6238 aff = isl_multi_aff_take_at(maff, i);
6239 aff = isl_aff_lift(aff);
6240 maff = isl_multi_aff_restore_at(maff, i, aff);
6243 return maff;
6246 #undef TYPE
6247 #define TYPE isl_pw_multi_aff
6248 static
6249 #include "check_type_range_templ.c"
6251 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6253 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6254 __isl_keep isl_pw_multi_aff *pma, int pos)
6256 int i;
6257 isl_size n_out;
6258 isl_space *space;
6259 isl_pw_aff *pa;
6261 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6262 return NULL;
6264 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6265 if (n_out < 0)
6266 return NULL;
6268 space = isl_pw_multi_aff_get_space(pma);
6269 space = isl_space_drop_dims(space, isl_dim_out,
6270 pos + 1, n_out - pos - 1);
6271 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6273 pa = isl_pw_aff_alloc_size(space, pma->n);
6274 for (i = 0; i < pma->n; ++i) {
6275 isl_aff *aff;
6276 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6277 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6280 return pa;
6283 /* This is an alternative name for the function above.
6285 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6286 __isl_keep isl_pw_multi_aff *pma, int pos)
6288 return isl_pw_multi_aff_get_at(pma, pos);
6291 /* Return an isl_pw_multi_aff with the given "set" as domain and
6292 * an unnamed zero-dimensional range.
6294 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6295 __isl_take isl_set *set)
6297 isl_multi_aff *ma;
6298 isl_space *space;
6300 space = isl_set_get_space(set);
6301 space = isl_space_from_domain(space);
6302 ma = isl_multi_aff_zero(space);
6303 return isl_pw_multi_aff_alloc(set, ma);
6306 /* Add an isl_pw_multi_aff with the given "set" as domain and
6307 * an unnamed zero-dimensional range to *user.
6309 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6310 void *user)
6312 isl_union_pw_multi_aff **upma = user;
6313 isl_pw_multi_aff *pma;
6315 pma = isl_pw_multi_aff_from_domain(set);
6316 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6318 return isl_stat_ok;
6321 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6322 * an unnamed zero-dimensional range.
6324 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6325 __isl_take isl_union_set *uset)
6327 isl_space *space;
6328 isl_union_pw_multi_aff *upma;
6330 if (!uset)
6331 return NULL;
6333 space = isl_union_set_get_space(uset);
6334 upma = isl_union_pw_multi_aff_empty(space);
6336 if (isl_union_set_foreach_set(uset,
6337 &add_pw_multi_aff_from_domain, &upma) < 0)
6338 goto error;
6340 isl_union_set_free(uset);
6341 return upma;
6342 error:
6343 isl_union_set_free(uset);
6344 isl_union_pw_multi_aff_free(upma);
6345 return NULL;
6348 /* Local data for bin_entry and the callback "fn".
6350 struct isl_union_pw_multi_aff_bin_data {
6351 isl_union_pw_multi_aff *upma2;
6352 isl_union_pw_multi_aff *res;
6353 isl_pw_multi_aff *pma;
6354 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6357 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6358 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6360 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6362 struct isl_union_pw_multi_aff_bin_data *data = user;
6363 isl_stat r;
6365 data->pma = pma;
6366 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6367 data->fn, data);
6368 isl_pw_multi_aff_free(pma);
6370 return r;
6373 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6374 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6375 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6376 * as *entry. The callback should adjust data->res if desired.
6378 static __isl_give isl_union_pw_multi_aff *bin_op(
6379 __isl_take isl_union_pw_multi_aff *upma1,
6380 __isl_take isl_union_pw_multi_aff *upma2,
6381 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6383 isl_space *space;
6384 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6386 space = isl_union_pw_multi_aff_get_space(upma2);
6387 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6388 space = isl_union_pw_multi_aff_get_space(upma1);
6389 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6391 if (!upma1 || !upma2)
6392 goto error;
6394 data.upma2 = upma2;
6395 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6396 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6397 &bin_entry, &data) < 0)
6398 goto error;
6400 isl_union_pw_multi_aff_free(upma1);
6401 isl_union_pw_multi_aff_free(upma2);
6402 return data.res;
6403 error:
6404 isl_union_pw_multi_aff_free(upma1);
6405 isl_union_pw_multi_aff_free(upma2);
6406 isl_union_pw_multi_aff_free(data.res);
6407 return NULL;
6410 /* Given two isl_pw_multi_affs A -> B and C -> D,
6411 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6413 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6414 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6416 isl_space *space;
6418 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6419 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6420 isl_pw_multi_aff_get_space(pma2));
6421 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6422 &isl_multi_aff_range_product);
6425 /* Given two isl_pw_multi_affs A -> B and C -> D,
6426 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6428 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6429 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6431 isl_space *space;
6433 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6434 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6435 isl_pw_multi_aff_get_space(pma2));
6436 space = isl_space_flatten_range(space);
6437 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6438 &isl_multi_aff_flat_range_product);
6441 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6442 * to compute some form of range product and add the result to data->res.
6444 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6445 __isl_give isl_pw_multi_aff *(*range_product)(
6446 __isl_take isl_pw_multi_aff *pma1,
6447 __isl_take isl_pw_multi_aff *pma2),
6448 void *user)
6450 struct isl_union_pw_multi_aff_bin_data *data = user;
6451 isl_bool match;
6452 isl_space *space1, *space2;
6454 space1 = isl_pw_multi_aff_peek_space(data->pma);
6455 space2 = isl_pw_multi_aff_peek_space(pma2);
6456 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6457 space2, isl_dim_in);
6458 if (match < 0 || !match) {
6459 isl_pw_multi_aff_free(pma2);
6460 return match < 0 ? isl_stat_error : isl_stat_ok;
6463 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6465 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6467 return isl_stat_ok;
6470 /* If data->pma and "pma2" have the same domain space, then compute
6471 * their flat range product and add the result to data->res.
6473 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6474 void *user)
6476 return gen_range_product_entry(pma2,
6477 &isl_pw_multi_aff_flat_range_product, user);
6480 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6481 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6483 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6484 __isl_take isl_union_pw_multi_aff *upma1,
6485 __isl_take isl_union_pw_multi_aff *upma2)
6487 return bin_op(upma1, upma2, &flat_range_product_entry);
6490 /* If data->pma and "pma2" have the same domain space, then compute
6491 * their range product and add the result to data->res.
6493 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6494 void *user)
6496 return gen_range_product_entry(pma2,
6497 &isl_pw_multi_aff_range_product, user);
6500 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6501 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6503 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6504 __isl_take isl_union_pw_multi_aff *upma1,
6505 __isl_take isl_union_pw_multi_aff *upma2)
6507 return bin_op(upma1, upma2, &range_product_entry);
6510 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6511 * The parameters are assumed to have been aligned.
6513 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6514 * except that it works on two different isl_pw_* types.
6516 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6517 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6518 __isl_take isl_pw_aff *pa)
6520 int i, j, n;
6521 isl_pw_multi_aff *res = NULL;
6523 if (!pma || !pa)
6524 goto error;
6526 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6527 pa->dim, isl_dim_in))
6528 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6529 "domains don't match", goto error);
6530 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6531 goto error;
6533 n = pma->n * pa->n;
6534 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6536 for (i = 0; i < pma->n; ++i) {
6537 for (j = 0; j < pa->n; ++j) {
6538 isl_set *common;
6539 isl_multi_aff *res_ij;
6540 int empty;
6542 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6543 isl_set_copy(pa->p[j].set));
6544 empty = isl_set_plain_is_empty(common);
6545 if (empty < 0 || empty) {
6546 isl_set_free(common);
6547 if (empty < 0)
6548 goto error;
6549 continue;
6552 res_ij = isl_multi_aff_set_aff(
6553 isl_multi_aff_copy(pma->p[i].maff), pos,
6554 isl_aff_copy(pa->p[j].aff));
6555 res_ij = isl_multi_aff_gist(res_ij,
6556 isl_set_copy(common));
6558 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6562 isl_pw_multi_aff_free(pma);
6563 isl_pw_aff_free(pa);
6564 return res;
6565 error:
6566 isl_pw_multi_aff_free(pma);
6567 isl_pw_aff_free(pa);
6568 return isl_pw_multi_aff_free(res);
6571 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6573 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6574 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6575 __isl_take isl_pw_aff *pa)
6577 isl_bool equal_params;
6579 if (!pma || !pa)
6580 goto error;
6581 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6582 if (equal_params < 0)
6583 goto error;
6584 if (equal_params)
6585 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6586 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6587 isl_pw_aff_check_named_params(pa) < 0)
6588 goto error;
6589 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6590 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6591 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6592 error:
6593 isl_pw_multi_aff_free(pma);
6594 isl_pw_aff_free(pa);
6595 return NULL;
6598 /* Do the parameters of "pa" match those of "space"?
6600 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6601 __isl_keep isl_space *space)
6603 isl_space *pa_space;
6604 isl_bool match;
6606 if (!pa || !space)
6607 return isl_bool_error;
6609 pa_space = isl_pw_aff_get_space(pa);
6611 match = isl_space_has_equal_params(space, pa_space);
6613 isl_space_free(pa_space);
6614 return match;
6617 /* Check that the domain space of "pa" matches "space".
6619 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6620 __isl_keep isl_space *space)
6622 isl_space *pa_space;
6623 isl_bool match;
6625 if (!pa || !space)
6626 return isl_stat_error;
6628 pa_space = isl_pw_aff_get_space(pa);
6630 match = isl_space_has_equal_params(space, pa_space);
6631 if (match < 0)
6632 goto error;
6633 if (!match)
6634 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6635 "parameters don't match", goto error);
6636 match = isl_space_tuple_is_equal(space, isl_dim_in,
6637 pa_space, isl_dim_in);
6638 if (match < 0)
6639 goto error;
6640 if (!match)
6641 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6642 "domains don't match", goto error);
6643 isl_space_free(pa_space);
6644 return isl_stat_ok;
6645 error:
6646 isl_space_free(pa_space);
6647 return isl_stat_error;
6650 #undef BASE
6651 #define BASE pw_aff
6652 #undef DOMBASE
6653 #define DOMBASE set
6655 #include <isl_multi_explicit_domain.c>
6656 #include <isl_multi_pw_aff_explicit_domain.c>
6657 #include <isl_multi_templ.c>
6658 #include <isl_multi_un_op_templ.c>
6659 #include <isl_multi_bin_val_templ.c>
6660 #include <isl_multi_add_constant_templ.c>
6661 #include <isl_multi_align_set.c>
6662 #include <isl_multi_apply_set_explicit_domain_templ.c>
6663 #include <isl_multi_arith_templ.c>
6664 #include <isl_multi_bind_templ.c>
6665 #include <isl_multi_bind_domain_templ.c>
6666 #include <isl_multi_coalesce.c>
6667 #include <isl_multi_domain_templ.c>
6668 #include <isl_multi_dim_id_templ.c>
6669 #include <isl_multi_dims.c>
6670 #include <isl_multi_from_base_templ.c>
6671 #include <isl_multi_check_domain_templ.c>
6672 #include <isl_multi_gist.c>
6673 #include <isl_multi_hash.c>
6674 #include <isl_multi_identity_templ.c>
6675 #include <isl_multi_insert_domain_templ.c>
6676 #include <isl_multi_intersect.c>
6677 #include <isl_multi_min_max_templ.c>
6678 #include <isl_multi_move_dims_templ.c>
6679 #include <isl_multi_nan_templ.c>
6680 #include <isl_multi_param_templ.c>
6681 #include <isl_multi_product_templ.c>
6682 #include <isl_multi_splice_templ.c>
6683 #include <isl_multi_tuple_id_templ.c>
6684 #include <isl_multi_union_add_templ.c>
6685 #include <isl_multi_zero_templ.c>
6686 #include <isl_multi_unbind_params_templ.c>
6688 /* Is every element of "mpa" defined over a single universe domain?
6690 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6692 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6695 /* Given that every element of "mpa" is defined over a single universe domain,
6696 * return the corresponding base expressions.
6698 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6699 __isl_take isl_multi_pw_aff *mpa)
6701 int i;
6702 isl_size n;
6703 isl_multi_aff *ma;
6705 n = isl_multi_pw_aff_size(mpa);
6706 if (n < 0)
6707 mpa = isl_multi_pw_aff_free(mpa);
6708 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6709 for (i = 0; i < n; ++i) {
6710 isl_aff *aff;
6712 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6713 ma = isl_multi_aff_set_aff(ma, i, aff);
6715 isl_multi_pw_aff_free(mpa);
6716 return ma;
6719 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6720 * with this explicit domain.
6722 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6723 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6725 isl_set *dom;
6727 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6728 return map;
6730 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6731 map = isl_map_intersect_domain(map, dom);
6733 return map;
6736 /* Are all elements of "mpa" piecewise constants?
6738 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6740 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6743 /* Does "mpa" have a non-trivial explicit domain?
6745 * The explicit domain, if present, is trivial if it represents
6746 * an (obviously) universe set.
6748 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6749 __isl_keep isl_multi_pw_aff *mpa)
6751 if (!mpa)
6752 return isl_bool_error;
6753 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6754 return isl_bool_false;
6755 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6758 #undef BASE
6759 #define BASE set
6761 #include "isl_opt_mpa_templ.c"
6763 /* Compute the minima of the set dimensions as a function of the
6764 * parameters, but independently of the other set dimensions.
6766 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6768 return set_opt_mpa(set, &isl_set_dim_min);
6771 /* Compute the maxima of the set dimensions as a function of the
6772 * parameters, but independently of the other set dimensions.
6774 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6776 return set_opt_mpa(set, &isl_set_dim_max);
6779 #undef BASE
6780 #define BASE map
6782 #include "isl_opt_mpa_templ.c"
6784 /* Compute the minima of the output dimensions as a function of the
6785 * parameters and input dimensions, but independently of
6786 * the other output dimensions.
6788 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6790 return map_opt_mpa(map, &isl_map_dim_min);
6793 /* Compute the maxima of the output dimensions as a function of the
6794 * parameters and input dimensions, but independently of
6795 * the other output dimensions.
6797 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6799 return map_opt_mpa(map, &isl_map_dim_max);
6802 #undef TYPE
6803 #define TYPE isl_pw_multi_aff
6804 #include "isl_type_check_match_range_multi_val.c"
6806 /* Apply "fn" to the base expressions of "pma" and "mv".
6808 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_op_multi_val(
6809 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv,
6810 __isl_give isl_multi_aff *(*fn)(__isl_take isl_multi_aff *ma,
6811 __isl_take isl_multi_val *mv))
6813 int i;
6814 isl_size n;
6816 if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
6817 goto error;
6819 n = isl_pw_multi_aff_n_piece(pma);
6820 if (n < 0)
6821 goto error;
6823 for (i = 0; i < n; ++i) {
6824 isl_multi_aff *ma;
6826 ma = isl_pw_multi_aff_take_base_at(pma, i);
6827 ma = fn(ma, isl_multi_val_copy(mv));
6828 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
6831 isl_multi_val_free(mv);
6832 return pma;
6833 error:
6834 isl_multi_val_free(mv);
6835 isl_pw_multi_aff_free(pma);
6836 return NULL;
6839 /* Scale the elements of "pma" by the corresponding elements of "mv".
6841 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6842 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6844 return isl_pw_multi_aff_op_multi_val(pma, mv,
6845 &isl_multi_aff_scale_multi_val);
6848 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6850 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
6851 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6853 return isl_pw_multi_aff_op_multi_val(pma, mv,
6854 &isl_multi_aff_scale_down_multi_val);
6857 /* This function is called for each entry of an isl_union_pw_multi_aff.
6858 * If the space of the entry matches that of data->mv,
6859 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6860 * Otherwise, return an empty isl_pw_multi_aff.
6862 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6863 __isl_take isl_pw_multi_aff *pma, void *user)
6865 isl_bool equal;
6866 isl_multi_val *mv = user;
6868 equal = isl_pw_multi_aff_match_range_multi_val(pma, mv);
6869 if (equal < 0)
6870 return isl_pw_multi_aff_free(pma);
6871 if (!equal) {
6872 isl_space *space = isl_pw_multi_aff_get_space(pma);
6873 isl_pw_multi_aff_free(pma);
6874 return isl_pw_multi_aff_empty(space);
6877 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6880 /* Scale the elements of "upma" by the corresponding elements of "mv",
6881 * for those entries that match the space of "mv".
6883 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6884 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6886 struct isl_union_pw_multi_aff_transform_control control = {
6887 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6888 .fn_user = mv,
6891 upma = isl_union_pw_multi_aff_align_params(upma,
6892 isl_multi_val_get_space(mv));
6893 mv = isl_multi_val_align_params(mv,
6894 isl_union_pw_multi_aff_get_space(upma));
6895 if (!upma || !mv)
6896 goto error;
6898 return isl_union_pw_multi_aff_transform(upma, &control);
6900 isl_multi_val_free(mv);
6901 return upma;
6902 error:
6903 isl_multi_val_free(mv);
6904 isl_union_pw_multi_aff_free(upma);
6905 return NULL;
6908 /* Construct and return a piecewise multi affine expression
6909 * in the given space with value zero in each of the output dimensions and
6910 * a universe domain.
6912 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6914 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6917 /* Construct and return a piecewise multi affine expression
6918 * that is equal to the given piecewise affine expression.
6920 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6921 __isl_take isl_pw_aff *pa)
6923 int i;
6924 isl_space *space;
6925 isl_pw_multi_aff *pma;
6927 if (!pa)
6928 return NULL;
6930 space = isl_pw_aff_get_space(pa);
6931 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6933 for (i = 0; i < pa->n; ++i) {
6934 isl_set *set;
6935 isl_multi_aff *ma;
6937 set = isl_set_copy(pa->p[i].set);
6938 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6939 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6942 isl_pw_aff_free(pa);
6943 return pma;
6946 /* Construct and return a piecewise multi affine expression
6947 * that is equal to the given multi piecewise affine expression
6948 * on the shared domain of the piecewise affine expressions,
6949 * in the special case of a 0D multi piecewise affine expression.
6951 * Create a piecewise multi affine expression with the explicit domain of
6952 * the 0D multi piecewise affine expression as domain.
6954 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6955 __isl_take isl_multi_pw_aff *mpa)
6957 isl_space *space;
6958 isl_set *dom;
6959 isl_multi_aff *ma;
6961 space = isl_multi_pw_aff_get_space(mpa);
6962 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6963 isl_multi_pw_aff_free(mpa);
6965 ma = isl_multi_aff_zero(space);
6966 return isl_pw_multi_aff_alloc(dom, ma);
6969 /* Construct and return a piecewise multi affine expression
6970 * that is equal to the given multi piecewise affine expression
6971 * on the shared domain of the piecewise affine expressions.
6973 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6974 __isl_take isl_multi_pw_aff *mpa)
6976 int i;
6977 isl_space *space;
6978 isl_pw_aff *pa;
6979 isl_pw_multi_aff *pma;
6981 if (!mpa)
6982 return NULL;
6984 if (mpa->n == 0)
6985 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6987 space = isl_multi_pw_aff_get_space(mpa);
6988 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6989 pma = isl_pw_multi_aff_from_pw_aff(pa);
6991 for (i = 1; i < mpa->n; ++i) {
6992 isl_pw_multi_aff *pma_i;
6994 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6995 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6996 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6999 pma = isl_pw_multi_aff_reset_space(pma, space);
7001 isl_multi_pw_aff_free(mpa);
7002 return pma;
7005 /* Convenience function that constructs an isl_multi_pw_aff
7006 * directly from an isl_aff.
7008 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
7010 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
7013 /* Construct and return a multi piecewise affine expression
7014 * that is equal to the given multi affine expression.
7016 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
7017 __isl_take isl_multi_aff *ma)
7019 int i;
7020 isl_size n;
7021 isl_multi_pw_aff *mpa;
7023 n = isl_multi_aff_dim(ma, isl_dim_out);
7024 if (n < 0)
7025 ma = isl_multi_aff_free(ma);
7026 if (!ma)
7027 return NULL;
7029 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
7031 for (i = 0; i < n; ++i) {
7032 isl_pw_aff *pa;
7034 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
7035 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
7038 isl_multi_aff_free(ma);
7039 return mpa;
7042 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
7043 * but is considered as a function on an isl_multi_aff when exported.
7045 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
7046 __isl_take isl_multi_aff *ma)
7048 return isl_multi_pw_aff_from_multi_aff(ma);
7051 /* Construct and return a multi piecewise affine expression
7052 * that is equal to the given piecewise multi affine expression.
7054 * If the resulting multi piecewise affine expression has
7055 * an explicit domain, then assign it the domain of the input.
7056 * In other cases, the domain is stored in the individual elements.
7058 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
7059 __isl_take isl_pw_multi_aff *pma)
7061 int i;
7062 isl_size n;
7063 isl_space *space;
7064 isl_multi_pw_aff *mpa;
7066 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
7067 if (n < 0)
7068 pma = isl_pw_multi_aff_free(pma);
7069 space = isl_pw_multi_aff_get_space(pma);
7070 mpa = isl_multi_pw_aff_alloc(space);
7072 for (i = 0; i < n; ++i) {
7073 isl_pw_aff *pa;
7075 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
7076 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
7078 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7079 isl_set *dom;
7081 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
7082 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
7085 isl_pw_multi_aff_free(pma);
7086 return mpa;
7089 /* This function performs the same operation as
7090 * isl_multi_pw_aff_from_pw_multi_aff,
7091 * but is considered as a function on an isl_pw_multi_aff when exported.
7093 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
7094 __isl_take isl_pw_multi_aff *pma)
7096 return isl_multi_pw_aff_from_pw_multi_aff(pma);
7099 /* Do "pa1" and "pa2" represent the same function?
7101 * We first check if they are obviously equal.
7102 * If not, we convert them to maps and check if those are equal.
7104 * If "pa1" or "pa2" contain any NaNs, then they are considered
7105 * not to be the same. A NaN is not equal to anything, not even
7106 * to another NaN.
7108 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7109 __isl_keep isl_pw_aff *pa2)
7111 isl_bool equal;
7112 isl_bool has_nan;
7113 isl_map *map1, *map2;
7115 if (!pa1 || !pa2)
7116 return isl_bool_error;
7118 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7119 if (equal < 0 || equal)
7120 return equal;
7121 has_nan = either_involves_nan(pa1, pa2);
7122 if (has_nan < 0)
7123 return isl_bool_error;
7124 if (has_nan)
7125 return isl_bool_false;
7127 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7128 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7129 equal = isl_map_is_equal(map1, map2);
7130 isl_map_free(map1);
7131 isl_map_free(map2);
7133 return equal;
7136 /* Do "mpa1" and "mpa2" represent the same function?
7138 * Note that we cannot convert the entire isl_multi_pw_aff
7139 * to a map because the domains of the piecewise affine expressions
7140 * may not be the same.
7142 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7143 __isl_keep isl_multi_pw_aff *mpa2)
7145 int i;
7146 isl_bool equal, equal_params;
7148 if (!mpa1 || !mpa2)
7149 return isl_bool_error;
7151 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7152 if (equal_params < 0)
7153 return isl_bool_error;
7154 if (!equal_params) {
7155 if (!isl_space_has_named_params(mpa1->space))
7156 return isl_bool_false;
7157 if (!isl_space_has_named_params(mpa2->space))
7158 return isl_bool_false;
7159 mpa1 = isl_multi_pw_aff_copy(mpa1);
7160 mpa2 = isl_multi_pw_aff_copy(mpa2);
7161 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7162 isl_multi_pw_aff_get_space(mpa2));
7163 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7164 isl_multi_pw_aff_get_space(mpa1));
7165 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7166 isl_multi_pw_aff_free(mpa1);
7167 isl_multi_pw_aff_free(mpa2);
7168 return equal;
7171 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7172 if (equal < 0 || !equal)
7173 return equal;
7175 for (i = 0; i < mpa1->n; ++i) {
7176 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7177 if (equal < 0 || !equal)
7178 return equal;
7181 return isl_bool_true;
7184 /* Do "pma1" and "pma2" represent the same function?
7186 * First check if they are obviously equal.
7187 * If not, then convert them to maps and check if those are equal.
7189 * If "pa1" or "pa2" contain any NaNs, then they are considered
7190 * not to be the same. A NaN is not equal to anything, not even
7191 * to another NaN.
7193 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7194 __isl_keep isl_pw_multi_aff *pma2)
7196 isl_bool equal;
7197 isl_bool has_nan;
7198 isl_map *map1, *map2;
7200 if (!pma1 || !pma2)
7201 return isl_bool_error;
7203 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7204 if (equal < 0 || equal)
7205 return equal;
7206 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7207 if (has_nan >= 0 && !has_nan)
7208 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7209 if (has_nan < 0 || has_nan)
7210 return isl_bool_not(has_nan);
7212 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7213 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7214 equal = isl_map_is_equal(map1, map2);
7215 isl_map_free(map1);
7216 isl_map_free(map2);
7218 return equal;
7221 #undef BASE
7222 #define BASE multi_aff
7224 #include "isl_multi_pw_aff_pullback_templ.c"
7226 #undef BASE
7227 #define BASE pw_multi_aff
7229 #include "isl_multi_pw_aff_pullback_templ.c"
7231 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7232 * with the domain of "aff". The domain of the result is the same
7233 * as that of "mpa".
7234 * "mpa" and "aff" are assumed to have been aligned.
7236 * We first extract the parametric constant from "aff", defined
7237 * over the correct domain.
7238 * Then we add the appropriate combinations of the members of "mpa".
7239 * Finally, we add the integer divisions through recursive calls.
7241 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7242 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7244 int i;
7245 isl_size n_in, n_div, n_mpa_in;
7246 isl_space *space;
7247 isl_val *v;
7248 isl_pw_aff *pa;
7249 isl_aff *tmp;
7251 n_in = isl_aff_dim(aff, isl_dim_in);
7252 n_div = isl_aff_dim(aff, isl_dim_div);
7253 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7254 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7255 goto error;
7257 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7258 tmp = isl_aff_copy(aff);
7259 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7260 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7261 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7262 tmp = isl_aff_reset_domain_space(tmp, space);
7263 pa = isl_pw_aff_from_aff(tmp);
7265 for (i = 0; i < n_in; ++i) {
7266 isl_pw_aff *pa_i;
7268 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7269 continue;
7270 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7271 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7272 pa_i = isl_pw_aff_scale_val(pa_i, v);
7273 pa = isl_pw_aff_add(pa, pa_i);
7276 for (i = 0; i < n_div; ++i) {
7277 isl_aff *div;
7278 isl_pw_aff *pa_i;
7280 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7281 continue;
7282 div = isl_aff_get_div(aff, i);
7283 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7284 isl_multi_pw_aff_copy(mpa), div);
7285 pa_i = isl_pw_aff_floor(pa_i);
7286 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7287 pa_i = isl_pw_aff_scale_val(pa_i, v);
7288 pa = isl_pw_aff_add(pa, pa_i);
7291 isl_multi_pw_aff_free(mpa);
7292 isl_aff_free(aff);
7294 return pa;
7295 error:
7296 isl_multi_pw_aff_free(mpa);
7297 isl_aff_free(aff);
7298 return NULL;
7301 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7302 * with the domain of "aff". The domain of the result is the same
7303 * as that of "mpa".
7305 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7306 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7308 isl_bool equal_params;
7310 if (!aff || !mpa)
7311 goto error;
7312 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7313 if (equal_params < 0)
7314 goto error;
7315 if (equal_params)
7316 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7318 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7319 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7321 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7322 error:
7323 isl_aff_free(aff);
7324 isl_multi_pw_aff_free(mpa);
7325 return NULL;
7328 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7329 * with the domain of "pa". The domain of the result is the same
7330 * as that of "mpa".
7331 * "mpa" and "pa" are assumed to have been aligned.
7333 * We consider each piece in turn. Note that the domains of the
7334 * pieces are assumed to be disjoint and they remain disjoint
7335 * after taking the preimage (over the same function).
7337 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7338 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7340 isl_space *space;
7341 isl_pw_aff *res;
7342 int i;
7344 if (!mpa || !pa)
7345 goto error;
7347 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7348 isl_pw_aff_get_space(pa));
7349 res = isl_pw_aff_empty(space);
7351 for (i = 0; i < pa->n; ++i) {
7352 isl_pw_aff *pa_i;
7353 isl_set *domain;
7355 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7356 isl_multi_pw_aff_copy(mpa),
7357 isl_aff_copy(pa->p[i].aff));
7358 domain = isl_set_copy(pa->p[i].set);
7359 domain = isl_set_preimage_multi_pw_aff(domain,
7360 isl_multi_pw_aff_copy(mpa));
7361 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7362 res = isl_pw_aff_add_disjoint(res, pa_i);
7365 isl_pw_aff_free(pa);
7366 isl_multi_pw_aff_free(mpa);
7367 return res;
7368 error:
7369 isl_pw_aff_free(pa);
7370 isl_multi_pw_aff_free(mpa);
7371 return NULL;
7374 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7375 * with the domain of "pa". The domain of the result is the same
7376 * as that of "mpa".
7378 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7379 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7381 isl_bool equal_params;
7383 if (!pa || !mpa)
7384 goto error;
7385 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7386 if (equal_params < 0)
7387 goto error;
7388 if (equal_params)
7389 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7391 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7392 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7394 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7395 error:
7396 isl_pw_aff_free(pa);
7397 isl_multi_pw_aff_free(mpa);
7398 return NULL;
7401 /* Compute the pullback of "pa" by the function represented by "mpa".
7402 * In other words, plug in "mpa" in "pa".
7404 * The pullback is computed by applying "pa" to "mpa".
7406 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7407 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7409 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7412 #undef BASE
7413 #define BASE multi_pw_aff
7415 #include "isl_multi_pw_aff_pullback_templ.c"
7417 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7418 * of "mpa1" and "mpa2" live in the same space, construct map space
7419 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7420 * with this map space as extract argument.
7422 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7423 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7424 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7425 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7427 int match;
7428 isl_space *space1, *space2;
7429 isl_map *res;
7431 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7432 isl_multi_pw_aff_get_space(mpa2));
7433 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7434 isl_multi_pw_aff_get_space(mpa1));
7435 if (!mpa1 || !mpa2)
7436 goto error;
7437 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7438 mpa2->space, isl_dim_out);
7439 if (match < 0)
7440 goto error;
7441 if (!match)
7442 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7443 "range spaces don't match", goto error);
7444 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7445 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7446 space1 = isl_space_map_from_domain_and_range(space1, space2);
7448 res = order(mpa1, mpa2, space1);
7449 isl_multi_pw_aff_free(mpa1);
7450 isl_multi_pw_aff_free(mpa2);
7451 return res;
7452 error:
7453 isl_multi_pw_aff_free(mpa1);
7454 isl_multi_pw_aff_free(mpa2);
7455 return NULL;
7458 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7459 * where the function values are equal. "space" is the space of the result.
7460 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7462 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7463 * in the sequences are equal.
7465 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7466 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7467 __isl_take isl_space *space)
7469 int i;
7470 isl_size n;
7471 isl_map *res;
7473 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7474 if (n < 0)
7475 space = isl_space_free(space);
7476 res = isl_map_universe(space);
7478 for (i = 0; i < n; ++i) {
7479 isl_pw_aff *pa1, *pa2;
7480 isl_map *map;
7482 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7483 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7484 map = isl_pw_aff_eq_map(pa1, pa2);
7485 res = isl_map_intersect(res, map);
7488 return res;
7491 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7492 * where the function values are equal.
7494 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7495 __isl_take isl_multi_pw_aff *mpa2)
7497 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7498 &isl_multi_pw_aff_eq_map_on_space);
7501 /* Intersect "map" with the result of applying "order"
7502 * on two copies of "mpa".
7504 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7505 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7506 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7507 __isl_take isl_multi_pw_aff *mpa2))
7509 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7512 /* Return the subset of "map" where the domain and the range
7513 * have equal "mpa" values.
7515 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7516 __isl_take isl_multi_pw_aff *mpa)
7518 return isl_map_order_at_multi_pw_aff(map, mpa,
7519 &isl_multi_pw_aff_eq_map);
7522 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7523 * where the function values of "mpa1" lexicographically satisfies
7524 * "strict_base"/"base" compared to that of "mpa2".
7525 * "space" is the space of the result.
7526 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7528 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7529 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7530 * when compared to the i-th element of "mpa2" while all previous elements are
7531 * pairwise equal.
7532 * In particular, if i corresponds to the final elements
7533 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7534 * for other values of i.
7535 * If "base" is a strict order, then "base" and "strict_base" are the same.
7537 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7538 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7539 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7540 __isl_take isl_pw_aff *pa2),
7541 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7542 __isl_take isl_pw_aff *pa2),
7543 __isl_take isl_space *space)
7545 int i;
7546 isl_size n;
7547 isl_map *res, *rest;
7549 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7550 if (n < 0)
7551 space = isl_space_free(space);
7552 res = isl_map_empty(isl_space_copy(space));
7553 rest = isl_map_universe(space);
7555 for (i = 0; i < n; ++i) {
7556 int last;
7557 isl_pw_aff *pa1, *pa2;
7558 isl_map *map;
7560 last = i == n - 1;
7562 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7563 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7564 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7565 map = isl_map_intersect(map, isl_map_copy(rest));
7566 res = isl_map_union(res, map);
7568 if (last)
7569 continue;
7571 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7572 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7573 map = isl_pw_aff_eq_map(pa1, pa2);
7574 rest = isl_map_intersect(rest, map);
7577 isl_map_free(rest);
7578 return res;
7581 #undef ORDER
7582 #define ORDER le
7583 #undef STRICT_ORDER
7584 #define STRICT_ORDER lt
7585 #include "isl_aff_lex_templ.c"
7587 #undef ORDER
7588 #define ORDER lt
7589 #undef STRICT_ORDER
7590 #define STRICT_ORDER lt
7591 #include "isl_aff_lex_templ.c"
7593 #undef ORDER
7594 #define ORDER ge
7595 #undef STRICT_ORDER
7596 #define STRICT_ORDER gt
7597 #include "isl_aff_lex_templ.c"
7599 #undef ORDER
7600 #define ORDER gt
7601 #undef STRICT_ORDER
7602 #define STRICT_ORDER gt
7603 #include "isl_aff_lex_templ.c"
7605 /* Compare two isl_affs.
7607 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7608 * than "aff2" and 0 if they are equal.
7610 * The order is fairly arbitrary. We do consider expressions that only involve
7611 * earlier dimensions as "smaller".
7613 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7615 int cmp;
7616 int last1, last2;
7618 if (aff1 == aff2)
7619 return 0;
7621 if (!aff1)
7622 return -1;
7623 if (!aff2)
7624 return 1;
7626 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7627 if (cmp != 0)
7628 return cmp;
7630 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7631 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7632 if (last1 != last2)
7633 return last1 - last2;
7635 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7638 /* Compare two isl_pw_affs.
7640 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7641 * than "pa2" and 0 if they are equal.
7643 * The order is fairly arbitrary. We do consider expressions that only involve
7644 * earlier dimensions as "smaller".
7646 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7647 __isl_keep isl_pw_aff *pa2)
7649 int i;
7650 int cmp;
7652 if (pa1 == pa2)
7653 return 0;
7655 if (!pa1)
7656 return -1;
7657 if (!pa2)
7658 return 1;
7660 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7661 if (cmp != 0)
7662 return cmp;
7664 if (pa1->n != pa2->n)
7665 return pa1->n - pa2->n;
7667 for (i = 0; i < pa1->n; ++i) {
7668 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7669 if (cmp != 0)
7670 return cmp;
7671 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7672 if (cmp != 0)
7673 return cmp;
7676 return 0;
7679 /* Return a piecewise affine expression that is equal to "v" on "domain".
7681 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7682 __isl_take isl_val *v)
7684 isl_space *space;
7685 isl_local_space *ls;
7686 isl_aff *aff;
7688 space = isl_set_get_space(domain);
7689 ls = isl_local_space_from_space(space);
7690 aff = isl_aff_val_on_domain(ls, v);
7692 return isl_pw_aff_alloc(domain, aff);
7695 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7696 * but is considered as a function on an isl_set when exported.
7698 __isl_give isl_pw_aff *isl_set_pw_aff_on_domain_val(__isl_take isl_set *domain,
7699 __isl_take isl_val *v)
7701 return isl_pw_aff_val_on_domain(domain, v);
7704 /* Return a piecewise affine expression that is equal to the parameter
7705 * with identifier "id" on "domain".
7707 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7708 __isl_take isl_set *domain, __isl_take isl_id *id)
7710 isl_space *space;
7711 isl_aff *aff;
7713 space = isl_set_get_space(domain);
7714 space = isl_space_add_param_id(space, isl_id_copy(id));
7715 domain = isl_set_align_params(domain, isl_space_copy(space));
7716 aff = isl_aff_param_on_domain_space_id(space, id);
7718 return isl_pw_aff_alloc(domain, aff);
7721 /* This function performs the same operation as
7722 * isl_pw_aff_param_on_domain_id,
7723 * but is considered as a function on an isl_set when exported.
7725 __isl_give isl_pw_aff *isl_set_param_pw_aff_on_domain_id(
7726 __isl_take isl_set *domain, __isl_take isl_id *id)
7728 return isl_pw_aff_param_on_domain_id(domain, id);
7731 /* Return a multi affine expression that is equal to "mv" on domain
7732 * space "space".
7734 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7735 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7737 int i;
7738 isl_size n;
7739 isl_space *space2;
7740 isl_local_space *ls;
7741 isl_multi_aff *ma;
7743 n = isl_multi_val_dim(mv, isl_dim_set);
7744 if (!space || n < 0)
7745 goto error;
7747 space2 = isl_multi_val_get_space(mv);
7748 space2 = isl_space_align_params(space2, isl_space_copy(space));
7749 space = isl_space_align_params(space, isl_space_copy(space2));
7750 space = isl_space_map_from_domain_and_range(space, space2);
7751 ma = isl_multi_aff_alloc(isl_space_copy(space));
7752 ls = isl_local_space_from_space(isl_space_domain(space));
7753 for (i = 0; i < n; ++i) {
7754 isl_val *v;
7755 isl_aff *aff;
7757 v = isl_multi_val_get_val(mv, i);
7758 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7759 ma = isl_multi_aff_set_aff(ma, i, aff);
7761 isl_local_space_free(ls);
7763 isl_multi_val_free(mv);
7764 return ma;
7765 error:
7766 isl_space_free(space);
7767 isl_multi_val_free(mv);
7768 return NULL;
7771 /* This is an alternative name for the function above.
7773 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7774 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7776 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7779 /* This function performs the same operation as
7780 * isl_multi_aff_multi_val_on_domain_space,
7781 * but is considered as a function on an isl_space when exported.
7783 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7784 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7786 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7789 /* Return a piecewise multi-affine expression
7790 * that is equal to "mv" on "domain".
7792 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7793 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7795 isl_space *space;
7796 isl_multi_aff *ma;
7798 space = isl_set_get_space(domain);
7799 ma = isl_multi_aff_multi_val_on_space(space, mv);
7801 return isl_pw_multi_aff_alloc(domain, ma);
7804 /* This function performs the same operation as
7805 * isl_pw_multi_aff_multi_val_on_domain,
7806 * but is considered as a function on an isl_set when exported.
7808 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7809 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7811 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7814 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7815 * mv is the value that should be attained on each domain set
7816 * res collects the results
7818 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7819 isl_multi_val *mv;
7820 isl_union_pw_multi_aff *res;
7823 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7824 * and add it to data->res.
7826 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7827 void *user)
7829 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7830 isl_pw_multi_aff *pma;
7831 isl_multi_val *mv;
7833 mv = isl_multi_val_copy(data->mv);
7834 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7835 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7837 return data->res ? isl_stat_ok : isl_stat_error;
7840 /* Return a union piecewise multi-affine expression
7841 * that is equal to "mv" on "domain".
7843 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7844 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7846 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7847 isl_space *space;
7849 space = isl_union_set_get_space(domain);
7850 data.res = isl_union_pw_multi_aff_empty(space);
7851 data.mv = mv;
7852 if (isl_union_set_foreach_set(domain,
7853 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7854 data.res = isl_union_pw_multi_aff_free(data.res);
7855 isl_union_set_free(domain);
7856 isl_multi_val_free(mv);
7857 return data.res;
7860 /* Compute the pullback of data->pma by the function represented by "pma2",
7861 * provided the spaces match, and add the results to data->res.
7863 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7865 struct isl_union_pw_multi_aff_bin_data *data = user;
7867 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7868 pma2->dim, isl_dim_out)) {
7869 isl_pw_multi_aff_free(pma2);
7870 return isl_stat_ok;
7873 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7874 isl_pw_multi_aff_copy(data->pma), pma2);
7876 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7877 if (!data->res)
7878 return isl_stat_error;
7880 return isl_stat_ok;
7883 /* Compute the pullback of "upma1" by the function represented by "upma2".
7885 __isl_give isl_union_pw_multi_aff *
7886 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7887 __isl_take isl_union_pw_multi_aff *upma1,
7888 __isl_take isl_union_pw_multi_aff *upma2)
7890 return bin_op(upma1, upma2, &pullback_entry);
7893 /* Apply "upma2" to "upma1".
7895 * That is, compute the pullback of "upma2" by "upma1".
7897 __isl_give isl_union_pw_multi_aff *
7898 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7899 __isl_take isl_union_pw_multi_aff *upma1,
7900 __isl_take isl_union_pw_multi_aff *upma2)
7902 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7905 #undef TYPE
7906 #define TYPE isl_pw_multi_aff
7907 static
7908 #include "isl_copy_tuple_id_templ.c"
7910 /* Given a function "pma1" of the form A[B -> C] -> D and
7911 * a function "pma2" of the form E -> B,
7912 * replace the domain of the wrapped relation inside the domain of "pma1"
7913 * by the preimage with respect to "pma2".
7914 * In other words, plug in "pma2" in this nested domain.
7915 * The result is of the form A[E -> C] -> D.
7917 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7918 * plug that into "pma1".
7920 __isl_give isl_pw_multi_aff *
7921 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7922 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7924 isl_space *pma1_space, *pma2_space;
7925 isl_space *space;
7926 isl_pw_multi_aff *id;
7928 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7929 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7931 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7932 goto error;
7933 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
7934 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
7935 goto error;
7937 space = isl_space_domain(isl_space_copy(pma1_space));
7938 space = isl_space_range(isl_space_unwrap(space));
7939 id = isl_pw_multi_aff_identity_on_domain_space(space);
7940 pma2 = isl_pw_multi_aff_product(pma2, id);
7942 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
7943 pma1_space, isl_dim_in);
7944 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
7945 pma1_space, isl_dim_in);
7947 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
7948 error:
7949 isl_pw_multi_aff_free(pma1);
7950 isl_pw_multi_aff_free(pma2);
7951 return NULL;
7954 /* If data->pma and "pma2" are such that
7955 * data->pma is of the form A[B -> C] -> D and
7956 * "pma2" is of the form E -> B,
7957 * then replace the domain of the wrapped relation
7958 * inside the domain of data->pma by the preimage with respect to "pma2" and
7959 * add the result to data->res.
7961 static isl_stat preimage_domain_wrapped_domain_entry(
7962 __isl_take isl_pw_multi_aff *pma2, void *user)
7964 struct isl_union_pw_multi_aff_bin_data *data = user;
7965 isl_space *pma1_space, *pma2_space;
7966 isl_bool match;
7968 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
7969 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7971 match = isl_space_domain_is_wrapping(pma1_space);
7972 if (match >= 0 && match)
7973 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
7974 isl_dim_in, pma2_space, isl_dim_out);
7975 if (match < 0 || !match) {
7976 isl_pw_multi_aff_free(pma2);
7977 return match < 0 ? isl_stat_error : isl_stat_ok;
7980 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7981 isl_pw_multi_aff_copy(data->pma), pma2);
7983 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7985 return isl_stat_non_null(data->res);
7988 /* For each pair of functions A[B -> C] -> D in "upma1" and
7989 * E -> B in "upma2",
7990 * replace the domain of the wrapped relation inside the domain of the first
7991 * by the preimage with respect to the second and collect the results.
7992 * In other words, plug in the second function in this nested domain.
7993 * The results are of the form A[E -> C] -> D.
7995 __isl_give isl_union_pw_multi_aff *
7996 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
7997 __isl_take isl_union_pw_multi_aff *upma1,
7998 __isl_take isl_union_pw_multi_aff *upma2)
8000 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
8003 /* Check that the domain space of "upa" matches "space".
8005 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8006 * can in principle never fail since the space "space" is that
8007 * of the isl_multi_union_pw_aff and is a set space such that
8008 * there is no domain space to match.
8010 * We check the parameters and double-check that "space" is
8011 * indeed that of a set.
8013 static isl_stat isl_union_pw_aff_check_match_domain_space(
8014 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8016 isl_space *upa_space;
8017 isl_bool match;
8019 if (!upa || !space)
8020 return isl_stat_error;
8022 match = isl_space_is_set(space);
8023 if (match < 0)
8024 return isl_stat_error;
8025 if (!match)
8026 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8027 "expecting set space", return isl_stat_error);
8029 upa_space = isl_union_pw_aff_get_space(upa);
8030 match = isl_space_has_equal_params(space, upa_space);
8031 if (match < 0)
8032 goto error;
8033 if (!match)
8034 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8035 "parameters don't match", goto error);
8037 isl_space_free(upa_space);
8038 return isl_stat_ok;
8039 error:
8040 isl_space_free(upa_space);
8041 return isl_stat_error;
8044 /* Do the parameters of "upa" match those of "space"?
8046 static isl_bool isl_union_pw_aff_matching_params(
8047 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8049 isl_space *upa_space;
8050 isl_bool match;
8052 if (!upa || !space)
8053 return isl_bool_error;
8055 upa_space = isl_union_pw_aff_get_space(upa);
8057 match = isl_space_has_equal_params(space, upa_space);
8059 isl_space_free(upa_space);
8060 return match;
8063 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8064 * space represents the new parameters.
8065 * res collects the results.
8067 struct isl_union_pw_aff_reset_params_data {
8068 isl_space *space;
8069 isl_union_pw_aff *res;
8072 /* Replace the parameters of "pa" by data->space and
8073 * add the result to data->res.
8075 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8077 struct isl_union_pw_aff_reset_params_data *data = user;
8078 isl_space *space;
8080 space = isl_pw_aff_get_space(pa);
8081 space = isl_space_replace_params(space, data->space);
8082 pa = isl_pw_aff_reset_space(pa, space);
8083 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8085 return data->res ? isl_stat_ok : isl_stat_error;
8088 /* Replace the domain space of "upa" by "space".
8089 * Since a union expression does not have a (single) domain space,
8090 * "space" is necessarily a parameter space.
8092 * Since the order and the names of the parameters determine
8093 * the hash value, we need to create a new hash table.
8095 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8096 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8098 struct isl_union_pw_aff_reset_params_data data = { space };
8099 isl_bool match;
8101 match = isl_union_pw_aff_matching_params(upa, space);
8102 if (match < 0)
8103 upa = isl_union_pw_aff_free(upa);
8104 else if (match) {
8105 isl_space_free(space);
8106 return upa;
8109 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8110 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8111 data.res = isl_union_pw_aff_free(data.res);
8113 isl_union_pw_aff_free(upa);
8114 isl_space_free(space);
8115 return data.res;
8118 /* Return the floor of "pa".
8120 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8122 return isl_pw_aff_floor(pa);
8125 /* Given f, return floor(f).
8127 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8128 __isl_take isl_union_pw_aff *upa)
8130 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8133 /* Compute
8135 * upa mod m = upa - m * floor(upa/m)
8137 * with m an integer value.
8139 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8140 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8142 isl_union_pw_aff *res;
8144 if (!upa || !m)
8145 goto error;
8147 if (!isl_val_is_int(m))
8148 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8149 "expecting integer modulo", goto error);
8150 if (!isl_val_is_pos(m))
8151 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8152 "expecting positive modulo", goto error);
8154 res = isl_union_pw_aff_copy(upa);
8155 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8156 upa = isl_union_pw_aff_floor(upa);
8157 upa = isl_union_pw_aff_scale_val(upa, m);
8158 res = isl_union_pw_aff_sub(res, upa);
8160 return res;
8161 error:
8162 isl_val_free(m);
8163 isl_union_pw_aff_free(upa);
8164 return NULL;
8167 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8168 * pos is the output position that needs to be extracted.
8169 * res collects the results.
8171 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8172 int pos;
8173 isl_union_pw_aff *res;
8176 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8177 * (assuming it has such a dimension) and add it to data->res.
8179 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8181 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8182 isl_size n_out;
8183 isl_pw_aff *pa;
8185 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8186 if (n_out < 0)
8187 return isl_stat_error;
8188 if (data->pos >= n_out) {
8189 isl_pw_multi_aff_free(pma);
8190 return isl_stat_ok;
8193 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8194 isl_pw_multi_aff_free(pma);
8196 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8198 return data->res ? isl_stat_ok : isl_stat_error;
8201 /* Extract an isl_union_pw_aff corresponding to
8202 * output dimension "pos" of "upma".
8204 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8205 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8207 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8208 isl_space *space;
8210 if (!upma)
8211 return NULL;
8213 if (pos < 0)
8214 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8215 "cannot extract at negative position", return NULL);
8217 space = isl_union_pw_multi_aff_get_space(upma);
8218 data.res = isl_union_pw_aff_empty(space);
8219 data.pos = pos;
8220 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8221 &get_union_pw_aff, &data) < 0)
8222 data.res = isl_union_pw_aff_free(data.res);
8224 return data.res;
8227 /* Return a union piecewise affine expression
8228 * that is equal to "aff" on "domain".
8230 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8231 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8233 isl_pw_aff *pa;
8235 pa = isl_pw_aff_from_aff(aff);
8236 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8239 /* Return a union piecewise affine expression
8240 * that is equal to the parameter identified by "id" on "domain".
8242 * Make sure the parameter appears in the space passed to
8243 * isl_aff_param_on_domain_space_id.
8245 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8246 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8248 isl_space *space;
8249 isl_aff *aff;
8251 space = isl_union_set_get_space(domain);
8252 space = isl_space_add_param_id(space, isl_id_copy(id));
8253 aff = isl_aff_param_on_domain_space_id(space, id);
8254 return isl_union_pw_aff_aff_on_domain(domain, aff);
8257 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8258 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8259 * needs to attain.
8260 * "res" collects the results.
8262 struct isl_union_pw_aff_pw_aff_on_domain_data {
8263 isl_pw_aff *pa;
8264 isl_union_pw_aff *res;
8267 /* Construct a piecewise affine expression that is equal to data->pa
8268 * on "domain" and add the result to data->res.
8270 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8272 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8273 isl_pw_aff *pa;
8274 isl_size dim;
8276 pa = isl_pw_aff_copy(data->pa);
8277 dim = isl_set_dim(domain, isl_dim_set);
8278 if (dim < 0)
8279 pa = isl_pw_aff_free(pa);
8280 pa = isl_pw_aff_from_range(pa);
8281 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8282 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8283 pa = isl_pw_aff_intersect_domain(pa, domain);
8284 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8286 return data->res ? isl_stat_ok : isl_stat_error;
8289 /* Return a union piecewise affine expression
8290 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8291 * have been aligned.
8293 * Construct an isl_pw_aff on each of the sets in "domain" and
8294 * collect the results.
8296 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8297 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8299 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8300 isl_space *space;
8302 space = isl_union_set_get_space(domain);
8303 data.res = isl_union_pw_aff_empty(space);
8304 data.pa = pa;
8305 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8306 data.res = isl_union_pw_aff_free(data.res);
8307 isl_union_set_free(domain);
8308 isl_pw_aff_free(pa);
8309 return data.res;
8312 /* Return a union piecewise affine expression
8313 * that is equal to "pa" on "domain".
8315 * Check that "pa" is a parametric expression,
8316 * align the parameters if needed and call
8317 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8319 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8320 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8322 isl_bool is_set;
8323 isl_bool equal_params;
8324 isl_space *domain_space, *pa_space;
8326 pa_space = isl_pw_aff_peek_space(pa);
8327 is_set = isl_space_is_set(pa_space);
8328 if (is_set < 0)
8329 goto error;
8330 if (!is_set)
8331 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8332 "expecting parametric expression", goto error);
8334 domain_space = isl_union_set_get_space(domain);
8335 pa_space = isl_pw_aff_get_space(pa);
8336 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8337 if (equal_params >= 0 && !equal_params) {
8338 isl_space *space;
8340 space = isl_space_align_params(domain_space, pa_space);
8341 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8342 domain = isl_union_set_align_params(domain, space);
8343 } else {
8344 isl_space_free(domain_space);
8345 isl_space_free(pa_space);
8348 if (equal_params < 0)
8349 goto error;
8350 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8351 error:
8352 isl_union_set_free(domain);
8353 isl_pw_aff_free(pa);
8354 return NULL;
8357 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8358 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8359 * "res" collects the results.
8361 struct isl_union_pw_aff_val_on_domain_data {
8362 isl_val *v;
8363 isl_union_pw_aff *res;
8366 /* Construct a piecewise affine expression that is equal to data->v
8367 * on "domain" and add the result to data->res.
8369 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8371 struct isl_union_pw_aff_val_on_domain_data *data = user;
8372 isl_pw_aff *pa;
8373 isl_val *v;
8375 v = isl_val_copy(data->v);
8376 pa = isl_pw_aff_val_on_domain(domain, v);
8377 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8379 return data->res ? isl_stat_ok : isl_stat_error;
8382 /* Return a union piecewise affine expression
8383 * that is equal to "v" on "domain".
8385 * Construct an isl_pw_aff on each of the sets in "domain" and
8386 * collect the results.
8388 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8389 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8391 struct isl_union_pw_aff_val_on_domain_data data;
8392 isl_space *space;
8394 space = isl_union_set_get_space(domain);
8395 data.res = isl_union_pw_aff_empty(space);
8396 data.v = v;
8397 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8398 data.res = isl_union_pw_aff_free(data.res);
8399 isl_union_set_free(domain);
8400 isl_val_free(v);
8401 return data.res;
8404 /* Construct a piecewise multi affine expression
8405 * that is equal to "pa" and add it to upma.
8407 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8408 void *user)
8410 isl_union_pw_multi_aff **upma = user;
8411 isl_pw_multi_aff *pma;
8413 pma = isl_pw_multi_aff_from_pw_aff(pa);
8414 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8416 return *upma ? isl_stat_ok : isl_stat_error;
8419 /* Construct and return a union piecewise multi affine expression
8420 * that is equal to the given union piecewise affine expression.
8422 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8423 __isl_take isl_union_pw_aff *upa)
8425 isl_space *space;
8426 isl_union_pw_multi_aff *upma;
8428 if (!upa)
8429 return NULL;
8431 space = isl_union_pw_aff_get_space(upa);
8432 upma = isl_union_pw_multi_aff_empty(space);
8434 if (isl_union_pw_aff_foreach_pw_aff(upa,
8435 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8436 upma = isl_union_pw_multi_aff_free(upma);
8438 isl_union_pw_aff_free(upa);
8439 return upma;
8442 /* Compute the set of elements in the domain of "pa" where it is zero and
8443 * add this set to "uset".
8445 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8447 isl_union_set **uset = (isl_union_set **)user;
8449 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8451 return *uset ? isl_stat_ok : isl_stat_error;
8454 /* Return a union set containing those elements in the domain
8455 * of "upa" where it is zero.
8457 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8458 __isl_take isl_union_pw_aff *upa)
8460 isl_union_set *zero;
8462 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8463 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8464 zero = isl_union_set_free(zero);
8466 isl_union_pw_aff_free(upa);
8467 return zero;
8470 /* Internal data structure for isl_union_pw_aff_bind_id,
8471 * storing the parameter that needs to be bound and
8472 * the accumulated results.
8474 struct isl_bind_id_data {
8475 isl_id *id;
8476 isl_union_set *bound;
8479 /* Bind the piecewise affine function "pa" to the parameter data->id,
8480 * adding the resulting elements in the domain where the expression
8481 * is equal to the parameter to data->bound.
8483 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8485 struct isl_bind_id_data *data = user;
8486 isl_set *bound;
8488 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8489 data->bound = isl_union_set_add_set(data->bound, bound);
8491 return data->bound ? isl_stat_ok : isl_stat_error;
8494 /* Bind the union piecewise affine function "upa" to the parameter "id",
8495 * returning the elements in the domain where the expression
8496 * is equal to the parameter.
8498 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8499 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8501 struct isl_bind_id_data data = { id };
8503 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8504 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8505 data.bound = isl_union_set_free(data.bound);
8507 isl_union_pw_aff_free(upa);
8508 isl_id_free(id);
8509 return data.bound;
8512 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8513 * upma is the function that is plugged in.
8514 * pa is the current part of the function in which upma is plugged in.
8515 * res collects the results.
8517 struct isl_union_pw_aff_pullback_upma_data {
8518 isl_union_pw_multi_aff *upma;
8519 isl_pw_aff *pa;
8520 isl_union_pw_aff *res;
8523 /* Check if "pma" can be plugged into data->pa.
8524 * If so, perform the pullback and add the result to data->res.
8526 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8528 struct isl_union_pw_aff_pullback_upma_data *data = user;
8529 isl_pw_aff *pa;
8531 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8532 pma->dim, isl_dim_out)) {
8533 isl_pw_multi_aff_free(pma);
8534 return isl_stat_ok;
8537 pa = isl_pw_aff_copy(data->pa);
8538 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8540 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8542 return data->res ? isl_stat_ok : isl_stat_error;
8545 /* Check if any of the elements of data->upma can be plugged into pa,
8546 * add if so add the result to data->res.
8548 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8550 struct isl_union_pw_aff_pullback_upma_data *data = user;
8551 isl_stat r;
8553 data->pa = pa;
8554 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8555 &pa_pb_pma, data);
8556 isl_pw_aff_free(pa);
8558 return r;
8561 /* Compute the pullback of "upa" by the function represented by "upma".
8562 * In other words, plug in "upma" in "upa". The result contains
8563 * expressions defined over the domain space of "upma".
8565 * Run over all pairs of elements in "upa" and "upma", perform
8566 * the pullback when appropriate and collect the results.
8567 * If the hash value were based on the domain space rather than
8568 * the function space, then we could run through all elements
8569 * of "upma" and directly pick out the corresponding element of "upa".
8571 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8572 __isl_take isl_union_pw_aff *upa,
8573 __isl_take isl_union_pw_multi_aff *upma)
8575 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8576 isl_space *space;
8578 space = isl_union_pw_multi_aff_get_space(upma);
8579 upa = isl_union_pw_aff_align_params(upa, space);
8580 space = isl_union_pw_aff_get_space(upa);
8581 upma = isl_union_pw_multi_aff_align_params(upma, space);
8583 if (!upa || !upma)
8584 goto error;
8586 data.upma = upma;
8587 data.res = isl_union_pw_aff_alloc_same_size(upa);
8588 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8589 data.res = isl_union_pw_aff_free(data.res);
8591 isl_union_pw_aff_free(upa);
8592 isl_union_pw_multi_aff_free(upma);
8593 return data.res;
8594 error:
8595 isl_union_pw_aff_free(upa);
8596 isl_union_pw_multi_aff_free(upma);
8597 return NULL;
8600 #undef BASE
8601 #define BASE union_pw_aff
8602 #undef DOMBASE
8603 #define DOMBASE union_set
8605 #include <isl_multi_explicit_domain.c>
8606 #include <isl_multi_union_pw_aff_explicit_domain.c>
8607 #include <isl_multi_templ.c>
8608 #include <isl_multi_un_op_templ.c>
8609 #include <isl_multi_bin_val_templ.c>
8610 #include <isl_multi_align_set.c>
8611 #include <isl_multi_align_union_set.c>
8612 #include <isl_multi_apply_set_explicit_domain_templ.c>
8613 #include <isl_multi_apply_union_set_explicit_domain_templ.c>
8614 #include <isl_multi_arith_templ.c>
8615 #include <isl_multi_bind_templ.c>
8616 #include <isl_multi_coalesce.c>
8617 #include <isl_multi_dim_id_templ.c>
8618 #include <isl_multi_floor.c>
8619 #include <isl_multi_from_base_templ.c>
8620 #include <isl_multi_check_domain_templ.c>
8621 #include <isl_multi_gist.c>
8622 #include <isl_multi_intersect.c>
8623 #include <isl_multi_nan_templ.c>
8624 #include <isl_multi_tuple_id_templ.c>
8625 #include <isl_multi_union_add_templ.c>
8626 #include <isl_multi_zero_space_templ.c>
8628 /* Does "mupa" have a non-trivial explicit domain?
8630 * The explicit domain, if present, is trivial if it represents
8631 * an (obviously) universe parameter set.
8633 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8634 __isl_keep isl_multi_union_pw_aff *mupa)
8636 isl_bool is_params, trivial;
8637 isl_set *set;
8639 if (!mupa)
8640 return isl_bool_error;
8641 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8642 return isl_bool_false;
8643 is_params = isl_union_set_is_params(mupa->u.dom);
8644 if (is_params < 0 || !is_params)
8645 return isl_bool_not(is_params);
8646 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8647 trivial = isl_set_plain_is_universe(set);
8648 isl_set_free(set);
8649 return isl_bool_not(trivial);
8652 /* Construct a multiple union piecewise affine expression
8653 * in the given space with value zero in each of the output dimensions.
8655 * Since there is no canonical zero value for
8656 * a union piecewise affine expression, we can only construct
8657 * a zero-dimensional "zero" value.
8659 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8660 __isl_take isl_space *space)
8662 isl_bool params;
8663 isl_size dim;
8665 if (!space)
8666 return NULL;
8668 params = isl_space_is_params(space);
8669 if (params < 0)
8670 goto error;
8671 if (params)
8672 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8673 "expecting proper set space", goto error);
8674 if (!isl_space_is_set(space))
8675 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8676 "expecting set space", goto error);
8677 dim = isl_space_dim(space, isl_dim_out);
8678 if (dim < 0)
8679 goto error;
8680 if (dim != 0)
8681 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8682 "expecting 0D space", goto error);
8684 return isl_multi_union_pw_aff_alloc(space);
8685 error:
8686 isl_space_free(space);
8687 return NULL;
8690 /* Construct and return a multi union piecewise affine expression
8691 * that is equal to the given multi affine expression.
8693 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8694 __isl_take isl_multi_aff *ma)
8696 isl_multi_pw_aff *mpa;
8698 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8699 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8702 /* This function performs the same operation as
8703 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8704 * isl_multi_aff when exported.
8706 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8707 __isl_take isl_multi_aff *ma)
8709 return isl_multi_union_pw_aff_from_multi_aff(ma);
8712 /* Construct and return a multi union piecewise affine expression
8713 * that is equal to the given multi piecewise affine expression.
8715 * If the resulting multi union piecewise affine expression has
8716 * an explicit domain, then assign it the domain of the input.
8717 * In other cases, the domain is stored in the individual elements.
8719 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8720 __isl_take isl_multi_pw_aff *mpa)
8722 int i;
8723 isl_size n;
8724 isl_space *space;
8725 isl_multi_union_pw_aff *mupa;
8727 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8728 if (n < 0)
8729 mpa = isl_multi_pw_aff_free(mpa);
8730 if (!mpa)
8731 return NULL;
8733 space = isl_multi_pw_aff_get_space(mpa);
8734 space = isl_space_range(space);
8735 mupa = isl_multi_union_pw_aff_alloc(space);
8737 for (i = 0; i < n; ++i) {
8738 isl_pw_aff *pa;
8739 isl_union_pw_aff *upa;
8741 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8742 upa = isl_union_pw_aff_from_pw_aff(pa);
8743 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8745 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8746 isl_union_set *dom;
8747 isl_multi_pw_aff *copy;
8749 copy = isl_multi_pw_aff_copy(mpa);
8750 dom = isl_union_set_from_set(isl_multi_pw_aff_domain(copy));
8751 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8754 isl_multi_pw_aff_free(mpa);
8756 return mupa;
8759 /* Extract the range space of "pma" and assign it to *space.
8760 * If *space has already been set (through a previous call to this function),
8761 * then check that the range space is the same.
8763 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8765 isl_space **space = user;
8766 isl_space *pma_space;
8767 isl_bool equal;
8769 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8770 isl_pw_multi_aff_free(pma);
8772 if (!pma_space)
8773 return isl_stat_error;
8774 if (!*space) {
8775 *space = pma_space;
8776 return isl_stat_ok;
8779 equal = isl_space_is_equal(pma_space, *space);
8780 isl_space_free(pma_space);
8782 if (equal < 0)
8783 return isl_stat_error;
8784 if (!equal)
8785 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8786 "range spaces not the same", return isl_stat_error);
8787 return isl_stat_ok;
8790 /* Construct and return a multi union piecewise affine expression
8791 * that is equal to the given union piecewise multi affine expression.
8793 * In order to be able to perform the conversion, the input
8794 * needs to be non-empty and may only involve a single range space.
8796 * If the resulting multi union piecewise affine expression has
8797 * an explicit domain, then assign it the domain of the input.
8798 * In other cases, the domain is stored in the individual elements.
8800 __isl_give isl_multi_union_pw_aff *
8801 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8802 __isl_take isl_union_pw_multi_aff *upma)
8804 isl_space *space = NULL;
8805 isl_multi_union_pw_aff *mupa;
8806 int i;
8807 isl_size n;
8809 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8810 if (n < 0)
8811 goto error;
8812 if (n == 0)
8813 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8814 "cannot extract range space from empty input",
8815 goto error);
8816 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8817 &space) < 0)
8818 goto error;
8820 if (!space)
8821 goto error;
8823 n = isl_space_dim(space, isl_dim_set);
8824 if (n < 0)
8825 space = isl_space_free(space);
8826 mupa = isl_multi_union_pw_aff_alloc(space);
8828 for (i = 0; i < n; ++i) {
8829 isl_union_pw_aff *upa;
8831 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8832 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8834 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8835 isl_union_set *dom;
8836 isl_union_pw_multi_aff *copy;
8838 copy = isl_union_pw_multi_aff_copy(upma);
8839 dom = isl_union_pw_multi_aff_domain(copy);
8840 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8843 isl_union_pw_multi_aff_free(upma);
8844 return mupa;
8845 error:
8846 isl_space_free(space);
8847 isl_union_pw_multi_aff_free(upma);
8848 return NULL;
8851 /* This function performs the same operation as
8852 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8853 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8855 __isl_give isl_multi_union_pw_aff *
8856 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8857 __isl_take isl_union_pw_multi_aff *upma)
8859 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8862 /* Try and create an isl_multi_union_pw_aff that is equivalent
8863 * to the given isl_union_map.
8864 * The isl_union_map is required to be single-valued in each space.
8865 * Moreover, it cannot be empty and all range spaces need to be the same.
8866 * Otherwise, an error is produced.
8868 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8869 __isl_take isl_union_map *umap)
8871 isl_union_pw_multi_aff *upma;
8873 upma = isl_union_pw_multi_aff_from_union_map(umap);
8874 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8877 /* This function performs the same operation as
8878 * isl_multi_union_pw_aff_from_union_map,
8879 * but is considered as a function on an isl_union_map when exported.
8881 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8882 __isl_take isl_union_map *umap)
8884 return isl_multi_union_pw_aff_from_union_map(umap);
8887 /* Return a multiple union piecewise affine expression
8888 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8889 * have been aligned.
8891 * If the resulting multi union piecewise affine expression has
8892 * an explicit domain, then assign it the input domain.
8893 * In other cases, the domain is stored in the individual elements.
8895 static __isl_give isl_multi_union_pw_aff *
8896 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8897 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8899 int i;
8900 isl_size n;
8901 isl_space *space;
8902 isl_multi_union_pw_aff *mupa;
8904 n = isl_multi_val_dim(mv, isl_dim_set);
8905 if (!domain || n < 0)
8906 goto error;
8908 space = isl_multi_val_get_space(mv);
8909 mupa = isl_multi_union_pw_aff_alloc(space);
8910 for (i = 0; i < n; ++i) {
8911 isl_val *v;
8912 isl_union_pw_aff *upa;
8914 v = isl_multi_val_get_val(mv, i);
8915 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8917 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8919 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8920 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8921 isl_union_set_copy(domain));
8923 isl_union_set_free(domain);
8924 isl_multi_val_free(mv);
8925 return mupa;
8926 error:
8927 isl_union_set_free(domain);
8928 isl_multi_val_free(mv);
8929 return NULL;
8932 /* Return a multiple union piecewise affine expression
8933 * that is equal to "mv" on "domain".
8935 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8936 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8938 isl_bool equal_params;
8940 if (!domain || !mv)
8941 goto error;
8942 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8943 if (equal_params < 0)
8944 goto error;
8945 if (equal_params)
8946 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8947 domain, mv);
8948 domain = isl_union_set_align_params(domain,
8949 isl_multi_val_get_space(mv));
8950 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8951 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8952 error:
8953 isl_union_set_free(domain);
8954 isl_multi_val_free(mv);
8955 return NULL;
8958 /* Return a multiple union piecewise affine expression
8959 * that is equal to "ma" on "domain".
8961 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8962 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8964 isl_pw_multi_aff *pma;
8966 pma = isl_pw_multi_aff_from_multi_aff(ma);
8967 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8970 /* Return a multiple union piecewise affine expression
8971 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8972 * have been aligned.
8974 * If the resulting multi union piecewise affine expression has
8975 * an explicit domain, then assign it the input domain.
8976 * In other cases, the domain is stored in the individual elements.
8978 static __isl_give isl_multi_union_pw_aff *
8979 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8980 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8982 int i;
8983 isl_size n;
8984 isl_space *space;
8985 isl_multi_union_pw_aff *mupa;
8987 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8988 if (!domain || n < 0)
8989 goto error;
8990 space = isl_pw_multi_aff_get_space(pma);
8991 mupa = isl_multi_union_pw_aff_alloc(space);
8992 for (i = 0; i < n; ++i) {
8993 isl_pw_aff *pa;
8994 isl_union_pw_aff *upa;
8996 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8997 upa = isl_union_pw_aff_pw_aff_on_domain(
8998 isl_union_set_copy(domain), pa);
8999 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9001 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9002 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
9003 isl_union_set_copy(domain));
9005 isl_union_set_free(domain);
9006 isl_pw_multi_aff_free(pma);
9007 return mupa;
9008 error:
9009 isl_union_set_free(domain);
9010 isl_pw_multi_aff_free(pma);
9011 return NULL;
9014 /* Return a multiple union piecewise affine expression
9015 * that is equal to "pma" on "domain".
9017 __isl_give isl_multi_union_pw_aff *
9018 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
9019 __isl_take isl_pw_multi_aff *pma)
9021 isl_bool equal_params;
9022 isl_space *space;
9024 space = isl_pw_multi_aff_peek_space(pma);
9025 equal_params = isl_union_set_space_has_equal_params(domain, space);
9026 if (equal_params < 0)
9027 goto error;
9028 if (equal_params)
9029 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9030 domain, pma);
9031 domain = isl_union_set_align_params(domain,
9032 isl_pw_multi_aff_get_space(pma));
9033 pma = isl_pw_multi_aff_align_params(pma,
9034 isl_union_set_get_space(domain));
9035 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
9036 pma);
9037 error:
9038 isl_union_set_free(domain);
9039 isl_pw_multi_aff_free(pma);
9040 return NULL;
9043 /* Return a union set containing those elements in the domains
9044 * of the elements of "mupa" where they are all zero.
9046 * If there are no elements, then simply return the entire domain.
9048 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
9049 __isl_take isl_multi_union_pw_aff *mupa)
9051 int i;
9052 isl_size n;
9053 isl_union_pw_aff *upa;
9054 isl_union_set *zero;
9056 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9057 if (n < 0)
9058 mupa = isl_multi_union_pw_aff_free(mupa);
9059 if (!mupa)
9060 return NULL;
9062 if (n == 0)
9063 return isl_multi_union_pw_aff_domain(mupa);
9065 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9066 zero = isl_union_pw_aff_zero_union_set(upa);
9068 for (i = 1; i < n; ++i) {
9069 isl_union_set *zero_i;
9071 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9072 zero_i = isl_union_pw_aff_zero_union_set(upa);
9074 zero = isl_union_set_intersect(zero, zero_i);
9077 isl_multi_union_pw_aff_free(mupa);
9078 return zero;
9081 /* Construct a union map mapping the shared domain
9082 * of the union piecewise affine expressions to the range of "mupa"
9083 * in the special case of a 0D multi union piecewise affine expression.
9085 * Construct a map between the explicit domain of "mupa" and
9086 * the range space.
9087 * Note that this assumes that the domain consists of explicit elements.
9089 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9090 __isl_take isl_multi_union_pw_aff *mupa)
9092 isl_bool is_params;
9093 isl_space *space;
9094 isl_union_set *dom, *ran;
9096 space = isl_multi_union_pw_aff_get_space(mupa);
9097 dom = isl_multi_union_pw_aff_domain(mupa);
9098 ran = isl_union_set_from_set(isl_set_universe(space));
9100 is_params = isl_union_set_is_params(dom);
9101 if (is_params < 0)
9102 dom = isl_union_set_free(dom);
9103 else if (is_params)
9104 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9105 "cannot create union map from expression without "
9106 "explicit domain elements",
9107 dom = isl_union_set_free(dom));
9109 return isl_union_map_from_domain_and_range(dom, ran);
9112 /* Construct a union map mapping the shared domain
9113 * of the union piecewise affine expressions to the range of "mupa"
9114 * with each dimension in the range equated to the
9115 * corresponding union piecewise affine expression.
9117 * If the input is zero-dimensional, then construct a mapping
9118 * from its explicit domain.
9120 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9121 __isl_take isl_multi_union_pw_aff *mupa)
9123 int i;
9124 isl_size n;
9125 isl_space *space;
9126 isl_union_map *umap;
9127 isl_union_pw_aff *upa;
9129 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9130 if (n < 0)
9131 mupa = isl_multi_union_pw_aff_free(mupa);
9132 if (!mupa)
9133 return NULL;
9135 if (n == 0)
9136 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9138 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9139 umap = isl_union_map_from_union_pw_aff(upa);
9141 for (i = 1; i < n; ++i) {
9142 isl_union_map *umap_i;
9144 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9145 umap_i = isl_union_map_from_union_pw_aff(upa);
9146 umap = isl_union_map_flat_range_product(umap, umap_i);
9149 space = isl_multi_union_pw_aff_get_space(mupa);
9150 umap = isl_union_map_reset_range_space(umap, space);
9152 isl_multi_union_pw_aff_free(mupa);
9153 return umap;
9156 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9157 * "range" is the space from which to set the range space.
9158 * "res" collects the results.
9160 struct isl_union_pw_multi_aff_reset_range_space_data {
9161 isl_space *range;
9162 isl_union_pw_multi_aff *res;
9165 /* Replace the range space of "pma" by the range space of data->range and
9166 * add the result to data->res.
9168 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9170 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9171 isl_space *space;
9173 space = isl_pw_multi_aff_get_space(pma);
9174 space = isl_space_domain(space);
9175 space = isl_space_extend_domain_with_range(space,
9176 isl_space_copy(data->range));
9177 pma = isl_pw_multi_aff_reset_space(pma, space);
9178 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9180 return data->res ? isl_stat_ok : isl_stat_error;
9183 /* Replace the range space of all the piecewise affine expressions in "upma" by
9184 * the range space of "space".
9186 * This assumes that all these expressions have the same output dimension.
9188 * Since the spaces of the expressions change, so do their hash values.
9189 * We therefore need to create a new isl_union_pw_multi_aff.
9190 * Note that the hash value is currently computed based on the entire
9191 * space even though there can only be a single expression with a given
9192 * domain space.
9194 static __isl_give isl_union_pw_multi_aff *
9195 isl_union_pw_multi_aff_reset_range_space(
9196 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9198 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9199 isl_space *space_upma;
9201 space_upma = isl_union_pw_multi_aff_get_space(upma);
9202 data.res = isl_union_pw_multi_aff_empty(space_upma);
9203 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9204 &reset_range_space, &data) < 0)
9205 data.res = isl_union_pw_multi_aff_free(data.res);
9207 isl_space_free(space);
9208 isl_union_pw_multi_aff_free(upma);
9209 return data.res;
9212 /* Construct and return a union piecewise multi affine expression
9213 * that is equal to the given multi union piecewise affine expression,
9214 * in the special case of a 0D multi union piecewise affine expression.
9216 * Construct a union piecewise multi affine expression
9217 * on top of the explicit domain of the input.
9219 __isl_give isl_union_pw_multi_aff *
9220 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9221 __isl_take isl_multi_union_pw_aff *mupa)
9223 isl_space *space;
9224 isl_multi_val *mv;
9225 isl_union_set *domain;
9227 space = isl_multi_union_pw_aff_get_space(mupa);
9228 mv = isl_multi_val_zero(space);
9229 domain = isl_multi_union_pw_aff_domain(mupa);
9230 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9233 /* Construct and return a union piecewise multi affine expression
9234 * that is equal to the given multi union piecewise affine expression.
9236 * If the input is zero-dimensional, then
9237 * construct a union piecewise multi affine expression
9238 * on top of the explicit domain of the input.
9240 __isl_give isl_union_pw_multi_aff *
9241 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9242 __isl_take isl_multi_union_pw_aff *mupa)
9244 int i;
9245 isl_size n;
9246 isl_space *space;
9247 isl_union_pw_multi_aff *upma;
9248 isl_union_pw_aff *upa;
9250 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9251 if (n < 0)
9252 mupa = isl_multi_union_pw_aff_free(mupa);
9253 if (!mupa)
9254 return NULL;
9256 if (n == 0)
9257 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9259 space = isl_multi_union_pw_aff_get_space(mupa);
9260 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9261 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9263 for (i = 1; i < n; ++i) {
9264 isl_union_pw_multi_aff *upma_i;
9266 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9267 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9268 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9271 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9273 isl_multi_union_pw_aff_free(mupa);
9274 return upma;
9277 /* Intersect the range of "mupa" with "range",
9278 * in the special case where "mupa" is 0D.
9280 * Intersect the domain of "mupa" with the constraints on the parameters
9281 * of "range".
9283 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9284 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9286 range = isl_set_params(range);
9287 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9288 return mupa;
9291 /* Intersect the range of "mupa" with "range".
9292 * That is, keep only those domain elements that have a function value
9293 * in "range".
9295 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9296 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9298 isl_union_pw_multi_aff *upma;
9299 isl_union_set *domain;
9300 isl_space *space;
9301 isl_size n;
9302 int match;
9304 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9305 if (n < 0 || !range)
9306 goto error;
9308 space = isl_set_get_space(range);
9309 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9310 space, isl_dim_set);
9311 isl_space_free(space);
9312 if (match < 0)
9313 goto error;
9314 if (!match)
9315 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9316 "space don't match", goto error);
9317 if (n == 0)
9318 return mupa_intersect_range_0D(mupa, range);
9320 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9321 isl_multi_union_pw_aff_copy(mupa));
9322 domain = isl_union_set_from_set(range);
9323 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9324 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9326 return mupa;
9327 error:
9328 isl_multi_union_pw_aff_free(mupa);
9329 isl_set_free(range);
9330 return NULL;
9333 /* Return the shared domain of the elements of "mupa",
9334 * in the special case where "mupa" is zero-dimensional.
9336 * Return the explicit domain of "mupa".
9337 * Note that this domain may be a parameter set, either
9338 * because "mupa" is meant to live in a set space or
9339 * because no explicit domain has been set.
9341 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9342 __isl_take isl_multi_union_pw_aff *mupa)
9344 isl_union_set *dom;
9346 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9347 isl_multi_union_pw_aff_free(mupa);
9349 return dom;
9352 /* Return the shared domain of the elements of "mupa".
9354 * If "mupa" is zero-dimensional, then return its explicit domain.
9356 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9357 __isl_take isl_multi_union_pw_aff *mupa)
9359 int i;
9360 isl_size n;
9361 isl_union_pw_aff *upa;
9362 isl_union_set *dom;
9364 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9365 if (n < 0)
9366 mupa = isl_multi_union_pw_aff_free(mupa);
9367 if (!mupa)
9368 return NULL;
9370 if (n == 0)
9371 return isl_multi_union_pw_aff_domain_0D(mupa);
9373 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9374 dom = isl_union_pw_aff_domain(upa);
9375 for (i = 1; i < n; ++i) {
9376 isl_union_set *dom_i;
9378 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9379 dom_i = isl_union_pw_aff_domain(upa);
9380 dom = isl_union_set_intersect(dom, dom_i);
9383 isl_multi_union_pw_aff_free(mupa);
9384 return dom;
9387 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9388 * In particular, the spaces have been aligned.
9389 * The result is defined over the shared domain of the elements of "mupa"
9391 * We first extract the parametric constant part of "aff" and
9392 * define that over the shared domain.
9393 * Then we iterate over all input dimensions of "aff" and add the corresponding
9394 * multiples of the elements of "mupa".
9395 * Finally, we consider the integer divisions, calling the function
9396 * recursively to obtain an isl_union_pw_aff corresponding to the
9397 * integer division argument.
9399 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9400 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9402 int i;
9403 isl_size n_in, n_div;
9404 isl_union_pw_aff *upa;
9405 isl_union_set *uset;
9406 isl_val *v;
9407 isl_aff *cst;
9409 n_in = isl_aff_dim(aff, isl_dim_in);
9410 n_div = isl_aff_dim(aff, isl_dim_div);
9411 if (n_in < 0 || n_div < 0)
9412 goto error;
9414 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9415 cst = isl_aff_copy(aff);
9416 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9417 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9418 cst = isl_aff_project_domain_on_params(cst);
9419 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9421 for (i = 0; i < n_in; ++i) {
9422 isl_union_pw_aff *upa_i;
9424 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9425 continue;
9426 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9427 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9428 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9429 upa = isl_union_pw_aff_add(upa, upa_i);
9432 for (i = 0; i < n_div; ++i) {
9433 isl_aff *div;
9434 isl_union_pw_aff *upa_i;
9436 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9437 continue;
9438 div = isl_aff_get_div(aff, i);
9439 upa_i = multi_union_pw_aff_apply_aff(
9440 isl_multi_union_pw_aff_copy(mupa), div);
9441 upa_i = isl_union_pw_aff_floor(upa_i);
9442 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9443 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9444 upa = isl_union_pw_aff_add(upa, upa_i);
9447 isl_multi_union_pw_aff_free(mupa);
9448 isl_aff_free(aff);
9450 return upa;
9451 error:
9452 isl_multi_union_pw_aff_free(mupa);
9453 isl_aff_free(aff);
9454 return NULL;
9457 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9458 * with the domain of "aff".
9459 * Furthermore, the dimension of this space needs to be greater than zero.
9460 * The result is defined over the shared domain of the elements of "mupa"
9462 * We perform these checks and then hand over control to
9463 * multi_union_pw_aff_apply_aff.
9465 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9466 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9468 isl_size dim;
9469 isl_space *space1, *space2;
9470 isl_bool equal;
9472 mupa = isl_multi_union_pw_aff_align_params(mupa,
9473 isl_aff_get_space(aff));
9474 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9475 if (!mupa || !aff)
9476 goto error;
9478 space1 = isl_multi_union_pw_aff_get_space(mupa);
9479 space2 = isl_aff_get_domain_space(aff);
9480 equal = isl_space_is_equal(space1, space2);
9481 isl_space_free(space1);
9482 isl_space_free(space2);
9483 if (equal < 0)
9484 goto error;
9485 if (!equal)
9486 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9487 "spaces don't match", goto error);
9488 dim = isl_aff_dim(aff, isl_dim_in);
9489 if (dim < 0)
9490 goto error;
9491 if (dim == 0)
9492 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9493 "cannot determine domains", goto error);
9495 return multi_union_pw_aff_apply_aff(mupa, aff);
9496 error:
9497 isl_multi_union_pw_aff_free(mupa);
9498 isl_aff_free(aff);
9499 return NULL;
9502 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9503 * The space of "mupa" is known to be compatible with the domain of "ma".
9505 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9506 * on the domain of "mupa".
9508 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9509 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9511 isl_union_set *dom;
9513 dom = isl_multi_union_pw_aff_domain(mupa);
9514 ma = isl_multi_aff_project_domain_on_params(ma);
9516 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9519 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9520 * with the domain of "ma".
9521 * The result is defined over the shared domain of the elements of "mupa"
9523 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9524 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9526 isl_space *space1, *space2;
9527 isl_multi_union_pw_aff *res;
9528 isl_bool equal;
9529 int i;
9530 isl_size n_in, n_out;
9532 mupa = isl_multi_union_pw_aff_align_params(mupa,
9533 isl_multi_aff_get_space(ma));
9534 ma = isl_multi_aff_align_params(ma,
9535 isl_multi_union_pw_aff_get_space(mupa));
9536 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9537 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9538 if (!mupa || n_in < 0 || n_out < 0)
9539 goto error;
9541 space1 = isl_multi_union_pw_aff_get_space(mupa);
9542 space2 = isl_multi_aff_get_domain_space(ma);
9543 equal = isl_space_is_equal(space1, space2);
9544 isl_space_free(space1);
9545 isl_space_free(space2);
9546 if (equal < 0)
9547 goto error;
9548 if (!equal)
9549 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9550 "spaces don't match", goto error);
9551 if (n_in == 0)
9552 return mupa_apply_multi_aff_0D(mupa, ma);
9554 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9555 res = isl_multi_union_pw_aff_alloc(space1);
9557 for (i = 0; i < n_out; ++i) {
9558 isl_aff *aff;
9559 isl_union_pw_aff *upa;
9561 aff = isl_multi_aff_get_aff(ma, i);
9562 upa = multi_union_pw_aff_apply_aff(
9563 isl_multi_union_pw_aff_copy(mupa), aff);
9564 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9567 isl_multi_aff_free(ma);
9568 isl_multi_union_pw_aff_free(mupa);
9569 return res;
9570 error:
9571 isl_multi_union_pw_aff_free(mupa);
9572 isl_multi_aff_free(ma);
9573 return NULL;
9576 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9577 * The space of "mupa" is known to be compatible with the domain of "pa".
9579 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9580 * on the domain of "mupa".
9582 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9583 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9585 isl_union_set *dom;
9587 dom = isl_multi_union_pw_aff_domain(mupa);
9588 pa = isl_pw_aff_project_domain_on_params(pa);
9590 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9593 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9594 * with the domain of "pa".
9595 * Furthermore, the dimension of this space needs to be greater than zero.
9596 * The result is defined over the shared domain of the elements of "mupa"
9598 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9599 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9601 int i;
9602 isl_bool equal;
9603 isl_size n_in;
9604 isl_space *space, *space2;
9605 isl_union_pw_aff *upa;
9607 mupa = isl_multi_union_pw_aff_align_params(mupa,
9608 isl_pw_aff_get_space(pa));
9609 pa = isl_pw_aff_align_params(pa,
9610 isl_multi_union_pw_aff_get_space(mupa));
9611 if (!mupa || !pa)
9612 goto error;
9614 space = isl_multi_union_pw_aff_get_space(mupa);
9615 space2 = isl_pw_aff_get_domain_space(pa);
9616 equal = isl_space_is_equal(space, space2);
9617 isl_space_free(space);
9618 isl_space_free(space2);
9619 if (equal < 0)
9620 goto error;
9621 if (!equal)
9622 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9623 "spaces don't match", goto error);
9624 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9625 if (n_in < 0)
9626 goto error;
9627 if (n_in == 0)
9628 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9630 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9631 upa = isl_union_pw_aff_empty(space);
9633 for (i = 0; i < pa->n; ++i) {
9634 isl_aff *aff;
9635 isl_set *domain;
9636 isl_multi_union_pw_aff *mupa_i;
9637 isl_union_pw_aff *upa_i;
9639 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9640 domain = isl_set_copy(pa->p[i].set);
9641 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9642 aff = isl_aff_copy(pa->p[i].aff);
9643 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9644 upa = isl_union_pw_aff_union_add(upa, upa_i);
9647 isl_multi_union_pw_aff_free(mupa);
9648 isl_pw_aff_free(pa);
9649 return upa;
9650 error:
9651 isl_multi_union_pw_aff_free(mupa);
9652 isl_pw_aff_free(pa);
9653 return NULL;
9656 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9657 * The space of "mupa" is known to be compatible with the domain of "pma".
9659 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9660 * on the domain of "mupa".
9662 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9663 __isl_take isl_multi_union_pw_aff *mupa,
9664 __isl_take isl_pw_multi_aff *pma)
9666 isl_union_set *dom;
9668 dom = isl_multi_union_pw_aff_domain(mupa);
9669 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9671 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9674 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9675 * with the domain of "pma".
9676 * The result is defined over the shared domain of the elements of "mupa"
9678 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9679 __isl_take isl_multi_union_pw_aff *mupa,
9680 __isl_take isl_pw_multi_aff *pma)
9682 isl_space *space1, *space2;
9683 isl_multi_union_pw_aff *res;
9684 isl_bool equal;
9685 int i;
9686 isl_size n_in, n_out;
9688 mupa = isl_multi_union_pw_aff_align_params(mupa,
9689 isl_pw_multi_aff_get_space(pma));
9690 pma = isl_pw_multi_aff_align_params(pma,
9691 isl_multi_union_pw_aff_get_space(mupa));
9692 if (!mupa || !pma)
9693 goto error;
9695 space1 = isl_multi_union_pw_aff_get_space(mupa);
9696 space2 = isl_pw_multi_aff_get_domain_space(pma);
9697 equal = isl_space_is_equal(space1, space2);
9698 isl_space_free(space1);
9699 isl_space_free(space2);
9700 if (equal < 0)
9701 goto error;
9702 if (!equal)
9703 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9704 "spaces don't match", goto error);
9705 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9706 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9707 if (n_in < 0 || n_out < 0)
9708 goto error;
9709 if (n_in == 0)
9710 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9712 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9713 res = isl_multi_union_pw_aff_alloc(space1);
9715 for (i = 0; i < n_out; ++i) {
9716 isl_pw_aff *pa;
9717 isl_union_pw_aff *upa;
9719 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9720 upa = isl_multi_union_pw_aff_apply_pw_aff(
9721 isl_multi_union_pw_aff_copy(mupa), pa);
9722 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9725 isl_pw_multi_aff_free(pma);
9726 isl_multi_union_pw_aff_free(mupa);
9727 return res;
9728 error:
9729 isl_multi_union_pw_aff_free(mupa);
9730 isl_pw_multi_aff_free(pma);
9731 return NULL;
9734 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9735 * If the explicit domain only keeps track of constraints on the parameters,
9736 * then only update those constraints.
9738 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9739 __isl_take isl_multi_union_pw_aff *mupa,
9740 __isl_keep isl_union_pw_multi_aff *upma)
9742 isl_bool is_params;
9744 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9745 return isl_multi_union_pw_aff_free(mupa);
9747 mupa = isl_multi_union_pw_aff_cow(mupa);
9748 if (!mupa)
9749 return NULL;
9751 is_params = isl_union_set_is_params(mupa->u.dom);
9752 if (is_params < 0)
9753 return isl_multi_union_pw_aff_free(mupa);
9755 upma = isl_union_pw_multi_aff_copy(upma);
9756 if (is_params)
9757 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9758 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9759 else
9760 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9761 mupa->u.dom, upma);
9762 if (!mupa->u.dom)
9763 return isl_multi_union_pw_aff_free(mupa);
9764 return mupa;
9767 /* Compute the pullback of "mupa" by the function represented by "upma".
9768 * In other words, plug in "upma" in "mupa". The result contains
9769 * expressions defined over the domain space of "upma".
9771 * Run over all elements of "mupa" and plug in "upma" in each of them.
9773 * If "mupa" has an explicit domain, then it is this domain
9774 * that needs to undergo a pullback instead, i.e., a preimage.
9776 __isl_give isl_multi_union_pw_aff *
9777 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9778 __isl_take isl_multi_union_pw_aff *mupa,
9779 __isl_take isl_union_pw_multi_aff *upma)
9781 int i;
9782 isl_size n;
9784 mupa = isl_multi_union_pw_aff_align_params(mupa,
9785 isl_union_pw_multi_aff_get_space(upma));
9786 upma = isl_union_pw_multi_aff_align_params(upma,
9787 isl_multi_union_pw_aff_get_space(mupa));
9788 mupa = isl_multi_union_pw_aff_cow(mupa);
9789 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9790 if (n < 0 || !upma)
9791 goto error;
9793 for (i = 0; i < n; ++i) {
9794 isl_union_pw_aff *upa;
9796 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9797 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9798 isl_union_pw_multi_aff_copy(upma));
9799 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9802 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9803 mupa = preimage_explicit_domain(mupa, upma);
9805 isl_union_pw_multi_aff_free(upma);
9806 return mupa;
9807 error:
9808 isl_multi_union_pw_aff_free(mupa);
9809 isl_union_pw_multi_aff_free(upma);
9810 return NULL;
9813 /* Extract the sequence of elements in "mupa" with domain space "space"
9814 * (ignoring parameters).
9816 * For the elements of "mupa" that are not defined on the specified space,
9817 * the corresponding element in the result is empty.
9819 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9820 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9822 int i;
9823 isl_size n;
9824 isl_space *space_mpa;
9825 isl_multi_pw_aff *mpa;
9827 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9828 if (n < 0 || !space)
9829 goto error;
9831 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9832 space = isl_space_replace_params(space, space_mpa);
9833 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9834 space_mpa);
9835 mpa = isl_multi_pw_aff_alloc(space_mpa);
9837 space = isl_space_from_domain(space);
9838 space = isl_space_add_dims(space, isl_dim_out, 1);
9839 for (i = 0; i < n; ++i) {
9840 isl_union_pw_aff *upa;
9841 isl_pw_aff *pa;
9843 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9844 pa = isl_union_pw_aff_extract_pw_aff(upa,
9845 isl_space_copy(space));
9846 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9847 isl_union_pw_aff_free(upa);
9850 isl_space_free(space);
9851 return mpa;
9852 error:
9853 isl_space_free(space);
9854 return NULL;
9857 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9858 * should modify the base expressions in the input.
9860 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9861 * are taken into account.
9862 * "fn" is applied to each entry in the input.
9864 struct isl_union_pw_multi_aff_un_op_control {
9865 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9866 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9869 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9870 * a second argument) for use as an isl_union_pw_multi_aff_transform
9871 * filter function (which does take a second argument).
9872 * Simply call control->filter without the second argument.
9874 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9875 __isl_take isl_pw_multi_aff *pma, void *user)
9877 struct isl_union_pw_multi_aff_un_op_control *control = user;
9879 return control->filter(pma);
9882 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9883 * a second argument) for use as an isl_union_pw_multi_aff_transform
9884 * base function (which does take a second argument).
9885 * Simply call control->fn without the second argument.
9887 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9888 __isl_take isl_pw_multi_aff *pma, void *user)
9890 struct isl_union_pw_multi_aff_un_op_control *control = user;
9892 return control->fn(pma);
9895 /* Construct an isl_union_pw_multi_aff that is obtained by
9896 * modifying "upma" according to "control".
9898 * isl_union_pw_multi_aff_transform performs essentially
9899 * the same operation, but takes a filter and a callback function
9900 * of a different form (with an extra argument).
9901 * Call isl_union_pw_multi_aff_transform with wrappers
9902 * that remove this extra argument.
9904 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9905 __isl_take isl_union_pw_multi_aff *upma,
9906 struct isl_union_pw_multi_aff_un_op_control *control)
9908 struct isl_union_pw_multi_aff_transform_control t_control = {
9909 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9910 .filter_user = control,
9911 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9912 .fn_user = control,
9915 return isl_union_pw_multi_aff_transform(upma, &t_control);
9918 /* For each function in "upma" of the form A -> [B -> C],
9919 * extract the function A -> B and collect the results.
9921 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9922 __isl_take isl_union_pw_multi_aff *upma)
9924 struct isl_union_pw_multi_aff_un_op_control control = {
9925 .filter = &isl_pw_multi_aff_range_is_wrapping,
9926 .fn = &isl_pw_multi_aff_range_factor_domain,
9928 return isl_union_pw_multi_aff_un_op(upma, &control);
9931 /* For each function in "upma" of the form A -> [B -> C],
9932 * extract the function A -> C and collect the results.
9934 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9935 __isl_take isl_union_pw_multi_aff *upma)
9937 struct isl_union_pw_multi_aff_un_op_control control = {
9938 .filter = &isl_pw_multi_aff_range_is_wrapping,
9939 .fn = &isl_pw_multi_aff_range_factor_range,
9941 return isl_union_pw_multi_aff_un_op(upma, &control);
9944 /* Evaluate the affine function "aff" in the void point "pnt".
9945 * In particular, return the value NaN.
9947 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9948 __isl_take isl_point *pnt)
9950 isl_ctx *ctx;
9952 ctx = isl_point_get_ctx(pnt);
9953 isl_aff_free(aff);
9954 isl_point_free(pnt);
9955 return isl_val_nan(ctx);
9958 /* Evaluate the affine expression "aff"
9959 * in the coordinates (with denominator) "pnt".
9961 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9962 __isl_keep isl_vec *pnt)
9964 isl_int n, d;
9965 isl_ctx *ctx;
9966 isl_val *v;
9968 if (!aff || !pnt)
9969 return NULL;
9971 ctx = isl_vec_get_ctx(aff);
9972 isl_int_init(n);
9973 isl_int_init(d);
9974 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9975 isl_int_mul(d, aff->el[0], pnt->el[0]);
9976 v = isl_val_rat_from_isl_int(ctx, n, d);
9977 v = isl_val_normalize(v);
9978 isl_int_clear(n);
9979 isl_int_clear(d);
9981 return v;
9984 /* Check that the domain space of "aff" is equal to "space".
9986 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9987 __isl_keep isl_space *space)
9989 isl_bool ok;
9991 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9992 if (ok < 0)
9993 return isl_stat_error;
9994 if (!ok)
9995 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9996 "incompatible spaces", return isl_stat_error);
9997 return isl_stat_ok;
10000 /* Evaluate the affine function "aff" in "pnt".
10002 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
10003 __isl_take isl_point *pnt)
10005 isl_bool is_void;
10006 isl_val *v;
10007 isl_local_space *ls;
10009 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
10010 goto error;
10011 is_void = isl_point_is_void(pnt);
10012 if (is_void < 0)
10013 goto error;
10014 if (is_void)
10015 return eval_void(aff, pnt);
10017 ls = isl_aff_get_domain_local_space(aff);
10018 pnt = isl_local_space_lift_point(ls, pnt);
10020 v = eval(aff->v, isl_point_peek_vec(pnt));
10022 isl_aff_free(aff);
10023 isl_point_free(pnt);
10025 return v;
10026 error:
10027 isl_aff_free(aff);
10028 isl_point_free(pnt);
10029 return NULL;