isl_pw_*_eval: align parameters of arguments
[isl.git] / isl_aff.c
blob6f3e7d7b4d4f296551156c26f37e9010c79d224a
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_bind_domain_templ.c>
2797 #include <isl_pw_eval.c>
2798 #include <isl_pw_hash.c>
2799 #include <isl_pw_insert_dims_templ.c>
2800 #include <isl_pw_insert_domain_templ.c>
2801 #include <isl_pw_move_dims_templ.c>
2802 #include <isl_pw_neg_templ.c>
2803 #include <isl_pw_pullback_templ.c>
2804 #include <isl_pw_sub_templ.c>
2805 #include <isl_pw_union_opt.c>
2807 #undef BASE
2808 #define BASE pw_aff
2810 #include <isl_union_single.c>
2811 #include <isl_union_neg.c>
2813 #undef BASE
2814 #define BASE aff
2816 #include <isl_union_pw_templ.c>
2818 /* Compute a piecewise quasi-affine expression with a domain that
2819 * is the union of those of pwaff1 and pwaff2 and such that on each
2820 * cell, the quasi-affine expression is the maximum of those of pwaff1
2821 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2822 * cell, then the associated expression is the defined one.
2824 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2825 __isl_take isl_pw_aff *pwaff2)
2827 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2828 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2831 /* Compute a piecewise quasi-affine expression with a domain that
2832 * is the union of those of pwaff1 and pwaff2 and such that on each
2833 * cell, the quasi-affine expression is the minimum of those of pwaff1
2834 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2835 * cell, then the associated expression is the defined one.
2837 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2838 __isl_take isl_pw_aff *pwaff2)
2840 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2841 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2844 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2845 __isl_take isl_pw_aff *pwaff2, int max)
2847 if (max)
2848 return isl_pw_aff_union_max(pwaff1, pwaff2);
2849 else
2850 return isl_pw_aff_union_min(pwaff1, pwaff2);
2853 /* Is the domain of "pa" a product?
2855 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2857 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2860 #undef TYPE
2861 #define TYPE isl_pw_aff
2862 #include <isl_domain_factor_templ.c>
2864 /* Return a set containing those elements in the domain
2865 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2866 * does not satisfy "fn" (if complement is 1).
2868 * The pieces with a NaN never belong to the result since
2869 * NaN does not satisfy any property.
2871 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2872 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2873 void *user),
2874 int complement, void *user)
2876 int i;
2877 isl_set *set;
2879 if (!pwaff)
2880 return NULL;
2882 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2884 for (i = 0; i < pwaff->n; ++i) {
2885 isl_basic_set *bset;
2886 isl_set *set_i, *locus;
2887 isl_bool rational;
2889 if (isl_aff_is_nan(pwaff->p[i].aff))
2890 continue;
2892 rational = isl_set_has_rational(pwaff->p[i].set);
2893 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2894 locus = isl_set_from_basic_set(bset);
2895 set_i = isl_set_copy(pwaff->p[i].set);
2896 if (complement)
2897 set_i = isl_set_subtract(set_i, locus);
2898 else
2899 set_i = isl_set_intersect(set_i, locus);
2900 set = isl_set_union_disjoint(set, set_i);
2903 isl_pw_aff_free(pwaff);
2905 return set;
2908 /* Return a set containing those elements in the domain
2909 * of "pa" where it is positive.
2911 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2913 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2916 /* Return a set containing those elements in the domain
2917 * of pwaff where it is non-negative.
2919 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2921 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2924 /* Return a set containing those elements in the domain
2925 * of pwaff where it is zero.
2927 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2929 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2932 /* Return a set containing those elements in the domain
2933 * of pwaff where it is not zero.
2935 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2937 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2940 /* Bind the affine function "aff" to the parameter "id",
2941 * returning the elements in the domain where the affine expression
2942 * is equal to the parameter.
2944 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2945 __isl_take isl_id *id)
2947 isl_space *space;
2948 isl_aff *aff_id;
2950 space = isl_aff_get_domain_space(aff);
2951 space = isl_space_add_param_id(space, isl_id_copy(id));
2953 aff = isl_aff_align_params(aff, isl_space_copy(space));
2954 aff_id = isl_aff_param_on_domain_space_id(space, id);
2956 return isl_aff_eq_basic_set(aff, aff_id);
2959 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2960 * "rational" should not be set.
2962 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2963 int rational, void *user)
2965 isl_id *id = user;
2967 if (!aff)
2968 return NULL;
2969 if (rational)
2970 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2971 "rational binding not supported", goto error);
2972 return isl_aff_bind_id(aff, isl_id_copy(id));
2973 error:
2974 isl_aff_free(aff);
2975 return NULL;
2978 /* Bind the piecewise affine function "pa" to the parameter "id",
2979 * returning the elements in the domain where the expression
2980 * is equal to the parameter.
2982 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2983 __isl_take isl_id *id)
2985 isl_set *bound;
2987 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2988 isl_id_free(id);
2990 return bound;
2993 /* Return a set containing those elements in the shared domain
2994 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2996 * We compute the difference on the shared domain and then construct
2997 * the set of values where this difference is non-negative.
2998 * If strict is set, we first subtract 1 from the difference.
2999 * If equal is set, we only return the elements where pwaff1 and pwaff2
3000 * are equal.
3002 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3003 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3005 isl_set *set1, *set2;
3007 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3008 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3009 set1 = isl_set_intersect(set1, set2);
3010 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3011 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3012 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3014 if (strict) {
3015 isl_space *space = isl_set_get_space(set1);
3016 isl_aff *aff;
3017 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3018 aff = isl_aff_add_constant_si(aff, -1);
3019 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3020 } else
3021 isl_set_free(set1);
3023 if (equal)
3024 return isl_pw_aff_zero_set(pwaff1);
3025 return isl_pw_aff_nonneg_set(pwaff1);
3028 /* Return a set containing those elements in the shared domain
3029 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3031 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3032 __isl_take isl_pw_aff *pwaff2)
3034 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3035 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3038 /* Return a set containing those elements in the shared domain
3039 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3041 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3042 __isl_take isl_pw_aff *pwaff2)
3044 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3045 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3048 /* Return a set containing those elements in the shared domain
3049 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3051 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3052 __isl_take isl_pw_aff *pwaff2)
3054 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3055 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3058 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3059 __isl_take isl_pw_aff *pwaff2)
3061 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3064 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3065 __isl_take isl_pw_aff *pwaff2)
3067 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3070 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3071 * where the function values are ordered in the same way as "order",
3072 * which returns a set in the shared domain of its two arguments.
3074 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3075 * We first pull back the two functions such that they are defined on
3076 * the domain [A -> B]. Then we apply "order", resulting in a set
3077 * in the space [A -> B]. Finally, we unwrap this set to obtain
3078 * a map in the space A -> B.
3080 static __isl_give isl_map *isl_pw_aff_order_map(
3081 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3082 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3083 __isl_take isl_pw_aff *pa2))
3085 isl_space *space1, *space2;
3086 isl_multi_aff *ma;
3087 isl_set *set;
3089 isl_pw_aff_align_params_bin(&pa1, &pa2);
3090 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3091 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3092 space1 = isl_space_map_from_domain_and_range(space1, space2);
3093 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3094 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3095 ma = isl_multi_aff_range_map(space1);
3096 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3097 set = order(pa1, pa2);
3099 return isl_set_unwrap(set);
3102 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3103 * where the function values are equal.
3105 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3106 __isl_take isl_pw_aff *pa2)
3108 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3111 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3112 * where the function value of "pa1" is less than or equal to
3113 * the function value of "pa2".
3115 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3116 __isl_take isl_pw_aff *pa2)
3118 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3121 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3122 * where the function value of "pa1" is less than the function value of "pa2".
3124 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3125 __isl_take isl_pw_aff *pa2)
3127 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3130 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3131 * where the function value of "pa1" is greater than or equal to
3132 * the function value of "pa2".
3134 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3135 __isl_take isl_pw_aff *pa2)
3137 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3140 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3141 * where the function value of "pa1" is greater than the function value
3142 * of "pa2".
3144 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3145 __isl_take isl_pw_aff *pa2)
3147 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3150 /* Return a set containing those elements in the shared domain
3151 * of the elements of list1 and list2 where each element in list1
3152 * has the relation specified by "fn" with each element in list2.
3154 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3155 __isl_take isl_pw_aff_list *list2,
3156 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3157 __isl_take isl_pw_aff *pwaff2))
3159 int i, j;
3160 isl_ctx *ctx;
3161 isl_set *set;
3163 if (!list1 || !list2)
3164 goto error;
3166 ctx = isl_pw_aff_list_get_ctx(list1);
3167 if (list1->n < 1 || list2->n < 1)
3168 isl_die(ctx, isl_error_invalid,
3169 "list should contain at least one element", goto error);
3171 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3172 for (i = 0; i < list1->n; ++i)
3173 for (j = 0; j < list2->n; ++j) {
3174 isl_set *set_ij;
3176 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3177 isl_pw_aff_copy(list2->p[j]));
3178 set = isl_set_intersect(set, set_ij);
3181 isl_pw_aff_list_free(list1);
3182 isl_pw_aff_list_free(list2);
3183 return set;
3184 error:
3185 isl_pw_aff_list_free(list1);
3186 isl_pw_aff_list_free(list2);
3187 return NULL;
3190 /* Return a set containing those elements in the shared domain
3191 * of the elements of list1 and list2 where each element in list1
3192 * is equal to each element in list2.
3194 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3195 __isl_take isl_pw_aff_list *list2)
3197 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3200 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3201 __isl_take isl_pw_aff_list *list2)
3203 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3206 /* Return a set containing those elements in the shared domain
3207 * of the elements of list1 and list2 where each element in list1
3208 * is less than or equal to each element in list2.
3210 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3211 __isl_take isl_pw_aff_list *list2)
3213 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3216 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3217 __isl_take isl_pw_aff_list *list2)
3219 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3222 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3223 __isl_take isl_pw_aff_list *list2)
3225 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3228 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3229 __isl_take isl_pw_aff_list *list2)
3231 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3235 /* Return a set containing those elements in the shared domain
3236 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3238 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3239 __isl_take isl_pw_aff *pwaff2)
3241 isl_set *set_lt, *set_gt;
3243 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3244 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3245 isl_pw_aff_copy(pwaff2));
3246 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3247 return isl_set_union_disjoint(set_lt, set_gt);
3250 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3251 isl_int v)
3253 int i;
3255 if (isl_int_is_one(v))
3256 return pwaff;
3257 if (!isl_int_is_pos(v))
3258 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3259 "factor needs to be positive",
3260 return isl_pw_aff_free(pwaff));
3261 pwaff = isl_pw_aff_cow(pwaff);
3262 if (!pwaff)
3263 return NULL;
3264 if (pwaff->n == 0)
3265 return pwaff;
3267 for (i = 0; i < pwaff->n; ++i) {
3268 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3269 if (!pwaff->p[i].aff)
3270 return isl_pw_aff_free(pwaff);
3273 return pwaff;
3276 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3278 return isl_pw_aff_un_op(pwaff, &isl_aff_floor);
3281 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3283 return isl_pw_aff_un_op(pwaff, &isl_aff_ceil);
3286 /* Assuming that "cond1" and "cond2" are disjoint,
3287 * return an affine expression that is equal to pwaff1 on cond1
3288 * and to pwaff2 on cond2.
3290 static __isl_give isl_pw_aff *isl_pw_aff_select(
3291 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3292 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3294 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3295 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3297 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3300 /* Return an affine expression that is equal to pwaff_true for elements
3301 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3302 * is zero.
3303 * That is, return cond ? pwaff_true : pwaff_false;
3305 * If "cond" involves and NaN, then we conservatively return a NaN
3306 * on its entire domain. In principle, we could consider the pieces
3307 * where it is NaN separately from those where it is not.
3309 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3310 * then only use the domain of "cond" to restrict the domain.
3312 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3313 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3315 isl_set *cond_true, *cond_false;
3316 isl_bool equal;
3318 if (!cond)
3319 goto error;
3320 if (isl_pw_aff_involves_nan(cond)) {
3321 isl_space *space = isl_pw_aff_get_domain_space(cond);
3322 isl_local_space *ls = isl_local_space_from_space(space);
3323 isl_pw_aff_free(cond);
3324 isl_pw_aff_free(pwaff_true);
3325 isl_pw_aff_free(pwaff_false);
3326 return isl_pw_aff_nan_on_domain(ls);
3329 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3330 isl_pw_aff_get_space(pwaff_false));
3331 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3332 isl_pw_aff_get_space(pwaff_true));
3333 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3334 if (equal < 0)
3335 goto error;
3336 if (equal) {
3337 isl_set *dom;
3339 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3340 isl_pw_aff_free(pwaff_false);
3341 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3344 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3345 cond_false = isl_pw_aff_zero_set(cond);
3346 return isl_pw_aff_select(cond_true, pwaff_true,
3347 cond_false, pwaff_false);
3348 error:
3349 isl_pw_aff_free(cond);
3350 isl_pw_aff_free(pwaff_true);
3351 isl_pw_aff_free(pwaff_false);
3352 return NULL;
3355 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3357 int pos;
3359 if (!aff)
3360 return isl_bool_error;
3362 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3363 return isl_bool_ok(pos == -1);
3366 /* Check whether pwaff is a piecewise constant.
3368 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3370 int i;
3372 if (!pwaff)
3373 return isl_bool_error;
3375 for (i = 0; i < pwaff->n; ++i) {
3376 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3377 if (is_cst < 0 || !is_cst)
3378 return is_cst;
3381 return isl_bool_true;
3384 /* Return the product of "aff1" and "aff2".
3386 * If either of the two is NaN, then the result is NaN.
3388 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3390 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3391 __isl_take isl_aff *aff2)
3393 if (!aff1 || !aff2)
3394 goto error;
3396 if (isl_aff_is_nan(aff1)) {
3397 isl_aff_free(aff2);
3398 return aff1;
3400 if (isl_aff_is_nan(aff2)) {
3401 isl_aff_free(aff1);
3402 return aff2;
3405 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3406 return isl_aff_mul(aff2, aff1);
3408 if (!isl_aff_is_cst(aff2))
3409 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3410 "at least one affine expression should be constant",
3411 goto error);
3413 aff1 = isl_aff_cow(aff1);
3414 if (!aff1 || !aff2)
3415 goto error;
3417 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3418 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3420 isl_aff_free(aff2);
3421 return aff1;
3422 error:
3423 isl_aff_free(aff1);
3424 isl_aff_free(aff2);
3425 return NULL;
3428 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3430 * If either of the two is NaN, then the result is NaN.
3431 * A division by zero also results in NaN.
3433 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3434 __isl_take isl_aff *aff2)
3436 isl_bool is_cst, is_zero;
3437 int neg;
3439 if (!aff1 || !aff2)
3440 goto error;
3442 if (isl_aff_is_nan(aff1)) {
3443 isl_aff_free(aff2);
3444 return aff1;
3446 if (isl_aff_is_nan(aff2)) {
3447 isl_aff_free(aff1);
3448 return aff2;
3451 is_cst = isl_aff_is_cst(aff2);
3452 if (is_cst < 0)
3453 goto error;
3454 if (!is_cst)
3455 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3456 "second argument should be a constant", goto error);
3457 is_zero = isl_aff_plain_is_zero(aff2);
3458 if (is_zero < 0)
3459 goto error;
3460 if (is_zero)
3461 return set_nan_free(aff1, aff2);
3463 neg = isl_int_is_neg(aff2->v->el[1]);
3464 if (neg) {
3465 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3466 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3469 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3470 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3472 if (neg) {
3473 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3474 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3477 isl_aff_free(aff2);
3478 return aff1;
3479 error:
3480 isl_aff_free(aff1);
3481 isl_aff_free(aff2);
3482 return NULL;
3485 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3486 __isl_take isl_pw_aff *pwaff2)
3488 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3489 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3492 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3493 __isl_take isl_pw_aff *pwaff2)
3495 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3498 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3499 __isl_take isl_pw_aff *pwaff2)
3501 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3502 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3505 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3507 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3508 __isl_take isl_pw_aff *pa2)
3510 int is_cst;
3512 is_cst = isl_pw_aff_is_cst(pa2);
3513 if (is_cst < 0)
3514 goto error;
3515 if (!is_cst)
3516 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3517 "second argument should be a piecewise constant",
3518 goto error);
3519 isl_pw_aff_align_params_bin(&pa1, &pa2);
3520 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3521 error:
3522 isl_pw_aff_free(pa1);
3523 isl_pw_aff_free(pa2);
3524 return NULL;
3527 /* Compute the quotient of the integer division of "pa1" by "pa2"
3528 * with rounding towards zero.
3529 * "pa2" is assumed to be a piecewise constant.
3531 * In particular, return
3533 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3536 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3537 __isl_take isl_pw_aff *pa2)
3539 int is_cst;
3540 isl_set *cond;
3541 isl_pw_aff *f, *c;
3543 is_cst = isl_pw_aff_is_cst(pa2);
3544 if (is_cst < 0)
3545 goto error;
3546 if (!is_cst)
3547 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3548 "second argument should be a piecewise constant",
3549 goto error);
3551 pa1 = isl_pw_aff_div(pa1, pa2);
3553 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3554 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3555 c = isl_pw_aff_ceil(pa1);
3556 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3557 error:
3558 isl_pw_aff_free(pa1);
3559 isl_pw_aff_free(pa2);
3560 return NULL;
3563 /* Compute the remainder of the integer division of "pa1" by "pa2"
3564 * with rounding towards zero.
3565 * "pa2" is assumed to be a piecewise constant.
3567 * In particular, return
3569 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3572 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3573 __isl_take isl_pw_aff *pa2)
3575 int is_cst;
3576 isl_pw_aff *res;
3578 is_cst = isl_pw_aff_is_cst(pa2);
3579 if (is_cst < 0)
3580 goto error;
3581 if (!is_cst)
3582 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3583 "second argument should be a piecewise constant",
3584 goto error);
3585 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3586 res = isl_pw_aff_mul(pa2, res);
3587 res = isl_pw_aff_sub(pa1, res);
3588 return res;
3589 error:
3590 isl_pw_aff_free(pa1);
3591 isl_pw_aff_free(pa2);
3592 return NULL;
3595 /* Does either of "pa1" or "pa2" involve any NaN?
3597 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3598 __isl_keep isl_pw_aff *pa2)
3600 isl_bool has_nan;
3602 has_nan = isl_pw_aff_involves_nan(pa1);
3603 if (has_nan < 0 || has_nan)
3604 return has_nan;
3605 return isl_pw_aff_involves_nan(pa2);
3608 /* Return a piecewise affine expression defined on the specified domain
3609 * that represents NaN.
3611 static __isl_give isl_pw_aff *nan_on_domain_set(__isl_take isl_set *dom)
3613 isl_local_space *ls;
3614 isl_pw_aff *pa;
3616 ls = isl_local_space_from_space(isl_set_get_space(dom));
3617 pa = isl_pw_aff_nan_on_domain(ls);
3618 pa = isl_pw_aff_intersect_domain(pa, dom);
3620 return pa;
3623 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3624 * by a NaN on their shared domain.
3626 * In principle, the result could be refined to only being NaN
3627 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3629 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3630 __isl_take isl_pw_aff *pa2)
3632 isl_set *dom;
3634 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3635 return nan_on_domain_set(dom);
3638 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3639 __isl_take isl_pw_aff *pwaff2)
3641 isl_set *le;
3642 isl_set *dom;
3644 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3645 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3646 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3647 isl_pw_aff_copy(pwaff2));
3648 dom = isl_set_subtract(dom, isl_set_copy(le));
3649 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3652 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3653 __isl_take isl_pw_aff *pwaff2)
3655 isl_set *ge;
3656 isl_set *dom;
3658 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3659 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3660 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3661 isl_pw_aff_copy(pwaff2));
3662 dom = isl_set_subtract(dom, isl_set_copy(ge));
3663 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3666 /* Return an expression for the minimum (if "max" is not set) or
3667 * the maximum (if "max" is set) of "pa1" and "pa2".
3668 * If either expression involves any NaN, then return a NaN
3669 * on the shared domain as result.
3671 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3672 __isl_take isl_pw_aff *pa2, int max)
3674 isl_bool has_nan;
3676 has_nan = either_involves_nan(pa1, pa2);
3677 if (has_nan < 0)
3678 pa1 = isl_pw_aff_free(pa1);
3679 else if (has_nan)
3680 return replace_by_nan(pa1, pa2);
3682 isl_pw_aff_align_params_bin(&pa1, &pa2);
3683 if (max)
3684 return pw_aff_max(pa1, pa2);
3685 else
3686 return pw_aff_min(pa1, pa2);
3689 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3691 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3692 __isl_take isl_pw_aff *pwaff2)
3694 return pw_aff_min_max(pwaff1, pwaff2, 0);
3697 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3699 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3700 __isl_take isl_pw_aff *pwaff2)
3702 return pw_aff_min_max(pwaff1, pwaff2, 1);
3705 /* Does "pa" not involve any NaN?
3707 static isl_bool pw_aff_no_nan(__isl_keep isl_pw_aff *pa, void *user)
3709 return isl_bool_not(isl_pw_aff_involves_nan(pa));
3712 /* Does any element of "list" involve any NaN?
3714 * That is, is it not the case that every element does not involve any NaN?
3716 static isl_bool isl_pw_aff_list_involves_nan(__isl_keep isl_pw_aff_list *list)
3718 return isl_bool_not(isl_pw_aff_list_every(list, &pw_aff_no_nan, NULL));
3721 /* Replace "list" (consisting of "n" elements, of which
3722 * at least one element involves a NaN)
3723 * by a NaN on the shared domain of the elements.
3725 * In principle, the result could be refined to only being NaN
3726 * on the parts of this domain where at least one of the elements is NaN.
3728 static __isl_give isl_pw_aff *replace_list_by_nan(
3729 __isl_take isl_pw_aff_list *list, int n)
3731 int i;
3732 isl_set *dom;
3734 dom = isl_pw_aff_domain(isl_pw_aff_list_get_at(list, 0));
3735 for (i = 1; i < n; ++i) {
3736 isl_set *dom_i;
3738 dom_i = isl_pw_aff_domain(isl_pw_aff_list_get_at(list, i));
3739 dom = isl_set_intersect(dom, dom_i);
3742 isl_pw_aff_list_free(list);
3743 return nan_on_domain_set(dom);
3746 /* Return the set where the element at "pos1" of "list" is less than or
3747 * equal to the element at "pos2".
3748 * Equality is only allowed if "pos1" is smaller than "pos2".
3750 static __isl_give isl_set *less(__isl_keep isl_pw_aff_list *list,
3751 int pos1, int pos2)
3753 isl_pw_aff *pa1, *pa2;
3755 pa1 = isl_pw_aff_list_get_at(list, pos1);
3756 pa2 = isl_pw_aff_list_get_at(list, pos2);
3758 if (pos1 < pos2)
3759 return isl_pw_aff_le_set(pa1, pa2);
3760 else
3761 return isl_pw_aff_lt_set(pa1, pa2);
3764 /* Return an isl_pw_aff that maps each element in the intersection of the
3765 * domains of the piecewise affine expressions in "list"
3766 * to the maximal (if "max" is set) or minimal (if "max" is not set)
3767 * expression in "list" at that element.
3768 * If any expression involves any NaN, then return a NaN
3769 * on the shared domain as result.
3771 * If "list" has n elements, then the result consists of n pieces,
3772 * where, in the case of a minimum, each piece has as value expression
3773 * the value expression of one of the elements and as domain
3774 * the set of elements where that value expression
3775 * is less than (or equal) to the other value expressions.
3776 * In the case of a maximum, the condition is
3777 * that all the other value expressions are less than (or equal)
3778 * to the given value expression.
3780 * In order to produce disjoint pieces, a pair of elements
3781 * in the original domain is only allowed to be equal to each other
3782 * on exactly one of the two pieces corresponding to the two elements.
3783 * The position in the list is used to break ties.
3784 * In particular, in the case of a minimum,
3785 * in the piece corresponding to a given element,
3786 * this element is allowed to be equal to any later element in the list,
3787 * but not to any earlier element in the list.
3789 static __isl_give isl_pw_aff *isl_pw_aff_list_opt(
3790 __isl_take isl_pw_aff_list *list, int max)
3792 int i, j;
3793 isl_bool has_nan;
3794 isl_size n;
3795 isl_space *space;
3796 isl_pw_aff *pa, *res;
3798 n = isl_pw_aff_list_size(list);
3799 if (n < 0)
3800 goto error;
3801 if (n < 1)
3802 isl_die(isl_pw_aff_list_get_ctx(list), isl_error_invalid,
3803 "list should contain at least one element", goto error);
3805 has_nan = isl_pw_aff_list_involves_nan(list);
3806 if (has_nan < 0)
3807 goto error;
3808 if (has_nan)
3809 return replace_list_by_nan(list, n);
3811 pa = isl_pw_aff_list_get_at(list, 0);
3812 space = isl_pw_aff_get_space(pa);
3813 isl_pw_aff_free(pa);
3814 res = isl_pw_aff_empty(space);
3816 for (i = 0; i < n; ++i) {
3817 pa = isl_pw_aff_list_get_at(list, i);
3818 for (j = 0; j < n; ++j) {
3819 isl_set *dom;
3821 if (j == i)
3822 continue;
3823 if (max)
3824 dom = less(list, j, i);
3825 else
3826 dom = less(list, i, j);
3828 pa = isl_pw_aff_intersect_domain(pa, dom);
3830 res = isl_pw_aff_add_disjoint(res, pa);
3833 isl_pw_aff_list_free(list);
3834 return res;
3835 error:
3836 isl_pw_aff_list_free(list);
3837 return NULL;
3840 /* Return an isl_pw_aff that maps each element in the intersection of the
3841 * domains of the elements of list to the minimal corresponding affine
3842 * expression.
3844 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3846 return isl_pw_aff_list_opt(list, 0);
3849 /* Return an isl_pw_aff that maps each element in the intersection of the
3850 * domains of the elements of list to the maximal corresponding affine
3851 * expression.
3853 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3855 return isl_pw_aff_list_opt(list, 1);
3858 /* Mark the domains of "pwaff" as rational.
3860 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3862 int i;
3864 pwaff = isl_pw_aff_cow(pwaff);
3865 if (!pwaff)
3866 return NULL;
3867 if (pwaff->n == 0)
3868 return pwaff;
3870 for (i = 0; i < pwaff->n; ++i) {
3871 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3872 if (!pwaff->p[i].set)
3873 return isl_pw_aff_free(pwaff);
3876 return pwaff;
3879 /* Mark the domains of the elements of "list" as rational.
3881 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3882 __isl_take isl_pw_aff_list *list)
3884 int i, n;
3886 if (!list)
3887 return NULL;
3888 if (list->n == 0)
3889 return list;
3891 n = list->n;
3892 for (i = 0; i < n; ++i) {
3893 isl_pw_aff *pa;
3895 pa = isl_pw_aff_list_get_pw_aff(list, i);
3896 pa = isl_pw_aff_set_rational(pa);
3897 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3900 return list;
3903 /* Do the parameters of "aff" match those of "space"?
3905 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3906 __isl_keep isl_space *space)
3908 isl_space *aff_space;
3909 isl_bool match;
3911 if (!aff || !space)
3912 return isl_bool_error;
3914 aff_space = isl_aff_get_domain_space(aff);
3916 match = isl_space_has_equal_params(space, aff_space);
3918 isl_space_free(aff_space);
3919 return match;
3922 /* Check that the domain space of "aff" matches "space".
3924 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3925 __isl_keep isl_space *space)
3927 isl_space *aff_space;
3928 isl_bool match;
3930 if (!aff || !space)
3931 return isl_stat_error;
3933 aff_space = isl_aff_get_domain_space(aff);
3935 match = isl_space_has_equal_params(space, aff_space);
3936 if (match < 0)
3937 goto error;
3938 if (!match)
3939 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3940 "parameters don't match", goto error);
3941 match = isl_space_tuple_is_equal(space, isl_dim_in,
3942 aff_space, isl_dim_set);
3943 if (match < 0)
3944 goto error;
3945 if (!match)
3946 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3947 "domains don't match", goto error);
3948 isl_space_free(aff_space);
3949 return isl_stat_ok;
3950 error:
3951 isl_space_free(aff_space);
3952 return isl_stat_error;
3955 /* Return the shared (universe) domain of the elements of "ma".
3957 * Since an isl_multi_aff (and an isl_aff) is always total,
3958 * the domain is always the universe set in its domain space.
3959 * This is a helper function for use in the generic isl_multi_*_bind.
3961 static __isl_give isl_basic_set *isl_multi_aff_domain(
3962 __isl_take isl_multi_aff *ma)
3964 isl_space *space;
3966 space = isl_multi_aff_get_space(ma);
3967 isl_multi_aff_free(ma);
3969 return isl_basic_set_universe(isl_space_domain(space));
3972 #undef BASE
3973 #define BASE aff
3975 #include <isl_multi_no_explicit_domain.c>
3976 #include <isl_multi_templ.c>
3977 #include <isl_multi_un_op_templ.c>
3978 #include <isl_multi_bin_val_templ.c>
3979 #include <isl_multi_add_constant_templ.c>
3980 #include <isl_multi_apply_set.c>
3981 #include <isl_multi_arith_templ.c>
3982 #include <isl_multi_bind_domain_templ.c>
3983 #include <isl_multi_cmp.c>
3984 #include <isl_multi_dim_id_templ.c>
3985 #include <isl_multi_dims.c>
3986 #include <isl_multi_floor.c>
3987 #include <isl_multi_from_base_templ.c>
3988 #include <isl_multi_identity_templ.c>
3989 #include <isl_multi_insert_domain_templ.c>
3990 #include <isl_multi_locals_templ.c>
3991 #include <isl_multi_move_dims_templ.c>
3992 #include <isl_multi_nan_templ.c>
3993 #include <isl_multi_product_templ.c>
3994 #include <isl_multi_splice_templ.c>
3995 #include <isl_multi_tuple_id_templ.c>
3996 #include <isl_multi_unbind_params_templ.c>
3997 #include <isl_multi_zero_templ.c>
3999 #undef DOMBASE
4000 #define DOMBASE set
4001 #include <isl_multi_gist.c>
4003 #undef DOMBASE
4004 #define DOMBASE basic_set
4005 #include <isl_multi_bind_templ.c>
4007 /* Construct an isl_multi_aff living in "space" that corresponds
4008 * to the affine transformation matrix "mat".
4010 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
4011 __isl_take isl_space *space, __isl_take isl_mat *mat)
4013 isl_ctx *ctx;
4014 isl_local_space *ls = NULL;
4015 isl_multi_aff *ma = NULL;
4016 isl_size n_row, n_col, n_out, total;
4017 int i;
4019 if (!space || !mat)
4020 goto error;
4022 ctx = isl_mat_get_ctx(mat);
4024 n_row = isl_mat_rows(mat);
4025 n_col = isl_mat_cols(mat);
4026 n_out = isl_space_dim(space, isl_dim_out);
4027 total = isl_space_dim(space, isl_dim_all);
4028 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
4029 goto error;
4030 if (n_row < 1)
4031 isl_die(ctx, isl_error_invalid,
4032 "insufficient number of rows", goto error);
4033 if (n_col < 1)
4034 isl_die(ctx, isl_error_invalid,
4035 "insufficient number of columns", goto error);
4036 if (1 + n_out != n_row || 2 + total != n_row + n_col)
4037 isl_die(ctx, isl_error_invalid,
4038 "dimension mismatch", goto error);
4040 ma = isl_multi_aff_zero(isl_space_copy(space));
4041 space = isl_space_domain(space);
4042 ls = isl_local_space_from_space(isl_space_copy(space));
4044 for (i = 0; i < n_row - 1; ++i) {
4045 isl_vec *v;
4046 isl_aff *aff;
4048 v = isl_vec_alloc(ctx, 1 + n_col);
4049 if (!v)
4050 goto error;
4051 isl_int_set(v->el[0], mat->row[0][0]);
4052 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
4053 v = isl_vec_normalize(v);
4054 aff = isl_aff_alloc_vec_validated(isl_local_space_copy(ls), v);
4055 ma = isl_multi_aff_set_aff(ma, i, aff);
4058 isl_space_free(space);
4059 isl_local_space_free(ls);
4060 isl_mat_free(mat);
4061 return ma;
4062 error:
4063 isl_space_free(space);
4064 isl_local_space_free(ls);
4065 isl_mat_free(mat);
4066 isl_multi_aff_free(ma);
4067 return NULL;
4070 /* Return the constant terms of the affine expressions of "ma".
4072 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
4073 __isl_keep isl_multi_aff *ma)
4075 int i;
4076 isl_size n;
4077 isl_space *space;
4078 isl_multi_val *mv;
4080 n = isl_multi_aff_size(ma);
4081 if (n < 0)
4082 return NULL;
4083 space = isl_space_range(isl_multi_aff_get_space(ma));
4084 space = isl_space_drop_all_params(space);
4085 mv = isl_multi_val_zero(space);
4087 for (i = 0; i < n; ++i) {
4088 isl_aff *aff;
4089 isl_val *val;
4091 aff = isl_multi_aff_get_at(ma, i);
4092 val = isl_aff_get_constant_val(aff);
4093 isl_aff_free(aff);
4094 mv = isl_multi_val_set_at(mv, i, val);
4097 return mv;
4100 /* Remove any internal structure of the domain of "ma".
4101 * If there is any such internal structure in the input,
4102 * then the name of the corresponding space is also removed.
4104 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4105 __isl_take isl_multi_aff *ma)
4107 isl_space *space;
4109 if (!ma)
4110 return NULL;
4112 if (!ma->space->nested[0])
4113 return ma;
4115 space = isl_multi_aff_get_space(ma);
4116 space = isl_space_flatten_domain(space);
4117 ma = isl_multi_aff_reset_space(ma, space);
4119 return ma;
4122 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4123 * of the space to its domain.
4125 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4127 int i;
4128 isl_size n_in;
4129 isl_local_space *ls;
4130 isl_multi_aff *ma;
4132 if (!space)
4133 return NULL;
4134 if (!isl_space_is_map(space))
4135 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4136 "not a map space", goto error);
4138 n_in = isl_space_dim(space, isl_dim_in);
4139 if (n_in < 0)
4140 goto error;
4141 space = isl_space_domain_map(space);
4143 ma = isl_multi_aff_alloc(isl_space_copy(space));
4144 if (n_in == 0) {
4145 isl_space_free(space);
4146 return ma;
4149 space = isl_space_domain(space);
4150 ls = isl_local_space_from_space(space);
4151 for (i = 0; i < n_in; ++i) {
4152 isl_aff *aff;
4154 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4155 isl_dim_set, i);
4156 ma = isl_multi_aff_set_aff(ma, i, aff);
4158 isl_local_space_free(ls);
4159 return ma;
4160 error:
4161 isl_space_free(space);
4162 return NULL;
4165 /* This function performs the same operation as isl_multi_aff_domain_map,
4166 * but is considered as a function on an isl_space when exported.
4168 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4169 __isl_take isl_space *space)
4171 return isl_multi_aff_domain_map(space);
4174 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4175 * of the space to its range.
4177 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4179 int i;
4180 isl_size n_in, n_out;
4181 isl_local_space *ls;
4182 isl_multi_aff *ma;
4184 if (!space)
4185 return NULL;
4186 if (!isl_space_is_map(space))
4187 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4188 "not a map space", goto error);
4190 n_in = isl_space_dim(space, isl_dim_in);
4191 n_out = isl_space_dim(space, isl_dim_out);
4192 if (n_in < 0 || n_out < 0)
4193 goto error;
4194 space = isl_space_range_map(space);
4196 ma = isl_multi_aff_alloc(isl_space_copy(space));
4197 if (n_out == 0) {
4198 isl_space_free(space);
4199 return ma;
4202 space = isl_space_domain(space);
4203 ls = isl_local_space_from_space(space);
4204 for (i = 0; i < n_out; ++i) {
4205 isl_aff *aff;
4207 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4208 isl_dim_set, n_in + i);
4209 ma = isl_multi_aff_set_aff(ma, i, aff);
4211 isl_local_space_free(ls);
4212 return ma;
4213 error:
4214 isl_space_free(space);
4215 return NULL;
4218 /* This function performs the same operation as isl_multi_aff_range_map,
4219 * but is considered as a function on an isl_space when exported.
4221 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4222 __isl_take isl_space *space)
4224 return isl_multi_aff_range_map(space);
4227 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4228 * of the space to its domain.
4230 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4231 __isl_take isl_space *space)
4233 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4236 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4237 * but is considered as a function on an isl_space when exported.
4239 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4240 __isl_take isl_space *space)
4242 return isl_pw_multi_aff_domain_map(space);
4245 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4246 * of the space to its range.
4248 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4249 __isl_take isl_space *space)
4251 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4254 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4255 * but is considered as a function on an isl_space when exported.
4257 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4258 __isl_take isl_space *space)
4260 return isl_pw_multi_aff_range_map(space);
4263 /* Given the space of a set and a range of set dimensions,
4264 * construct an isl_multi_aff that projects out those dimensions.
4266 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4267 __isl_take isl_space *space, enum isl_dim_type type,
4268 unsigned first, unsigned n)
4270 int i;
4271 isl_size dim;
4272 isl_local_space *ls;
4273 isl_multi_aff *ma;
4275 if (!space)
4276 return NULL;
4277 if (!isl_space_is_set(space))
4278 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4279 "expecting set space", goto error);
4280 if (type != isl_dim_set)
4281 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4282 "only set dimensions can be projected out", goto error);
4283 if (isl_space_check_range(space, type, first, n) < 0)
4284 goto error;
4286 dim = isl_space_dim(space, isl_dim_set);
4287 if (dim < 0)
4288 goto error;
4290 space = isl_space_from_domain(space);
4291 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4293 if (dim == n)
4294 return isl_multi_aff_alloc(space);
4296 ma = isl_multi_aff_alloc(isl_space_copy(space));
4297 space = isl_space_domain(space);
4298 ls = isl_local_space_from_space(space);
4300 for (i = 0; i < first; ++i) {
4301 isl_aff *aff;
4303 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4304 isl_dim_set, i);
4305 ma = isl_multi_aff_set_aff(ma, i, aff);
4308 for (i = 0; i < dim - (first + n); ++i) {
4309 isl_aff *aff;
4311 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4312 isl_dim_set, first + n + i);
4313 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4316 isl_local_space_free(ls);
4317 return ma;
4318 error:
4319 isl_space_free(space);
4320 return NULL;
4323 /* Given the space of a set and a range of set dimensions,
4324 * construct an isl_pw_multi_aff that projects out those dimensions.
4326 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4327 __isl_take isl_space *space, enum isl_dim_type type,
4328 unsigned first, unsigned n)
4330 isl_multi_aff *ma;
4332 ma = isl_multi_aff_project_out_map(space, type, first, n);
4333 return isl_pw_multi_aff_from_multi_aff(ma);
4336 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4337 * but is considered as a function on an isl_multi_aff when exported.
4339 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4340 __isl_take isl_multi_aff *ma)
4342 return isl_pw_multi_aff_from_multi_aff(ma);
4345 /* Create a piecewise multi-affine expression in the given space that maps each
4346 * input dimension to the corresponding output dimension.
4348 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4349 __isl_take isl_space *space)
4351 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4354 /* Create a piecewise multi expression that maps elements in the given space
4355 * to themselves.
4357 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4358 __isl_take isl_space *space)
4360 isl_multi_aff *ma;
4362 ma = isl_multi_aff_identity_on_domain_space(space);
4363 return isl_pw_multi_aff_from_multi_aff(ma);
4366 /* This function performs the same operation as
4367 * isl_pw_multi_aff_identity_on_domain_space,
4368 * but is considered as a function on an isl_space when exported.
4370 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4371 __isl_take isl_space *space)
4373 return isl_pw_multi_aff_identity_on_domain_space(space);
4376 /* Exploit the equalities in "eq" to simplify the affine expressions.
4378 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4379 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4381 isl_size n;
4382 int i;
4384 n = isl_multi_aff_size(maff);
4385 if (n < 0 || !eq)
4386 goto error;
4388 for (i = 0; i < n; ++i) {
4389 isl_aff *aff;
4391 aff = isl_multi_aff_take_at(maff, i);
4392 aff = isl_aff_substitute_equalities(aff,
4393 isl_basic_set_copy(eq));
4394 maff = isl_multi_aff_restore_at(maff, i, aff);
4397 isl_basic_set_free(eq);
4398 return maff;
4399 error:
4400 isl_basic_set_free(eq);
4401 isl_multi_aff_free(maff);
4402 return NULL;
4405 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4406 isl_int f)
4408 isl_size n;
4409 int i;
4411 n = isl_multi_aff_size(maff);
4412 if (n < 0)
4413 return isl_multi_aff_free(maff);
4415 for (i = 0; i < n; ++i) {
4416 isl_aff *aff;
4418 aff = isl_multi_aff_take_at(maff, i);
4419 aff = isl_aff_scale(aff, f);
4420 maff = isl_multi_aff_restore_at(maff, i, aff);
4423 return maff;
4426 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4427 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4429 maff1 = isl_multi_aff_add(maff1, maff2);
4430 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4431 return maff1;
4434 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4436 if (!maff)
4437 return isl_bool_error;
4439 return isl_bool_false;
4442 /* Return the set of domain elements where "ma1" is lexicographically
4443 * smaller than or equal to "ma2".
4445 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4446 __isl_take isl_multi_aff *ma2)
4448 return isl_multi_aff_lex_ge_set(ma2, ma1);
4451 /* Return the set of domain elements where "ma1" is lexicographically
4452 * smaller than "ma2".
4454 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4455 __isl_take isl_multi_aff *ma2)
4457 return isl_multi_aff_lex_gt_set(ma2, ma1);
4460 /* Return the set of domain elements where "ma1" is lexicographically
4461 * greater than to "ma2". If "equal" is set, then include the domain
4462 * elements where they are equal.
4463 * Do this for the case where there are no entries.
4464 * In this case, "ma1" cannot be greater than "ma2",
4465 * but it is (greater than or) equal to "ma2".
4467 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4468 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4470 isl_space *space;
4472 space = isl_multi_aff_get_domain_space(ma1);
4474 isl_multi_aff_free(ma1);
4475 isl_multi_aff_free(ma2);
4477 if (equal)
4478 return isl_set_universe(space);
4479 else
4480 return isl_set_empty(space);
4483 /* Return the set where entry "i" of "ma1" and "ma2"
4484 * satisfy the relation prescribed by "cmp".
4486 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4487 __isl_keep isl_multi_aff *ma2, int i,
4488 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4489 __isl_take isl_aff *aff2))
4491 isl_aff *aff1, *aff2;
4493 aff1 = isl_multi_aff_get_at(ma1, i);
4494 aff2 = isl_multi_aff_get_at(ma2, i);
4495 return cmp(aff1, aff2);
4498 /* Return the set of domain elements where "ma1" is lexicographically
4499 * greater than to "ma2". If "equal" is set, then include the domain
4500 * elements where they are equal.
4502 * In particular, for all but the final entry,
4503 * include the set of elements where this entry is strictly greater in "ma1"
4504 * and all previous entries are equal.
4505 * The final entry is also allowed to be equal in the two functions
4506 * if "equal" is set.
4508 * The case where there are no entries is handled separately.
4510 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4511 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4513 int i;
4514 isl_size n;
4515 isl_space *space;
4516 isl_set *res;
4517 isl_set *equal_set;
4518 isl_set *gte;
4520 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4521 goto error;
4522 n = isl_multi_aff_size(ma1);
4523 if (n < 0)
4524 goto error;
4525 if (n == 0)
4526 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4528 space = isl_multi_aff_get_domain_space(ma1);
4529 res = isl_set_empty(isl_space_copy(space));
4530 equal_set = isl_set_universe(space);
4532 for (i = 0; i + 1 < n; ++i) {
4533 isl_bool empty;
4534 isl_set *gt, *eq;
4536 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4537 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4538 res = isl_set_union(res, gt);
4539 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4540 equal_set = isl_set_intersect(equal_set, eq);
4542 empty = isl_set_is_empty(equal_set);
4543 if (empty >= 0 && empty)
4544 break;
4547 if (equal)
4548 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4549 else
4550 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4551 isl_multi_aff_free(ma1);
4552 isl_multi_aff_free(ma2);
4554 gte = isl_set_intersect(gte, equal_set);
4555 return isl_set_union(res, gte);
4556 error:
4557 isl_multi_aff_free(ma1);
4558 isl_multi_aff_free(ma2);
4559 return NULL;
4562 /* Return the set of domain elements where "ma1" is lexicographically
4563 * greater than or equal to "ma2".
4565 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4566 __isl_take isl_multi_aff *ma2)
4568 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4571 /* Return the set of domain elements where "ma1" is lexicographically
4572 * greater than "ma2".
4574 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4575 __isl_take isl_multi_aff *ma2)
4577 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4580 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4582 #undef PW
4583 #define PW isl_pw_multi_aff
4584 #undef BASE
4585 #define BASE multi_aff
4586 #undef EL_IS_ZERO
4587 #define EL_IS_ZERO is_empty
4588 #undef ZERO
4589 #define ZERO empty
4590 #undef IS_ZERO
4591 #define IS_ZERO is_empty
4592 #undef FIELD
4593 #define FIELD maff
4594 #undef DEFAULT_IS_ZERO
4595 #define DEFAULT_IS_ZERO 0
4597 #include <isl_pw_templ.c>
4598 #include <isl_pw_un_op_templ.c>
4599 #include <isl_pw_add_constant_multi_val_templ.c>
4600 #include <isl_pw_add_constant_val_templ.c>
4601 #include <isl_pw_bind_domain_templ.c>
4602 #include <isl_pw_insert_dims_templ.c>
4603 #include <isl_pw_insert_domain_templ.c>
4604 #include <isl_pw_locals_templ.c>
4605 #include <isl_pw_move_dims_templ.c>
4606 #include <isl_pw_neg_templ.c>
4607 #include <isl_pw_pullback_templ.c>
4608 #include <isl_pw_range_tuple_id_templ.c>
4609 #include <isl_pw_union_opt.c>
4611 #undef BASE
4612 #define BASE pw_multi_aff
4614 #include <isl_union_multi.c>
4615 #include "isl_union_locals_templ.c"
4616 #include <isl_union_neg.c>
4618 #undef BASE
4619 #define BASE multi_aff
4621 #include <isl_union_pw_templ.c>
4623 /* Generic function for extracting a factor from a product "pma".
4624 * "check_space" checks that the space is that of the right kind of product.
4625 * "space_factor" extracts the factor from the space.
4626 * "multi_aff_factor" extracts the factor from the constituent functions.
4628 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4629 __isl_take isl_pw_multi_aff *pma,
4630 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4631 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4632 __isl_give isl_multi_aff *(*multi_aff_factor)(
4633 __isl_take isl_multi_aff *ma))
4635 int i;
4636 isl_space *space;
4638 if (check_space(pma) < 0)
4639 return isl_pw_multi_aff_free(pma);
4641 space = isl_pw_multi_aff_take_space(pma);
4642 space = space_factor(space);
4644 for (i = 0; pma && i < pma->n; ++i) {
4645 isl_multi_aff *ma;
4647 ma = isl_pw_multi_aff_take_base_at(pma, i);
4648 ma = multi_aff_factor(ma);
4649 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4652 pma = isl_pw_multi_aff_restore_space(pma, space);
4654 return pma;
4657 /* Is the range of "pma" a wrapped relation?
4659 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4660 __isl_keep isl_pw_multi_aff *pma)
4662 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4665 /* Check that the range of "pma" is a product.
4667 static isl_stat pw_multi_aff_check_range_product(
4668 __isl_keep isl_pw_multi_aff *pma)
4670 isl_bool wraps;
4672 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4673 if (wraps < 0)
4674 return isl_stat_error;
4675 if (!wraps)
4676 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4677 "range is not a product", return isl_stat_error);
4678 return isl_stat_ok;
4681 /* Given a function A -> [B -> C], extract the function A -> B.
4683 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4684 __isl_take isl_pw_multi_aff *pma)
4686 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4687 &isl_space_range_factor_domain,
4688 &isl_multi_aff_range_factor_domain);
4691 /* Given a function A -> [B -> C], extract the function A -> C.
4693 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4694 __isl_take isl_pw_multi_aff *pma)
4696 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4697 &isl_space_range_factor_range,
4698 &isl_multi_aff_range_factor_range);
4701 /* Given two piecewise multi affine expressions, return a piecewise
4702 * multi-affine expression defined on the union of the definition domains
4703 * of the inputs that is equal to the lexicographic maximum of the two
4704 * inputs on each cell. If only one of the two inputs is defined on
4705 * a given cell, then it is considered to be the maximum.
4707 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4708 __isl_take isl_pw_multi_aff *pma1,
4709 __isl_take isl_pw_multi_aff *pma2)
4711 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4712 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4713 &isl_multi_aff_lex_ge_set);
4716 /* Given two piecewise multi affine expressions, return a piecewise
4717 * multi-affine expression defined on the union of the definition domains
4718 * of the inputs that is equal to the lexicographic minimum of the two
4719 * inputs on each cell. If only one of the two inputs is defined on
4720 * a given cell, then it is considered to be the minimum.
4722 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4723 __isl_take isl_pw_multi_aff *pma1,
4724 __isl_take isl_pw_multi_aff *pma2)
4726 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4727 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4728 &isl_multi_aff_lex_le_set);
4731 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4732 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4734 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4735 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4736 &isl_multi_aff_add);
4739 /* Subtract "pma2" from "pma1" and return the result.
4741 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4742 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4744 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4745 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4746 &isl_multi_aff_sub);
4749 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4750 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4752 return isl_pw_multi_aff_union_add_(pma1, pma2);
4755 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4756 * with the actual sum on the shared domain and
4757 * the defined expression on the symmetric difference of the domains.
4759 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4760 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4762 return isl_union_pw_aff_union_add_(upa1, upa2);
4765 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4766 * with the actual sum on the shared domain and
4767 * the defined expression on the symmetric difference of the domains.
4769 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4770 __isl_take isl_union_pw_multi_aff *upma1,
4771 __isl_take isl_union_pw_multi_aff *upma2)
4773 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4776 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4777 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4779 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4780 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4782 int i, j, n;
4783 isl_space *space;
4784 isl_pw_multi_aff *res;
4786 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4787 goto error;
4789 n = pma1->n * pma2->n;
4790 space = isl_space_product(isl_space_copy(pma1->dim),
4791 isl_space_copy(pma2->dim));
4792 res = isl_pw_multi_aff_alloc_size(space, n);
4794 for (i = 0; i < pma1->n; ++i) {
4795 for (j = 0; j < pma2->n; ++j) {
4796 isl_set *domain;
4797 isl_multi_aff *ma;
4799 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4800 isl_set_copy(pma2->p[j].set));
4801 ma = isl_multi_aff_product(
4802 isl_multi_aff_copy(pma1->p[i].maff),
4803 isl_multi_aff_copy(pma2->p[j].maff));
4804 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4808 isl_pw_multi_aff_free(pma1);
4809 isl_pw_multi_aff_free(pma2);
4810 return res;
4811 error:
4812 isl_pw_multi_aff_free(pma1);
4813 isl_pw_multi_aff_free(pma2);
4814 return NULL;
4817 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4818 * denominator "denom".
4819 * "denom" is allowed to be negative, in which case the actual denominator
4820 * is -denom and the expressions are added instead.
4822 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4823 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4825 int i, first;
4826 int sign;
4827 isl_int d;
4829 first = isl_seq_first_non_zero(c, n);
4830 if (first == -1)
4831 return aff;
4833 sign = isl_int_sgn(denom);
4834 isl_int_init(d);
4835 isl_int_abs(d, denom);
4836 for (i = first; i < n; ++i) {
4837 isl_aff *aff_i;
4839 if (isl_int_is_zero(c[i]))
4840 continue;
4841 aff_i = isl_multi_aff_get_aff(ma, i);
4842 aff_i = isl_aff_scale(aff_i, c[i]);
4843 aff_i = isl_aff_scale_down(aff_i, d);
4844 if (sign >= 0)
4845 aff = isl_aff_sub(aff, aff_i);
4846 else
4847 aff = isl_aff_add(aff, aff_i);
4849 isl_int_clear(d);
4851 return aff;
4854 /* Extract an affine expression that expresses the output dimension "pos"
4855 * of "bmap" in terms of the parameters and input dimensions from
4856 * equality "eq".
4857 * Note that this expression may involve integer divisions defined
4858 * in terms of parameters and input dimensions.
4859 * The equality may also involve references to earlier (but not later)
4860 * output dimensions. These are replaced by the corresponding elements
4861 * in "ma".
4863 * If the equality is of the form
4865 * f(i) + h(j) + a x + g(i) = 0,
4867 * with f(i) a linear combinations of the parameters and input dimensions,
4868 * g(i) a linear combination of integer divisions defined in terms of the same
4869 * and h(j) a linear combinations of earlier output dimensions,
4870 * then the affine expression is
4872 * (-f(i) - g(i))/a - h(j)/a
4874 * If the equality is of the form
4876 * f(i) + h(j) - a x + g(i) = 0,
4878 * then the affine expression is
4880 * (f(i) + g(i))/a - h(j)/(-a)
4883 * If "div" refers to an integer division (i.e., it is smaller than
4884 * the number of integer divisions), then the equality constraint
4885 * does involve an integer division (the one at position "div") that
4886 * is defined in terms of output dimensions. However, this integer
4887 * division can be eliminated by exploiting a pair of constraints
4888 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4889 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4890 * -l + x >= 0.
4891 * In particular, let
4893 * x = e(i) + m floor(...)
4895 * with e(i) the expression derived above and floor(...) the integer
4896 * division involving output dimensions.
4897 * From
4899 * l <= x <= l + n,
4901 * we have
4903 * 0 <= x - l <= n
4905 * This means
4907 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4908 * = (e(i) - l) mod m
4910 * Therefore,
4912 * x - l = (e(i) - l) mod m
4914 * or
4916 * x = ((e(i) - l) mod m) + l
4918 * The variable "shift" below contains the expression -l, which may
4919 * also involve a linear combination of earlier output dimensions.
4921 static __isl_give isl_aff *extract_aff_from_equality(
4922 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4923 __isl_keep isl_multi_aff *ma)
4925 unsigned o_out;
4926 isl_size n_div, n_out;
4927 isl_ctx *ctx;
4928 isl_local_space *ls;
4929 isl_aff *aff, *shift;
4930 isl_val *mod;
4932 ctx = isl_basic_map_get_ctx(bmap);
4933 ls = isl_basic_map_get_local_space(bmap);
4934 ls = isl_local_space_domain(ls);
4935 aff = isl_aff_alloc(isl_local_space_copy(ls));
4936 if (!aff)
4937 goto error;
4938 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4939 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4940 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4941 if (n_out < 0 || n_div < 0)
4942 goto error;
4943 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4944 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4945 isl_seq_cpy(aff->v->el + 1 + o_out,
4946 bmap->eq[eq] + o_out + n_out, n_div);
4947 } else {
4948 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4949 isl_seq_neg(aff->v->el + 1 + o_out,
4950 bmap->eq[eq] + o_out + n_out, n_div);
4952 if (div < n_div)
4953 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4954 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4955 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4956 bmap->eq[eq][o_out + pos]);
4957 if (div < n_div) {
4958 shift = isl_aff_alloc(isl_local_space_copy(ls));
4959 if (!shift)
4960 goto error;
4961 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4962 isl_seq_cpy(shift->v->el + 1 + o_out,
4963 bmap->ineq[ineq] + o_out + n_out, n_div);
4964 isl_int_set_si(shift->v->el[0], 1);
4965 shift = subtract_initial(shift, ma, pos,
4966 bmap->ineq[ineq] + o_out, ctx->negone);
4967 aff = isl_aff_add(aff, isl_aff_copy(shift));
4968 mod = isl_val_int_from_isl_int(ctx,
4969 bmap->eq[eq][o_out + n_out + div]);
4970 mod = isl_val_abs(mod);
4971 aff = isl_aff_mod_val(aff, mod);
4972 aff = isl_aff_sub(aff, shift);
4975 isl_local_space_free(ls);
4976 return aff;
4977 error:
4978 isl_local_space_free(ls);
4979 isl_aff_free(aff);
4980 return NULL;
4983 /* Given a basic map with output dimensions defined
4984 * in terms of the parameters input dimensions and earlier
4985 * output dimensions using an equality (and possibly a pair on inequalities),
4986 * extract an isl_aff that expresses output dimension "pos" in terms
4987 * of the parameters and input dimensions.
4988 * Note that this expression may involve integer divisions defined
4989 * in terms of parameters and input dimensions.
4990 * "ma" contains the expressions corresponding to earlier output dimensions.
4992 * This function shares some similarities with
4993 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4995 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4996 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4998 int eq, div, ineq;
4999 isl_aff *aff;
5001 if (!bmap)
5002 return NULL;
5003 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
5004 if (eq >= bmap->n_eq)
5005 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5006 "unable to find suitable equality", return NULL);
5007 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
5009 aff = isl_aff_remove_unused_divs(aff);
5010 return aff;
5013 /* Given a basic map where each output dimension is defined
5014 * in terms of the parameters and input dimensions using an equality,
5015 * extract an isl_multi_aff that expresses the output dimensions in terms
5016 * of the parameters and input dimensions.
5018 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
5019 __isl_take isl_basic_map *bmap)
5021 int i;
5022 isl_size n_out;
5023 isl_multi_aff *ma;
5025 if (!bmap)
5026 return NULL;
5028 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
5029 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5030 if (n_out < 0)
5031 ma = isl_multi_aff_free(ma);
5033 for (i = 0; i < n_out; ++i) {
5034 isl_aff *aff;
5036 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
5037 ma = isl_multi_aff_set_aff(ma, i, aff);
5040 isl_basic_map_free(bmap);
5042 return ma;
5045 /* Given a basic set where each set dimension is defined
5046 * in terms of the parameters using an equality,
5047 * extract an isl_multi_aff that expresses the set dimensions in terms
5048 * of the parameters.
5050 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
5051 __isl_take isl_basic_set *bset)
5053 return extract_isl_multi_aff_from_basic_map(bset);
5056 /* Create an isl_pw_multi_aff that is equivalent to
5057 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
5058 * The given basic map is such that each output dimension is defined
5059 * in terms of the parameters and input dimensions using an equality.
5061 * Since some applications expect the result of isl_pw_multi_aff_from_map
5062 * to only contain integer affine expressions, we compute the floor
5063 * of the expression before returning.
5065 * Remove all constraints involving local variables without
5066 * an explicit representation (resulting in the removal of those
5067 * local variables) prior to the actual extraction to ensure
5068 * that the local spaces in which the resulting affine expressions
5069 * are created do not contain any unknown local variables.
5070 * Removing such constraints is safe because constraints involving
5071 * unknown local variables are not used to determine whether
5072 * a basic map is obviously single-valued.
5074 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
5075 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
5077 isl_multi_aff *ma;
5079 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
5080 ma = extract_isl_multi_aff_from_basic_map(bmap);
5081 ma = isl_multi_aff_floor(ma);
5082 return isl_pw_multi_aff_alloc(domain, ma);
5085 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5086 * This obviously only works if the input "map" is single-valued.
5087 * If so, we compute the lexicographic minimum of the image in the form
5088 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5089 * to its lexicographic minimum.
5090 * If the input is not single-valued, we produce an error.
5092 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
5093 __isl_take isl_map *map)
5095 int i;
5096 int sv;
5097 isl_pw_multi_aff *pma;
5099 sv = isl_map_is_single_valued(map);
5100 if (sv < 0)
5101 goto error;
5102 if (!sv)
5103 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5104 "map is not single-valued", goto error);
5105 map = isl_map_make_disjoint(map);
5106 if (!map)
5107 return NULL;
5109 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5111 for (i = 0; i < map->n; ++i) {
5112 isl_pw_multi_aff *pma_i;
5113 isl_basic_map *bmap;
5114 bmap = isl_basic_map_copy(map->p[i]);
5115 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5116 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5119 isl_map_free(map);
5120 return pma;
5121 error:
5122 isl_map_free(map);
5123 return NULL;
5126 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5127 * taking into account that the output dimension at position "d"
5128 * can be represented as
5130 * x = floor((e(...) + c1) / m)
5132 * given that constraint "i" is of the form
5134 * e(...) + c1 - m x >= 0
5137 * Let "map" be of the form
5139 * A -> B
5141 * We construct a mapping
5143 * A -> [A -> x = floor(...)]
5145 * apply that to the map, obtaining
5147 * [A -> x = floor(...)] -> B
5149 * and equate dimension "d" to x.
5150 * We then compute a isl_pw_multi_aff representation of the resulting map
5151 * and plug in the mapping above.
5153 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5154 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5156 isl_ctx *ctx;
5157 isl_space *space = NULL;
5158 isl_local_space *ls;
5159 isl_multi_aff *ma;
5160 isl_aff *aff;
5161 isl_vec *v;
5162 isl_map *insert;
5163 int offset;
5164 isl_size n;
5165 isl_size n_in;
5166 isl_pw_multi_aff *pma;
5167 isl_bool is_set;
5169 is_set = isl_map_is_set(map);
5170 if (is_set < 0)
5171 goto error;
5173 offset = isl_basic_map_offset(hull, isl_dim_out);
5174 ctx = isl_map_get_ctx(map);
5175 space = isl_space_domain(isl_map_get_space(map));
5176 n_in = isl_space_dim(space, isl_dim_set);
5177 n = isl_space_dim(space, isl_dim_all);
5178 if (n_in < 0 || n < 0)
5179 goto error;
5181 v = isl_vec_alloc(ctx, 1 + 1 + n);
5182 if (v) {
5183 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5184 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5186 isl_basic_map_free(hull);
5188 ls = isl_local_space_from_space(isl_space_copy(space));
5189 aff = isl_aff_alloc_vec_validated(ls, v);
5190 aff = isl_aff_floor(aff);
5191 if (is_set) {
5192 isl_space_free(space);
5193 ma = isl_multi_aff_from_aff(aff);
5194 } else {
5195 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5196 ma = isl_multi_aff_range_product(ma,
5197 isl_multi_aff_from_aff(aff));
5200 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5201 map = isl_map_apply_domain(map, insert);
5202 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5203 pma = isl_pw_multi_aff_from_map(map);
5204 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5206 return pma;
5207 error:
5208 isl_space_free(space);
5209 isl_map_free(map);
5210 isl_basic_map_free(hull);
5211 return NULL;
5214 /* Is constraint "c" of the form
5216 * e(...) + c1 - m x >= 0
5218 * or
5220 * -e(...) + c2 + m x >= 0
5222 * where m > 1 and e only depends on parameters and input dimensions?
5224 * "offset" is the offset of the output dimensions
5225 * "pos" is the position of output dimension x.
5227 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5229 if (isl_int_is_zero(c[offset + d]))
5230 return 0;
5231 if (isl_int_is_one(c[offset + d]))
5232 return 0;
5233 if (isl_int_is_negone(c[offset + d]))
5234 return 0;
5235 if (isl_seq_first_non_zero(c + offset, d) != -1)
5236 return 0;
5237 if (isl_seq_first_non_zero(c + offset + d + 1,
5238 total - (offset + d + 1)) != -1)
5239 return 0;
5240 return 1;
5243 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5245 * As a special case, we first check if there is any pair of constraints,
5246 * shared by all the basic maps in "map" that force a given dimension
5247 * to be equal to the floor of some affine combination of the input dimensions.
5249 * In particular, if we can find two constraints
5251 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5253 * and
5255 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5257 * where m > 1 and e only depends on parameters and input dimensions,
5258 * and such that
5260 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5262 * then we know that we can take
5264 * x = floor((e(...) + c1) / m)
5266 * without having to perform any computation.
5268 * Note that we know that
5270 * c1 + c2 >= 1
5272 * If c1 + c2 were 0, then we would have detected an equality during
5273 * simplification. If c1 + c2 were negative, then we would have detected
5274 * a contradiction.
5276 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5277 __isl_take isl_map *map)
5279 int d;
5280 isl_size dim;
5281 int i, j, n;
5282 int offset;
5283 isl_size total;
5284 isl_int sum;
5285 isl_basic_map *hull;
5287 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5288 dim = isl_map_dim(map, isl_dim_out);
5289 total = isl_basic_map_dim(hull, isl_dim_all);
5290 if (dim < 0 || total < 0)
5291 goto error;
5293 isl_int_init(sum);
5294 offset = isl_basic_map_offset(hull, isl_dim_out);
5295 n = hull->n_ineq;
5296 for (d = 0; d < dim; ++d) {
5297 for (i = 0; i < n; ++i) {
5298 if (!is_potential_div_constraint(hull->ineq[i],
5299 offset, d, 1 + total))
5300 continue;
5301 for (j = i + 1; j < n; ++j) {
5302 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5303 hull->ineq[j] + 1, total))
5304 continue;
5305 isl_int_add(sum, hull->ineq[i][0],
5306 hull->ineq[j][0]);
5307 if (isl_int_abs_lt(sum,
5308 hull->ineq[i][offset + d]))
5309 break;
5312 if (j >= n)
5313 continue;
5314 isl_int_clear(sum);
5315 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5316 j = i;
5317 return pw_multi_aff_from_map_div(map, hull, d, j);
5320 isl_int_clear(sum);
5321 isl_basic_map_free(hull);
5322 return pw_multi_aff_from_map_base(map);
5323 error:
5324 isl_map_free(map);
5325 isl_basic_map_free(hull);
5326 return NULL;
5329 /* Given an affine expression
5331 * [A -> B] -> f(A,B)
5333 * construct an isl_multi_aff
5335 * [A -> B] -> B'
5337 * such that dimension "d" in B' is set to "aff" and the remaining
5338 * dimensions are set equal to the corresponding dimensions in B.
5339 * "n_in" is the dimension of the space A.
5340 * "n_out" is the dimension of the space B.
5342 * If "is_set" is set, then the affine expression is of the form
5344 * [B] -> f(B)
5346 * and we construct an isl_multi_aff
5348 * B -> B'
5350 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5351 unsigned n_in, unsigned n_out, int is_set)
5353 int i;
5354 isl_multi_aff *ma;
5355 isl_space *space, *space2;
5356 isl_local_space *ls;
5358 space = isl_aff_get_domain_space(aff);
5359 ls = isl_local_space_from_space(isl_space_copy(space));
5360 space2 = isl_space_copy(space);
5361 if (!is_set)
5362 space2 = isl_space_range(isl_space_unwrap(space2));
5363 space = isl_space_map_from_domain_and_range(space, space2);
5364 ma = isl_multi_aff_alloc(space);
5365 ma = isl_multi_aff_set_aff(ma, d, aff);
5367 for (i = 0; i < n_out; ++i) {
5368 if (i == d)
5369 continue;
5370 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5371 isl_dim_set, n_in + i);
5372 ma = isl_multi_aff_set_aff(ma, i, aff);
5375 isl_local_space_free(ls);
5377 return ma;
5380 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5381 * taking into account that the dimension at position "d" can be written as
5383 * x = m a + f(..) (1)
5385 * where m is equal to "gcd".
5386 * "i" is the index of the equality in "hull" that defines f(..).
5387 * In particular, the equality is of the form
5389 * f(..) - x + m g(existentials) = 0
5391 * or
5393 * -f(..) + x + m g(existentials) = 0
5395 * We basically plug (1) into "map", resulting in a map with "a"
5396 * in the range instead of "x". The corresponding isl_pw_multi_aff
5397 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5399 * Specifically, given the input map
5401 * A -> B
5403 * We first wrap it into a set
5405 * [A -> B]
5407 * and define (1) on top of the corresponding space, resulting in "aff".
5408 * We use this to create an isl_multi_aff that maps the output position "d"
5409 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5410 * We plug this into the wrapped map, unwrap the result and compute the
5411 * corresponding isl_pw_multi_aff.
5412 * The result is an expression
5414 * A -> T(A)
5416 * We adjust that to
5418 * A -> [A -> T(A)]
5420 * so that we can plug that into "aff", after extending the latter to
5421 * a mapping
5423 * [A -> B] -> B'
5426 * If "map" is actually a set, then there is no "A" space, meaning
5427 * that we do not need to perform any wrapping, and that the result
5428 * of the recursive call is of the form
5430 * [T]
5432 * which is plugged into a mapping of the form
5434 * B -> B'
5436 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5437 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5438 isl_int gcd)
5440 isl_set *set;
5441 isl_space *space;
5442 isl_local_space *ls;
5443 isl_aff *aff;
5444 isl_multi_aff *ma;
5445 isl_pw_multi_aff *pma, *id;
5446 isl_size n_in;
5447 unsigned o_out;
5448 isl_size n_out;
5449 isl_bool is_set;
5451 is_set = isl_map_is_set(map);
5452 if (is_set < 0)
5453 goto error;
5455 n_in = isl_basic_map_dim(hull, isl_dim_in);
5456 n_out = isl_basic_map_dim(hull, isl_dim_out);
5457 if (n_in < 0 || n_out < 0)
5458 goto error;
5459 o_out = isl_basic_map_offset(hull, isl_dim_out);
5461 if (is_set)
5462 set = map;
5463 else
5464 set = isl_map_wrap(map);
5465 space = isl_space_map_from_set(isl_set_get_space(set));
5466 ma = isl_multi_aff_identity(space);
5467 ls = isl_local_space_from_space(isl_set_get_space(set));
5468 aff = isl_aff_alloc(ls);
5469 if (aff) {
5470 isl_int_set_si(aff->v->el[0], 1);
5471 if (isl_int_is_one(hull->eq[i][o_out + d]))
5472 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5473 aff->v->size - 1);
5474 else
5475 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5476 aff->v->size - 1);
5477 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5479 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5480 set = isl_set_preimage_multi_aff(set, ma);
5482 ma = range_map(aff, d, n_in, n_out, is_set);
5484 if (is_set)
5485 map = set;
5486 else
5487 map = isl_set_unwrap(set);
5488 pma = isl_pw_multi_aff_from_map(map);
5490 if (!is_set) {
5491 space = isl_pw_multi_aff_get_domain_space(pma);
5492 space = isl_space_map_from_set(space);
5493 id = isl_pw_multi_aff_identity(space);
5494 pma = isl_pw_multi_aff_range_product(id, pma);
5496 id = isl_pw_multi_aff_from_multi_aff(ma);
5497 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5499 isl_basic_map_free(hull);
5500 return pma;
5501 error:
5502 isl_map_free(map);
5503 isl_basic_map_free(hull);
5504 return NULL;
5507 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5508 * "hull" contains the equalities valid for "map".
5510 * Check if any of the output dimensions is "strided".
5511 * That is, we check if it can be written as
5513 * x = m a + f(..)
5515 * with m greater than 1, a some combination of existentially quantified
5516 * variables and f an expression in the parameters and input dimensions.
5517 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5519 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5520 * special case.
5522 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5523 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5525 int i, j;
5526 isl_size n_out;
5527 unsigned o_out;
5528 isl_size n_div;
5529 unsigned o_div;
5530 isl_int gcd;
5532 n_div = isl_basic_map_dim(hull, isl_dim_div);
5533 n_out = isl_basic_map_dim(hull, isl_dim_out);
5534 if (n_div < 0 || n_out < 0)
5535 goto error;
5537 if (n_div == 0) {
5538 isl_basic_map_free(hull);
5539 return pw_multi_aff_from_map_check_div(map);
5542 isl_int_init(gcd);
5544 o_div = isl_basic_map_offset(hull, isl_dim_div);
5545 o_out = isl_basic_map_offset(hull, isl_dim_out);
5547 for (i = 0; i < n_out; ++i) {
5548 for (j = 0; j < hull->n_eq; ++j) {
5549 isl_int *eq = hull->eq[j];
5550 isl_pw_multi_aff *res;
5552 if (!isl_int_is_one(eq[o_out + i]) &&
5553 !isl_int_is_negone(eq[o_out + i]))
5554 continue;
5555 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5556 continue;
5557 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5558 n_out - (i + 1)) != -1)
5559 continue;
5560 isl_seq_gcd(eq + o_div, n_div, &gcd);
5561 if (isl_int_is_zero(gcd))
5562 continue;
5563 if (isl_int_is_one(gcd))
5564 continue;
5566 res = pw_multi_aff_from_map_stride(map, hull,
5567 i, j, gcd);
5568 isl_int_clear(gcd);
5569 return res;
5573 isl_int_clear(gcd);
5574 isl_basic_map_free(hull);
5575 return pw_multi_aff_from_map_check_div(map);
5576 error:
5577 isl_map_free(map);
5578 isl_basic_map_free(hull);
5579 return NULL;
5582 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5584 * As a special case, we first check if all output dimensions are uniquely
5585 * defined in terms of the parameters and input dimensions over the entire
5586 * domain. If so, we extract the desired isl_pw_multi_aff directly
5587 * from the affine hull of "map" and its domain.
5589 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5590 * special cases.
5592 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5594 isl_bool sv;
5595 isl_size n;
5596 isl_basic_map *hull;
5598 n = isl_map_n_basic_map(map);
5599 if (n < 0)
5600 goto error;
5602 if (n == 1) {
5603 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5604 hull = isl_basic_map_plain_affine_hull(hull);
5605 sv = isl_basic_map_plain_is_single_valued(hull);
5606 if (sv >= 0 && sv)
5607 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5608 hull);
5609 isl_basic_map_free(hull);
5611 map = isl_map_detect_equalities(map);
5612 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5613 sv = isl_basic_map_plain_is_single_valued(hull);
5614 if (sv >= 0 && sv)
5615 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5616 if (sv >= 0)
5617 return pw_multi_aff_from_map_check_strides(map, hull);
5618 isl_basic_map_free(hull);
5619 error:
5620 isl_map_free(map);
5621 return NULL;
5624 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5625 * but is considered as a function on an isl_map when exported.
5627 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5629 return isl_pw_multi_aff_from_map(map);
5632 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5634 return isl_pw_multi_aff_from_map(set);
5637 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5638 * but is considered as a function on an isl_set when exported.
5640 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5642 return isl_pw_multi_aff_from_set(set);
5645 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5646 * add it to *user.
5648 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5650 isl_union_pw_multi_aff **upma = user;
5651 isl_pw_multi_aff *pma;
5653 pma = isl_pw_multi_aff_from_map(map);
5654 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5656 return *upma ? isl_stat_ok : isl_stat_error;
5659 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5660 * domain.
5662 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5663 __isl_take isl_aff *aff)
5665 isl_multi_aff *ma;
5666 isl_pw_multi_aff *pma;
5668 ma = isl_multi_aff_from_aff(aff);
5669 pma = isl_pw_multi_aff_from_multi_aff(ma);
5670 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5673 /* Try and create an isl_union_pw_multi_aff that is equivalent
5674 * to the given isl_union_map.
5675 * The isl_union_map is required to be single-valued in each space.
5676 * Otherwise, an error is produced.
5678 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5679 __isl_take isl_union_map *umap)
5681 isl_space *space;
5682 isl_union_pw_multi_aff *upma;
5684 space = isl_union_map_get_space(umap);
5685 upma = isl_union_pw_multi_aff_empty(space);
5686 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5687 upma = isl_union_pw_multi_aff_free(upma);
5688 isl_union_map_free(umap);
5690 return upma;
5693 /* This function performs the same operation as
5694 * isl_union_pw_multi_aff_from_union_map,
5695 * but is considered as a function on an isl_union_map when exported.
5697 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5698 __isl_take isl_union_map *umap)
5700 return isl_union_pw_multi_aff_from_union_map(umap);
5703 /* Try and create an isl_union_pw_multi_aff that is equivalent
5704 * to the given isl_union_set.
5705 * The isl_union_set is required to be a singleton in each space.
5706 * Otherwise, an error is produced.
5708 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5709 __isl_take isl_union_set *uset)
5711 return isl_union_pw_multi_aff_from_union_map(uset);
5714 /* Return the piecewise affine expression "set ? 1 : 0".
5716 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5718 isl_pw_aff *pa;
5719 isl_space *space = isl_set_get_space(set);
5720 isl_local_space *ls = isl_local_space_from_space(space);
5721 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5722 isl_aff *one = isl_aff_zero_on_domain(ls);
5724 one = isl_aff_add_constant_si(one, 1);
5725 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5726 set = isl_set_complement(set);
5727 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5729 return pa;
5732 /* Plug in "subs" for dimension "type", "pos" of "aff".
5734 * Let i be the dimension to replace and let "subs" be of the form
5736 * f/d
5738 * and "aff" of the form
5740 * (a i + g)/m
5742 * The result is
5744 * (a f + d g')/(m d)
5746 * where g' is the result of plugging in "subs" in each of the integer
5747 * divisions in g.
5749 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5750 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5752 isl_ctx *ctx;
5753 isl_int v;
5754 isl_size n_div;
5756 aff = isl_aff_cow(aff);
5757 if (!aff || !subs)
5758 return isl_aff_free(aff);
5760 ctx = isl_aff_get_ctx(aff);
5761 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5762 isl_die(ctx, isl_error_invalid,
5763 "spaces don't match", return isl_aff_free(aff));
5764 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5765 if (n_div < 0)
5766 return isl_aff_free(aff);
5767 if (n_div != 0)
5768 isl_die(ctx, isl_error_unsupported,
5769 "cannot handle divs yet", return isl_aff_free(aff));
5771 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5772 if (!aff->ls)
5773 return isl_aff_free(aff);
5775 aff->v = isl_vec_cow(aff->v);
5776 if (!aff->v)
5777 return isl_aff_free(aff);
5779 pos += isl_local_space_offset(aff->ls, type);
5781 isl_int_init(v);
5782 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5783 aff->v->size, subs->v->size, v);
5784 isl_int_clear(v);
5786 return aff;
5789 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5790 * expressions in "maff".
5792 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5793 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5794 __isl_keep isl_aff *subs)
5796 isl_size n;
5797 int i;
5799 n = isl_multi_aff_size(maff);
5800 if (n < 0 || !subs)
5801 return isl_multi_aff_free(maff);
5803 if (type == isl_dim_in)
5804 type = isl_dim_set;
5806 for (i = 0; i < n; ++i) {
5807 isl_aff *aff;
5809 aff = isl_multi_aff_take_at(maff, i);
5810 aff = isl_aff_substitute(aff, type, pos, subs);
5811 maff = isl_multi_aff_restore_at(maff, i, aff);
5814 return maff;
5817 /* Plug in "subs" for input dimension "pos" of "pma".
5819 * pma is of the form
5821 * A_i(v) -> M_i(v)
5823 * while subs is of the form
5825 * v' = B_j(v) -> S_j
5827 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5828 * has a contribution in the result, in particular
5830 * C_ij(S_j) -> M_i(S_j)
5832 * Note that plugging in S_j in C_ij may also result in an empty set
5833 * and this contribution should simply be discarded.
5835 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5836 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5837 __isl_keep isl_pw_aff *subs)
5839 int i, j, n;
5840 isl_pw_multi_aff *res;
5842 if (!pma || !subs)
5843 return isl_pw_multi_aff_free(pma);
5845 n = pma->n * subs->n;
5846 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5848 for (i = 0; i < pma->n; ++i) {
5849 for (j = 0; j < subs->n; ++j) {
5850 isl_set *common;
5851 isl_multi_aff *res_ij;
5852 int empty;
5854 common = isl_set_intersect(
5855 isl_set_copy(pma->p[i].set),
5856 isl_set_copy(subs->p[j].set));
5857 common = isl_set_substitute(common,
5858 pos, subs->p[j].aff);
5859 empty = isl_set_plain_is_empty(common);
5860 if (empty < 0 || empty) {
5861 isl_set_free(common);
5862 if (empty < 0)
5863 goto error;
5864 continue;
5867 res_ij = isl_multi_aff_substitute(
5868 isl_multi_aff_copy(pma->p[i].maff),
5869 isl_dim_in, pos, subs->p[j].aff);
5871 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5875 isl_pw_multi_aff_free(pma);
5876 return res;
5877 error:
5878 isl_pw_multi_aff_free(pma);
5879 isl_pw_multi_aff_free(res);
5880 return NULL;
5883 /* Compute the preimage of a range of dimensions in the affine expression "src"
5884 * under "ma" and put the result in "dst". The number of dimensions in "src"
5885 * that precede the range is given by "n_before". The number of dimensions
5886 * in the range is given by the number of output dimensions of "ma".
5887 * The number of dimensions that follow the range is given by "n_after".
5888 * If "has_denom" is set (to one),
5889 * then "src" and "dst" have an extra initial denominator.
5890 * "n_div_ma" is the number of existentials in "ma"
5891 * "n_div_bset" is the number of existentials in "src"
5892 * The resulting "dst" (which is assumed to have been allocated by
5893 * the caller) contains coefficients for both sets of existentials,
5894 * first those in "ma" and then those in "src".
5895 * f, c1, c2 and g are temporary objects that have been initialized
5896 * by the caller.
5898 * Let src represent the expression
5900 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5902 * and let ma represent the expressions
5904 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5906 * We start out with the following expression for dst:
5908 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5910 * with the multiplication factor f initially equal to 1
5911 * and f \sum_i b_i v_i kept separately.
5912 * For each x_i that we substitute, we multiply the numerator
5913 * (and denominator) of dst by c_1 = m_i and add the numerator
5914 * of the x_i expression multiplied by c_2 = f b_i,
5915 * after removing the common factors of c_1 and c_2.
5916 * The multiplication factor f also needs to be multiplied by c_1
5917 * for the next x_j, j > i.
5919 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5920 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5921 int n_div_ma, int n_div_bmap,
5922 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5924 int i;
5925 isl_size n_param, n_in, n_out;
5926 int o_dst, o_src;
5928 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5929 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5930 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5931 if (n_param < 0 || n_in < 0 || n_out < 0)
5932 return isl_stat_error;
5934 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5935 o_dst = o_src = has_denom + 1 + n_param + n_before;
5936 isl_seq_clr(dst + o_dst, n_in);
5937 o_dst += n_in;
5938 o_src += n_out;
5939 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5940 o_dst += n_after;
5941 o_src += n_after;
5942 isl_seq_clr(dst + o_dst, n_div_ma);
5943 o_dst += n_div_ma;
5944 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5946 isl_int_set_si(f, 1);
5948 for (i = 0; i < n_out; ++i) {
5949 int offset = has_denom + 1 + n_param + n_before + i;
5951 if (isl_int_is_zero(src[offset]))
5952 continue;
5953 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5954 isl_int_mul(c2, f, src[offset]);
5955 isl_int_gcd(g, c1, c2);
5956 isl_int_divexact(c1, c1, g);
5957 isl_int_divexact(c2, c2, g);
5959 isl_int_mul(f, f, c1);
5960 o_dst = has_denom;
5961 o_src = 1;
5962 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5963 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5964 o_dst += 1 + n_param;
5965 o_src += 1 + n_param;
5966 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5967 o_dst += n_before;
5968 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5969 c2, ma->u.p[i]->v->el + o_src, n_in);
5970 o_dst += n_in;
5971 o_src += n_in;
5972 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5973 o_dst += n_after;
5974 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5975 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5976 o_dst += n_div_ma;
5977 o_src += n_div_ma;
5978 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5979 if (has_denom)
5980 isl_int_mul(dst[0], dst[0], c1);
5983 return isl_stat_ok;
5986 /* Compute the pullback of "aff" by the function represented by "ma".
5987 * In other words, plug in "ma" in "aff". The result is an affine expression
5988 * defined over the domain space of "ma".
5990 * If "aff" is represented by
5992 * (a(p) + b x + c(divs))/d
5994 * and ma is represented by
5996 * x = D(p) + F(y) + G(divs')
5998 * then the result is
6000 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
6002 * The divs in the local space of the input are similarly adjusted
6003 * through a call to isl_local_space_preimage_multi_aff.
6005 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
6006 __isl_take isl_multi_aff *ma)
6008 isl_aff *res = NULL;
6009 isl_local_space *ls;
6010 isl_size n_div_aff, n_div_ma;
6011 isl_int f, c1, c2, g;
6013 ma = isl_multi_aff_align_divs(ma);
6014 if (!aff || !ma)
6015 goto error;
6017 n_div_aff = isl_aff_dim(aff, isl_dim_div);
6018 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
6019 if (n_div_aff < 0 || n_div_ma < 0)
6020 goto error;
6022 ls = isl_aff_get_domain_local_space(aff);
6023 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
6024 res = isl_aff_alloc(ls);
6025 if (!res)
6026 goto error;
6028 isl_int_init(f);
6029 isl_int_init(c1);
6030 isl_int_init(c2);
6031 isl_int_init(g);
6033 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
6034 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
6035 res = isl_aff_free(res);
6037 isl_int_clear(f);
6038 isl_int_clear(c1);
6039 isl_int_clear(c2);
6040 isl_int_clear(g);
6042 isl_aff_free(aff);
6043 isl_multi_aff_free(ma);
6044 res = isl_aff_normalize(res);
6045 return res;
6046 error:
6047 isl_aff_free(aff);
6048 isl_multi_aff_free(ma);
6049 isl_aff_free(res);
6050 return NULL;
6053 /* Compute the pullback of "aff1" by the function represented by "aff2".
6054 * In other words, plug in "aff2" in "aff1". The result is an affine expression
6055 * defined over the domain space of "aff1".
6057 * The domain of "aff1" should match the range of "aff2", which means
6058 * that it should be single-dimensional.
6060 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
6061 __isl_take isl_aff *aff2)
6063 isl_multi_aff *ma;
6065 ma = isl_multi_aff_from_aff(aff2);
6066 return isl_aff_pullback_multi_aff(aff1, ma);
6069 /* Compute the pullback of "ma1" by the function represented by "ma2".
6070 * In other words, plug in "ma2" in "ma1".
6072 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
6073 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
6075 int i;
6076 isl_size n;
6077 isl_space *space = NULL;
6079 isl_multi_aff_align_params_bin(&ma1, &ma2);
6080 ma2 = isl_multi_aff_align_divs(ma2);
6081 n = isl_multi_aff_size(ma1);
6082 if (n < 0 || !ma2)
6083 goto error;
6085 space = isl_space_join(isl_multi_aff_get_space(ma2),
6086 isl_multi_aff_get_space(ma1));
6088 for (i = 0; i < n; ++i) {
6089 isl_aff *aff;
6091 aff = isl_multi_aff_take_at(ma1, i);
6092 aff = isl_aff_pullback_multi_aff(aff, isl_multi_aff_copy(ma2));
6093 ma1 = isl_multi_aff_restore_at(ma1, i, aff);
6096 ma1 = isl_multi_aff_reset_space(ma1, space);
6097 isl_multi_aff_free(ma2);
6098 return ma1;
6099 error:
6100 isl_space_free(space);
6101 isl_multi_aff_free(ma2);
6102 isl_multi_aff_free(ma1);
6103 return NULL;
6106 /* Extend the local space of "dst" to include the divs
6107 * in the local space of "src".
6109 * If "src" does not have any divs or if the local spaces of "dst" and
6110 * "src" are the same, then no extension is required.
6112 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6113 __isl_keep isl_aff *src)
6115 isl_ctx *ctx;
6116 isl_size src_n_div, dst_n_div;
6117 int *exp1 = NULL;
6118 int *exp2 = NULL;
6119 isl_bool equal;
6120 isl_mat *div;
6122 if (!src || !dst)
6123 return isl_aff_free(dst);
6125 ctx = isl_aff_get_ctx(src);
6126 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6127 if (equal < 0)
6128 return isl_aff_free(dst);
6129 if (!equal)
6130 isl_die(ctx, isl_error_invalid,
6131 "spaces don't match", goto error);
6133 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6134 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6135 if (src_n_div == 0)
6136 return dst;
6137 equal = isl_local_space_is_equal(src->ls, dst->ls);
6138 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6139 return isl_aff_free(dst);
6140 if (equal)
6141 return dst;
6143 exp1 = isl_alloc_array(ctx, int, src_n_div);
6144 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6145 if (!exp1 || (dst_n_div && !exp2))
6146 goto error;
6148 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6149 dst = isl_aff_expand_divs(dst, div, exp2);
6150 free(exp1);
6151 free(exp2);
6153 return dst;
6154 error:
6155 free(exp1);
6156 free(exp2);
6157 return isl_aff_free(dst);
6160 /* Adjust the local spaces of the affine expressions in "maff"
6161 * such that they all have the save divs.
6163 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6164 __isl_take isl_multi_aff *maff)
6166 isl_aff *aff_0;
6167 isl_size n;
6168 int i;
6170 n = isl_multi_aff_size(maff);
6171 if (n < 0)
6172 return isl_multi_aff_free(maff);
6173 if (n <= 1)
6174 return maff;
6176 aff_0 = isl_multi_aff_take_at(maff, 0);
6177 for (i = 1; i < n; ++i) {
6178 isl_aff *aff_i;
6180 aff_i = isl_multi_aff_peek_at(maff, i);
6181 aff_0 = isl_aff_align_divs(aff_0, aff_i);
6183 maff = isl_multi_aff_restore_at(maff, 0, aff_0);
6185 aff_0 = isl_multi_aff_peek_at(maff, 0);
6186 for (i = 1; i < n; ++i) {
6187 isl_aff *aff_i;
6189 aff_i = isl_multi_aff_take_at(maff, i);
6190 aff_i = isl_aff_align_divs(aff_i, aff_0);
6191 maff = isl_multi_aff_restore_at(maff, i, aff_i);
6194 return maff;
6197 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6199 aff = isl_aff_cow(aff);
6200 if (!aff)
6201 return NULL;
6203 aff->ls = isl_local_space_lift(aff->ls);
6204 if (!aff->ls)
6205 return isl_aff_free(aff);
6207 return aff;
6210 /* Lift "maff" to a space with extra dimensions such that the result
6211 * has no more existentially quantified variables.
6212 * If "ls" is not NULL, then *ls is assigned the local space that lies
6213 * at the basis of the lifting applied to "maff".
6215 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6216 __isl_give isl_local_space **ls)
6218 int i;
6219 isl_space *space;
6220 isl_aff *aff;
6221 isl_size n, n_div;
6223 if (ls)
6224 *ls = NULL;
6226 n = isl_multi_aff_size(maff);
6227 if (n < 0)
6228 return isl_multi_aff_free(maff);
6230 if (n == 0) {
6231 if (ls) {
6232 isl_space *space = isl_multi_aff_get_domain_space(maff);
6233 *ls = isl_local_space_from_space(space);
6234 if (!*ls)
6235 return isl_multi_aff_free(maff);
6237 return maff;
6240 maff = isl_multi_aff_align_divs(maff);
6242 aff = isl_multi_aff_peek_at(maff, 0);
6243 n_div = isl_aff_dim(aff, isl_dim_div);
6244 if (n_div < 0)
6245 return isl_multi_aff_free(maff);
6246 space = isl_multi_aff_get_space(maff);
6247 space = isl_space_lift(isl_space_domain(space), n_div);
6248 space = isl_space_extend_domain_with_range(space,
6249 isl_multi_aff_get_space(maff));
6250 maff = isl_multi_aff_restore_space(maff, space);
6252 if (ls) {
6253 aff = isl_multi_aff_peek_at(maff, 0);
6254 *ls = isl_aff_get_domain_local_space(aff);
6255 if (!*ls)
6256 return isl_multi_aff_free(maff);
6259 for (i = 0; i < n; ++i) {
6260 aff = isl_multi_aff_take_at(maff, i);
6261 aff = isl_aff_lift(aff);
6262 maff = isl_multi_aff_restore_at(maff, i, aff);
6265 return maff;
6268 #undef TYPE
6269 #define TYPE isl_pw_multi_aff
6270 static
6271 #include "check_type_range_templ.c"
6273 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6275 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6276 __isl_keep isl_pw_multi_aff *pma, int pos)
6278 int i;
6279 isl_size n_out;
6280 isl_space *space;
6281 isl_pw_aff *pa;
6283 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6284 return NULL;
6286 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6287 if (n_out < 0)
6288 return NULL;
6290 space = isl_pw_multi_aff_get_space(pma);
6291 space = isl_space_drop_dims(space, isl_dim_out,
6292 pos + 1, n_out - pos - 1);
6293 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6295 pa = isl_pw_aff_alloc_size(space, pma->n);
6296 for (i = 0; i < pma->n; ++i) {
6297 isl_aff *aff;
6298 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6299 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6302 return pa;
6305 /* This is an alternative name for the function above.
6307 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6308 __isl_keep isl_pw_multi_aff *pma, int pos)
6310 return isl_pw_multi_aff_get_at(pma, pos);
6313 /* Return an isl_pw_multi_aff with the given "set" as domain and
6314 * an unnamed zero-dimensional range.
6316 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6317 __isl_take isl_set *set)
6319 isl_multi_aff *ma;
6320 isl_space *space;
6322 space = isl_set_get_space(set);
6323 space = isl_space_from_domain(space);
6324 ma = isl_multi_aff_zero(space);
6325 return isl_pw_multi_aff_alloc(set, ma);
6328 /* Add an isl_pw_multi_aff with the given "set" as domain and
6329 * an unnamed zero-dimensional range to *user.
6331 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6332 void *user)
6334 isl_union_pw_multi_aff **upma = user;
6335 isl_pw_multi_aff *pma;
6337 pma = isl_pw_multi_aff_from_domain(set);
6338 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6340 return isl_stat_ok;
6343 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6344 * an unnamed zero-dimensional range.
6346 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6347 __isl_take isl_union_set *uset)
6349 isl_space *space;
6350 isl_union_pw_multi_aff *upma;
6352 if (!uset)
6353 return NULL;
6355 space = isl_union_set_get_space(uset);
6356 upma = isl_union_pw_multi_aff_empty(space);
6358 if (isl_union_set_foreach_set(uset,
6359 &add_pw_multi_aff_from_domain, &upma) < 0)
6360 goto error;
6362 isl_union_set_free(uset);
6363 return upma;
6364 error:
6365 isl_union_set_free(uset);
6366 isl_union_pw_multi_aff_free(upma);
6367 return NULL;
6370 /* Local data for bin_entry and the callback "fn".
6372 struct isl_union_pw_multi_aff_bin_data {
6373 isl_union_pw_multi_aff *upma2;
6374 isl_union_pw_multi_aff *res;
6375 isl_pw_multi_aff *pma;
6376 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6379 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6380 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6382 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6384 struct isl_union_pw_multi_aff_bin_data *data = user;
6385 isl_stat r;
6387 data->pma = pma;
6388 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6389 data->fn, data);
6390 isl_pw_multi_aff_free(pma);
6392 return r;
6395 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6396 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6397 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6398 * as *entry. The callback should adjust data->res if desired.
6400 static __isl_give isl_union_pw_multi_aff *bin_op(
6401 __isl_take isl_union_pw_multi_aff *upma1,
6402 __isl_take isl_union_pw_multi_aff *upma2,
6403 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6405 isl_space *space;
6406 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6408 space = isl_union_pw_multi_aff_get_space(upma2);
6409 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6410 space = isl_union_pw_multi_aff_get_space(upma1);
6411 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6413 if (!upma1 || !upma2)
6414 goto error;
6416 data.upma2 = upma2;
6417 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6418 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6419 &bin_entry, &data) < 0)
6420 goto error;
6422 isl_union_pw_multi_aff_free(upma1);
6423 isl_union_pw_multi_aff_free(upma2);
6424 return data.res;
6425 error:
6426 isl_union_pw_multi_aff_free(upma1);
6427 isl_union_pw_multi_aff_free(upma2);
6428 isl_union_pw_multi_aff_free(data.res);
6429 return NULL;
6432 /* Given two isl_pw_multi_affs A -> B and C -> D,
6433 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6435 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6436 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6438 isl_space *space;
6440 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6441 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6442 isl_pw_multi_aff_get_space(pma2));
6443 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6444 &isl_multi_aff_range_product);
6447 /* Given two isl_pw_multi_affs A -> B and C -> D,
6448 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6450 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6451 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6453 isl_space *space;
6455 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6456 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6457 isl_pw_multi_aff_get_space(pma2));
6458 space = isl_space_flatten_range(space);
6459 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6460 &isl_multi_aff_flat_range_product);
6463 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6464 * to compute some form of range product and add the result to data->res.
6466 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6467 __isl_give isl_pw_multi_aff *(*range_product)(
6468 __isl_take isl_pw_multi_aff *pma1,
6469 __isl_take isl_pw_multi_aff *pma2),
6470 void *user)
6472 struct isl_union_pw_multi_aff_bin_data *data = user;
6473 isl_bool match;
6474 isl_space *space1, *space2;
6476 space1 = isl_pw_multi_aff_peek_space(data->pma);
6477 space2 = isl_pw_multi_aff_peek_space(pma2);
6478 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6479 space2, isl_dim_in);
6480 if (match < 0 || !match) {
6481 isl_pw_multi_aff_free(pma2);
6482 return match < 0 ? isl_stat_error : isl_stat_ok;
6485 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6487 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6489 return isl_stat_ok;
6492 /* If data->pma and "pma2" have the same domain space, then compute
6493 * their flat range product and add the result to data->res.
6495 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6496 void *user)
6498 return gen_range_product_entry(pma2,
6499 &isl_pw_multi_aff_flat_range_product, user);
6502 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6503 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6505 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6506 __isl_take isl_union_pw_multi_aff *upma1,
6507 __isl_take isl_union_pw_multi_aff *upma2)
6509 return bin_op(upma1, upma2, &flat_range_product_entry);
6512 /* If data->pma and "pma2" have the same domain space, then compute
6513 * their range product and add the result to data->res.
6515 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6516 void *user)
6518 return gen_range_product_entry(pma2,
6519 &isl_pw_multi_aff_range_product, user);
6522 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6523 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6525 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6526 __isl_take isl_union_pw_multi_aff *upma1,
6527 __isl_take isl_union_pw_multi_aff *upma2)
6529 return bin_op(upma1, upma2, &range_product_entry);
6532 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6533 * The parameters are assumed to have been aligned.
6535 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6536 * except that it works on two different isl_pw_* types.
6538 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6539 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6540 __isl_take isl_pw_aff *pa)
6542 int i, j, n;
6543 isl_pw_multi_aff *res = NULL;
6545 if (!pma || !pa)
6546 goto error;
6548 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6549 pa->dim, isl_dim_in))
6550 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6551 "domains don't match", goto error);
6552 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6553 goto error;
6555 n = pma->n * pa->n;
6556 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6558 for (i = 0; i < pma->n; ++i) {
6559 for (j = 0; j < pa->n; ++j) {
6560 isl_set *common;
6561 isl_multi_aff *res_ij;
6562 int empty;
6564 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6565 isl_set_copy(pa->p[j].set));
6566 empty = isl_set_plain_is_empty(common);
6567 if (empty < 0 || empty) {
6568 isl_set_free(common);
6569 if (empty < 0)
6570 goto error;
6571 continue;
6574 res_ij = isl_multi_aff_set_aff(
6575 isl_multi_aff_copy(pma->p[i].maff), pos,
6576 isl_aff_copy(pa->p[j].aff));
6577 res_ij = isl_multi_aff_gist(res_ij,
6578 isl_set_copy(common));
6580 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6584 isl_pw_multi_aff_free(pma);
6585 isl_pw_aff_free(pa);
6586 return res;
6587 error:
6588 isl_pw_multi_aff_free(pma);
6589 isl_pw_aff_free(pa);
6590 return isl_pw_multi_aff_free(res);
6593 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6595 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6596 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6597 __isl_take isl_pw_aff *pa)
6599 isl_bool equal_params;
6601 if (!pma || !pa)
6602 goto error;
6603 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6604 if (equal_params < 0)
6605 goto error;
6606 if (equal_params)
6607 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6608 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6609 isl_pw_aff_check_named_params(pa) < 0)
6610 goto error;
6611 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6612 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6613 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6614 error:
6615 isl_pw_multi_aff_free(pma);
6616 isl_pw_aff_free(pa);
6617 return NULL;
6620 /* Do the parameters of "pa" match those of "space"?
6622 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6623 __isl_keep isl_space *space)
6625 isl_space *pa_space;
6626 isl_bool match;
6628 if (!pa || !space)
6629 return isl_bool_error;
6631 pa_space = isl_pw_aff_get_space(pa);
6633 match = isl_space_has_equal_params(space, pa_space);
6635 isl_space_free(pa_space);
6636 return match;
6639 /* Check that the domain space of "pa" matches "space".
6641 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6642 __isl_keep isl_space *space)
6644 isl_space *pa_space;
6645 isl_bool match;
6647 if (!pa || !space)
6648 return isl_stat_error;
6650 pa_space = isl_pw_aff_get_space(pa);
6652 match = isl_space_has_equal_params(space, pa_space);
6653 if (match < 0)
6654 goto error;
6655 if (!match)
6656 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6657 "parameters don't match", goto error);
6658 match = isl_space_tuple_is_equal(space, isl_dim_in,
6659 pa_space, isl_dim_in);
6660 if (match < 0)
6661 goto error;
6662 if (!match)
6663 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6664 "domains don't match", goto error);
6665 isl_space_free(pa_space);
6666 return isl_stat_ok;
6667 error:
6668 isl_space_free(pa_space);
6669 return isl_stat_error;
6672 #undef BASE
6673 #define BASE pw_aff
6674 #undef DOMBASE
6675 #define DOMBASE set
6677 #include <isl_multi_explicit_domain.c>
6678 #include <isl_multi_pw_aff_explicit_domain.c>
6679 #include <isl_multi_templ.c>
6680 #include <isl_multi_un_op_templ.c>
6681 #include <isl_multi_bin_val_templ.c>
6682 #include <isl_multi_add_constant_templ.c>
6683 #include <isl_multi_apply_set.c>
6684 #include <isl_multi_arith_templ.c>
6685 #include <isl_multi_bind_templ.c>
6686 #include <isl_multi_bind_domain_templ.c>
6687 #include <isl_multi_coalesce.c>
6688 #include <isl_multi_domain_templ.c>
6689 #include <isl_multi_dim_id_templ.c>
6690 #include <isl_multi_dims.c>
6691 #include <isl_multi_from_base_templ.c>
6692 #include <isl_multi_gist.c>
6693 #include <isl_multi_hash.c>
6694 #include <isl_multi_identity_templ.c>
6695 #include <isl_multi_align_set.c>
6696 #include <isl_multi_insert_domain_templ.c>
6697 #include <isl_multi_intersect.c>
6698 #include <isl_multi_min_max_templ.c>
6699 #include <isl_multi_move_dims_templ.c>
6700 #include <isl_multi_nan_templ.c>
6701 #include <isl_multi_param_templ.c>
6702 #include <isl_multi_product_templ.c>
6703 #include <isl_multi_splice_templ.c>
6704 #include <isl_multi_tuple_id_templ.c>
6705 #include <isl_multi_union_add_templ.c>
6706 #include <isl_multi_zero_templ.c>
6707 #include <isl_multi_unbind_params_templ.c>
6709 /* Is every element of "mpa" defined over a single universe domain?
6711 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6713 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6716 /* Given that every element of "mpa" is defined over a single universe domain,
6717 * return the corresponding base expressions.
6719 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6720 __isl_take isl_multi_pw_aff *mpa)
6722 int i;
6723 isl_size n;
6724 isl_multi_aff *ma;
6726 n = isl_multi_pw_aff_size(mpa);
6727 if (n < 0)
6728 mpa = isl_multi_pw_aff_free(mpa);
6729 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6730 for (i = 0; i < n; ++i) {
6731 isl_aff *aff;
6733 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6734 ma = isl_multi_aff_set_aff(ma, i, aff);
6736 isl_multi_pw_aff_free(mpa);
6737 return ma;
6740 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6741 * with this explicit domain.
6743 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6744 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6746 isl_set *dom;
6748 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6749 return map;
6751 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6752 map = isl_map_intersect_domain(map, dom);
6754 return map;
6757 /* Are all elements of "mpa" piecewise constants?
6759 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6761 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6764 /* Does "mpa" have a non-trivial explicit domain?
6766 * The explicit domain, if present, is trivial if it represents
6767 * an (obviously) universe set.
6769 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6770 __isl_keep isl_multi_pw_aff *mpa)
6772 if (!mpa)
6773 return isl_bool_error;
6774 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6775 return isl_bool_false;
6776 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6779 #undef BASE
6780 #define BASE set
6782 #include "isl_opt_mpa_templ.c"
6784 /* Compute the minima of the set dimensions as a function of the
6785 * parameters, but independently of the other set dimensions.
6787 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6789 return set_opt_mpa(set, &isl_set_dim_min);
6792 /* Compute the maxima of the set dimensions as a function of the
6793 * parameters, but independently of the other set dimensions.
6795 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6797 return set_opt_mpa(set, &isl_set_dim_max);
6800 #undef BASE
6801 #define BASE map
6803 #include "isl_opt_mpa_templ.c"
6805 /* Compute the minima of the output dimensions as a function of the
6806 * parameters and input dimensions, but independently of
6807 * the other output dimensions.
6809 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6811 return map_opt_mpa(map, &isl_map_dim_min);
6814 /* Compute the maxima of the output dimensions as a function of the
6815 * parameters and input dimensions, but independently of
6816 * the other output dimensions.
6818 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6820 return map_opt_mpa(map, &isl_map_dim_max);
6823 #undef TYPE
6824 #define TYPE isl_pw_multi_aff
6825 #include "isl_type_check_match_range_multi_val.c"
6827 /* Apply "fn" to the base expressions of "pma" and "mv".
6829 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_op_multi_val(
6830 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv,
6831 __isl_give isl_multi_aff *(*fn)(__isl_take isl_multi_aff *ma,
6832 __isl_take isl_multi_val *mv))
6834 int i;
6835 isl_size n;
6837 if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
6838 goto error;
6840 n = isl_pw_multi_aff_n_piece(pma);
6841 if (n < 0)
6842 goto error;
6844 for (i = 0; i < n; ++i) {
6845 isl_multi_aff *ma;
6847 ma = isl_pw_multi_aff_take_base_at(pma, i);
6848 ma = fn(ma, isl_multi_val_copy(mv));
6849 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
6852 isl_multi_val_free(mv);
6853 return pma;
6854 error:
6855 isl_multi_val_free(mv);
6856 isl_pw_multi_aff_free(pma);
6857 return NULL;
6860 /* Scale the elements of "pma" by the corresponding elements of "mv".
6862 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6863 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6865 return isl_pw_multi_aff_op_multi_val(pma, mv,
6866 &isl_multi_aff_scale_multi_val);
6869 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6871 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
6872 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6874 return isl_pw_multi_aff_op_multi_val(pma, mv,
6875 &isl_multi_aff_scale_down_multi_val);
6878 /* This function is called for each entry of an isl_union_pw_multi_aff.
6879 * If the space of the entry matches that of data->mv,
6880 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6881 * Otherwise, return an empty isl_pw_multi_aff.
6883 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6884 __isl_take isl_pw_multi_aff *pma, void *user)
6886 isl_bool equal;
6887 isl_multi_val *mv = user;
6889 equal = isl_pw_multi_aff_match_range_multi_val(pma, mv);
6890 if (equal < 0)
6891 return isl_pw_multi_aff_free(pma);
6892 if (!equal) {
6893 isl_space *space = isl_pw_multi_aff_get_space(pma);
6894 isl_pw_multi_aff_free(pma);
6895 return isl_pw_multi_aff_empty(space);
6898 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6901 /* Scale the elements of "upma" by the corresponding elements of "mv",
6902 * for those entries that match the space of "mv".
6904 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6905 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6907 struct isl_union_pw_multi_aff_transform_control control = {
6908 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6909 .fn_user = mv,
6912 upma = isl_union_pw_multi_aff_align_params(upma,
6913 isl_multi_val_get_space(mv));
6914 mv = isl_multi_val_align_params(mv,
6915 isl_union_pw_multi_aff_get_space(upma));
6916 if (!upma || !mv)
6917 goto error;
6919 return isl_union_pw_multi_aff_transform(upma, &control);
6921 isl_multi_val_free(mv);
6922 return upma;
6923 error:
6924 isl_multi_val_free(mv);
6925 isl_union_pw_multi_aff_free(upma);
6926 return NULL;
6929 /* Construct and return a piecewise multi affine expression
6930 * in the given space with value zero in each of the output dimensions and
6931 * a universe domain.
6933 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6935 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6938 /* Construct and return a piecewise multi affine expression
6939 * that is equal to the given piecewise affine expression.
6941 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6942 __isl_take isl_pw_aff *pa)
6944 int i;
6945 isl_space *space;
6946 isl_pw_multi_aff *pma;
6948 if (!pa)
6949 return NULL;
6951 space = isl_pw_aff_get_space(pa);
6952 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6954 for (i = 0; i < pa->n; ++i) {
6955 isl_set *set;
6956 isl_multi_aff *ma;
6958 set = isl_set_copy(pa->p[i].set);
6959 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6960 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6963 isl_pw_aff_free(pa);
6964 return pma;
6967 /* Construct and return a piecewise multi affine expression
6968 * that is equal to the given multi piecewise affine expression
6969 * on the shared domain of the piecewise affine expressions,
6970 * in the special case of a 0D multi piecewise affine expression.
6972 * Create a piecewise multi affine expression with the explicit domain of
6973 * the 0D multi piecewise affine expression as domain.
6975 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6976 __isl_take isl_multi_pw_aff *mpa)
6978 isl_space *space;
6979 isl_set *dom;
6980 isl_multi_aff *ma;
6982 space = isl_multi_pw_aff_get_space(mpa);
6983 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6984 isl_multi_pw_aff_free(mpa);
6986 ma = isl_multi_aff_zero(space);
6987 return isl_pw_multi_aff_alloc(dom, ma);
6990 /* Construct and return a piecewise multi affine expression
6991 * that is equal to the given multi piecewise affine expression
6992 * on the shared domain of the piecewise affine expressions.
6994 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6995 __isl_take isl_multi_pw_aff *mpa)
6997 int i;
6998 isl_space *space;
6999 isl_pw_aff *pa;
7000 isl_pw_multi_aff *pma;
7002 if (!mpa)
7003 return NULL;
7005 if (mpa->n == 0)
7006 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
7008 space = isl_multi_pw_aff_get_space(mpa);
7009 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
7010 pma = isl_pw_multi_aff_from_pw_aff(pa);
7012 for (i = 1; i < mpa->n; ++i) {
7013 isl_pw_multi_aff *pma_i;
7015 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7016 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
7017 pma = isl_pw_multi_aff_range_product(pma, pma_i);
7020 pma = isl_pw_multi_aff_reset_space(pma, space);
7022 isl_multi_pw_aff_free(mpa);
7023 return pma;
7026 /* Convenience function that constructs an isl_multi_pw_aff
7027 * directly from an isl_aff.
7029 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
7031 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
7034 /* Construct and return a multi piecewise affine expression
7035 * that is equal to the given multi affine expression.
7037 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
7038 __isl_take isl_multi_aff *ma)
7040 int i;
7041 isl_size n;
7042 isl_multi_pw_aff *mpa;
7044 n = isl_multi_aff_dim(ma, isl_dim_out);
7045 if (n < 0)
7046 ma = isl_multi_aff_free(ma);
7047 if (!ma)
7048 return NULL;
7050 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
7052 for (i = 0; i < n; ++i) {
7053 isl_pw_aff *pa;
7055 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
7056 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
7059 isl_multi_aff_free(ma);
7060 return mpa;
7063 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
7064 * but is considered as a function on an isl_multi_aff when exported.
7066 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
7067 __isl_take isl_multi_aff *ma)
7069 return isl_multi_pw_aff_from_multi_aff(ma);
7072 /* Construct and return a multi piecewise affine expression
7073 * that is equal to the given piecewise multi affine expression.
7075 * If the resulting multi piecewise affine expression has
7076 * an explicit domain, then assign it the domain of the input.
7077 * In other cases, the domain is stored in the individual elements.
7079 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
7080 __isl_take isl_pw_multi_aff *pma)
7082 int i;
7083 isl_size n;
7084 isl_space *space;
7085 isl_multi_pw_aff *mpa;
7087 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
7088 if (n < 0)
7089 pma = isl_pw_multi_aff_free(pma);
7090 space = isl_pw_multi_aff_get_space(pma);
7091 mpa = isl_multi_pw_aff_alloc(space);
7093 for (i = 0; i < n; ++i) {
7094 isl_pw_aff *pa;
7096 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
7097 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
7099 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7100 isl_set *dom;
7102 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
7103 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
7106 isl_pw_multi_aff_free(pma);
7107 return mpa;
7110 /* This function performs the same operation as
7111 * isl_multi_pw_aff_from_pw_multi_aff,
7112 * but is considered as a function on an isl_pw_multi_aff when exported.
7114 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
7115 __isl_take isl_pw_multi_aff *pma)
7117 return isl_multi_pw_aff_from_pw_multi_aff(pma);
7120 /* Do "pa1" and "pa2" represent the same function?
7122 * We first check if they are obviously equal.
7123 * If not, we convert them to maps and check if those are equal.
7125 * If "pa1" or "pa2" contain any NaNs, then they are considered
7126 * not to be the same. A NaN is not equal to anything, not even
7127 * to another NaN.
7129 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7130 __isl_keep isl_pw_aff *pa2)
7132 isl_bool equal;
7133 isl_bool has_nan;
7134 isl_map *map1, *map2;
7136 if (!pa1 || !pa2)
7137 return isl_bool_error;
7139 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7140 if (equal < 0 || equal)
7141 return equal;
7142 has_nan = either_involves_nan(pa1, pa2);
7143 if (has_nan < 0)
7144 return isl_bool_error;
7145 if (has_nan)
7146 return isl_bool_false;
7148 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7149 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7150 equal = isl_map_is_equal(map1, map2);
7151 isl_map_free(map1);
7152 isl_map_free(map2);
7154 return equal;
7157 /* Do "mpa1" and "mpa2" represent the same function?
7159 * Note that we cannot convert the entire isl_multi_pw_aff
7160 * to a map because the domains of the piecewise affine expressions
7161 * may not be the same.
7163 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7164 __isl_keep isl_multi_pw_aff *mpa2)
7166 int i;
7167 isl_bool equal, equal_params;
7169 if (!mpa1 || !mpa2)
7170 return isl_bool_error;
7172 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7173 if (equal_params < 0)
7174 return isl_bool_error;
7175 if (!equal_params) {
7176 if (!isl_space_has_named_params(mpa1->space))
7177 return isl_bool_false;
7178 if (!isl_space_has_named_params(mpa2->space))
7179 return isl_bool_false;
7180 mpa1 = isl_multi_pw_aff_copy(mpa1);
7181 mpa2 = isl_multi_pw_aff_copy(mpa2);
7182 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7183 isl_multi_pw_aff_get_space(mpa2));
7184 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7185 isl_multi_pw_aff_get_space(mpa1));
7186 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7187 isl_multi_pw_aff_free(mpa1);
7188 isl_multi_pw_aff_free(mpa2);
7189 return equal;
7192 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7193 if (equal < 0 || !equal)
7194 return equal;
7196 for (i = 0; i < mpa1->n; ++i) {
7197 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7198 if (equal < 0 || !equal)
7199 return equal;
7202 return isl_bool_true;
7205 /* Do "pma1" and "pma2" represent the same function?
7207 * First check if they are obviously equal.
7208 * If not, then convert them to maps and check if those are equal.
7210 * If "pa1" or "pa2" contain any NaNs, then they are considered
7211 * not to be the same. A NaN is not equal to anything, not even
7212 * to another NaN.
7214 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7215 __isl_keep isl_pw_multi_aff *pma2)
7217 isl_bool equal;
7218 isl_bool has_nan;
7219 isl_map *map1, *map2;
7221 if (!pma1 || !pma2)
7222 return isl_bool_error;
7224 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7225 if (equal < 0 || equal)
7226 return equal;
7227 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7228 if (has_nan >= 0 && !has_nan)
7229 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7230 if (has_nan < 0 || has_nan)
7231 return isl_bool_not(has_nan);
7233 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7234 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7235 equal = isl_map_is_equal(map1, map2);
7236 isl_map_free(map1);
7237 isl_map_free(map2);
7239 return equal;
7242 #undef BASE
7243 #define BASE multi_aff
7245 #include "isl_multi_pw_aff_pullback_templ.c"
7247 #undef BASE
7248 #define BASE pw_multi_aff
7250 #include "isl_multi_pw_aff_pullback_templ.c"
7252 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7253 * with the domain of "aff". The domain of the result is the same
7254 * as that of "mpa".
7255 * "mpa" and "aff" are assumed to have been aligned.
7257 * We first extract the parametric constant from "aff", defined
7258 * over the correct domain.
7259 * Then we add the appropriate combinations of the members of "mpa".
7260 * Finally, we add the integer divisions through recursive calls.
7262 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7263 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7265 int i;
7266 isl_size n_in, n_div, n_mpa_in;
7267 isl_space *space;
7268 isl_val *v;
7269 isl_pw_aff *pa;
7270 isl_aff *tmp;
7272 n_in = isl_aff_dim(aff, isl_dim_in);
7273 n_div = isl_aff_dim(aff, isl_dim_div);
7274 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7275 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7276 goto error;
7278 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7279 tmp = isl_aff_copy(aff);
7280 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7281 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7282 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7283 tmp = isl_aff_reset_domain_space(tmp, space);
7284 pa = isl_pw_aff_from_aff(tmp);
7286 for (i = 0; i < n_in; ++i) {
7287 isl_pw_aff *pa_i;
7289 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7290 continue;
7291 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7292 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7293 pa_i = isl_pw_aff_scale_val(pa_i, v);
7294 pa = isl_pw_aff_add(pa, pa_i);
7297 for (i = 0; i < n_div; ++i) {
7298 isl_aff *div;
7299 isl_pw_aff *pa_i;
7301 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7302 continue;
7303 div = isl_aff_get_div(aff, i);
7304 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7305 isl_multi_pw_aff_copy(mpa), div);
7306 pa_i = isl_pw_aff_floor(pa_i);
7307 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7308 pa_i = isl_pw_aff_scale_val(pa_i, v);
7309 pa = isl_pw_aff_add(pa, pa_i);
7312 isl_multi_pw_aff_free(mpa);
7313 isl_aff_free(aff);
7315 return pa;
7316 error:
7317 isl_multi_pw_aff_free(mpa);
7318 isl_aff_free(aff);
7319 return NULL;
7322 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7323 * with the domain of "aff". The domain of the result is the same
7324 * as that of "mpa".
7326 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7327 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7329 isl_bool equal_params;
7331 if (!aff || !mpa)
7332 goto error;
7333 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7334 if (equal_params < 0)
7335 goto error;
7336 if (equal_params)
7337 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7339 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7340 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7342 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7343 error:
7344 isl_aff_free(aff);
7345 isl_multi_pw_aff_free(mpa);
7346 return NULL;
7349 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7350 * with the domain of "pa". The domain of the result is the same
7351 * as that of "mpa".
7352 * "mpa" and "pa" are assumed to have been aligned.
7354 * We consider each piece in turn. Note that the domains of the
7355 * pieces are assumed to be disjoint and they remain disjoint
7356 * after taking the preimage (over the same function).
7358 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7359 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7361 isl_space *space;
7362 isl_pw_aff *res;
7363 int i;
7365 if (!mpa || !pa)
7366 goto error;
7368 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7369 isl_pw_aff_get_space(pa));
7370 res = isl_pw_aff_empty(space);
7372 for (i = 0; i < pa->n; ++i) {
7373 isl_pw_aff *pa_i;
7374 isl_set *domain;
7376 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7377 isl_multi_pw_aff_copy(mpa),
7378 isl_aff_copy(pa->p[i].aff));
7379 domain = isl_set_copy(pa->p[i].set);
7380 domain = isl_set_preimage_multi_pw_aff(domain,
7381 isl_multi_pw_aff_copy(mpa));
7382 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7383 res = isl_pw_aff_add_disjoint(res, pa_i);
7386 isl_pw_aff_free(pa);
7387 isl_multi_pw_aff_free(mpa);
7388 return res;
7389 error:
7390 isl_pw_aff_free(pa);
7391 isl_multi_pw_aff_free(mpa);
7392 return NULL;
7395 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7396 * with the domain of "pa". The domain of the result is the same
7397 * as that of "mpa".
7399 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7400 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7402 isl_bool equal_params;
7404 if (!pa || !mpa)
7405 goto error;
7406 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7407 if (equal_params < 0)
7408 goto error;
7409 if (equal_params)
7410 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7412 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7413 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7415 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7416 error:
7417 isl_pw_aff_free(pa);
7418 isl_multi_pw_aff_free(mpa);
7419 return NULL;
7422 /* Compute the pullback of "pa" by the function represented by "mpa".
7423 * In other words, plug in "mpa" in "pa".
7425 * The pullback is computed by applying "pa" to "mpa".
7427 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7428 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7430 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7433 #undef BASE
7434 #define BASE multi_pw_aff
7436 #include "isl_multi_pw_aff_pullback_templ.c"
7438 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7439 * of "mpa1" and "mpa2" live in the same space, construct map space
7440 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7441 * with this map space as extract argument.
7443 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7444 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7445 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7446 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7448 int match;
7449 isl_space *space1, *space2;
7450 isl_map *res;
7452 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7453 isl_multi_pw_aff_get_space(mpa2));
7454 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7455 isl_multi_pw_aff_get_space(mpa1));
7456 if (!mpa1 || !mpa2)
7457 goto error;
7458 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7459 mpa2->space, isl_dim_out);
7460 if (match < 0)
7461 goto error;
7462 if (!match)
7463 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7464 "range spaces don't match", goto error);
7465 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7466 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7467 space1 = isl_space_map_from_domain_and_range(space1, space2);
7469 res = order(mpa1, mpa2, space1);
7470 isl_multi_pw_aff_free(mpa1);
7471 isl_multi_pw_aff_free(mpa2);
7472 return res;
7473 error:
7474 isl_multi_pw_aff_free(mpa1);
7475 isl_multi_pw_aff_free(mpa2);
7476 return NULL;
7479 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7480 * where the function values are equal. "space" is the space of the result.
7481 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7483 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7484 * in the sequences are equal.
7486 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7487 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7488 __isl_take isl_space *space)
7490 int i;
7491 isl_size n;
7492 isl_map *res;
7494 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7495 if (n < 0)
7496 space = isl_space_free(space);
7497 res = isl_map_universe(space);
7499 for (i = 0; i < n; ++i) {
7500 isl_pw_aff *pa1, *pa2;
7501 isl_map *map;
7503 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7504 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7505 map = isl_pw_aff_eq_map(pa1, pa2);
7506 res = isl_map_intersect(res, map);
7509 return res;
7512 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7513 * where the function values are equal.
7515 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7516 __isl_take isl_multi_pw_aff *mpa2)
7518 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7519 &isl_multi_pw_aff_eq_map_on_space);
7522 /* Intersect "map" with the result of applying "order"
7523 * on two copies of "mpa".
7525 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7526 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7527 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7528 __isl_take isl_multi_pw_aff *mpa2))
7530 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7533 /* Return the subset of "map" where the domain and the range
7534 * have equal "mpa" values.
7536 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7537 __isl_take isl_multi_pw_aff *mpa)
7539 return isl_map_order_at_multi_pw_aff(map, mpa,
7540 &isl_multi_pw_aff_eq_map);
7543 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7544 * where the function values of "mpa1" lexicographically satisfies
7545 * "strict_base"/"base" compared to that of "mpa2".
7546 * "space" is the space of the result.
7547 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7549 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7550 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7551 * when compared to the i-th element of "mpa2" while all previous elements are
7552 * pairwise equal.
7553 * In particular, if i corresponds to the final elements
7554 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7555 * for other values of i.
7556 * If "base" is a strict order, then "base" and "strict_base" are the same.
7558 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7559 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7560 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7561 __isl_take isl_pw_aff *pa2),
7562 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7563 __isl_take isl_pw_aff *pa2),
7564 __isl_take isl_space *space)
7566 int i;
7567 isl_size n;
7568 isl_map *res, *rest;
7570 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7571 if (n < 0)
7572 space = isl_space_free(space);
7573 res = isl_map_empty(isl_space_copy(space));
7574 rest = isl_map_universe(space);
7576 for (i = 0; i < n; ++i) {
7577 int last;
7578 isl_pw_aff *pa1, *pa2;
7579 isl_map *map;
7581 last = i == n - 1;
7583 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7584 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7585 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7586 map = isl_map_intersect(map, isl_map_copy(rest));
7587 res = isl_map_union(res, map);
7589 if (last)
7590 continue;
7592 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7593 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7594 map = isl_pw_aff_eq_map(pa1, pa2);
7595 rest = isl_map_intersect(rest, map);
7598 isl_map_free(rest);
7599 return res;
7602 #undef ORDER
7603 #define ORDER le
7604 #undef STRICT_ORDER
7605 #define STRICT_ORDER lt
7606 #include "isl_aff_lex_templ.c"
7608 #undef ORDER
7609 #define ORDER lt
7610 #undef STRICT_ORDER
7611 #define STRICT_ORDER lt
7612 #include "isl_aff_lex_templ.c"
7614 #undef ORDER
7615 #define ORDER ge
7616 #undef STRICT_ORDER
7617 #define STRICT_ORDER gt
7618 #include "isl_aff_lex_templ.c"
7620 #undef ORDER
7621 #define ORDER gt
7622 #undef STRICT_ORDER
7623 #define STRICT_ORDER gt
7624 #include "isl_aff_lex_templ.c"
7626 /* Compare two isl_affs.
7628 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7629 * than "aff2" and 0 if they are equal.
7631 * The order is fairly arbitrary. We do consider expressions that only involve
7632 * earlier dimensions as "smaller".
7634 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7636 int cmp;
7637 int last1, last2;
7639 if (aff1 == aff2)
7640 return 0;
7642 if (!aff1)
7643 return -1;
7644 if (!aff2)
7645 return 1;
7647 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7648 if (cmp != 0)
7649 return cmp;
7651 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7652 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7653 if (last1 != last2)
7654 return last1 - last2;
7656 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7659 /* Compare two isl_pw_affs.
7661 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7662 * than "pa2" and 0 if they are equal.
7664 * The order is fairly arbitrary. We do consider expressions that only involve
7665 * earlier dimensions as "smaller".
7667 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7668 __isl_keep isl_pw_aff *pa2)
7670 int i;
7671 int cmp;
7673 if (pa1 == pa2)
7674 return 0;
7676 if (!pa1)
7677 return -1;
7678 if (!pa2)
7679 return 1;
7681 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7682 if (cmp != 0)
7683 return cmp;
7685 if (pa1->n != pa2->n)
7686 return pa1->n - pa2->n;
7688 for (i = 0; i < pa1->n; ++i) {
7689 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7690 if (cmp != 0)
7691 return cmp;
7692 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7693 if (cmp != 0)
7694 return cmp;
7697 return 0;
7700 /* Return a piecewise affine expression that is equal to "v" on "domain".
7702 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7703 __isl_take isl_val *v)
7705 isl_space *space;
7706 isl_local_space *ls;
7707 isl_aff *aff;
7709 space = isl_set_get_space(domain);
7710 ls = isl_local_space_from_space(space);
7711 aff = isl_aff_val_on_domain(ls, v);
7713 return isl_pw_aff_alloc(domain, aff);
7716 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7717 * but is considered as a function on an isl_set when exported.
7719 __isl_give isl_pw_aff *isl_set_pw_aff_on_domain_val(__isl_take isl_set *domain,
7720 __isl_take isl_val *v)
7722 return isl_pw_aff_val_on_domain(domain, v);
7725 /* Return a piecewise affine expression that is equal to the parameter
7726 * with identifier "id" on "domain".
7728 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7729 __isl_take isl_set *domain, __isl_take isl_id *id)
7731 isl_space *space;
7732 isl_aff *aff;
7734 space = isl_set_get_space(domain);
7735 space = isl_space_add_param_id(space, isl_id_copy(id));
7736 domain = isl_set_align_params(domain, isl_space_copy(space));
7737 aff = isl_aff_param_on_domain_space_id(space, id);
7739 return isl_pw_aff_alloc(domain, aff);
7742 /* This function performs the same operation as
7743 * isl_pw_aff_param_on_domain_id,
7744 * but is considered as a function on an isl_set when exported.
7746 __isl_give isl_pw_aff *isl_set_param_pw_aff_on_domain_id(
7747 __isl_take isl_set *domain, __isl_take isl_id *id)
7749 return isl_pw_aff_param_on_domain_id(domain, id);
7752 /* Return a multi affine expression that is equal to "mv" on domain
7753 * space "space".
7755 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7756 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7758 int i;
7759 isl_size n;
7760 isl_space *space2;
7761 isl_local_space *ls;
7762 isl_multi_aff *ma;
7764 n = isl_multi_val_dim(mv, isl_dim_set);
7765 if (!space || n < 0)
7766 goto error;
7768 space2 = isl_multi_val_get_space(mv);
7769 space2 = isl_space_align_params(space2, isl_space_copy(space));
7770 space = isl_space_align_params(space, isl_space_copy(space2));
7771 space = isl_space_map_from_domain_and_range(space, space2);
7772 ma = isl_multi_aff_alloc(isl_space_copy(space));
7773 ls = isl_local_space_from_space(isl_space_domain(space));
7774 for (i = 0; i < n; ++i) {
7775 isl_val *v;
7776 isl_aff *aff;
7778 v = isl_multi_val_get_val(mv, i);
7779 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7780 ma = isl_multi_aff_set_aff(ma, i, aff);
7782 isl_local_space_free(ls);
7784 isl_multi_val_free(mv);
7785 return ma;
7786 error:
7787 isl_space_free(space);
7788 isl_multi_val_free(mv);
7789 return NULL;
7792 /* This is an alternative name for the function above.
7794 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7795 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7797 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7800 /* This function performs the same operation as
7801 * isl_multi_aff_multi_val_on_domain_space,
7802 * but is considered as a function on an isl_space when exported.
7804 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7805 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7807 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7810 /* Return a piecewise multi-affine expression
7811 * that is equal to "mv" on "domain".
7813 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7814 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7816 isl_space *space;
7817 isl_multi_aff *ma;
7819 space = isl_set_get_space(domain);
7820 ma = isl_multi_aff_multi_val_on_space(space, mv);
7822 return isl_pw_multi_aff_alloc(domain, ma);
7825 /* This function performs the same operation as
7826 * isl_pw_multi_aff_multi_val_on_domain,
7827 * but is considered as a function on an isl_set when exported.
7829 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7830 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7832 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7835 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7836 * mv is the value that should be attained on each domain set
7837 * res collects the results
7839 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7840 isl_multi_val *mv;
7841 isl_union_pw_multi_aff *res;
7844 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7845 * and add it to data->res.
7847 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7848 void *user)
7850 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7851 isl_pw_multi_aff *pma;
7852 isl_multi_val *mv;
7854 mv = isl_multi_val_copy(data->mv);
7855 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7856 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7858 return data->res ? isl_stat_ok : isl_stat_error;
7861 /* Return a union piecewise multi-affine expression
7862 * that is equal to "mv" on "domain".
7864 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7865 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7867 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7868 isl_space *space;
7870 space = isl_union_set_get_space(domain);
7871 data.res = isl_union_pw_multi_aff_empty(space);
7872 data.mv = mv;
7873 if (isl_union_set_foreach_set(domain,
7874 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7875 data.res = isl_union_pw_multi_aff_free(data.res);
7876 isl_union_set_free(domain);
7877 isl_multi_val_free(mv);
7878 return data.res;
7881 /* Compute the pullback of data->pma by the function represented by "pma2",
7882 * provided the spaces match, and add the results to data->res.
7884 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7886 struct isl_union_pw_multi_aff_bin_data *data = user;
7888 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7889 pma2->dim, isl_dim_out)) {
7890 isl_pw_multi_aff_free(pma2);
7891 return isl_stat_ok;
7894 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7895 isl_pw_multi_aff_copy(data->pma), pma2);
7897 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7898 if (!data->res)
7899 return isl_stat_error;
7901 return isl_stat_ok;
7904 /* Compute the pullback of "upma1" by the function represented by "upma2".
7906 __isl_give isl_union_pw_multi_aff *
7907 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7908 __isl_take isl_union_pw_multi_aff *upma1,
7909 __isl_take isl_union_pw_multi_aff *upma2)
7911 return bin_op(upma1, upma2, &pullback_entry);
7914 /* Apply "upma2" to "upma1".
7916 * That is, compute the pullback of "upma2" by "upma1".
7918 __isl_give isl_union_pw_multi_aff *
7919 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7920 __isl_take isl_union_pw_multi_aff *upma1,
7921 __isl_take isl_union_pw_multi_aff *upma2)
7923 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7926 #undef TYPE
7927 #define TYPE isl_pw_multi_aff
7928 static
7929 #include "isl_copy_tuple_id_templ.c"
7931 /* Given a function "pma1" of the form A[B -> C] -> D and
7932 * a function "pma2" of the form E -> B,
7933 * replace the domain of the wrapped relation inside the domain of "pma1"
7934 * by the preimage with respect to "pma2".
7935 * In other words, plug in "pma2" in this nested domain.
7936 * The result is of the form A[E -> C] -> D.
7938 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7939 * plug that into "pma1".
7941 __isl_give isl_pw_multi_aff *
7942 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7943 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7945 isl_space *pma1_space, *pma2_space;
7946 isl_space *space;
7947 isl_pw_multi_aff *id;
7949 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7950 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7952 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7953 goto error;
7954 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
7955 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
7956 goto error;
7958 space = isl_space_domain(isl_space_copy(pma1_space));
7959 space = isl_space_range(isl_space_unwrap(space));
7960 id = isl_pw_multi_aff_identity_on_domain_space(space);
7961 pma2 = isl_pw_multi_aff_product(pma2, id);
7963 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
7964 pma1_space, isl_dim_in);
7965 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
7966 pma1_space, isl_dim_in);
7968 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
7969 error:
7970 isl_pw_multi_aff_free(pma1);
7971 isl_pw_multi_aff_free(pma2);
7972 return NULL;
7975 /* If data->pma and "pma2" are such that
7976 * data->pma is of the form A[B -> C] -> D and
7977 * "pma2" is of the form E -> B,
7978 * then replace the domain of the wrapped relation
7979 * inside the domain of data->pma by the preimage with respect to "pma2" and
7980 * add the result to data->res.
7982 static isl_stat preimage_domain_wrapped_domain_entry(
7983 __isl_take isl_pw_multi_aff *pma2, void *user)
7985 struct isl_union_pw_multi_aff_bin_data *data = user;
7986 isl_space *pma1_space, *pma2_space;
7987 isl_bool match;
7989 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
7990 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7992 match = isl_space_domain_is_wrapping(pma1_space);
7993 if (match >= 0 && match)
7994 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
7995 isl_dim_in, pma2_space, isl_dim_out);
7996 if (match < 0 || !match) {
7997 isl_pw_multi_aff_free(pma2);
7998 return match < 0 ? isl_stat_error : isl_stat_ok;
8001 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8002 isl_pw_multi_aff_copy(data->pma), pma2);
8004 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
8006 return isl_stat_non_null(data->res);
8009 /* For each pair of functions A[B -> C] -> D in "upma1" and
8010 * E -> B in "upma2",
8011 * replace the domain of the wrapped relation inside the domain of the first
8012 * by the preimage with respect to the second and collect the results.
8013 * In other words, plug in the second function in this nested domain.
8014 * The results are of the form A[E -> C] -> D.
8016 __isl_give isl_union_pw_multi_aff *
8017 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8018 __isl_take isl_union_pw_multi_aff *upma1,
8019 __isl_take isl_union_pw_multi_aff *upma2)
8021 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
8024 /* Check that the domain space of "upa" matches "space".
8026 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8027 * can in principle never fail since the space "space" is that
8028 * of the isl_multi_union_pw_aff and is a set space such that
8029 * there is no domain space to match.
8031 * We check the parameters and double-check that "space" is
8032 * indeed that of a set.
8034 static isl_stat isl_union_pw_aff_check_match_domain_space(
8035 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8037 isl_space *upa_space;
8038 isl_bool match;
8040 if (!upa || !space)
8041 return isl_stat_error;
8043 match = isl_space_is_set(space);
8044 if (match < 0)
8045 return isl_stat_error;
8046 if (!match)
8047 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8048 "expecting set space", return isl_stat_error);
8050 upa_space = isl_union_pw_aff_get_space(upa);
8051 match = isl_space_has_equal_params(space, upa_space);
8052 if (match < 0)
8053 goto error;
8054 if (!match)
8055 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8056 "parameters don't match", goto error);
8058 isl_space_free(upa_space);
8059 return isl_stat_ok;
8060 error:
8061 isl_space_free(upa_space);
8062 return isl_stat_error;
8065 /* Do the parameters of "upa" match those of "space"?
8067 static isl_bool isl_union_pw_aff_matching_params(
8068 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8070 isl_space *upa_space;
8071 isl_bool match;
8073 if (!upa || !space)
8074 return isl_bool_error;
8076 upa_space = isl_union_pw_aff_get_space(upa);
8078 match = isl_space_has_equal_params(space, upa_space);
8080 isl_space_free(upa_space);
8081 return match;
8084 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8085 * space represents the new parameters.
8086 * res collects the results.
8088 struct isl_union_pw_aff_reset_params_data {
8089 isl_space *space;
8090 isl_union_pw_aff *res;
8093 /* Replace the parameters of "pa" by data->space and
8094 * add the result to data->res.
8096 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8098 struct isl_union_pw_aff_reset_params_data *data = user;
8099 isl_space *space;
8101 space = isl_pw_aff_get_space(pa);
8102 space = isl_space_replace_params(space, data->space);
8103 pa = isl_pw_aff_reset_space(pa, space);
8104 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8106 return data->res ? isl_stat_ok : isl_stat_error;
8109 /* Replace the domain space of "upa" by "space".
8110 * Since a union expression does not have a (single) domain space,
8111 * "space" is necessarily a parameter space.
8113 * Since the order and the names of the parameters determine
8114 * the hash value, we need to create a new hash table.
8116 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8117 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8119 struct isl_union_pw_aff_reset_params_data data = { space };
8120 isl_bool match;
8122 match = isl_union_pw_aff_matching_params(upa, space);
8123 if (match < 0)
8124 upa = isl_union_pw_aff_free(upa);
8125 else if (match) {
8126 isl_space_free(space);
8127 return upa;
8130 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8131 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8132 data.res = isl_union_pw_aff_free(data.res);
8134 isl_union_pw_aff_free(upa);
8135 isl_space_free(space);
8136 return data.res;
8139 /* Return the floor of "pa".
8141 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8143 return isl_pw_aff_floor(pa);
8146 /* Given f, return floor(f).
8148 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8149 __isl_take isl_union_pw_aff *upa)
8151 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8154 /* Compute
8156 * upa mod m = upa - m * floor(upa/m)
8158 * with m an integer value.
8160 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8161 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8163 isl_union_pw_aff *res;
8165 if (!upa || !m)
8166 goto error;
8168 if (!isl_val_is_int(m))
8169 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8170 "expecting integer modulo", goto error);
8171 if (!isl_val_is_pos(m))
8172 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8173 "expecting positive modulo", goto error);
8175 res = isl_union_pw_aff_copy(upa);
8176 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8177 upa = isl_union_pw_aff_floor(upa);
8178 upa = isl_union_pw_aff_scale_val(upa, m);
8179 res = isl_union_pw_aff_sub(res, upa);
8181 return res;
8182 error:
8183 isl_val_free(m);
8184 isl_union_pw_aff_free(upa);
8185 return NULL;
8188 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8189 * pos is the output position that needs to be extracted.
8190 * res collects the results.
8192 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8193 int pos;
8194 isl_union_pw_aff *res;
8197 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8198 * (assuming it has such a dimension) and add it to data->res.
8200 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8202 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8203 isl_size n_out;
8204 isl_pw_aff *pa;
8206 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8207 if (n_out < 0)
8208 return isl_stat_error;
8209 if (data->pos >= n_out) {
8210 isl_pw_multi_aff_free(pma);
8211 return isl_stat_ok;
8214 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8215 isl_pw_multi_aff_free(pma);
8217 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8219 return data->res ? isl_stat_ok : isl_stat_error;
8222 /* Extract an isl_union_pw_aff corresponding to
8223 * output dimension "pos" of "upma".
8225 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8226 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8228 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8229 isl_space *space;
8231 if (!upma)
8232 return NULL;
8234 if (pos < 0)
8235 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8236 "cannot extract at negative position", return NULL);
8238 space = isl_union_pw_multi_aff_get_space(upma);
8239 data.res = isl_union_pw_aff_empty(space);
8240 data.pos = pos;
8241 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8242 &get_union_pw_aff, &data) < 0)
8243 data.res = isl_union_pw_aff_free(data.res);
8245 return data.res;
8248 /* Return a union piecewise affine expression
8249 * that is equal to "aff" on "domain".
8251 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8252 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8254 isl_pw_aff *pa;
8256 pa = isl_pw_aff_from_aff(aff);
8257 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8260 /* Return a union piecewise affine expression
8261 * that is equal to the parameter identified by "id" on "domain".
8263 * Make sure the parameter appears in the space passed to
8264 * isl_aff_param_on_domain_space_id.
8266 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8267 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8269 isl_space *space;
8270 isl_aff *aff;
8272 space = isl_union_set_get_space(domain);
8273 space = isl_space_add_param_id(space, isl_id_copy(id));
8274 aff = isl_aff_param_on_domain_space_id(space, id);
8275 return isl_union_pw_aff_aff_on_domain(domain, aff);
8278 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8279 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8280 * needs to attain.
8281 * "res" collects the results.
8283 struct isl_union_pw_aff_pw_aff_on_domain_data {
8284 isl_pw_aff *pa;
8285 isl_union_pw_aff *res;
8288 /* Construct a piecewise affine expression that is equal to data->pa
8289 * on "domain" and add the result to data->res.
8291 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8293 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8294 isl_pw_aff *pa;
8295 isl_size dim;
8297 pa = isl_pw_aff_copy(data->pa);
8298 dim = isl_set_dim(domain, isl_dim_set);
8299 if (dim < 0)
8300 pa = isl_pw_aff_free(pa);
8301 pa = isl_pw_aff_from_range(pa);
8302 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8303 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8304 pa = isl_pw_aff_intersect_domain(pa, domain);
8305 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8307 return data->res ? isl_stat_ok : isl_stat_error;
8310 /* Return a union piecewise affine expression
8311 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8312 * have been aligned.
8314 * Construct an isl_pw_aff on each of the sets in "domain" and
8315 * collect the results.
8317 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8318 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8320 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8321 isl_space *space;
8323 space = isl_union_set_get_space(domain);
8324 data.res = isl_union_pw_aff_empty(space);
8325 data.pa = pa;
8326 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8327 data.res = isl_union_pw_aff_free(data.res);
8328 isl_union_set_free(domain);
8329 isl_pw_aff_free(pa);
8330 return data.res;
8333 /* Return a union piecewise affine expression
8334 * that is equal to "pa" on "domain".
8336 * Check that "pa" is a parametric expression,
8337 * align the parameters if needed and call
8338 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8340 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8341 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8343 isl_bool is_set;
8344 isl_bool equal_params;
8345 isl_space *domain_space, *pa_space;
8347 pa_space = isl_pw_aff_peek_space(pa);
8348 is_set = isl_space_is_set(pa_space);
8349 if (is_set < 0)
8350 goto error;
8351 if (!is_set)
8352 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8353 "expecting parametric expression", goto error);
8355 domain_space = isl_union_set_get_space(domain);
8356 pa_space = isl_pw_aff_get_space(pa);
8357 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8358 if (equal_params >= 0 && !equal_params) {
8359 isl_space *space;
8361 space = isl_space_align_params(domain_space, pa_space);
8362 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8363 domain = isl_union_set_align_params(domain, space);
8364 } else {
8365 isl_space_free(domain_space);
8366 isl_space_free(pa_space);
8369 if (equal_params < 0)
8370 goto error;
8371 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8372 error:
8373 isl_union_set_free(domain);
8374 isl_pw_aff_free(pa);
8375 return NULL;
8378 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8379 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8380 * "res" collects the results.
8382 struct isl_union_pw_aff_val_on_domain_data {
8383 isl_val *v;
8384 isl_union_pw_aff *res;
8387 /* Construct a piecewise affine expression that is equal to data->v
8388 * on "domain" and add the result to data->res.
8390 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8392 struct isl_union_pw_aff_val_on_domain_data *data = user;
8393 isl_pw_aff *pa;
8394 isl_val *v;
8396 v = isl_val_copy(data->v);
8397 pa = isl_pw_aff_val_on_domain(domain, v);
8398 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8400 return data->res ? isl_stat_ok : isl_stat_error;
8403 /* Return a union piecewise affine expression
8404 * that is equal to "v" on "domain".
8406 * Construct an isl_pw_aff on each of the sets in "domain" and
8407 * collect the results.
8409 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8410 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8412 struct isl_union_pw_aff_val_on_domain_data data;
8413 isl_space *space;
8415 space = isl_union_set_get_space(domain);
8416 data.res = isl_union_pw_aff_empty(space);
8417 data.v = v;
8418 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8419 data.res = isl_union_pw_aff_free(data.res);
8420 isl_union_set_free(domain);
8421 isl_val_free(v);
8422 return data.res;
8425 /* Construct a piecewise multi affine expression
8426 * that is equal to "pa" and add it to upma.
8428 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8429 void *user)
8431 isl_union_pw_multi_aff **upma = user;
8432 isl_pw_multi_aff *pma;
8434 pma = isl_pw_multi_aff_from_pw_aff(pa);
8435 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8437 return *upma ? isl_stat_ok : isl_stat_error;
8440 /* Construct and return a union piecewise multi affine expression
8441 * that is equal to the given union piecewise affine expression.
8443 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8444 __isl_take isl_union_pw_aff *upa)
8446 isl_space *space;
8447 isl_union_pw_multi_aff *upma;
8449 if (!upa)
8450 return NULL;
8452 space = isl_union_pw_aff_get_space(upa);
8453 upma = isl_union_pw_multi_aff_empty(space);
8455 if (isl_union_pw_aff_foreach_pw_aff(upa,
8456 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8457 upma = isl_union_pw_multi_aff_free(upma);
8459 isl_union_pw_aff_free(upa);
8460 return upma;
8463 /* Compute the set of elements in the domain of "pa" where it is zero and
8464 * add this set to "uset".
8466 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8468 isl_union_set **uset = (isl_union_set **)user;
8470 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8472 return *uset ? isl_stat_ok : isl_stat_error;
8475 /* Return a union set containing those elements in the domain
8476 * of "upa" where it is zero.
8478 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8479 __isl_take isl_union_pw_aff *upa)
8481 isl_union_set *zero;
8483 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8484 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8485 zero = isl_union_set_free(zero);
8487 isl_union_pw_aff_free(upa);
8488 return zero;
8491 /* Internal data structure for isl_union_pw_aff_bind_id,
8492 * storing the parameter that needs to be bound and
8493 * the accumulated results.
8495 struct isl_bind_id_data {
8496 isl_id *id;
8497 isl_union_set *bound;
8500 /* Bind the piecewise affine function "pa" to the parameter data->id,
8501 * adding the resulting elements in the domain where the expression
8502 * is equal to the parameter to data->bound.
8504 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8506 struct isl_bind_id_data *data = user;
8507 isl_set *bound;
8509 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8510 data->bound = isl_union_set_add_set(data->bound, bound);
8512 return data->bound ? isl_stat_ok : isl_stat_error;
8515 /* Bind the union piecewise affine function "upa" to the parameter "id",
8516 * returning the elements in the domain where the expression
8517 * is equal to the parameter.
8519 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8520 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8522 struct isl_bind_id_data data = { id };
8524 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8525 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8526 data.bound = isl_union_set_free(data.bound);
8528 isl_union_pw_aff_free(upa);
8529 isl_id_free(id);
8530 return data.bound;
8533 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8534 * upma is the function that is plugged in.
8535 * pa is the current part of the function in which upma is plugged in.
8536 * res collects the results.
8538 struct isl_union_pw_aff_pullback_upma_data {
8539 isl_union_pw_multi_aff *upma;
8540 isl_pw_aff *pa;
8541 isl_union_pw_aff *res;
8544 /* Check if "pma" can be plugged into data->pa.
8545 * If so, perform the pullback and add the result to data->res.
8547 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8549 struct isl_union_pw_aff_pullback_upma_data *data = user;
8550 isl_pw_aff *pa;
8552 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8553 pma->dim, isl_dim_out)) {
8554 isl_pw_multi_aff_free(pma);
8555 return isl_stat_ok;
8558 pa = isl_pw_aff_copy(data->pa);
8559 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8561 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8563 return data->res ? isl_stat_ok : isl_stat_error;
8566 /* Check if any of the elements of data->upma can be plugged into pa,
8567 * add if so add the result to data->res.
8569 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8571 struct isl_union_pw_aff_pullback_upma_data *data = user;
8572 isl_stat r;
8574 data->pa = pa;
8575 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8576 &pa_pb_pma, data);
8577 isl_pw_aff_free(pa);
8579 return r;
8582 /* Compute the pullback of "upa" by the function represented by "upma".
8583 * In other words, plug in "upma" in "upa". The result contains
8584 * expressions defined over the domain space of "upma".
8586 * Run over all pairs of elements in "upa" and "upma", perform
8587 * the pullback when appropriate and collect the results.
8588 * If the hash value were based on the domain space rather than
8589 * the function space, then we could run through all elements
8590 * of "upma" and directly pick out the corresponding element of "upa".
8592 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8593 __isl_take isl_union_pw_aff *upa,
8594 __isl_take isl_union_pw_multi_aff *upma)
8596 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8597 isl_space *space;
8599 space = isl_union_pw_multi_aff_get_space(upma);
8600 upa = isl_union_pw_aff_align_params(upa, space);
8601 space = isl_union_pw_aff_get_space(upa);
8602 upma = isl_union_pw_multi_aff_align_params(upma, space);
8604 if (!upa || !upma)
8605 goto error;
8607 data.upma = upma;
8608 data.res = isl_union_pw_aff_alloc_same_size(upa);
8609 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8610 data.res = isl_union_pw_aff_free(data.res);
8612 isl_union_pw_aff_free(upa);
8613 isl_union_pw_multi_aff_free(upma);
8614 return data.res;
8615 error:
8616 isl_union_pw_aff_free(upa);
8617 isl_union_pw_multi_aff_free(upma);
8618 return NULL;
8621 #undef BASE
8622 #define BASE union_pw_aff
8623 #undef DOMBASE
8624 #define DOMBASE union_set
8626 #include <isl_multi_explicit_domain.c>
8627 #include <isl_multi_union_pw_aff_explicit_domain.c>
8628 #include <isl_multi_templ.c>
8629 #include <isl_multi_un_op_templ.c>
8630 #include <isl_multi_bin_val_templ.c>
8631 #include <isl_multi_apply_set.c>
8632 #include <isl_multi_apply_union_set.c>
8633 #include <isl_multi_arith_templ.c>
8634 #include <isl_multi_bind_templ.c>
8635 #include <isl_multi_coalesce.c>
8636 #include <isl_multi_dim_id_templ.c>
8637 #include <isl_multi_floor.c>
8638 #include <isl_multi_from_base_templ.c>
8639 #include <isl_multi_gist.c>
8640 #include <isl_multi_align_set.c>
8641 #include <isl_multi_align_union_set.c>
8642 #include <isl_multi_intersect.c>
8643 #include <isl_multi_nan_templ.c>
8644 #include <isl_multi_tuple_id_templ.c>
8645 #include <isl_multi_union_add_templ.c>
8646 #include <isl_multi_zero_space_templ.c>
8648 /* Does "mupa" have a non-trivial explicit domain?
8650 * The explicit domain, if present, is trivial if it represents
8651 * an (obviously) universe parameter set.
8653 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8654 __isl_keep isl_multi_union_pw_aff *mupa)
8656 isl_bool is_params, trivial;
8657 isl_set *set;
8659 if (!mupa)
8660 return isl_bool_error;
8661 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8662 return isl_bool_false;
8663 is_params = isl_union_set_is_params(mupa->u.dom);
8664 if (is_params < 0 || !is_params)
8665 return isl_bool_not(is_params);
8666 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8667 trivial = isl_set_plain_is_universe(set);
8668 isl_set_free(set);
8669 return isl_bool_not(trivial);
8672 /* Construct a multiple union piecewise affine expression
8673 * in the given space with value zero in each of the output dimensions.
8675 * Since there is no canonical zero value for
8676 * a union piecewise affine expression, we can only construct
8677 * a zero-dimensional "zero" value.
8679 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8680 __isl_take isl_space *space)
8682 isl_bool params;
8683 isl_size dim;
8685 if (!space)
8686 return NULL;
8688 params = isl_space_is_params(space);
8689 if (params < 0)
8690 goto error;
8691 if (params)
8692 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8693 "expecting proper set space", goto error);
8694 if (!isl_space_is_set(space))
8695 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8696 "expecting set space", goto error);
8697 dim = isl_space_dim(space, isl_dim_out);
8698 if (dim < 0)
8699 goto error;
8700 if (dim != 0)
8701 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8702 "expecting 0D space", goto error);
8704 return isl_multi_union_pw_aff_alloc(space);
8705 error:
8706 isl_space_free(space);
8707 return NULL;
8710 /* Construct and return a multi union piecewise affine expression
8711 * that is equal to the given multi affine expression.
8713 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8714 __isl_take isl_multi_aff *ma)
8716 isl_multi_pw_aff *mpa;
8718 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8719 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8722 /* This function performs the same operation as
8723 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8724 * isl_multi_aff when exported.
8726 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8727 __isl_take isl_multi_aff *ma)
8729 return isl_multi_union_pw_aff_from_multi_aff(ma);
8732 /* Construct and return a multi union piecewise affine expression
8733 * that is equal to the given multi piecewise affine expression.
8735 * If the resulting multi union piecewise affine expression has
8736 * an explicit domain, then assign it the domain of the input.
8737 * In other cases, the domain is stored in the individual elements.
8739 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8740 __isl_take isl_multi_pw_aff *mpa)
8742 int i;
8743 isl_size n;
8744 isl_space *space;
8745 isl_multi_union_pw_aff *mupa;
8747 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8748 if (n < 0)
8749 mpa = isl_multi_pw_aff_free(mpa);
8750 if (!mpa)
8751 return NULL;
8753 space = isl_multi_pw_aff_get_space(mpa);
8754 space = isl_space_range(space);
8755 mupa = isl_multi_union_pw_aff_alloc(space);
8757 for (i = 0; i < n; ++i) {
8758 isl_pw_aff *pa;
8759 isl_union_pw_aff *upa;
8761 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8762 upa = isl_union_pw_aff_from_pw_aff(pa);
8763 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8765 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8766 isl_union_set *dom;
8767 isl_multi_pw_aff *copy;
8769 copy = isl_multi_pw_aff_copy(mpa);
8770 dom = isl_union_set_from_set(isl_multi_pw_aff_domain(copy));
8771 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8774 isl_multi_pw_aff_free(mpa);
8776 return mupa;
8779 /* Extract the range space of "pma" and assign it to *space.
8780 * If *space has already been set (through a previous call to this function),
8781 * then check that the range space is the same.
8783 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8785 isl_space **space = user;
8786 isl_space *pma_space;
8787 isl_bool equal;
8789 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8790 isl_pw_multi_aff_free(pma);
8792 if (!pma_space)
8793 return isl_stat_error;
8794 if (!*space) {
8795 *space = pma_space;
8796 return isl_stat_ok;
8799 equal = isl_space_is_equal(pma_space, *space);
8800 isl_space_free(pma_space);
8802 if (equal < 0)
8803 return isl_stat_error;
8804 if (!equal)
8805 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8806 "range spaces not the same", return isl_stat_error);
8807 return isl_stat_ok;
8810 /* Construct and return a multi union piecewise affine expression
8811 * that is equal to the given union piecewise multi affine expression.
8813 * In order to be able to perform the conversion, the input
8814 * needs to be non-empty and may only involve a single range space.
8816 * If the resulting multi union piecewise affine expression has
8817 * an explicit domain, then assign it the domain of the input.
8818 * In other cases, the domain is stored in the individual elements.
8820 __isl_give isl_multi_union_pw_aff *
8821 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8822 __isl_take isl_union_pw_multi_aff *upma)
8824 isl_space *space = NULL;
8825 isl_multi_union_pw_aff *mupa;
8826 int i;
8827 isl_size n;
8829 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8830 if (n < 0)
8831 goto error;
8832 if (n == 0)
8833 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8834 "cannot extract range space from empty input",
8835 goto error);
8836 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8837 &space) < 0)
8838 goto error;
8840 if (!space)
8841 goto error;
8843 n = isl_space_dim(space, isl_dim_set);
8844 if (n < 0)
8845 space = isl_space_free(space);
8846 mupa = isl_multi_union_pw_aff_alloc(space);
8848 for (i = 0; i < n; ++i) {
8849 isl_union_pw_aff *upa;
8851 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8852 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8854 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8855 isl_union_set *dom;
8856 isl_union_pw_multi_aff *copy;
8858 copy = isl_union_pw_multi_aff_copy(upma);
8859 dom = isl_union_pw_multi_aff_domain(copy);
8860 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8863 isl_union_pw_multi_aff_free(upma);
8864 return mupa;
8865 error:
8866 isl_space_free(space);
8867 isl_union_pw_multi_aff_free(upma);
8868 return NULL;
8871 /* This function performs the same operation as
8872 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8873 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8875 __isl_give isl_multi_union_pw_aff *
8876 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8877 __isl_take isl_union_pw_multi_aff *upma)
8879 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8882 /* Try and create an isl_multi_union_pw_aff that is equivalent
8883 * to the given isl_union_map.
8884 * The isl_union_map is required to be single-valued in each space.
8885 * Moreover, it cannot be empty and all range spaces need to be the same.
8886 * Otherwise, an error is produced.
8888 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8889 __isl_take isl_union_map *umap)
8891 isl_union_pw_multi_aff *upma;
8893 upma = isl_union_pw_multi_aff_from_union_map(umap);
8894 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8897 /* This function performs the same operation as
8898 * isl_multi_union_pw_aff_from_union_map,
8899 * but is considered as a function on an isl_union_map when exported.
8901 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8902 __isl_take isl_union_map *umap)
8904 return isl_multi_union_pw_aff_from_union_map(umap);
8907 /* Return a multiple union piecewise affine expression
8908 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8909 * have been aligned.
8911 * If the resulting multi union piecewise affine expression has
8912 * an explicit domain, then assign it the input domain.
8913 * In other cases, the domain is stored in the individual elements.
8915 static __isl_give isl_multi_union_pw_aff *
8916 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8917 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8919 int i;
8920 isl_size n;
8921 isl_space *space;
8922 isl_multi_union_pw_aff *mupa;
8924 n = isl_multi_val_dim(mv, isl_dim_set);
8925 if (!domain || n < 0)
8926 goto error;
8928 space = isl_multi_val_get_space(mv);
8929 mupa = isl_multi_union_pw_aff_alloc(space);
8930 for (i = 0; i < n; ++i) {
8931 isl_val *v;
8932 isl_union_pw_aff *upa;
8934 v = isl_multi_val_get_val(mv, i);
8935 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8937 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8939 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8940 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8941 isl_union_set_copy(domain));
8943 isl_union_set_free(domain);
8944 isl_multi_val_free(mv);
8945 return mupa;
8946 error:
8947 isl_union_set_free(domain);
8948 isl_multi_val_free(mv);
8949 return NULL;
8952 /* Return a multiple union piecewise affine expression
8953 * that is equal to "mv" on "domain".
8955 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8956 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8958 isl_bool equal_params;
8960 if (!domain || !mv)
8961 goto error;
8962 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8963 if (equal_params < 0)
8964 goto error;
8965 if (equal_params)
8966 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8967 domain, mv);
8968 domain = isl_union_set_align_params(domain,
8969 isl_multi_val_get_space(mv));
8970 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8971 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8972 error:
8973 isl_union_set_free(domain);
8974 isl_multi_val_free(mv);
8975 return NULL;
8978 /* Return a multiple union piecewise affine expression
8979 * that is equal to "ma" on "domain".
8981 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8982 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8984 isl_pw_multi_aff *pma;
8986 pma = isl_pw_multi_aff_from_multi_aff(ma);
8987 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8990 /* Return a multiple union piecewise affine expression
8991 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8992 * have been aligned.
8994 * If the resulting multi union piecewise affine expression has
8995 * an explicit domain, then assign it the input domain.
8996 * In other cases, the domain is stored in the individual elements.
8998 static __isl_give isl_multi_union_pw_aff *
8999 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9000 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
9002 int i;
9003 isl_size n;
9004 isl_space *space;
9005 isl_multi_union_pw_aff *mupa;
9007 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
9008 if (!domain || n < 0)
9009 goto error;
9010 space = isl_pw_multi_aff_get_space(pma);
9011 mupa = isl_multi_union_pw_aff_alloc(space);
9012 for (i = 0; i < n; ++i) {
9013 isl_pw_aff *pa;
9014 isl_union_pw_aff *upa;
9016 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9017 upa = isl_union_pw_aff_pw_aff_on_domain(
9018 isl_union_set_copy(domain), pa);
9019 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9021 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9022 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
9023 isl_union_set_copy(domain));
9025 isl_union_set_free(domain);
9026 isl_pw_multi_aff_free(pma);
9027 return mupa;
9028 error:
9029 isl_union_set_free(domain);
9030 isl_pw_multi_aff_free(pma);
9031 return NULL;
9034 /* Return a multiple union piecewise affine expression
9035 * that is equal to "pma" on "domain".
9037 __isl_give isl_multi_union_pw_aff *
9038 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
9039 __isl_take isl_pw_multi_aff *pma)
9041 isl_bool equal_params;
9042 isl_space *space;
9044 space = isl_pw_multi_aff_peek_space(pma);
9045 equal_params = isl_union_set_space_has_equal_params(domain, space);
9046 if (equal_params < 0)
9047 goto error;
9048 if (equal_params)
9049 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9050 domain, pma);
9051 domain = isl_union_set_align_params(domain,
9052 isl_pw_multi_aff_get_space(pma));
9053 pma = isl_pw_multi_aff_align_params(pma,
9054 isl_union_set_get_space(domain));
9055 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
9056 pma);
9057 error:
9058 isl_union_set_free(domain);
9059 isl_pw_multi_aff_free(pma);
9060 return NULL;
9063 /* Return a union set containing those elements in the domains
9064 * of the elements of "mupa" where they are all zero.
9066 * If there are no elements, then simply return the entire domain.
9068 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
9069 __isl_take isl_multi_union_pw_aff *mupa)
9071 int i;
9072 isl_size n;
9073 isl_union_pw_aff *upa;
9074 isl_union_set *zero;
9076 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9077 if (n < 0)
9078 mupa = isl_multi_union_pw_aff_free(mupa);
9079 if (!mupa)
9080 return NULL;
9082 if (n == 0)
9083 return isl_multi_union_pw_aff_domain(mupa);
9085 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9086 zero = isl_union_pw_aff_zero_union_set(upa);
9088 for (i = 1; i < n; ++i) {
9089 isl_union_set *zero_i;
9091 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9092 zero_i = isl_union_pw_aff_zero_union_set(upa);
9094 zero = isl_union_set_intersect(zero, zero_i);
9097 isl_multi_union_pw_aff_free(mupa);
9098 return zero;
9101 /* Construct a union map mapping the shared domain
9102 * of the union piecewise affine expressions to the range of "mupa"
9103 * in the special case of a 0D multi union piecewise affine expression.
9105 * Construct a map between the explicit domain of "mupa" and
9106 * the range space.
9107 * Note that this assumes that the domain consists of explicit elements.
9109 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9110 __isl_take isl_multi_union_pw_aff *mupa)
9112 isl_bool is_params;
9113 isl_space *space;
9114 isl_union_set *dom, *ran;
9116 space = isl_multi_union_pw_aff_get_space(mupa);
9117 dom = isl_multi_union_pw_aff_domain(mupa);
9118 ran = isl_union_set_from_set(isl_set_universe(space));
9120 is_params = isl_union_set_is_params(dom);
9121 if (is_params < 0)
9122 dom = isl_union_set_free(dom);
9123 else if (is_params)
9124 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9125 "cannot create union map from expression without "
9126 "explicit domain elements",
9127 dom = isl_union_set_free(dom));
9129 return isl_union_map_from_domain_and_range(dom, ran);
9132 /* Construct a union map mapping the shared domain
9133 * of the union piecewise affine expressions to the range of "mupa"
9134 * with each dimension in the range equated to the
9135 * corresponding union piecewise affine expression.
9137 * If the input is zero-dimensional, then construct a mapping
9138 * from its explicit domain.
9140 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9141 __isl_take isl_multi_union_pw_aff *mupa)
9143 int i;
9144 isl_size n;
9145 isl_space *space;
9146 isl_union_map *umap;
9147 isl_union_pw_aff *upa;
9149 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9150 if (n < 0)
9151 mupa = isl_multi_union_pw_aff_free(mupa);
9152 if (!mupa)
9153 return NULL;
9155 if (n == 0)
9156 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9158 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9159 umap = isl_union_map_from_union_pw_aff(upa);
9161 for (i = 1; i < n; ++i) {
9162 isl_union_map *umap_i;
9164 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9165 umap_i = isl_union_map_from_union_pw_aff(upa);
9166 umap = isl_union_map_flat_range_product(umap, umap_i);
9169 space = isl_multi_union_pw_aff_get_space(mupa);
9170 umap = isl_union_map_reset_range_space(umap, space);
9172 isl_multi_union_pw_aff_free(mupa);
9173 return umap;
9176 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9177 * "range" is the space from which to set the range space.
9178 * "res" collects the results.
9180 struct isl_union_pw_multi_aff_reset_range_space_data {
9181 isl_space *range;
9182 isl_union_pw_multi_aff *res;
9185 /* Replace the range space of "pma" by the range space of data->range and
9186 * add the result to data->res.
9188 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9190 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9191 isl_space *space;
9193 space = isl_pw_multi_aff_get_space(pma);
9194 space = isl_space_domain(space);
9195 space = isl_space_extend_domain_with_range(space,
9196 isl_space_copy(data->range));
9197 pma = isl_pw_multi_aff_reset_space(pma, space);
9198 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9200 return data->res ? isl_stat_ok : isl_stat_error;
9203 /* Replace the range space of all the piecewise affine expressions in "upma" by
9204 * the range space of "space".
9206 * This assumes that all these expressions have the same output dimension.
9208 * Since the spaces of the expressions change, so do their hash values.
9209 * We therefore need to create a new isl_union_pw_multi_aff.
9210 * Note that the hash value is currently computed based on the entire
9211 * space even though there can only be a single expression with a given
9212 * domain space.
9214 static __isl_give isl_union_pw_multi_aff *
9215 isl_union_pw_multi_aff_reset_range_space(
9216 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9218 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9219 isl_space *space_upma;
9221 space_upma = isl_union_pw_multi_aff_get_space(upma);
9222 data.res = isl_union_pw_multi_aff_empty(space_upma);
9223 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9224 &reset_range_space, &data) < 0)
9225 data.res = isl_union_pw_multi_aff_free(data.res);
9227 isl_space_free(space);
9228 isl_union_pw_multi_aff_free(upma);
9229 return data.res;
9232 /* Construct and return a union piecewise multi affine expression
9233 * that is equal to the given multi union piecewise affine expression,
9234 * in the special case of a 0D multi union piecewise affine expression.
9236 * Construct a union piecewise multi affine expression
9237 * on top of the explicit domain of the input.
9239 __isl_give isl_union_pw_multi_aff *
9240 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9241 __isl_take isl_multi_union_pw_aff *mupa)
9243 isl_space *space;
9244 isl_multi_val *mv;
9245 isl_union_set *domain;
9247 space = isl_multi_union_pw_aff_get_space(mupa);
9248 mv = isl_multi_val_zero(space);
9249 domain = isl_multi_union_pw_aff_domain(mupa);
9250 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9253 /* Construct and return a union piecewise multi affine expression
9254 * that is equal to the given multi union piecewise affine expression.
9256 * If the input is zero-dimensional, then
9257 * construct a union piecewise multi affine expression
9258 * on top of the explicit domain of the input.
9260 __isl_give isl_union_pw_multi_aff *
9261 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9262 __isl_take isl_multi_union_pw_aff *mupa)
9264 int i;
9265 isl_size n;
9266 isl_space *space;
9267 isl_union_pw_multi_aff *upma;
9268 isl_union_pw_aff *upa;
9270 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9271 if (n < 0)
9272 mupa = isl_multi_union_pw_aff_free(mupa);
9273 if (!mupa)
9274 return NULL;
9276 if (n == 0)
9277 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9279 space = isl_multi_union_pw_aff_get_space(mupa);
9280 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9281 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9283 for (i = 1; i < n; ++i) {
9284 isl_union_pw_multi_aff *upma_i;
9286 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9287 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9288 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9291 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9293 isl_multi_union_pw_aff_free(mupa);
9294 return upma;
9297 /* Intersect the range of "mupa" with "range",
9298 * in the special case where "mupa" is 0D.
9300 * Intersect the domain of "mupa" with the constraints on the parameters
9301 * of "range".
9303 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9304 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9306 range = isl_set_params(range);
9307 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9308 return mupa;
9311 /* Intersect the range of "mupa" with "range".
9312 * That is, keep only those domain elements that have a function value
9313 * in "range".
9315 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9316 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9318 isl_union_pw_multi_aff *upma;
9319 isl_union_set *domain;
9320 isl_space *space;
9321 isl_size n;
9322 int match;
9324 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9325 if (n < 0 || !range)
9326 goto error;
9328 space = isl_set_get_space(range);
9329 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9330 space, isl_dim_set);
9331 isl_space_free(space);
9332 if (match < 0)
9333 goto error;
9334 if (!match)
9335 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9336 "space don't match", goto error);
9337 if (n == 0)
9338 return mupa_intersect_range_0D(mupa, range);
9340 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9341 isl_multi_union_pw_aff_copy(mupa));
9342 domain = isl_union_set_from_set(range);
9343 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9344 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9346 return mupa;
9347 error:
9348 isl_multi_union_pw_aff_free(mupa);
9349 isl_set_free(range);
9350 return NULL;
9353 /* Return the shared domain of the elements of "mupa",
9354 * in the special case where "mupa" is zero-dimensional.
9356 * Return the explicit domain of "mupa".
9357 * Note that this domain may be a parameter set, either
9358 * because "mupa" is meant to live in a set space or
9359 * because no explicit domain has been set.
9361 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9362 __isl_take isl_multi_union_pw_aff *mupa)
9364 isl_union_set *dom;
9366 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9367 isl_multi_union_pw_aff_free(mupa);
9369 return dom;
9372 /* Return the shared domain of the elements of "mupa".
9374 * If "mupa" is zero-dimensional, then return its explicit domain.
9376 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9377 __isl_take isl_multi_union_pw_aff *mupa)
9379 int i;
9380 isl_size n;
9381 isl_union_pw_aff *upa;
9382 isl_union_set *dom;
9384 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9385 if (n < 0)
9386 mupa = isl_multi_union_pw_aff_free(mupa);
9387 if (!mupa)
9388 return NULL;
9390 if (n == 0)
9391 return isl_multi_union_pw_aff_domain_0D(mupa);
9393 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9394 dom = isl_union_pw_aff_domain(upa);
9395 for (i = 1; i < n; ++i) {
9396 isl_union_set *dom_i;
9398 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9399 dom_i = isl_union_pw_aff_domain(upa);
9400 dom = isl_union_set_intersect(dom, dom_i);
9403 isl_multi_union_pw_aff_free(mupa);
9404 return dom;
9407 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9408 * In particular, the spaces have been aligned.
9409 * The result is defined over the shared domain of the elements of "mupa"
9411 * We first extract the parametric constant part of "aff" and
9412 * define that over the shared domain.
9413 * Then we iterate over all input dimensions of "aff" and add the corresponding
9414 * multiples of the elements of "mupa".
9415 * Finally, we consider the integer divisions, calling the function
9416 * recursively to obtain an isl_union_pw_aff corresponding to the
9417 * integer division argument.
9419 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9420 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9422 int i;
9423 isl_size n_in, n_div;
9424 isl_union_pw_aff *upa;
9425 isl_union_set *uset;
9426 isl_val *v;
9427 isl_aff *cst;
9429 n_in = isl_aff_dim(aff, isl_dim_in);
9430 n_div = isl_aff_dim(aff, isl_dim_div);
9431 if (n_in < 0 || n_div < 0)
9432 goto error;
9434 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9435 cst = isl_aff_copy(aff);
9436 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9437 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9438 cst = isl_aff_project_domain_on_params(cst);
9439 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9441 for (i = 0; i < n_in; ++i) {
9442 isl_union_pw_aff *upa_i;
9444 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9445 continue;
9446 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9447 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9448 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9449 upa = isl_union_pw_aff_add(upa, upa_i);
9452 for (i = 0; i < n_div; ++i) {
9453 isl_aff *div;
9454 isl_union_pw_aff *upa_i;
9456 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9457 continue;
9458 div = isl_aff_get_div(aff, i);
9459 upa_i = multi_union_pw_aff_apply_aff(
9460 isl_multi_union_pw_aff_copy(mupa), div);
9461 upa_i = isl_union_pw_aff_floor(upa_i);
9462 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9463 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9464 upa = isl_union_pw_aff_add(upa, upa_i);
9467 isl_multi_union_pw_aff_free(mupa);
9468 isl_aff_free(aff);
9470 return upa;
9471 error:
9472 isl_multi_union_pw_aff_free(mupa);
9473 isl_aff_free(aff);
9474 return NULL;
9477 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9478 * with the domain of "aff".
9479 * Furthermore, the dimension of this space needs to be greater than zero.
9480 * The result is defined over the shared domain of the elements of "mupa"
9482 * We perform these checks and then hand over control to
9483 * multi_union_pw_aff_apply_aff.
9485 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9486 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9488 isl_size dim;
9489 isl_space *space1, *space2;
9490 isl_bool equal;
9492 mupa = isl_multi_union_pw_aff_align_params(mupa,
9493 isl_aff_get_space(aff));
9494 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9495 if (!mupa || !aff)
9496 goto error;
9498 space1 = isl_multi_union_pw_aff_get_space(mupa);
9499 space2 = isl_aff_get_domain_space(aff);
9500 equal = isl_space_is_equal(space1, space2);
9501 isl_space_free(space1);
9502 isl_space_free(space2);
9503 if (equal < 0)
9504 goto error;
9505 if (!equal)
9506 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9507 "spaces don't match", goto error);
9508 dim = isl_aff_dim(aff, isl_dim_in);
9509 if (dim < 0)
9510 goto error;
9511 if (dim == 0)
9512 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9513 "cannot determine domains", goto error);
9515 return multi_union_pw_aff_apply_aff(mupa, aff);
9516 error:
9517 isl_multi_union_pw_aff_free(mupa);
9518 isl_aff_free(aff);
9519 return NULL;
9522 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9523 * The space of "mupa" is known to be compatible with the domain of "ma".
9525 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9526 * on the domain of "mupa".
9528 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9529 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9531 isl_union_set *dom;
9533 dom = isl_multi_union_pw_aff_domain(mupa);
9534 ma = isl_multi_aff_project_domain_on_params(ma);
9536 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9539 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9540 * with the domain of "ma".
9541 * The result is defined over the shared domain of the elements of "mupa"
9543 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9544 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9546 isl_space *space1, *space2;
9547 isl_multi_union_pw_aff *res;
9548 isl_bool equal;
9549 int i;
9550 isl_size n_in, n_out;
9552 mupa = isl_multi_union_pw_aff_align_params(mupa,
9553 isl_multi_aff_get_space(ma));
9554 ma = isl_multi_aff_align_params(ma,
9555 isl_multi_union_pw_aff_get_space(mupa));
9556 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9557 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9558 if (!mupa || n_in < 0 || n_out < 0)
9559 goto error;
9561 space1 = isl_multi_union_pw_aff_get_space(mupa);
9562 space2 = isl_multi_aff_get_domain_space(ma);
9563 equal = isl_space_is_equal(space1, space2);
9564 isl_space_free(space1);
9565 isl_space_free(space2);
9566 if (equal < 0)
9567 goto error;
9568 if (!equal)
9569 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9570 "spaces don't match", goto error);
9571 if (n_in == 0)
9572 return mupa_apply_multi_aff_0D(mupa, ma);
9574 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9575 res = isl_multi_union_pw_aff_alloc(space1);
9577 for (i = 0; i < n_out; ++i) {
9578 isl_aff *aff;
9579 isl_union_pw_aff *upa;
9581 aff = isl_multi_aff_get_aff(ma, i);
9582 upa = multi_union_pw_aff_apply_aff(
9583 isl_multi_union_pw_aff_copy(mupa), aff);
9584 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9587 isl_multi_aff_free(ma);
9588 isl_multi_union_pw_aff_free(mupa);
9589 return res;
9590 error:
9591 isl_multi_union_pw_aff_free(mupa);
9592 isl_multi_aff_free(ma);
9593 return NULL;
9596 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9597 * The space of "mupa" is known to be compatible with the domain of "pa".
9599 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9600 * on the domain of "mupa".
9602 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9603 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9605 isl_union_set *dom;
9607 dom = isl_multi_union_pw_aff_domain(mupa);
9608 pa = isl_pw_aff_project_domain_on_params(pa);
9610 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9613 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9614 * with the domain of "pa".
9615 * Furthermore, the dimension of this space needs to be greater than zero.
9616 * The result is defined over the shared domain of the elements of "mupa"
9618 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9619 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9621 int i;
9622 isl_bool equal;
9623 isl_size n_in;
9624 isl_space *space, *space2;
9625 isl_union_pw_aff *upa;
9627 mupa = isl_multi_union_pw_aff_align_params(mupa,
9628 isl_pw_aff_get_space(pa));
9629 pa = isl_pw_aff_align_params(pa,
9630 isl_multi_union_pw_aff_get_space(mupa));
9631 if (!mupa || !pa)
9632 goto error;
9634 space = isl_multi_union_pw_aff_get_space(mupa);
9635 space2 = isl_pw_aff_get_domain_space(pa);
9636 equal = isl_space_is_equal(space, space2);
9637 isl_space_free(space);
9638 isl_space_free(space2);
9639 if (equal < 0)
9640 goto error;
9641 if (!equal)
9642 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9643 "spaces don't match", goto error);
9644 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9645 if (n_in < 0)
9646 goto error;
9647 if (n_in == 0)
9648 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9650 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9651 upa = isl_union_pw_aff_empty(space);
9653 for (i = 0; i < pa->n; ++i) {
9654 isl_aff *aff;
9655 isl_set *domain;
9656 isl_multi_union_pw_aff *mupa_i;
9657 isl_union_pw_aff *upa_i;
9659 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9660 domain = isl_set_copy(pa->p[i].set);
9661 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9662 aff = isl_aff_copy(pa->p[i].aff);
9663 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9664 upa = isl_union_pw_aff_union_add(upa, upa_i);
9667 isl_multi_union_pw_aff_free(mupa);
9668 isl_pw_aff_free(pa);
9669 return upa;
9670 error:
9671 isl_multi_union_pw_aff_free(mupa);
9672 isl_pw_aff_free(pa);
9673 return NULL;
9676 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9677 * The space of "mupa" is known to be compatible with the domain of "pma".
9679 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9680 * on the domain of "mupa".
9682 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9683 __isl_take isl_multi_union_pw_aff *mupa,
9684 __isl_take isl_pw_multi_aff *pma)
9686 isl_union_set *dom;
9688 dom = isl_multi_union_pw_aff_domain(mupa);
9689 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9691 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9694 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9695 * with the domain of "pma".
9696 * The result is defined over the shared domain of the elements of "mupa"
9698 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9699 __isl_take isl_multi_union_pw_aff *mupa,
9700 __isl_take isl_pw_multi_aff *pma)
9702 isl_space *space1, *space2;
9703 isl_multi_union_pw_aff *res;
9704 isl_bool equal;
9705 int i;
9706 isl_size n_in, n_out;
9708 mupa = isl_multi_union_pw_aff_align_params(mupa,
9709 isl_pw_multi_aff_get_space(pma));
9710 pma = isl_pw_multi_aff_align_params(pma,
9711 isl_multi_union_pw_aff_get_space(mupa));
9712 if (!mupa || !pma)
9713 goto error;
9715 space1 = isl_multi_union_pw_aff_get_space(mupa);
9716 space2 = isl_pw_multi_aff_get_domain_space(pma);
9717 equal = isl_space_is_equal(space1, space2);
9718 isl_space_free(space1);
9719 isl_space_free(space2);
9720 if (equal < 0)
9721 goto error;
9722 if (!equal)
9723 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9724 "spaces don't match", goto error);
9725 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9726 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9727 if (n_in < 0 || n_out < 0)
9728 goto error;
9729 if (n_in == 0)
9730 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9732 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9733 res = isl_multi_union_pw_aff_alloc(space1);
9735 for (i = 0; i < n_out; ++i) {
9736 isl_pw_aff *pa;
9737 isl_union_pw_aff *upa;
9739 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9740 upa = isl_multi_union_pw_aff_apply_pw_aff(
9741 isl_multi_union_pw_aff_copy(mupa), pa);
9742 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9745 isl_pw_multi_aff_free(pma);
9746 isl_multi_union_pw_aff_free(mupa);
9747 return res;
9748 error:
9749 isl_multi_union_pw_aff_free(mupa);
9750 isl_pw_multi_aff_free(pma);
9751 return NULL;
9754 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9755 * If the explicit domain only keeps track of constraints on the parameters,
9756 * then only update those constraints.
9758 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9759 __isl_take isl_multi_union_pw_aff *mupa,
9760 __isl_keep isl_union_pw_multi_aff *upma)
9762 isl_bool is_params;
9764 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9765 return isl_multi_union_pw_aff_free(mupa);
9767 mupa = isl_multi_union_pw_aff_cow(mupa);
9768 if (!mupa)
9769 return NULL;
9771 is_params = isl_union_set_is_params(mupa->u.dom);
9772 if (is_params < 0)
9773 return isl_multi_union_pw_aff_free(mupa);
9775 upma = isl_union_pw_multi_aff_copy(upma);
9776 if (is_params)
9777 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9778 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9779 else
9780 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9781 mupa->u.dom, upma);
9782 if (!mupa->u.dom)
9783 return isl_multi_union_pw_aff_free(mupa);
9784 return mupa;
9787 /* Compute the pullback of "mupa" by the function represented by "upma".
9788 * In other words, plug in "upma" in "mupa". The result contains
9789 * expressions defined over the domain space of "upma".
9791 * Run over all elements of "mupa" and plug in "upma" in each of them.
9793 * If "mupa" has an explicit domain, then it is this domain
9794 * that needs to undergo a pullback instead, i.e., a preimage.
9796 __isl_give isl_multi_union_pw_aff *
9797 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9798 __isl_take isl_multi_union_pw_aff *mupa,
9799 __isl_take isl_union_pw_multi_aff *upma)
9801 int i;
9802 isl_size n;
9804 mupa = isl_multi_union_pw_aff_align_params(mupa,
9805 isl_union_pw_multi_aff_get_space(upma));
9806 upma = isl_union_pw_multi_aff_align_params(upma,
9807 isl_multi_union_pw_aff_get_space(mupa));
9808 mupa = isl_multi_union_pw_aff_cow(mupa);
9809 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9810 if (n < 0 || !upma)
9811 goto error;
9813 for (i = 0; i < n; ++i) {
9814 isl_union_pw_aff *upa;
9816 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9817 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9818 isl_union_pw_multi_aff_copy(upma));
9819 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9822 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9823 mupa = preimage_explicit_domain(mupa, upma);
9825 isl_union_pw_multi_aff_free(upma);
9826 return mupa;
9827 error:
9828 isl_multi_union_pw_aff_free(mupa);
9829 isl_union_pw_multi_aff_free(upma);
9830 return NULL;
9833 /* Extract the sequence of elements in "mupa" with domain space "space"
9834 * (ignoring parameters).
9836 * For the elements of "mupa" that are not defined on the specified space,
9837 * the corresponding element in the result is empty.
9839 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9840 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9842 int i;
9843 isl_size n;
9844 isl_space *space_mpa;
9845 isl_multi_pw_aff *mpa;
9847 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9848 if (n < 0 || !space)
9849 goto error;
9851 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9852 space = isl_space_replace_params(space, space_mpa);
9853 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9854 space_mpa);
9855 mpa = isl_multi_pw_aff_alloc(space_mpa);
9857 space = isl_space_from_domain(space);
9858 space = isl_space_add_dims(space, isl_dim_out, 1);
9859 for (i = 0; i < n; ++i) {
9860 isl_union_pw_aff *upa;
9861 isl_pw_aff *pa;
9863 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9864 pa = isl_union_pw_aff_extract_pw_aff(upa,
9865 isl_space_copy(space));
9866 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9867 isl_union_pw_aff_free(upa);
9870 isl_space_free(space);
9871 return mpa;
9872 error:
9873 isl_space_free(space);
9874 return NULL;
9877 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9878 * should modify the base expressions in the input.
9880 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9881 * are taken into account.
9882 * "fn" is applied to each entry in the input.
9884 struct isl_union_pw_multi_aff_un_op_control {
9885 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9886 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9889 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9890 * a second argument) for use as an isl_union_pw_multi_aff_transform
9891 * filter function (which does take a second argument).
9892 * Simply call control->filter without the second argument.
9894 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9895 __isl_take isl_pw_multi_aff *pma, void *user)
9897 struct isl_union_pw_multi_aff_un_op_control *control = user;
9899 return control->filter(pma);
9902 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9903 * a second argument) for use as an isl_union_pw_multi_aff_transform
9904 * base function (which does take a second argument).
9905 * Simply call control->fn without the second argument.
9907 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9908 __isl_take isl_pw_multi_aff *pma, void *user)
9910 struct isl_union_pw_multi_aff_un_op_control *control = user;
9912 return control->fn(pma);
9915 /* Construct an isl_union_pw_multi_aff that is obtained by
9916 * modifying "upma" according to "control".
9918 * isl_union_pw_multi_aff_transform performs essentially
9919 * the same operation, but takes a filter and a callback function
9920 * of a different form (with an extra argument).
9921 * Call isl_union_pw_multi_aff_transform with wrappers
9922 * that remove this extra argument.
9924 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9925 __isl_take isl_union_pw_multi_aff *upma,
9926 struct isl_union_pw_multi_aff_un_op_control *control)
9928 struct isl_union_pw_multi_aff_transform_control t_control = {
9929 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9930 .filter_user = control,
9931 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9932 .fn_user = control,
9935 return isl_union_pw_multi_aff_transform(upma, &t_control);
9938 /* For each function in "upma" of the form A -> [B -> C],
9939 * extract the function A -> B and collect the results.
9941 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9942 __isl_take isl_union_pw_multi_aff *upma)
9944 struct isl_union_pw_multi_aff_un_op_control control = {
9945 .filter = &isl_pw_multi_aff_range_is_wrapping,
9946 .fn = &isl_pw_multi_aff_range_factor_domain,
9948 return isl_union_pw_multi_aff_un_op(upma, &control);
9951 /* For each function in "upma" of the form A -> [B -> C],
9952 * extract the function A -> C and collect the results.
9954 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9955 __isl_take isl_union_pw_multi_aff *upma)
9957 struct isl_union_pw_multi_aff_un_op_control control = {
9958 .filter = &isl_pw_multi_aff_range_is_wrapping,
9959 .fn = &isl_pw_multi_aff_range_factor_range,
9961 return isl_union_pw_multi_aff_un_op(upma, &control);
9964 /* Evaluate the affine function "aff" in the void point "pnt".
9965 * In particular, return the value NaN.
9967 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9968 __isl_take isl_point *pnt)
9970 isl_ctx *ctx;
9972 ctx = isl_point_get_ctx(pnt);
9973 isl_aff_free(aff);
9974 isl_point_free(pnt);
9975 return isl_val_nan(ctx);
9978 /* Evaluate the affine expression "aff"
9979 * in the coordinates (with denominator) "pnt".
9981 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9982 __isl_keep isl_vec *pnt)
9984 isl_int n, d;
9985 isl_ctx *ctx;
9986 isl_val *v;
9988 if (!aff || !pnt)
9989 return NULL;
9991 ctx = isl_vec_get_ctx(aff);
9992 isl_int_init(n);
9993 isl_int_init(d);
9994 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9995 isl_int_mul(d, aff->el[0], pnt->el[0]);
9996 v = isl_val_rat_from_isl_int(ctx, n, d);
9997 v = isl_val_normalize(v);
9998 isl_int_clear(n);
9999 isl_int_clear(d);
10001 return v;
10004 /* Check that the domain space of "aff" is equal to "space".
10006 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
10007 __isl_keep isl_space *space)
10009 isl_bool ok;
10011 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
10012 if (ok < 0)
10013 return isl_stat_error;
10014 if (!ok)
10015 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
10016 "incompatible spaces", return isl_stat_error);
10017 return isl_stat_ok;
10020 /* Evaluate the affine function "aff" in "pnt".
10022 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
10023 __isl_take isl_point *pnt)
10025 isl_bool is_void;
10026 isl_val *v;
10027 isl_local_space *ls;
10029 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
10030 goto error;
10031 is_void = isl_point_is_void(pnt);
10032 if (is_void < 0)
10033 goto error;
10034 if (is_void)
10035 return eval_void(aff, pnt);
10037 ls = isl_aff_get_domain_local_space(aff);
10038 pnt = isl_local_space_lift_point(ls, pnt);
10040 v = eval(aff->v, isl_point_peek_vec(pnt));
10042 isl_aff_free(aff);
10043 isl_point_free(pnt);
10045 return v;
10046 error:
10047 isl_aff_free(aff);
10048 isl_point_free(pnt);
10049 return NULL;