add exported isl_*_list_read_from_str
[isl.git] / isl_aff.c
blob40ca78dec6b29e7d549b939ff50afecd4bbf967d
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
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
13 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
14 * 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_union_map_private.h>
24 #include <isl_aff_private.h>
25 #include <isl_space_private.h>
26 #include <isl_local_space_private.h>
27 #include <isl_vec_private.h>
28 #include <isl_mat_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl_val_private.h>
34 #include <isl_point_private.h>
35 #include <isl_config.h>
37 #undef EL_BASE
38 #define EL_BASE aff
40 #include <isl_list_templ.c>
41 #include <isl_list_read_templ.c>
43 #undef EL_BASE
44 #define EL_BASE pw_aff
46 #include <isl_list_templ.c>
47 #include <isl_list_read_templ.c>
49 #undef EL_BASE
50 #define EL_BASE pw_multi_aff
52 #include <isl_list_templ.c>
53 #include <isl_list_read_templ.c>
55 #undef EL_BASE
56 #define EL_BASE union_pw_aff
58 #include <isl_list_templ.c>
59 #include <isl_list_read_templ.c>
61 #undef EL_BASE
62 #define EL_BASE union_pw_multi_aff
64 #include <isl_list_templ.c>
66 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
67 __isl_take isl_vec *v)
69 isl_aff *aff;
71 if (!ls || !v)
72 goto error;
74 aff = isl_calloc_type(v->ctx, struct isl_aff);
75 if (!aff)
76 goto error;
78 aff->ref = 1;
79 aff->ls = ls;
80 aff->v = v;
82 return aff;
83 error:
84 isl_local_space_free(ls);
85 isl_vec_free(v);
86 return NULL;
89 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
91 isl_ctx *ctx;
92 isl_vec *v;
93 isl_size total;
95 if (!ls)
96 return NULL;
98 ctx = isl_local_space_get_ctx(ls);
99 if (!isl_local_space_divs_known(ls))
100 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
101 goto error);
102 if (!isl_local_space_is_set(ls))
103 isl_die(ctx, isl_error_invalid,
104 "domain of affine expression should be a set",
105 goto error);
107 total = isl_local_space_dim(ls, isl_dim_all);
108 if (total < 0)
109 goto error;
110 v = isl_vec_alloc(ctx, 1 + 1 + total);
111 return isl_aff_alloc_vec(ls, v);
112 error:
113 isl_local_space_free(ls);
114 return NULL;
117 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
119 if (!aff)
120 return NULL;
122 aff->ref++;
123 return aff;
126 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
128 if (!aff)
129 return NULL;
131 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
132 isl_vec_copy(aff->v));
135 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
137 if (!aff)
138 return NULL;
140 if (aff->ref == 1)
141 return aff;
142 aff->ref--;
143 return isl_aff_dup(aff);
146 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
148 isl_aff *aff;
150 aff = isl_aff_alloc(ls);
151 if (!aff)
152 return NULL;
154 isl_int_set_si(aff->v->el[0], 1);
155 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
157 return aff;
160 /* Return an affine expression that is equal to zero on domain space "space".
162 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
164 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
167 /* This function performs the same operation as isl_aff_zero_on_domain_space,
168 * but is considered as a function on an isl_space when exported.
170 __isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
172 return isl_aff_zero_on_domain_space(space);
175 /* Return a piecewise affine expression defined on the specified domain
176 * that is equal to zero.
178 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
180 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
183 /* Change "aff" into a NaN.
185 * Note that this function gets called from isl_aff_nan_on_domain,
186 * so "aff" may not have been initialized yet.
188 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
190 aff = isl_aff_cow(aff);
191 if (!aff)
192 return NULL;
194 aff->v = isl_vec_clr(aff->v);
195 if (!aff->v)
196 return isl_aff_free(aff);
198 return aff;
201 /* Return an affine expression defined on the specified domain
202 * that represents NaN.
204 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
206 isl_aff *aff;
208 aff = isl_aff_alloc(ls);
209 return isl_aff_set_nan(aff);
212 /* Return an affine expression defined on the specified domain space
213 * that represents NaN.
215 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
217 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
220 /* Return a piecewise affine expression defined on the specified domain space
221 * that represents NaN.
223 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
224 __isl_take isl_space *space)
226 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
229 /* Return a piecewise affine expression defined on the specified domain
230 * that represents NaN.
232 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
234 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
237 /* Return an affine expression that is equal to "val" on
238 * domain local space "ls".
240 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
241 __isl_take isl_val *val)
243 isl_aff *aff;
245 if (!ls || !val)
246 goto error;
247 if (!isl_val_is_rat(val))
248 isl_die(isl_val_get_ctx(val), isl_error_invalid,
249 "expecting rational value", goto error);
251 aff = isl_aff_alloc(isl_local_space_copy(ls));
252 if (!aff)
253 goto error;
255 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
256 isl_int_set(aff->v->el[1], val->n);
257 isl_int_set(aff->v->el[0], val->d);
259 isl_local_space_free(ls);
260 isl_val_free(val);
261 return aff;
262 error:
263 isl_local_space_free(ls);
264 isl_val_free(val);
265 return NULL;
268 /* Return an affine expression that is equal to "val" on domain space "space".
270 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
271 __isl_take isl_val *val)
273 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
276 /* Return an affine expression that is equal to the specified dimension
277 * in "ls".
279 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
280 enum isl_dim_type type, unsigned pos)
282 isl_space *space;
283 isl_aff *aff;
285 if (!ls)
286 return NULL;
288 space = isl_local_space_get_space(ls);
289 if (!space)
290 goto error;
291 if (isl_space_is_map(space))
292 isl_die(isl_space_get_ctx(space), isl_error_invalid,
293 "expecting (parameter) set space", goto error);
294 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
295 goto error;
297 isl_space_free(space);
298 aff = isl_aff_alloc(ls);
299 if (!aff)
300 return NULL;
302 pos += isl_local_space_offset(aff->ls, type);
304 isl_int_set_si(aff->v->el[0], 1);
305 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
306 isl_int_set_si(aff->v->el[1 + pos], 1);
308 return aff;
309 error:
310 isl_local_space_free(ls);
311 isl_space_free(space);
312 return NULL;
315 /* Return a piecewise affine expression that is equal to
316 * the specified dimension in "ls".
318 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
319 enum isl_dim_type type, unsigned pos)
321 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
324 /* Return an affine expression that is equal to the parameter
325 * in the domain space "space" with identifier "id".
327 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
328 __isl_take isl_space *space, __isl_take isl_id *id)
330 int pos;
331 isl_local_space *ls;
333 if (!space || !id)
334 goto error;
335 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
336 if (pos < 0)
337 isl_die(isl_space_get_ctx(space), isl_error_invalid,
338 "parameter not found in space", goto error);
339 isl_id_free(id);
340 ls = isl_local_space_from_space(space);
341 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
342 error:
343 isl_space_free(space);
344 isl_id_free(id);
345 return NULL;
348 /* This function performs the same operation as
349 * isl_aff_param_on_domain_space_id,
350 * but is considered as a function on an isl_space when exported.
352 __isl_give isl_aff *isl_space_param_aff_on_domain_id(
353 __isl_take isl_space *space, __isl_take isl_id *id)
355 return isl_aff_param_on_domain_space_id(space, id);
358 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
360 if (!aff)
361 return NULL;
363 if (--aff->ref > 0)
364 return NULL;
366 isl_local_space_free(aff->ls);
367 isl_vec_free(aff->v);
369 free(aff);
371 return NULL;
374 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
376 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
379 /* Return a hash value that digests "aff".
381 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
383 uint32_t hash, ls_hash, v_hash;
385 if (!aff)
386 return 0;
388 hash = isl_hash_init();
389 ls_hash = isl_local_space_get_hash(aff->ls);
390 isl_hash_hash(hash, ls_hash);
391 v_hash = isl_vec_get_hash(aff->v);
392 isl_hash_hash(hash, v_hash);
394 return hash;
397 /* Return the domain local space of "aff".
399 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
400 __isl_keep isl_aff *aff)
402 return aff ? aff->ls : NULL;
405 /* Return the number of variables of the given type in the domain of "aff".
407 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
409 isl_local_space *ls;
411 ls = isl_aff_peek_domain_local_space(aff);
412 return isl_local_space_dim(ls, type);
415 /* Externally, an isl_aff has a map space, but internally, the
416 * ls field corresponds to the domain of that space.
418 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
420 if (!aff)
421 return isl_size_error;
422 if (type == isl_dim_out)
423 return 1;
424 if (type == isl_dim_in)
425 type = isl_dim_set;
426 return isl_aff_domain_dim(aff, type);
429 /* Return the offset of the first coefficient of type "type" in
430 * the domain of "aff".
432 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
434 isl_local_space *ls;
436 ls = isl_aff_peek_domain_local_space(aff);
437 return isl_local_space_offset(ls, type);
440 /* Return the position of the dimension of the given type and name
441 * in "aff".
442 * Return -1 if no such dimension can be found.
444 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
445 const char *name)
447 if (!aff)
448 return -1;
449 if (type == isl_dim_out)
450 return -1;
451 if (type == isl_dim_in)
452 type = isl_dim_set;
453 return isl_local_space_find_dim_by_name(aff->ls, type, name);
456 /* Return the domain space of "aff".
458 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
460 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
463 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
465 return isl_space_copy(isl_aff_peek_domain_space(aff));
468 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
470 isl_space *space;
471 if (!aff)
472 return NULL;
473 space = isl_local_space_get_space(aff->ls);
474 space = isl_space_from_domain(space);
475 space = isl_space_add_dims(space, isl_dim_out, 1);
476 return space;
479 /* Return a copy of the domain space of "aff".
481 __isl_give isl_local_space *isl_aff_get_domain_local_space(
482 __isl_keep isl_aff *aff)
484 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
487 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
489 isl_local_space *ls;
490 if (!aff)
491 return NULL;
492 ls = isl_local_space_copy(aff->ls);
493 ls = isl_local_space_from_domain(ls);
494 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
495 return ls;
498 /* Return the local space of the domain of "aff".
499 * This may be either a copy or the local space itself
500 * if there is only one reference to "aff".
501 * This allows the local space to be modified inplace
502 * if both the expression and its local space have only a single reference.
503 * The caller is not allowed to modify "aff" between this call and
504 * a subsequent call to isl_aff_restore_domain_local_space.
505 * The only exception is that isl_aff_free can be called instead.
507 __isl_give isl_local_space *isl_aff_take_domain_local_space(
508 __isl_keep isl_aff *aff)
510 isl_local_space *ls;
512 if (!aff)
513 return NULL;
514 if (aff->ref != 1)
515 return isl_aff_get_domain_local_space(aff);
516 ls = aff->ls;
517 aff->ls = NULL;
518 return ls;
521 /* Set the local space of the domain of "aff" to "ls",
522 * where the local space of "aff" may be missing
523 * due to a preceding call to isl_aff_take_domain_local_space.
524 * However, in this case, "aff" only has a single reference and
525 * then the call to isl_aff_cow has no effect.
527 __isl_give isl_aff *isl_aff_restore_domain_local_space(
528 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
530 if (!aff || !ls)
531 goto error;
533 if (aff->ls == ls) {
534 isl_local_space_free(ls);
535 return aff;
538 aff = isl_aff_cow(aff);
539 if (!aff)
540 goto error;
541 isl_local_space_free(aff->ls);
542 aff->ls = ls;
544 return aff;
545 error:
546 isl_aff_free(aff);
547 isl_local_space_free(ls);
548 return NULL;
551 /* Externally, an isl_aff has a map space, but internally, the
552 * ls field corresponds to the domain of that space.
554 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
555 enum isl_dim_type type, unsigned pos)
557 if (!aff)
558 return NULL;
559 if (type == isl_dim_out)
560 return NULL;
561 if (type == isl_dim_in)
562 type = isl_dim_set;
563 return isl_local_space_get_dim_name(aff->ls, type, pos);
566 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
567 __isl_take isl_space *space)
569 aff = isl_aff_cow(aff);
570 if (!aff || !space)
571 goto error;
573 aff->ls = isl_local_space_reset_space(aff->ls, space);
574 if (!aff->ls)
575 return isl_aff_free(aff);
577 return aff;
578 error:
579 isl_aff_free(aff);
580 isl_space_free(space);
581 return NULL;
584 /* Reset the space of "aff". This function is called from isl_pw_templ.c
585 * and doesn't know if the space of an element object is represented
586 * directly or through its domain. It therefore passes along both.
588 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
589 __isl_take isl_space *space, __isl_take isl_space *domain)
591 isl_space_free(space);
592 return isl_aff_reset_domain_space(aff, domain);
595 /* Reorder the coefficients of the affine expression based
596 * on the given reordering.
597 * The reordering r is assumed to have been extended with the local
598 * variables.
600 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
601 __isl_take isl_reordering *r, int n_div)
603 isl_space *space;
604 isl_vec *res;
605 isl_size dim;
606 int i;
608 if (!vec || !r)
609 goto error;
611 space = isl_reordering_peek_space(r);
612 dim = isl_space_dim(space, isl_dim_all);
613 if (dim < 0)
614 goto error;
615 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
616 if (!res)
617 goto error;
618 isl_seq_cpy(res->el, vec->el, 2);
619 isl_seq_clr(res->el + 2, res->size - 2);
620 for (i = 0; i < r->len; ++i)
621 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
623 isl_reordering_free(r);
624 isl_vec_free(vec);
625 return res;
626 error:
627 isl_vec_free(vec);
628 isl_reordering_free(r);
629 return NULL;
632 /* Reorder the dimensions of the domain of "aff" according
633 * to the given reordering.
635 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
636 __isl_take isl_reordering *r)
638 aff = isl_aff_cow(aff);
639 if (!aff)
640 goto error;
642 r = isl_reordering_extend(r, aff->ls->div->n_row);
643 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
644 aff->ls->div->n_row);
645 aff->ls = isl_local_space_realign(aff->ls, r);
647 if (!aff->v || !aff->ls)
648 return isl_aff_free(aff);
650 return aff;
651 error:
652 isl_aff_free(aff);
653 isl_reordering_free(r);
654 return NULL;
657 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
658 __isl_take isl_space *model)
660 isl_bool equal_params;
662 if (!aff || !model)
663 goto error;
665 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
666 if (equal_params < 0)
667 goto error;
668 if (!equal_params) {
669 isl_reordering *exp;
671 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
672 exp = isl_reordering_extend_space(exp,
673 isl_aff_get_domain_space(aff));
674 aff = isl_aff_realign_domain(aff, exp);
677 isl_space_free(model);
678 return aff;
679 error:
680 isl_space_free(model);
681 isl_aff_free(aff);
682 return NULL;
685 #undef TYPE
686 #define TYPE isl_aff
687 #include "isl_unbind_params_templ.c"
689 /* Is "aff" obviously equal to zero?
691 * If the denominator is zero, then "aff" is not equal to zero.
693 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
695 int pos;
697 if (!aff)
698 return isl_bool_error;
700 if (isl_int_is_zero(aff->v->el[0]))
701 return isl_bool_false;
702 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
703 return isl_bool_ok(pos < 0);
706 /* Does "aff" represent NaN?
708 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
710 if (!aff)
711 return isl_bool_error;
713 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
716 /* Are "aff1" and "aff2" obviously equal?
718 * NaN is not equal to anything, not even to another NaN.
720 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
721 __isl_keep isl_aff *aff2)
723 isl_bool equal;
725 if (!aff1 || !aff2)
726 return isl_bool_error;
728 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
729 return isl_bool_false;
731 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
732 if (equal < 0 || !equal)
733 return equal;
735 return isl_vec_is_equal(aff1->v, aff2->v);
738 /* Return the common denominator of "aff" in "v".
740 * We cannot return anything meaningful in case of a NaN.
742 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
744 if (!aff)
745 return isl_stat_error;
746 if (isl_aff_is_nan(aff))
747 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
748 "cannot get denominator of NaN", return isl_stat_error);
749 isl_int_set(*v, aff->v->el[0]);
750 return isl_stat_ok;
753 /* Return the common denominator of "aff".
755 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
757 isl_ctx *ctx;
759 if (!aff)
760 return NULL;
762 ctx = isl_aff_get_ctx(aff);
763 if (isl_aff_is_nan(aff))
764 return isl_val_nan(ctx);
765 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
768 /* Return the constant term of "aff".
770 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
772 isl_ctx *ctx;
773 isl_val *v;
775 if (!aff)
776 return NULL;
778 ctx = isl_aff_get_ctx(aff);
779 if (isl_aff_is_nan(aff))
780 return isl_val_nan(ctx);
781 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
782 return isl_val_normalize(v);
785 /* Return the coefficient of the variable of type "type" at position "pos"
786 * of "aff".
788 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
789 enum isl_dim_type type, int pos)
791 isl_ctx *ctx;
792 isl_val *v;
794 if (!aff)
795 return NULL;
797 ctx = isl_aff_get_ctx(aff);
798 if (type == isl_dim_out)
799 isl_die(ctx, isl_error_invalid,
800 "output/set dimension does not have a coefficient",
801 return NULL);
802 if (type == isl_dim_in)
803 type = isl_dim_set;
805 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
806 return NULL;
808 if (isl_aff_is_nan(aff))
809 return isl_val_nan(ctx);
810 pos += isl_local_space_offset(aff->ls, type);
811 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
812 return isl_val_normalize(v);
815 /* Return the sign of the coefficient of the variable of type "type"
816 * at position "pos" of "aff".
818 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
819 int pos)
821 isl_ctx *ctx;
823 if (!aff)
824 return 0;
826 ctx = isl_aff_get_ctx(aff);
827 if (type == isl_dim_out)
828 isl_die(ctx, isl_error_invalid,
829 "output/set dimension does not have a coefficient",
830 return 0);
831 if (type == isl_dim_in)
832 type = isl_dim_set;
834 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
835 return 0;
837 pos += isl_local_space_offset(aff->ls, type);
838 return isl_int_sgn(aff->v->el[1 + pos]);
841 /* Replace the numerator of the constant term of "aff" by "v".
843 * A NaN is unaffected by this operation.
845 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
847 if (!aff)
848 return NULL;
849 if (isl_aff_is_nan(aff))
850 return aff;
851 aff = isl_aff_cow(aff);
852 if (!aff)
853 return NULL;
855 aff->v = isl_vec_cow(aff->v);
856 if (!aff->v)
857 return isl_aff_free(aff);
859 isl_int_set(aff->v->el[1], v);
861 return aff;
864 /* Replace the constant term of "aff" by "v".
866 * A NaN is unaffected by this operation.
868 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
869 __isl_take isl_val *v)
871 if (!aff || !v)
872 goto error;
874 if (isl_aff_is_nan(aff)) {
875 isl_val_free(v);
876 return aff;
879 if (!isl_val_is_rat(v))
880 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
881 "expecting rational value", goto error);
883 if (isl_int_eq(aff->v->el[1], v->n) &&
884 isl_int_eq(aff->v->el[0], v->d)) {
885 isl_val_free(v);
886 return aff;
889 aff = isl_aff_cow(aff);
890 if (!aff)
891 goto error;
892 aff->v = isl_vec_cow(aff->v);
893 if (!aff->v)
894 goto error;
896 if (isl_int_eq(aff->v->el[0], v->d)) {
897 isl_int_set(aff->v->el[1], v->n);
898 } else if (isl_int_is_one(v->d)) {
899 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
900 } else {
901 isl_seq_scale(aff->v->el + 1,
902 aff->v->el + 1, v->d, aff->v->size - 1);
903 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
904 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
905 aff->v = isl_vec_normalize(aff->v);
906 if (!aff->v)
907 goto error;
910 isl_val_free(v);
911 return aff;
912 error:
913 isl_aff_free(aff);
914 isl_val_free(v);
915 return NULL;
918 /* Add "v" to the constant term of "aff".
920 * A NaN is unaffected by this operation.
922 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
924 if (isl_int_is_zero(v))
925 return aff;
927 if (!aff)
928 return NULL;
929 if (isl_aff_is_nan(aff))
930 return aff;
931 aff = isl_aff_cow(aff);
932 if (!aff)
933 return NULL;
935 aff->v = isl_vec_cow(aff->v);
936 if (!aff->v)
937 return isl_aff_free(aff);
939 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
941 return aff;
944 /* Add "v" to the constant term of "aff",
945 * in case "aff" is a rational expression.
947 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
948 __isl_take isl_val *v)
950 aff = isl_aff_cow(aff);
951 if (!aff)
952 goto error;
954 aff->v = isl_vec_cow(aff->v);
955 if (!aff->v)
956 goto error;
958 if (isl_int_is_one(v->d)) {
959 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
960 } else if (isl_int_eq(aff->v->el[0], v->d)) {
961 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
962 aff->v = isl_vec_normalize(aff->v);
963 if (!aff->v)
964 goto error;
965 } else {
966 isl_seq_scale(aff->v->el + 1,
967 aff->v->el + 1, v->d, aff->v->size - 1);
968 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
969 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
970 aff->v = isl_vec_normalize(aff->v);
971 if (!aff->v)
972 goto error;
975 isl_val_free(v);
976 return aff;
977 error:
978 isl_aff_free(aff);
979 isl_val_free(v);
980 return NULL;
983 /* Return the first argument and free the second.
985 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
986 __isl_take isl_val *v)
988 isl_val_free(v);
989 return aff;
992 /* Replace the first argument by NaN and free the second argument.
994 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
995 __isl_take isl_val *v)
997 isl_val_free(v);
998 return isl_aff_set_nan(aff);
1001 /* Add "v" to the constant term of "aff".
1003 * A NaN is unaffected by this operation.
1004 * Conversely, adding a NaN turns "aff" into a NaN.
1006 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
1007 __isl_take isl_val *v)
1009 isl_bool is_nan, is_zero, is_rat;
1011 is_nan = isl_aff_is_nan(aff);
1012 is_zero = isl_val_is_zero(v);
1013 if (is_nan < 0 || is_zero < 0)
1014 goto error;
1015 if (is_nan || is_zero)
1016 return pick_free(aff, v);
1018 is_nan = isl_val_is_nan(v);
1019 is_rat = isl_val_is_rat(v);
1020 if (is_nan < 0 || is_rat < 0)
1021 goto error;
1022 if (is_nan)
1023 return set_nan_free_val(aff, v);
1024 if (!is_rat)
1025 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1026 "expecting rational value or NaN", goto error);
1028 return isl_aff_add_rat_constant_val(aff, v);
1029 error:
1030 isl_aff_free(aff);
1031 isl_val_free(v);
1032 return NULL;
1035 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1037 isl_int t;
1039 isl_int_init(t);
1040 isl_int_set_si(t, v);
1041 aff = isl_aff_add_constant(aff, t);
1042 isl_int_clear(t);
1044 return aff;
1047 /* Add "v" to the numerator of the constant term of "aff".
1049 * A NaN is unaffected by this operation.
1051 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1053 if (isl_int_is_zero(v))
1054 return aff;
1056 if (!aff)
1057 return NULL;
1058 if (isl_aff_is_nan(aff))
1059 return aff;
1060 aff = isl_aff_cow(aff);
1061 if (!aff)
1062 return NULL;
1064 aff->v = isl_vec_cow(aff->v);
1065 if (!aff->v)
1066 return isl_aff_free(aff);
1068 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1070 return aff;
1073 /* Add "v" to the numerator of the constant term of "aff".
1075 * A NaN is unaffected by this operation.
1077 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1079 isl_int t;
1081 if (v == 0)
1082 return aff;
1084 isl_int_init(t);
1085 isl_int_set_si(t, v);
1086 aff = isl_aff_add_constant_num(aff, t);
1087 isl_int_clear(t);
1089 return aff;
1092 /* Replace the numerator of the constant term of "aff" by "v".
1094 * A NaN is unaffected by this operation.
1096 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1098 if (!aff)
1099 return NULL;
1100 if (isl_aff_is_nan(aff))
1101 return aff;
1102 aff = isl_aff_cow(aff);
1103 if (!aff)
1104 return NULL;
1106 aff->v = isl_vec_cow(aff->v);
1107 if (!aff->v)
1108 return isl_aff_free(aff);
1110 isl_int_set_si(aff->v->el[1], v);
1112 return aff;
1115 /* Replace the numerator of the coefficient of the variable of type "type"
1116 * at position "pos" of "aff" by "v".
1118 * A NaN is unaffected by this operation.
1120 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1121 enum isl_dim_type type, int pos, isl_int v)
1123 if (!aff)
1124 return NULL;
1126 if (type == isl_dim_out)
1127 isl_die(aff->v->ctx, isl_error_invalid,
1128 "output/set dimension does not have a coefficient",
1129 return isl_aff_free(aff));
1130 if (type == isl_dim_in)
1131 type = isl_dim_set;
1133 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1134 return isl_aff_free(aff);
1136 if (isl_aff_is_nan(aff))
1137 return aff;
1138 aff = isl_aff_cow(aff);
1139 if (!aff)
1140 return NULL;
1142 aff->v = isl_vec_cow(aff->v);
1143 if (!aff->v)
1144 return isl_aff_free(aff);
1146 pos += isl_local_space_offset(aff->ls, type);
1147 isl_int_set(aff->v->el[1 + pos], v);
1149 return aff;
1152 /* Replace the numerator of the coefficient of the variable of type "type"
1153 * at position "pos" of "aff" by "v".
1155 * A NaN is unaffected by this operation.
1157 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1158 enum isl_dim_type type, int pos, int v)
1160 if (!aff)
1161 return NULL;
1163 if (type == isl_dim_out)
1164 isl_die(aff->v->ctx, isl_error_invalid,
1165 "output/set dimension does not have a coefficient",
1166 return isl_aff_free(aff));
1167 if (type == isl_dim_in)
1168 type = isl_dim_set;
1170 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1171 return isl_aff_free(aff);
1173 if (isl_aff_is_nan(aff))
1174 return aff;
1175 pos += isl_local_space_offset(aff->ls, type);
1176 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1177 return aff;
1179 aff = isl_aff_cow(aff);
1180 if (!aff)
1181 return NULL;
1183 aff->v = isl_vec_cow(aff->v);
1184 if (!aff->v)
1185 return isl_aff_free(aff);
1187 isl_int_set_si(aff->v->el[1 + pos], v);
1189 return aff;
1192 /* Replace the coefficient of the variable of type "type" at position "pos"
1193 * of "aff" by "v".
1195 * A NaN is unaffected by this operation.
1197 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1198 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1200 if (!aff || !v)
1201 goto error;
1203 if (type == isl_dim_out)
1204 isl_die(aff->v->ctx, isl_error_invalid,
1205 "output/set dimension does not have a coefficient",
1206 goto error);
1207 if (type == isl_dim_in)
1208 type = isl_dim_set;
1210 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1211 return isl_aff_free(aff);
1213 if (isl_aff_is_nan(aff)) {
1214 isl_val_free(v);
1215 return aff;
1217 if (!isl_val_is_rat(v))
1218 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1219 "expecting rational value", goto error);
1221 pos += isl_local_space_offset(aff->ls, type);
1222 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1223 isl_int_eq(aff->v->el[0], v->d)) {
1224 isl_val_free(v);
1225 return aff;
1228 aff = isl_aff_cow(aff);
1229 if (!aff)
1230 goto error;
1231 aff->v = isl_vec_cow(aff->v);
1232 if (!aff->v)
1233 goto error;
1235 if (isl_int_eq(aff->v->el[0], v->d)) {
1236 isl_int_set(aff->v->el[1 + pos], v->n);
1237 } else if (isl_int_is_one(v->d)) {
1238 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1239 } else {
1240 isl_seq_scale(aff->v->el + 1,
1241 aff->v->el + 1, v->d, aff->v->size - 1);
1242 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1243 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1244 aff->v = isl_vec_normalize(aff->v);
1245 if (!aff->v)
1246 goto error;
1249 isl_val_free(v);
1250 return aff;
1251 error:
1252 isl_aff_free(aff);
1253 isl_val_free(v);
1254 return NULL;
1257 /* Add "v" to the coefficient of the variable of type "type"
1258 * at position "pos" of "aff".
1260 * A NaN is unaffected by this operation.
1262 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1263 enum isl_dim_type type, int pos, isl_int v)
1265 if (!aff)
1266 return NULL;
1268 if (type == isl_dim_out)
1269 isl_die(aff->v->ctx, isl_error_invalid,
1270 "output/set dimension does not have a coefficient",
1271 return isl_aff_free(aff));
1272 if (type == isl_dim_in)
1273 type = isl_dim_set;
1275 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1276 return isl_aff_free(aff);
1278 if (isl_aff_is_nan(aff))
1279 return aff;
1280 aff = isl_aff_cow(aff);
1281 if (!aff)
1282 return NULL;
1284 aff->v = isl_vec_cow(aff->v);
1285 if (!aff->v)
1286 return isl_aff_free(aff);
1288 pos += isl_local_space_offset(aff->ls, type);
1289 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1291 return aff;
1294 /* Add "v" to the coefficient of the variable of type "type"
1295 * at position "pos" of "aff".
1297 * A NaN is unaffected by this operation.
1299 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1300 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1302 if (!aff || !v)
1303 goto error;
1305 if (isl_val_is_zero(v)) {
1306 isl_val_free(v);
1307 return aff;
1310 if (type == isl_dim_out)
1311 isl_die(aff->v->ctx, isl_error_invalid,
1312 "output/set dimension does not have a coefficient",
1313 goto error);
1314 if (type == isl_dim_in)
1315 type = isl_dim_set;
1317 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1318 goto error;
1320 if (isl_aff_is_nan(aff)) {
1321 isl_val_free(v);
1322 return aff;
1324 if (!isl_val_is_rat(v))
1325 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1326 "expecting rational value", goto error);
1328 aff = isl_aff_cow(aff);
1329 if (!aff)
1330 goto error;
1332 aff->v = isl_vec_cow(aff->v);
1333 if (!aff->v)
1334 goto error;
1336 pos += isl_local_space_offset(aff->ls, type);
1337 if (isl_int_is_one(v->d)) {
1338 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1339 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1340 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1341 aff->v = isl_vec_normalize(aff->v);
1342 if (!aff->v)
1343 goto error;
1344 } else {
1345 isl_seq_scale(aff->v->el + 1,
1346 aff->v->el + 1, v->d, aff->v->size - 1);
1347 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1348 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1349 aff->v = isl_vec_normalize(aff->v);
1350 if (!aff->v)
1351 goto error;
1354 isl_val_free(v);
1355 return aff;
1356 error:
1357 isl_aff_free(aff);
1358 isl_val_free(v);
1359 return NULL;
1362 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1363 enum isl_dim_type type, int pos, int v)
1365 isl_int t;
1367 isl_int_init(t);
1368 isl_int_set_si(t, v);
1369 aff = isl_aff_add_coefficient(aff, type, pos, t);
1370 isl_int_clear(t);
1372 return aff;
1375 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1377 if (!aff)
1378 return NULL;
1380 return isl_local_space_get_div(aff->ls, pos);
1383 /* Return the negation of "aff".
1385 * As a special case, -NaN = NaN.
1387 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1389 if (!aff)
1390 return NULL;
1391 if (isl_aff_is_nan(aff))
1392 return aff;
1393 aff = isl_aff_cow(aff);
1394 if (!aff)
1395 return NULL;
1396 aff->v = isl_vec_cow(aff->v);
1397 if (!aff->v)
1398 return isl_aff_free(aff);
1400 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1402 return aff;
1405 /* Remove divs from the local space that do not appear in the affine
1406 * expression.
1407 * We currently only remove divs at the end.
1408 * Some intermediate divs may also not appear directly in the affine
1409 * expression, but we would also need to check that no other divs are
1410 * defined in terms of them.
1412 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1414 int pos;
1415 isl_size off;
1416 isl_size n;
1418 n = isl_aff_domain_dim(aff, isl_dim_div);
1419 off = isl_aff_domain_offset(aff, isl_dim_div);
1420 if (n < 0 || off < 0)
1421 return isl_aff_free(aff);
1423 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1424 if (pos == n)
1425 return aff;
1427 aff = isl_aff_cow(aff);
1428 if (!aff)
1429 return NULL;
1431 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1432 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1433 if (!aff->ls || !aff->v)
1434 return isl_aff_free(aff);
1436 return aff;
1439 /* Look for any divs in the aff->ls with a denominator equal to one
1440 * and plug them into the affine expression and any subsequent divs
1441 * that may reference the div.
1443 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1445 int i;
1446 isl_size n;
1447 int len;
1448 isl_int v;
1449 isl_vec *vec;
1450 isl_local_space *ls;
1451 isl_size off;
1453 n = isl_aff_domain_dim(aff, isl_dim_div);
1454 off = isl_aff_domain_offset(aff, isl_dim_div);
1455 if (n < 0 || off < 0)
1456 return isl_aff_free(aff);
1457 len = aff->v->size;
1458 for (i = 0; i < n; ++i) {
1459 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1460 continue;
1461 ls = isl_local_space_copy(aff->ls);
1462 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1463 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1464 vec = isl_vec_copy(aff->v);
1465 vec = isl_vec_cow(vec);
1466 if (!ls || !vec)
1467 goto error;
1469 isl_int_init(v);
1471 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1472 len, len, v);
1474 isl_int_clear(v);
1476 isl_vec_free(aff->v);
1477 aff->v = vec;
1478 isl_local_space_free(aff->ls);
1479 aff->ls = ls;
1482 return aff;
1483 error:
1484 isl_vec_free(vec);
1485 isl_local_space_free(ls);
1486 return isl_aff_free(aff);
1489 /* Look for any divs j that appear with a unit coefficient inside
1490 * the definitions of other divs i and plug them into the definitions
1491 * of the divs i.
1493 * In particular, an expression of the form
1495 * floor((f(..) + floor(g(..)/n))/m)
1497 * is simplified to
1499 * floor((n * f(..) + g(..))/(n * m))
1501 * This simplification is correct because we can move the expression
1502 * f(..) into the inner floor in the original expression to obtain
1504 * floor(floor((n * f(..) + g(..))/n)/m)
1506 * from which we can derive the simplified expression.
1508 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1510 int i, j;
1511 isl_size n;
1512 isl_size off;
1514 n = isl_aff_domain_dim(aff, isl_dim_div);
1515 off = isl_aff_domain_offset(aff, isl_dim_div);
1516 if (n < 0 || off < 0)
1517 return isl_aff_free(aff);
1518 for (i = 1; i < n; ++i) {
1519 for (j = 0; j < i; ++j) {
1520 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1521 continue;
1522 aff->ls = isl_local_space_substitute_seq(aff->ls,
1523 isl_dim_div, j, aff->ls->div->row[j],
1524 aff->v->size, i, 1);
1525 if (!aff->ls)
1526 return isl_aff_free(aff);
1530 return aff;
1533 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1535 * Even though this function is only called on isl_affs with a single
1536 * reference, we are careful to only change aff->v and aff->ls together.
1538 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1540 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1541 isl_local_space *ls;
1542 isl_vec *v;
1544 if (off < 0)
1545 return isl_aff_free(aff);
1547 ls = isl_local_space_copy(aff->ls);
1548 ls = isl_local_space_swap_div(ls, a, b);
1549 v = isl_vec_copy(aff->v);
1550 v = isl_vec_cow(v);
1551 if (!ls || !v)
1552 goto error;
1554 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1555 isl_vec_free(aff->v);
1556 aff->v = v;
1557 isl_local_space_free(aff->ls);
1558 aff->ls = ls;
1560 return aff;
1561 error:
1562 isl_vec_free(v);
1563 isl_local_space_free(ls);
1564 return isl_aff_free(aff);
1567 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1569 * We currently do not actually remove div "b", but simply add its
1570 * coefficient to that of "a" and then zero it out.
1572 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1574 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1576 if (off < 0)
1577 return isl_aff_free(aff);
1579 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1580 return aff;
1582 aff->v = isl_vec_cow(aff->v);
1583 if (!aff->v)
1584 return isl_aff_free(aff);
1586 isl_int_add(aff->v->el[1 + off + a],
1587 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1588 isl_int_set_si(aff->v->el[1 + off + b], 0);
1590 return aff;
1593 /* Sort the divs in the local space of "aff" according to
1594 * the comparison function "cmp_row" in isl_local_space.c,
1595 * combining the coefficients of identical divs.
1597 * Reordering divs does not change the semantics of "aff",
1598 * so there is no need to call isl_aff_cow.
1599 * Moreover, this function is currently only called on isl_affs
1600 * with a single reference.
1602 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1604 isl_size n;
1605 int i, j;
1607 n = isl_aff_dim(aff, isl_dim_div);
1608 if (n < 0)
1609 return isl_aff_free(aff);
1610 for (i = 1; i < n; ++i) {
1611 for (j = i - 1; j >= 0; --j) {
1612 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1613 if (cmp < 0)
1614 break;
1615 if (cmp == 0)
1616 aff = merge_divs(aff, j, j + 1);
1617 else
1618 aff = swap_div(aff, j, j + 1);
1619 if (!aff)
1620 return NULL;
1624 return aff;
1627 /* Normalize the representation of "aff".
1629 * This function should only be called on "new" isl_affs, i.e.,
1630 * with only a single reference. We therefore do not need to
1631 * worry about affecting other instances.
1633 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1635 if (!aff)
1636 return NULL;
1637 aff->v = isl_vec_normalize(aff->v);
1638 if (!aff->v)
1639 return isl_aff_free(aff);
1640 aff = plug_in_integral_divs(aff);
1641 aff = plug_in_unit_divs(aff);
1642 aff = sort_divs(aff);
1643 aff = isl_aff_remove_unused_divs(aff);
1644 return aff;
1647 /* Given f, return floor(f).
1648 * If f is an integer expression, then just return f.
1649 * If f is a constant, then return the constant floor(f).
1650 * Otherwise, if f = g/m, write g = q m + r,
1651 * create a new div d = [r/m] and return the expression q + d.
1652 * The coefficients in r are taken to lie between -m/2 and m/2.
1654 * reduce_div_coefficients performs the same normalization.
1656 * As a special case, floor(NaN) = NaN.
1658 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1660 int i;
1661 int size;
1662 isl_ctx *ctx;
1663 isl_vec *div;
1665 if (!aff)
1666 return NULL;
1668 if (isl_aff_is_nan(aff))
1669 return aff;
1670 if (isl_int_is_one(aff->v->el[0]))
1671 return aff;
1673 aff = isl_aff_cow(aff);
1674 if (!aff)
1675 return NULL;
1677 aff->v = isl_vec_cow(aff->v);
1678 if (!aff->v)
1679 return isl_aff_free(aff);
1681 if (isl_aff_is_cst(aff)) {
1682 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1683 isl_int_set_si(aff->v->el[0], 1);
1684 return aff;
1687 div = isl_vec_copy(aff->v);
1688 div = isl_vec_cow(div);
1689 if (!div)
1690 return isl_aff_free(aff);
1692 ctx = isl_aff_get_ctx(aff);
1693 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1694 for (i = 1; i < aff->v->size; ++i) {
1695 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1696 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1697 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1698 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1699 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1703 aff->ls = isl_local_space_add_div(aff->ls, div);
1704 if (!aff->ls)
1705 return isl_aff_free(aff);
1707 size = aff->v->size;
1708 aff->v = isl_vec_extend(aff->v, size + 1);
1709 if (!aff->v)
1710 return isl_aff_free(aff);
1711 isl_int_set_si(aff->v->el[0], 1);
1712 isl_int_set_si(aff->v->el[size], 1);
1714 aff = isl_aff_normalize(aff);
1716 return aff;
1719 /* Compute
1721 * aff mod m = aff - m * floor(aff/m)
1723 * with m an integer value.
1725 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1726 __isl_take isl_val *m)
1728 isl_aff *res;
1730 if (!aff || !m)
1731 goto error;
1733 if (!isl_val_is_int(m))
1734 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1735 "expecting integer modulo", goto error);
1737 res = isl_aff_copy(aff);
1738 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1739 aff = isl_aff_floor(aff);
1740 aff = isl_aff_scale_val(aff, m);
1741 res = isl_aff_sub(res, aff);
1743 return res;
1744 error:
1745 isl_aff_free(aff);
1746 isl_val_free(m);
1747 return NULL;
1750 /* Compute
1752 * pwaff mod m = pwaff - m * floor(pwaff/m)
1754 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1756 isl_pw_aff *res;
1758 res = isl_pw_aff_copy(pwaff);
1759 pwaff = isl_pw_aff_scale_down(pwaff, m);
1760 pwaff = isl_pw_aff_floor(pwaff);
1761 pwaff = isl_pw_aff_scale(pwaff, m);
1762 res = isl_pw_aff_sub(res, pwaff);
1764 return res;
1767 /* Compute
1769 * pa mod m = pa - m * floor(pa/m)
1771 * with m an integer value.
1773 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1774 __isl_take isl_val *m)
1776 if (!pa || !m)
1777 goto error;
1778 if (!isl_val_is_int(m))
1779 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1780 "expecting integer modulo", goto error);
1781 pa = isl_pw_aff_mod(pa, m->n);
1782 isl_val_free(m);
1783 return pa;
1784 error:
1785 isl_pw_aff_free(pa);
1786 isl_val_free(m);
1787 return NULL;
1790 /* Given f, return ceil(f).
1791 * If f is an integer expression, then just return f.
1792 * Otherwise, let f be the expression
1794 * e/m
1796 * then return
1798 * floor((e + m - 1)/m)
1800 * As a special case, ceil(NaN) = NaN.
1802 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1804 if (!aff)
1805 return NULL;
1807 if (isl_aff_is_nan(aff))
1808 return aff;
1809 if (isl_int_is_one(aff->v->el[0]))
1810 return aff;
1812 aff = isl_aff_cow(aff);
1813 if (!aff)
1814 return NULL;
1815 aff->v = isl_vec_cow(aff->v);
1816 if (!aff->v)
1817 return isl_aff_free(aff);
1819 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1820 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1821 aff = isl_aff_floor(aff);
1823 return aff;
1826 /* Apply the expansion computed by isl_merge_divs.
1827 * The expansion itself is given by "exp" while the resulting
1828 * list of divs is given by "div".
1830 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1831 __isl_take isl_mat *div, int *exp)
1833 isl_size old_n_div;
1834 isl_size new_n_div;
1835 isl_size offset;
1837 aff = isl_aff_cow(aff);
1839 offset = isl_aff_domain_offset(aff, isl_dim_div);
1840 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1841 new_n_div = isl_mat_rows(div);
1842 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1843 goto error;
1845 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1846 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1847 if (!aff->v || !aff->ls)
1848 return isl_aff_free(aff);
1849 return aff;
1850 error:
1851 isl_aff_free(aff);
1852 isl_mat_free(div);
1853 return NULL;
1856 /* Add two affine expressions that live in the same local space.
1858 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1859 __isl_take isl_aff *aff2)
1861 isl_int gcd, f;
1863 aff1 = isl_aff_cow(aff1);
1864 if (!aff1 || !aff2)
1865 goto error;
1867 aff1->v = isl_vec_cow(aff1->v);
1868 if (!aff1->v)
1869 goto error;
1871 isl_int_init(gcd);
1872 isl_int_init(f);
1873 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1874 isl_int_divexact(f, aff2->v->el[0], gcd);
1875 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1876 isl_int_divexact(f, aff1->v->el[0], gcd);
1877 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1878 isl_int_divexact(f, aff2->v->el[0], gcd);
1879 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1880 isl_int_clear(f);
1881 isl_int_clear(gcd);
1883 isl_aff_free(aff2);
1884 aff1 = isl_aff_normalize(aff1);
1885 return aff1;
1886 error:
1887 isl_aff_free(aff1);
1888 isl_aff_free(aff2);
1889 return NULL;
1892 /* Replace one of the arguments by a NaN and free the other one.
1894 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1895 __isl_take isl_aff *aff2)
1897 isl_aff_free(aff2);
1898 return isl_aff_set_nan(aff1);
1901 /* Return the sum of "aff1" and "aff2".
1903 * If either of the two is NaN, then the result is NaN.
1905 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1906 __isl_take isl_aff *aff2)
1908 isl_ctx *ctx;
1909 int *exp1 = NULL;
1910 int *exp2 = NULL;
1911 isl_mat *div;
1912 isl_size n_div1, n_div2;
1914 if (!aff1 || !aff2)
1915 goto error;
1917 ctx = isl_aff_get_ctx(aff1);
1918 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1919 isl_die(ctx, isl_error_invalid,
1920 "spaces don't match", goto error);
1922 if (isl_aff_is_nan(aff1)) {
1923 isl_aff_free(aff2);
1924 return aff1;
1926 if (isl_aff_is_nan(aff2)) {
1927 isl_aff_free(aff1);
1928 return aff2;
1931 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1932 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1933 if (n_div1 < 0 || n_div2 < 0)
1934 goto error;
1935 if (n_div1 == 0 && n_div2 == 0)
1936 return add_expanded(aff1, aff2);
1938 exp1 = isl_alloc_array(ctx, int, n_div1);
1939 exp2 = isl_alloc_array(ctx, int, n_div2);
1940 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1941 goto error;
1943 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1944 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1945 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1946 free(exp1);
1947 free(exp2);
1949 return add_expanded(aff1, aff2);
1950 error:
1951 free(exp1);
1952 free(exp2);
1953 isl_aff_free(aff1);
1954 isl_aff_free(aff2);
1955 return NULL;
1958 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1959 __isl_take isl_aff *aff2)
1961 return isl_aff_add(aff1, isl_aff_neg(aff2));
1964 /* Return the result of scaling "aff" by a factor of "f".
1966 * As a special case, f * NaN = NaN.
1968 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1970 isl_int gcd;
1972 if (!aff)
1973 return NULL;
1974 if (isl_aff_is_nan(aff))
1975 return aff;
1977 if (isl_int_is_one(f))
1978 return aff;
1980 aff = isl_aff_cow(aff);
1981 if (!aff)
1982 return NULL;
1983 aff->v = isl_vec_cow(aff->v);
1984 if (!aff->v)
1985 return isl_aff_free(aff);
1987 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1988 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1989 return aff;
1992 isl_int_init(gcd);
1993 isl_int_gcd(gcd, aff->v->el[0], f);
1994 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1995 isl_int_divexact(gcd, f, gcd);
1996 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1997 isl_int_clear(gcd);
1999 return aff;
2002 /* Multiple "aff" by "v".
2004 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
2005 __isl_take isl_val *v)
2007 if (!aff || !v)
2008 goto error;
2010 if (isl_val_is_one(v)) {
2011 isl_val_free(v);
2012 return aff;
2015 if (!isl_val_is_rat(v))
2016 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2017 "expecting rational factor", goto error);
2019 aff = isl_aff_scale(aff, v->n);
2020 aff = isl_aff_scale_down(aff, v->d);
2022 isl_val_free(v);
2023 return aff;
2024 error:
2025 isl_aff_free(aff);
2026 isl_val_free(v);
2027 return NULL;
2030 /* Return the result of scaling "aff" down by a factor of "f".
2032 * As a special case, NaN/f = NaN.
2034 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2036 isl_int gcd;
2038 if (!aff)
2039 return NULL;
2040 if (isl_aff_is_nan(aff))
2041 return aff;
2043 if (isl_int_is_one(f))
2044 return aff;
2046 aff = isl_aff_cow(aff);
2047 if (!aff)
2048 return NULL;
2050 if (isl_int_is_zero(f))
2051 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2052 "cannot scale down by zero", return isl_aff_free(aff));
2054 aff->v = isl_vec_cow(aff->v);
2055 if (!aff->v)
2056 return isl_aff_free(aff);
2058 isl_int_init(gcd);
2059 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2060 isl_int_gcd(gcd, gcd, f);
2061 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2062 isl_int_divexact(gcd, f, gcd);
2063 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2064 isl_int_clear(gcd);
2066 return aff;
2069 /* Divide "aff" by "v".
2071 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2072 __isl_take isl_val *v)
2074 if (!aff || !v)
2075 goto error;
2077 if (isl_val_is_one(v)) {
2078 isl_val_free(v);
2079 return aff;
2082 if (!isl_val_is_rat(v))
2083 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2084 "expecting rational factor", goto error);
2085 if (!isl_val_is_pos(v))
2086 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2087 "factor needs to be positive", goto error);
2089 aff = isl_aff_scale(aff, v->d);
2090 aff = isl_aff_scale_down(aff, v->n);
2092 isl_val_free(v);
2093 return aff;
2094 error:
2095 isl_aff_free(aff);
2096 isl_val_free(v);
2097 return NULL;
2100 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2102 isl_int v;
2104 if (f == 1)
2105 return aff;
2107 isl_int_init(v);
2108 isl_int_set_ui(v, f);
2109 aff = isl_aff_scale_down(aff, v);
2110 isl_int_clear(v);
2112 return aff;
2115 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2116 enum isl_dim_type type, unsigned pos, const char *s)
2118 aff = isl_aff_cow(aff);
2119 if (!aff)
2120 return NULL;
2121 if (type == isl_dim_out)
2122 isl_die(aff->v->ctx, isl_error_invalid,
2123 "cannot set name of output/set dimension",
2124 return isl_aff_free(aff));
2125 if (type == isl_dim_in)
2126 type = isl_dim_set;
2127 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2128 if (!aff->ls)
2129 return isl_aff_free(aff);
2131 return aff;
2134 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2135 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2137 aff = isl_aff_cow(aff);
2138 if (!aff)
2139 goto error;
2140 if (type == isl_dim_out)
2141 isl_die(aff->v->ctx, isl_error_invalid,
2142 "cannot set name of output/set dimension",
2143 goto error);
2144 if (type == isl_dim_in)
2145 type = isl_dim_set;
2146 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2147 if (!aff->ls)
2148 return isl_aff_free(aff);
2150 return aff;
2151 error:
2152 isl_id_free(id);
2153 isl_aff_free(aff);
2154 return NULL;
2157 /* Replace the identifier of the input tuple of "aff" by "id".
2158 * type is currently required to be equal to isl_dim_in
2160 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2161 enum isl_dim_type type, __isl_take isl_id *id)
2163 aff = isl_aff_cow(aff);
2164 if (!aff)
2165 goto error;
2166 if (type != isl_dim_in)
2167 isl_die(aff->v->ctx, isl_error_invalid,
2168 "cannot only set id of input tuple", goto error);
2169 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2170 if (!aff->ls)
2171 return isl_aff_free(aff);
2173 return aff;
2174 error:
2175 isl_id_free(id);
2176 isl_aff_free(aff);
2177 return NULL;
2180 /* Exploit the equalities in "eq" to simplify the affine expression
2181 * and the expressions of the integer divisions in the local space.
2182 * The integer divisions in this local space are assumed to appear
2183 * as regular dimensions in "eq".
2185 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2186 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2188 int i, j;
2189 unsigned o_div;
2190 unsigned n_div;
2192 if (!eq)
2193 goto error;
2194 if (eq->n_eq == 0) {
2195 isl_basic_set_free(eq);
2196 return aff;
2199 aff = isl_aff_cow(aff);
2200 if (!aff)
2201 goto error;
2203 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2204 isl_basic_set_copy(eq));
2205 aff->v = isl_vec_cow(aff->v);
2206 if (!aff->ls || !aff->v)
2207 goto error;
2209 o_div = isl_basic_set_offset(eq, isl_dim_div);
2210 n_div = eq->n_div;
2211 for (i = 0; i < eq->n_eq; ++i) {
2212 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2213 if (j < 0 || j == 0 || j >= o_div)
2214 continue;
2216 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2217 &aff->v->el[0]);
2220 isl_basic_set_free(eq);
2221 aff = isl_aff_normalize(aff);
2222 return aff;
2223 error:
2224 isl_basic_set_free(eq);
2225 isl_aff_free(aff);
2226 return NULL;
2229 /* Exploit the equalities in "eq" to simplify the affine expression
2230 * and the expressions of the integer divisions in the local space.
2232 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2233 __isl_take isl_basic_set *eq)
2235 isl_size n_div;
2237 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2238 if (n_div < 0)
2239 goto error;
2240 if (n_div > 0)
2241 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2242 return isl_aff_substitute_equalities_lifted(aff, eq);
2243 error:
2244 isl_basic_set_free(eq);
2245 isl_aff_free(aff);
2246 return NULL;
2249 /* Look for equalities among the variables shared by context and aff
2250 * and the integer divisions of aff, if any.
2251 * The equalities are then used to eliminate coefficients and/or integer
2252 * divisions from aff.
2254 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2255 __isl_take isl_set *context)
2257 isl_local_space *ls;
2258 isl_basic_set *hull;
2260 ls = isl_aff_get_domain_local_space(aff);
2261 context = isl_local_space_lift_set(ls, context);
2263 hull = isl_set_affine_hull(context);
2264 return isl_aff_substitute_equalities_lifted(aff, hull);
2267 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2268 __isl_take isl_set *context)
2270 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2271 dom_context = isl_set_intersect_params(dom_context, context);
2272 return isl_aff_gist(aff, dom_context);
2275 /* Return a basic set containing those elements in the space
2276 * of aff where it is positive. "rational" should not be set.
2278 * If "aff" is NaN, then it is not positive.
2280 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2281 int rational, void *user)
2283 isl_constraint *ineq;
2284 isl_basic_set *bset;
2285 isl_val *c;
2287 if (!aff)
2288 return NULL;
2289 if (isl_aff_is_nan(aff)) {
2290 isl_space *space = isl_aff_get_domain_space(aff);
2291 isl_aff_free(aff);
2292 return isl_basic_set_empty(space);
2294 if (rational)
2295 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2296 "rational sets not supported", goto error);
2298 ineq = isl_inequality_from_aff(aff);
2299 c = isl_constraint_get_constant_val(ineq);
2300 c = isl_val_sub_ui(c, 1);
2301 ineq = isl_constraint_set_constant_val(ineq, c);
2303 bset = isl_basic_set_from_constraint(ineq);
2304 bset = isl_basic_set_simplify(bset);
2305 return bset;
2306 error:
2307 isl_aff_free(aff);
2308 return NULL;
2311 /* Return a basic set containing those elements in the space
2312 * of aff where it is non-negative.
2313 * If "rational" is set, then return a rational basic set.
2315 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2317 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2318 __isl_take isl_aff *aff, int rational, void *user)
2320 isl_constraint *ineq;
2321 isl_basic_set *bset;
2323 if (!aff)
2324 return NULL;
2325 if (isl_aff_is_nan(aff)) {
2326 isl_space *space = isl_aff_get_domain_space(aff);
2327 isl_aff_free(aff);
2328 return isl_basic_set_empty(space);
2331 ineq = isl_inequality_from_aff(aff);
2333 bset = isl_basic_set_from_constraint(ineq);
2334 if (rational)
2335 bset = isl_basic_set_set_rational(bset);
2336 bset = isl_basic_set_simplify(bset);
2337 return bset;
2340 /* Return a basic set containing those elements in the space
2341 * of aff where it is non-negative.
2343 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2345 return aff_nonneg_basic_set(aff, 0, NULL);
2348 /* Return a basic set containing those elements in the domain space
2349 * of "aff" where it is positive.
2351 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2353 aff = isl_aff_add_constant_num_si(aff, -1);
2354 return isl_aff_nonneg_basic_set(aff);
2357 /* Return a basic set containing those elements in the domain space
2358 * of aff where it is negative.
2360 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2362 aff = isl_aff_neg(aff);
2363 return isl_aff_pos_basic_set(aff);
2366 /* Return a basic set containing those elements in the space
2367 * of aff where it is zero.
2368 * If "rational" is set, then return a rational basic set.
2370 * If "aff" is NaN, then it is not zero.
2372 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2373 int rational, void *user)
2375 isl_constraint *ineq;
2376 isl_basic_set *bset;
2378 if (!aff)
2379 return NULL;
2380 if (isl_aff_is_nan(aff)) {
2381 isl_space *space = isl_aff_get_domain_space(aff);
2382 isl_aff_free(aff);
2383 return isl_basic_set_empty(space);
2386 ineq = isl_equality_from_aff(aff);
2388 bset = isl_basic_set_from_constraint(ineq);
2389 if (rational)
2390 bset = isl_basic_set_set_rational(bset);
2391 bset = isl_basic_set_simplify(bset);
2392 return bset;
2395 /* Return a basic set containing those elements in the space
2396 * of aff where it is zero.
2398 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2400 return aff_zero_basic_set(aff, 0, NULL);
2403 /* Return a basic set containing those elements in the shared space
2404 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2406 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2407 __isl_take isl_aff *aff2)
2409 aff1 = isl_aff_sub(aff1, aff2);
2411 return isl_aff_nonneg_basic_set(aff1);
2414 /* Return a basic set containing those elements in the shared domain space
2415 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2417 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2418 __isl_take isl_aff *aff2)
2420 aff1 = isl_aff_sub(aff1, aff2);
2422 return isl_aff_pos_basic_set(aff1);
2425 /* Return a set containing those elements in the shared space
2426 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2428 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2429 __isl_take isl_aff *aff2)
2431 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2434 /* Return a set containing those elements in the shared domain space
2435 * of aff1 and aff2 where aff1 is greater than aff2.
2437 * If either of the two inputs is NaN, then the result is empty,
2438 * as comparisons with NaN always return false.
2440 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2441 __isl_take isl_aff *aff2)
2443 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2446 /* Return a basic set containing those elements in the shared space
2447 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2449 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2450 __isl_take isl_aff *aff2)
2452 return isl_aff_ge_basic_set(aff2, aff1);
2455 /* Return a basic set containing those elements in the shared domain space
2456 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2458 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2459 __isl_take isl_aff *aff2)
2461 return isl_aff_gt_basic_set(aff2, aff1);
2464 /* Return a set containing those elements in the shared space
2465 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2467 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2468 __isl_take isl_aff *aff2)
2470 return isl_aff_ge_set(aff2, aff1);
2473 /* Return a set containing those elements in the shared domain space
2474 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2476 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2477 __isl_take isl_aff *aff2)
2479 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2482 /* Return a basic set containing those elements in the shared space
2483 * of aff1 and aff2 where aff1 and aff2 are equal.
2485 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2486 __isl_take isl_aff *aff2)
2488 aff1 = isl_aff_sub(aff1, aff2);
2490 return isl_aff_zero_basic_set(aff1);
2493 /* Return a set containing those elements in the shared space
2494 * of aff1 and aff2 where aff1 and aff2 are equal.
2496 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2497 __isl_take isl_aff *aff2)
2499 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2502 /* Return a set containing those elements in the shared domain space
2503 * of aff1 and aff2 where aff1 and aff2 are not equal.
2505 * If either of the two inputs is NaN, then the result is empty,
2506 * as comparisons with NaN always return false.
2508 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2509 __isl_take isl_aff *aff2)
2511 isl_set *set_lt, *set_gt;
2513 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2514 isl_aff_copy(aff2));
2515 set_gt = isl_aff_gt_set(aff1, aff2);
2516 return isl_set_union_disjoint(set_lt, set_gt);
2519 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2520 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2522 aff1 = isl_aff_add(aff1, aff2);
2523 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2524 return aff1;
2527 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2529 if (!aff)
2530 return isl_bool_error;
2532 return isl_bool_false;
2535 #undef TYPE
2536 #define TYPE isl_aff
2537 static
2538 #include "check_type_range_templ.c"
2540 /* Check whether the given affine expression has non-zero coefficient
2541 * for any dimension in the given range or if any of these dimensions
2542 * appear with non-zero coefficients in any of the integer divisions
2543 * involved in the affine expression.
2545 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2546 enum isl_dim_type type, unsigned first, unsigned n)
2548 int i;
2549 int *active = NULL;
2550 isl_bool involves = isl_bool_false;
2552 if (!aff)
2553 return isl_bool_error;
2554 if (n == 0)
2555 return isl_bool_false;
2556 if (isl_aff_check_range(aff, type, first, n) < 0)
2557 return isl_bool_error;
2559 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2560 if (!active)
2561 goto error;
2563 first += isl_local_space_offset(aff->ls, type) - 1;
2564 for (i = 0; i < n; ++i)
2565 if (active[first + i]) {
2566 involves = isl_bool_true;
2567 break;
2570 free(active);
2572 return involves;
2573 error:
2574 free(active);
2575 return isl_bool_error;
2578 /* Does "aff" involve any local variables, i.e., integer divisions?
2580 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2582 isl_size n;
2584 n = isl_aff_dim(aff, isl_dim_div);
2585 if (n < 0)
2586 return isl_bool_error;
2587 return isl_bool_ok(n > 0);
2590 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2591 enum isl_dim_type type, unsigned first, unsigned n)
2593 isl_ctx *ctx;
2595 if (!aff)
2596 return NULL;
2597 if (type == isl_dim_out)
2598 isl_die(aff->v->ctx, isl_error_invalid,
2599 "cannot drop output/set dimension",
2600 return isl_aff_free(aff));
2601 if (type == isl_dim_in)
2602 type = isl_dim_set;
2603 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2604 return aff;
2606 ctx = isl_aff_get_ctx(aff);
2607 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2608 return isl_aff_free(aff);
2610 aff = isl_aff_cow(aff);
2611 if (!aff)
2612 return NULL;
2614 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2615 if (!aff->ls)
2616 return isl_aff_free(aff);
2618 first += 1 + isl_local_space_offset(aff->ls, type);
2619 aff->v = isl_vec_drop_els(aff->v, first, n);
2620 if (!aff->v)
2621 return isl_aff_free(aff);
2623 return aff;
2626 /* Is the domain of "aff" a product?
2628 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2630 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2633 #undef TYPE
2634 #define TYPE isl_aff
2635 #include <isl_domain_factor_templ.c>
2637 /* Project the domain of the affine expression onto its parameter space.
2638 * The affine expression may not involve any of the domain dimensions.
2640 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2642 isl_space *space;
2643 isl_size n;
2645 n = isl_aff_dim(aff, isl_dim_in);
2646 if (n < 0)
2647 return isl_aff_free(aff);
2648 aff = isl_aff_drop_domain(aff, 0, n);
2649 space = isl_aff_get_domain_space(aff);
2650 space = isl_space_params(space);
2651 aff = isl_aff_reset_domain_space(aff, space);
2652 return aff;
2655 /* Convert an affine expression defined over a parameter domain
2656 * into one that is defined over a zero-dimensional set.
2658 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2660 isl_local_space *ls;
2662 ls = isl_aff_take_domain_local_space(aff);
2663 ls = isl_local_space_set_from_params(ls);
2664 aff = isl_aff_restore_domain_local_space(aff, ls);
2666 return aff;
2669 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2670 enum isl_dim_type type, unsigned first, unsigned n)
2672 isl_ctx *ctx;
2674 if (!aff)
2675 return NULL;
2676 if (type == isl_dim_out)
2677 isl_die(aff->v->ctx, isl_error_invalid,
2678 "cannot insert output/set dimensions",
2679 return isl_aff_free(aff));
2680 if (type == isl_dim_in)
2681 type = isl_dim_set;
2682 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2683 return aff;
2685 ctx = isl_aff_get_ctx(aff);
2686 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2687 return isl_aff_free(aff);
2689 aff = isl_aff_cow(aff);
2690 if (!aff)
2691 return NULL;
2693 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2694 if (!aff->ls)
2695 return isl_aff_free(aff);
2697 first += 1 + isl_local_space_offset(aff->ls, type);
2698 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2699 if (!aff->v)
2700 return isl_aff_free(aff);
2702 return aff;
2705 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2706 enum isl_dim_type type, unsigned n)
2708 isl_size pos;
2710 pos = isl_aff_dim(aff, type);
2711 if (pos < 0)
2712 return isl_aff_free(aff);
2714 return isl_aff_insert_dims(aff, type, pos, n);
2717 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2718 * to dimensions of "dst_type" at "dst_pos".
2720 * We only support moving input dimensions to parameters and vice versa.
2722 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2723 enum isl_dim_type dst_type, unsigned dst_pos,
2724 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2726 unsigned g_dst_pos;
2727 unsigned g_src_pos;
2728 isl_size src_off, dst_off;
2730 if (!aff)
2731 return NULL;
2732 if (n == 0 &&
2733 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2734 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2735 return aff;
2737 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2738 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2739 "cannot move output/set dimension",
2740 return isl_aff_free(aff));
2741 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2742 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2743 "cannot move divs", return isl_aff_free(aff));
2744 if (dst_type == isl_dim_in)
2745 dst_type = isl_dim_set;
2746 if (src_type == isl_dim_in)
2747 src_type = isl_dim_set;
2749 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2750 return isl_aff_free(aff);
2751 if (dst_type == src_type)
2752 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2753 "moving dims within the same type not supported",
2754 return isl_aff_free(aff));
2756 aff = isl_aff_cow(aff);
2757 src_off = isl_aff_domain_offset(aff, src_type);
2758 dst_off = isl_aff_domain_offset(aff, dst_type);
2759 if (src_off < 0 || dst_off < 0)
2760 return isl_aff_free(aff);
2762 g_src_pos = 1 + src_off + src_pos;
2763 g_dst_pos = 1 + dst_off + dst_pos;
2764 if (dst_type > src_type)
2765 g_dst_pos -= n;
2767 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2768 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2769 src_type, src_pos, n);
2770 if (!aff->v || !aff->ls)
2771 return isl_aff_free(aff);
2773 aff = sort_divs(aff);
2775 return aff;
2778 /* Return a zero isl_aff in the given space.
2780 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2781 * interface over all piecewise types.
2783 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2785 isl_local_space *ls;
2787 ls = isl_local_space_from_space(isl_space_domain(space));
2788 return isl_aff_zero_on_domain(ls);
2791 #define isl_aff_involves_nan isl_aff_is_nan
2793 #undef PW
2794 #define PW isl_pw_aff
2795 #undef BASE
2796 #define BASE aff
2797 #undef EL_IS_ZERO
2798 #define EL_IS_ZERO is_empty
2799 #undef ZERO
2800 #define ZERO empty
2801 #undef IS_ZERO
2802 #define IS_ZERO is_empty
2803 #undef FIELD
2804 #define FIELD aff
2805 #undef DEFAULT_IS_ZERO
2806 #define DEFAULT_IS_ZERO 0
2808 #include <isl_pw_templ.c>
2809 #include <isl_pw_add_constant_val_templ.c>
2810 #include <isl_pw_bind_domain_templ.c>
2811 #include <isl_pw_eval.c>
2812 #include <isl_pw_hash.c>
2813 #include <isl_pw_insert_dims_templ.c>
2814 #include <isl_pw_insert_domain_templ.c>
2815 #include <isl_pw_move_dims_templ.c>
2816 #include <isl_pw_neg_templ.c>
2817 #include <isl_pw_pullback_templ.c>
2818 #include <isl_pw_sub_templ.c>
2819 #include <isl_pw_union_opt.c>
2821 #undef BASE
2822 #define BASE pw_aff
2824 #include <isl_union_single.c>
2825 #include <isl_union_neg.c>
2827 #undef BASE
2828 #define BASE aff
2830 #include <isl_union_pw_templ.c>
2832 /* Compute a piecewise quasi-affine expression with a domain that
2833 * is the union of those of pwaff1 and pwaff2 and such that on each
2834 * cell, the quasi-affine expression is the maximum of those of pwaff1
2835 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2836 * cell, then the associated expression is the defined one.
2838 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2839 __isl_take isl_pw_aff *pwaff2)
2841 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2842 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2845 /* Compute a piecewise quasi-affine expression with a domain that
2846 * is the union of those of pwaff1 and pwaff2 and such that on each
2847 * cell, the quasi-affine expression is the minimum of those of pwaff1
2848 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2849 * cell, then the associated expression is the defined one.
2851 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2852 __isl_take isl_pw_aff *pwaff2)
2854 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2855 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2858 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2859 __isl_take isl_pw_aff *pwaff2, int max)
2861 if (max)
2862 return isl_pw_aff_union_max(pwaff1, pwaff2);
2863 else
2864 return isl_pw_aff_union_min(pwaff1, pwaff2);
2867 /* Is the domain of "pa" a product?
2869 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2871 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2874 #undef TYPE
2875 #define TYPE isl_pw_aff
2876 #include <isl_domain_factor_templ.c>
2878 /* Return a set containing those elements in the domain
2879 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2880 * does not satisfy "fn" (if complement is 1).
2882 * The pieces with a NaN never belong to the result since
2883 * NaN does not satisfy any property.
2885 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2886 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2887 void *user),
2888 int complement, void *user)
2890 int i;
2891 isl_set *set;
2893 if (!pwaff)
2894 return NULL;
2896 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2898 for (i = 0; i < pwaff->n; ++i) {
2899 isl_basic_set *bset;
2900 isl_set *set_i, *locus;
2901 isl_bool rational;
2903 if (isl_aff_is_nan(pwaff->p[i].aff))
2904 continue;
2906 rational = isl_set_has_rational(pwaff->p[i].set);
2907 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2908 locus = isl_set_from_basic_set(bset);
2909 set_i = isl_set_copy(pwaff->p[i].set);
2910 if (complement)
2911 set_i = isl_set_subtract(set_i, locus);
2912 else
2913 set_i = isl_set_intersect(set_i, locus);
2914 set = isl_set_union_disjoint(set, set_i);
2917 isl_pw_aff_free(pwaff);
2919 return set;
2922 /* Return a set containing those elements in the domain
2923 * of "pa" where it is positive.
2925 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2927 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2930 /* Return a set containing those elements in the domain
2931 * of pwaff where it is non-negative.
2933 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2935 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2938 /* Return a set containing those elements in the domain
2939 * of pwaff where it is zero.
2941 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2943 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2946 /* Return a set containing those elements in the domain
2947 * of pwaff where it is not zero.
2949 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2951 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2954 /* Bind the affine function "aff" to the parameter "id",
2955 * returning the elements in the domain where the affine expression
2956 * is equal to the parameter.
2958 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2959 __isl_take isl_id *id)
2961 isl_space *space;
2962 isl_aff *aff_id;
2964 space = isl_aff_get_domain_space(aff);
2965 space = isl_space_add_param_id(space, isl_id_copy(id));
2967 aff = isl_aff_align_params(aff, isl_space_copy(space));
2968 aff_id = isl_aff_param_on_domain_space_id(space, id);
2970 return isl_aff_eq_basic_set(aff, aff_id);
2973 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2974 * "rational" should not be set.
2976 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2977 int rational, void *user)
2979 isl_id *id = user;
2981 if (!aff)
2982 return NULL;
2983 if (rational)
2984 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2985 "rational binding not supported", goto error);
2986 return isl_aff_bind_id(aff, isl_id_copy(id));
2987 error:
2988 isl_aff_free(aff);
2989 return NULL;
2992 /* Bind the piecewise affine function "pa" to the parameter "id",
2993 * returning the elements in the domain where the expression
2994 * is equal to the parameter.
2996 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2997 __isl_take isl_id *id)
2999 isl_set *bound;
3001 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
3002 isl_id_free(id);
3004 return bound;
3007 /* Return a set containing those elements in the shared domain
3008 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3010 * We compute the difference on the shared domain and then construct
3011 * the set of values where this difference is non-negative.
3012 * If strict is set, we first subtract 1 from the difference.
3013 * If equal is set, we only return the elements where pwaff1 and pwaff2
3014 * are equal.
3016 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3017 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3019 isl_set *set1, *set2;
3021 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3022 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3023 set1 = isl_set_intersect(set1, set2);
3024 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3025 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3026 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3028 if (strict) {
3029 isl_space *space = isl_set_get_space(set1);
3030 isl_aff *aff;
3031 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3032 aff = isl_aff_add_constant_si(aff, -1);
3033 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3034 } else
3035 isl_set_free(set1);
3037 if (equal)
3038 return isl_pw_aff_zero_set(pwaff1);
3039 return isl_pw_aff_nonneg_set(pwaff1);
3042 /* Return a set containing those elements in the shared domain
3043 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3045 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3046 __isl_take isl_pw_aff *pwaff2)
3048 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3049 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3052 /* Return a set containing those elements in the shared domain
3053 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3055 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3056 __isl_take isl_pw_aff *pwaff2)
3058 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3059 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3062 /* Return a set containing those elements in the shared domain
3063 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3065 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3066 __isl_take isl_pw_aff *pwaff2)
3068 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3069 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3072 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3073 __isl_take isl_pw_aff *pwaff2)
3075 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3078 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3079 __isl_take isl_pw_aff *pwaff2)
3081 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3084 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3085 * where the function values are ordered in the same way as "order",
3086 * which returns a set in the shared domain of its two arguments.
3088 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3089 * We first pull back the two functions such that they are defined on
3090 * the domain [A -> B]. Then we apply "order", resulting in a set
3091 * in the space [A -> B]. Finally, we unwrap this set to obtain
3092 * a map in the space A -> B.
3094 static __isl_give isl_map *isl_pw_aff_order_map(
3095 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3096 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3097 __isl_take isl_pw_aff *pa2))
3099 isl_space *space1, *space2;
3100 isl_multi_aff *ma;
3101 isl_set *set;
3103 isl_pw_aff_align_params_bin(&pa1, &pa2);
3104 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3105 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3106 space1 = isl_space_map_from_domain_and_range(space1, space2);
3107 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3108 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3109 ma = isl_multi_aff_range_map(space1);
3110 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3111 set = order(pa1, pa2);
3113 return isl_set_unwrap(set);
3116 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3117 * where the function values are equal.
3119 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3120 __isl_take isl_pw_aff *pa2)
3122 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3125 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3126 * where the function value of "pa1" is less than or equal to
3127 * the function value of "pa2".
3129 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3130 __isl_take isl_pw_aff *pa2)
3132 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3135 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3136 * where the function value of "pa1" is less than the function value of "pa2".
3138 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3139 __isl_take isl_pw_aff *pa2)
3141 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3144 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3145 * where the function value of "pa1" is greater than or equal to
3146 * the function value of "pa2".
3148 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3149 __isl_take isl_pw_aff *pa2)
3151 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3154 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3155 * where the function value of "pa1" is greater than the function value
3156 * of "pa2".
3158 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3159 __isl_take isl_pw_aff *pa2)
3161 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3164 /* Return a set containing those elements in the shared domain
3165 * of the elements of list1 and list2 where each element in list1
3166 * has the relation specified by "fn" with each element in list2.
3168 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3169 __isl_take isl_pw_aff_list *list2,
3170 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3171 __isl_take isl_pw_aff *pwaff2))
3173 int i, j;
3174 isl_ctx *ctx;
3175 isl_set *set;
3177 if (!list1 || !list2)
3178 goto error;
3180 ctx = isl_pw_aff_list_get_ctx(list1);
3181 if (list1->n < 1 || list2->n < 1)
3182 isl_die(ctx, isl_error_invalid,
3183 "list should contain at least one element", goto error);
3185 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3186 for (i = 0; i < list1->n; ++i)
3187 for (j = 0; j < list2->n; ++j) {
3188 isl_set *set_ij;
3190 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3191 isl_pw_aff_copy(list2->p[j]));
3192 set = isl_set_intersect(set, set_ij);
3195 isl_pw_aff_list_free(list1);
3196 isl_pw_aff_list_free(list2);
3197 return set;
3198 error:
3199 isl_pw_aff_list_free(list1);
3200 isl_pw_aff_list_free(list2);
3201 return NULL;
3204 /* Return a set containing those elements in the shared domain
3205 * of the elements of list1 and list2 where each element in list1
3206 * is equal to each element in list2.
3208 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3209 __isl_take isl_pw_aff_list *list2)
3211 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3214 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3215 __isl_take isl_pw_aff_list *list2)
3217 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3220 /* Return a set containing those elements in the shared domain
3221 * of the elements of list1 and list2 where each element in list1
3222 * is less than or equal to each element in list2.
3224 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3225 __isl_take isl_pw_aff_list *list2)
3227 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3230 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3231 __isl_take isl_pw_aff_list *list2)
3233 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3236 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3237 __isl_take isl_pw_aff_list *list2)
3239 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3242 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3243 __isl_take isl_pw_aff_list *list2)
3245 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3249 /* Return a set containing those elements in the shared domain
3250 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3252 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3253 __isl_take isl_pw_aff *pwaff2)
3255 isl_set *set_lt, *set_gt;
3257 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3258 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3259 isl_pw_aff_copy(pwaff2));
3260 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3261 return isl_set_union_disjoint(set_lt, set_gt);
3264 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3265 isl_int v)
3267 int i;
3269 if (isl_int_is_one(v))
3270 return pwaff;
3271 if (!isl_int_is_pos(v))
3272 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3273 "factor needs to be positive",
3274 return isl_pw_aff_free(pwaff));
3275 pwaff = isl_pw_aff_cow(pwaff);
3276 if (!pwaff)
3277 return NULL;
3278 if (pwaff->n == 0)
3279 return pwaff;
3281 for (i = 0; i < pwaff->n; ++i) {
3282 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3283 if (!pwaff->p[i].aff)
3284 return isl_pw_aff_free(pwaff);
3287 return pwaff;
3290 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3292 int i;
3294 pwaff = isl_pw_aff_cow(pwaff);
3295 if (!pwaff)
3296 return NULL;
3297 if (pwaff->n == 0)
3298 return pwaff;
3300 for (i = 0; i < pwaff->n; ++i) {
3301 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3302 if (!pwaff->p[i].aff)
3303 return isl_pw_aff_free(pwaff);
3306 return pwaff;
3309 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3311 int i;
3313 pwaff = isl_pw_aff_cow(pwaff);
3314 if (!pwaff)
3315 return NULL;
3316 if (pwaff->n == 0)
3317 return pwaff;
3319 for (i = 0; i < pwaff->n; ++i) {
3320 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3321 if (!pwaff->p[i].aff)
3322 return isl_pw_aff_free(pwaff);
3325 return pwaff;
3328 /* Assuming that "cond1" and "cond2" are disjoint,
3329 * return an affine expression that is equal to pwaff1 on cond1
3330 * and to pwaff2 on cond2.
3332 static __isl_give isl_pw_aff *isl_pw_aff_select(
3333 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3334 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3336 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3337 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3339 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3342 /* Return an affine expression that is equal to pwaff_true for elements
3343 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3344 * is zero.
3345 * That is, return cond ? pwaff_true : pwaff_false;
3347 * If "cond" involves and NaN, then we conservatively return a NaN
3348 * on its entire domain. In principle, we could consider the pieces
3349 * where it is NaN separately from those where it is not.
3351 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3352 * then only use the domain of "cond" to restrict the domain.
3354 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3355 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3357 isl_set *cond_true, *cond_false;
3358 isl_bool equal;
3360 if (!cond)
3361 goto error;
3362 if (isl_pw_aff_involves_nan(cond)) {
3363 isl_space *space = isl_pw_aff_get_domain_space(cond);
3364 isl_local_space *ls = isl_local_space_from_space(space);
3365 isl_pw_aff_free(cond);
3366 isl_pw_aff_free(pwaff_true);
3367 isl_pw_aff_free(pwaff_false);
3368 return isl_pw_aff_nan_on_domain(ls);
3371 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3372 isl_pw_aff_get_space(pwaff_false));
3373 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3374 isl_pw_aff_get_space(pwaff_true));
3375 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3376 if (equal < 0)
3377 goto error;
3378 if (equal) {
3379 isl_set *dom;
3381 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3382 isl_pw_aff_free(pwaff_false);
3383 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3386 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3387 cond_false = isl_pw_aff_zero_set(cond);
3388 return isl_pw_aff_select(cond_true, pwaff_true,
3389 cond_false, pwaff_false);
3390 error:
3391 isl_pw_aff_free(cond);
3392 isl_pw_aff_free(pwaff_true);
3393 isl_pw_aff_free(pwaff_false);
3394 return NULL;
3397 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3399 int pos;
3401 if (!aff)
3402 return isl_bool_error;
3404 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3405 return isl_bool_ok(pos == -1);
3408 /* Check whether pwaff is a piecewise constant.
3410 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3412 int i;
3414 if (!pwaff)
3415 return isl_bool_error;
3417 for (i = 0; i < pwaff->n; ++i) {
3418 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3419 if (is_cst < 0 || !is_cst)
3420 return is_cst;
3423 return isl_bool_true;
3426 /* Return the product of "aff1" and "aff2".
3428 * If either of the two is NaN, then the result is NaN.
3430 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3432 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3433 __isl_take isl_aff *aff2)
3435 if (!aff1 || !aff2)
3436 goto error;
3438 if (isl_aff_is_nan(aff1)) {
3439 isl_aff_free(aff2);
3440 return aff1;
3442 if (isl_aff_is_nan(aff2)) {
3443 isl_aff_free(aff1);
3444 return aff2;
3447 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3448 return isl_aff_mul(aff2, aff1);
3450 if (!isl_aff_is_cst(aff2))
3451 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3452 "at least one affine expression should be constant",
3453 goto error);
3455 aff1 = isl_aff_cow(aff1);
3456 if (!aff1 || !aff2)
3457 goto error;
3459 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3460 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3462 isl_aff_free(aff2);
3463 return aff1;
3464 error:
3465 isl_aff_free(aff1);
3466 isl_aff_free(aff2);
3467 return NULL;
3470 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3472 * If either of the two is NaN, then the result is NaN.
3473 * A division by zero also results in NaN.
3475 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3476 __isl_take isl_aff *aff2)
3478 isl_bool is_cst, is_zero;
3479 int neg;
3481 if (!aff1 || !aff2)
3482 goto error;
3484 if (isl_aff_is_nan(aff1)) {
3485 isl_aff_free(aff2);
3486 return aff1;
3488 if (isl_aff_is_nan(aff2)) {
3489 isl_aff_free(aff1);
3490 return aff2;
3493 is_cst = isl_aff_is_cst(aff2);
3494 if (is_cst < 0)
3495 goto error;
3496 if (!is_cst)
3497 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3498 "second argument should be a constant", goto error);
3499 is_zero = isl_aff_plain_is_zero(aff2);
3500 if (is_zero < 0)
3501 goto error;
3502 if (is_zero)
3503 return set_nan_free(aff1, aff2);
3505 neg = isl_int_is_neg(aff2->v->el[1]);
3506 if (neg) {
3507 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3508 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3511 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3512 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3514 if (neg) {
3515 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3516 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3519 isl_aff_free(aff2);
3520 return aff1;
3521 error:
3522 isl_aff_free(aff1);
3523 isl_aff_free(aff2);
3524 return NULL;
3527 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3528 __isl_take isl_pw_aff *pwaff2)
3530 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3531 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3534 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3535 __isl_take isl_pw_aff *pwaff2)
3537 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3540 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3541 __isl_take isl_pw_aff *pwaff2)
3543 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3544 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3547 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3549 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3550 __isl_take isl_pw_aff *pa2)
3552 int is_cst;
3554 is_cst = isl_pw_aff_is_cst(pa2);
3555 if (is_cst < 0)
3556 goto error;
3557 if (!is_cst)
3558 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3559 "second argument should be a piecewise constant",
3560 goto error);
3561 isl_pw_aff_align_params_bin(&pa1, &pa2);
3562 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3563 error:
3564 isl_pw_aff_free(pa1);
3565 isl_pw_aff_free(pa2);
3566 return NULL;
3569 /* Compute the quotient of the integer division of "pa1" by "pa2"
3570 * with rounding towards zero.
3571 * "pa2" is assumed to be a piecewise constant.
3573 * In particular, return
3575 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3578 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3579 __isl_take isl_pw_aff *pa2)
3581 int is_cst;
3582 isl_set *cond;
3583 isl_pw_aff *f, *c;
3585 is_cst = isl_pw_aff_is_cst(pa2);
3586 if (is_cst < 0)
3587 goto error;
3588 if (!is_cst)
3589 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3590 "second argument should be a piecewise constant",
3591 goto error);
3593 pa1 = isl_pw_aff_div(pa1, pa2);
3595 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3596 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3597 c = isl_pw_aff_ceil(pa1);
3598 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3599 error:
3600 isl_pw_aff_free(pa1);
3601 isl_pw_aff_free(pa2);
3602 return NULL;
3605 /* Compute the remainder of the integer division of "pa1" by "pa2"
3606 * with rounding towards zero.
3607 * "pa2" is assumed to be a piecewise constant.
3609 * In particular, return
3611 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3614 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3615 __isl_take isl_pw_aff *pa2)
3617 int is_cst;
3618 isl_pw_aff *res;
3620 is_cst = isl_pw_aff_is_cst(pa2);
3621 if (is_cst < 0)
3622 goto error;
3623 if (!is_cst)
3624 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3625 "second argument should be a piecewise constant",
3626 goto error);
3627 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3628 res = isl_pw_aff_mul(pa2, res);
3629 res = isl_pw_aff_sub(pa1, res);
3630 return res;
3631 error:
3632 isl_pw_aff_free(pa1);
3633 isl_pw_aff_free(pa2);
3634 return NULL;
3637 /* Does either of "pa1" or "pa2" involve any NaN2?
3639 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3640 __isl_keep isl_pw_aff *pa2)
3642 isl_bool has_nan;
3644 has_nan = isl_pw_aff_involves_nan(pa1);
3645 if (has_nan < 0 || has_nan)
3646 return has_nan;
3647 return isl_pw_aff_involves_nan(pa2);
3650 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3651 * by a NaN on their shared domain.
3653 * In principle, the result could be refined to only being NaN
3654 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3656 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3657 __isl_take isl_pw_aff *pa2)
3659 isl_local_space *ls;
3660 isl_set *dom;
3661 isl_pw_aff *pa;
3663 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3664 ls = isl_local_space_from_space(isl_set_get_space(dom));
3665 pa = isl_pw_aff_nan_on_domain(ls);
3666 pa = isl_pw_aff_intersect_domain(pa, dom);
3668 return pa;
3671 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3672 __isl_take isl_pw_aff *pwaff2)
3674 isl_set *le;
3675 isl_set *dom;
3677 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3678 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3679 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3680 isl_pw_aff_copy(pwaff2));
3681 dom = isl_set_subtract(dom, isl_set_copy(le));
3682 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3685 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3686 __isl_take isl_pw_aff *pwaff2)
3688 isl_set *ge;
3689 isl_set *dom;
3691 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3692 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3693 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3694 isl_pw_aff_copy(pwaff2));
3695 dom = isl_set_subtract(dom, isl_set_copy(ge));
3696 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3699 /* Return an expression for the minimum (if "max" is not set) or
3700 * the maximum (if "max" is set) of "pa1" and "pa2".
3701 * If either expression involves any NaN, then return a NaN
3702 * on the shared domain as result.
3704 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3705 __isl_take isl_pw_aff *pa2, int max)
3707 isl_bool has_nan;
3709 has_nan = either_involves_nan(pa1, pa2);
3710 if (has_nan < 0)
3711 pa1 = isl_pw_aff_free(pa1);
3712 else if (has_nan)
3713 return replace_by_nan(pa1, pa2);
3715 isl_pw_aff_align_params_bin(&pa1, &pa2);
3716 if (max)
3717 return pw_aff_max(pa1, pa2);
3718 else
3719 return pw_aff_min(pa1, pa2);
3722 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3724 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3725 __isl_take isl_pw_aff *pwaff2)
3727 return pw_aff_min_max(pwaff1, pwaff2, 0);
3730 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3732 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3733 __isl_take isl_pw_aff *pwaff2)
3735 return pw_aff_min_max(pwaff1, pwaff2, 1);
3738 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3739 __isl_take isl_pw_aff_list *list,
3740 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3741 __isl_take isl_pw_aff *pwaff2))
3743 int i;
3744 isl_ctx *ctx;
3745 isl_pw_aff *res;
3747 if (!list)
3748 return NULL;
3750 ctx = isl_pw_aff_list_get_ctx(list);
3751 if (list->n < 1)
3752 isl_die(ctx, isl_error_invalid,
3753 "list should contain at least one element", goto error);
3755 res = isl_pw_aff_copy(list->p[0]);
3756 for (i = 1; i < list->n; ++i)
3757 res = fn(res, isl_pw_aff_copy(list->p[i]));
3759 isl_pw_aff_list_free(list);
3760 return res;
3761 error:
3762 isl_pw_aff_list_free(list);
3763 return NULL;
3766 /* Return an isl_pw_aff that maps each element in the intersection of the
3767 * domains of the elements of list to the minimal corresponding affine
3768 * expression.
3770 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3772 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3775 /* Return an isl_pw_aff that maps each element in the intersection of the
3776 * domains of the elements of list to the maximal corresponding affine
3777 * expression.
3779 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3781 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3784 /* Mark the domains of "pwaff" as rational.
3786 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3788 int i;
3790 pwaff = isl_pw_aff_cow(pwaff);
3791 if (!pwaff)
3792 return NULL;
3793 if (pwaff->n == 0)
3794 return pwaff;
3796 for (i = 0; i < pwaff->n; ++i) {
3797 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3798 if (!pwaff->p[i].set)
3799 return isl_pw_aff_free(pwaff);
3802 return pwaff;
3805 /* Mark the domains of the elements of "list" as rational.
3807 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3808 __isl_take isl_pw_aff_list *list)
3810 int i, n;
3812 if (!list)
3813 return NULL;
3814 if (list->n == 0)
3815 return list;
3817 n = list->n;
3818 for (i = 0; i < n; ++i) {
3819 isl_pw_aff *pa;
3821 pa = isl_pw_aff_list_get_pw_aff(list, i);
3822 pa = isl_pw_aff_set_rational(pa);
3823 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3826 return list;
3829 /* Do the parameters of "aff" match those of "space"?
3831 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3832 __isl_keep isl_space *space)
3834 isl_space *aff_space;
3835 isl_bool match;
3837 if (!aff || !space)
3838 return isl_bool_error;
3840 aff_space = isl_aff_get_domain_space(aff);
3842 match = isl_space_has_equal_params(space, aff_space);
3844 isl_space_free(aff_space);
3845 return match;
3848 /* Check that the domain space of "aff" matches "space".
3850 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3851 __isl_keep isl_space *space)
3853 isl_space *aff_space;
3854 isl_bool match;
3856 if (!aff || !space)
3857 return isl_stat_error;
3859 aff_space = isl_aff_get_domain_space(aff);
3861 match = isl_space_has_equal_params(space, aff_space);
3862 if (match < 0)
3863 goto error;
3864 if (!match)
3865 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3866 "parameters don't match", goto error);
3867 match = isl_space_tuple_is_equal(space, isl_dim_in,
3868 aff_space, isl_dim_set);
3869 if (match < 0)
3870 goto error;
3871 if (!match)
3872 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3873 "domains don't match", goto error);
3874 isl_space_free(aff_space);
3875 return isl_stat_ok;
3876 error:
3877 isl_space_free(aff_space);
3878 return isl_stat_error;
3881 /* Return the shared (universe) domain of the elements of "ma".
3883 * Since an isl_multi_aff (and an isl_aff) is always total,
3884 * the domain is always the universe set in its domain space.
3885 * This is a helper function for use in the generic isl_multi_*_bind.
3887 static __isl_give isl_basic_set *isl_multi_aff_domain(
3888 __isl_take isl_multi_aff *ma)
3890 isl_space *space;
3892 space = isl_multi_aff_get_space(ma);
3893 isl_multi_aff_free(ma);
3895 return isl_basic_set_universe(isl_space_domain(space));
3898 #undef BASE
3899 #define BASE aff
3901 #include <isl_multi_no_explicit_domain.c>
3902 #include <isl_multi_templ.c>
3903 #include <isl_multi_add_constant_templ.c>
3904 #include <isl_multi_apply_set.c>
3905 #include <isl_multi_arith_templ.c>
3906 #include <isl_multi_bind_domain_templ.c>
3907 #include <isl_multi_cmp.c>
3908 #include <isl_multi_dim_id_templ.c>
3909 #include <isl_multi_dims.c>
3910 #include <isl_multi_floor.c>
3911 #include <isl_multi_from_base_templ.c>
3912 #include <isl_multi_identity_templ.c>
3913 #include <isl_multi_insert_domain_templ.c>
3914 #include <isl_multi_locals_templ.c>
3915 #include <isl_multi_move_dims_templ.c>
3916 #include <isl_multi_nan_templ.c>
3917 #include <isl_multi_product_templ.c>
3918 #include <isl_multi_splice_templ.c>
3919 #include <isl_multi_tuple_id_templ.c>
3920 #include <isl_multi_unbind_params_templ.c>
3921 #include <isl_multi_zero_templ.c>
3923 #undef DOMBASE
3924 #define DOMBASE set
3925 #include <isl_multi_gist.c>
3927 #undef DOMBASE
3928 #define DOMBASE basic_set
3929 #include <isl_multi_bind_templ.c>
3931 /* Construct an isl_multi_aff living in "space" that corresponds
3932 * to the affine transformation matrix "mat".
3934 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3935 __isl_take isl_space *space, __isl_take isl_mat *mat)
3937 isl_ctx *ctx;
3938 isl_local_space *ls = NULL;
3939 isl_multi_aff *ma = NULL;
3940 isl_size n_row, n_col, n_out, total;
3941 int i;
3943 if (!space || !mat)
3944 goto error;
3946 ctx = isl_mat_get_ctx(mat);
3948 n_row = isl_mat_rows(mat);
3949 n_col = isl_mat_cols(mat);
3950 n_out = isl_space_dim(space, isl_dim_out);
3951 total = isl_space_dim(space, isl_dim_all);
3952 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3953 goto error;
3954 if (n_row < 1)
3955 isl_die(ctx, isl_error_invalid,
3956 "insufficient number of rows", goto error);
3957 if (n_col < 1)
3958 isl_die(ctx, isl_error_invalid,
3959 "insufficient number of columns", goto error);
3960 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3961 isl_die(ctx, isl_error_invalid,
3962 "dimension mismatch", goto error);
3964 ma = isl_multi_aff_zero(isl_space_copy(space));
3965 space = isl_space_domain(space);
3966 ls = isl_local_space_from_space(isl_space_copy(space));
3968 for (i = 0; i < n_row - 1; ++i) {
3969 isl_vec *v;
3970 isl_aff *aff;
3972 v = isl_vec_alloc(ctx, 1 + n_col);
3973 if (!v)
3974 goto error;
3975 isl_int_set(v->el[0], mat->row[0][0]);
3976 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3977 v = isl_vec_normalize(v);
3978 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3979 ma = isl_multi_aff_set_aff(ma, i, aff);
3982 isl_space_free(space);
3983 isl_local_space_free(ls);
3984 isl_mat_free(mat);
3985 return ma;
3986 error:
3987 isl_space_free(space);
3988 isl_local_space_free(ls);
3989 isl_mat_free(mat);
3990 isl_multi_aff_free(ma);
3991 return NULL;
3994 /* Return the constant terms of the affine expressions of "ma".
3996 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3997 __isl_keep isl_multi_aff *ma)
3999 int i;
4000 isl_size n;
4001 isl_space *space;
4002 isl_multi_val *mv;
4004 n = isl_multi_aff_size(ma);
4005 if (n < 0)
4006 return NULL;
4007 space = isl_space_range(isl_multi_aff_get_space(ma));
4008 space = isl_space_drop_all_params(space);
4009 mv = isl_multi_val_zero(space);
4011 for (i = 0; i < n; ++i) {
4012 isl_aff *aff;
4013 isl_val *val;
4015 aff = isl_multi_aff_get_at(ma, i);
4016 val = isl_aff_get_constant_val(aff);
4017 isl_aff_free(aff);
4018 mv = isl_multi_val_set_at(mv, i, val);
4021 return mv;
4024 /* Remove any internal structure of the domain of "ma".
4025 * If there is any such internal structure in the input,
4026 * then the name of the corresponding space is also removed.
4028 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4029 __isl_take isl_multi_aff *ma)
4031 isl_space *space;
4033 if (!ma)
4034 return NULL;
4036 if (!ma->space->nested[0])
4037 return ma;
4039 space = isl_multi_aff_get_space(ma);
4040 space = isl_space_flatten_domain(space);
4041 ma = isl_multi_aff_reset_space(ma, space);
4043 return ma;
4046 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4047 * of the space to its domain.
4049 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4051 int i;
4052 isl_size n_in;
4053 isl_local_space *ls;
4054 isl_multi_aff *ma;
4056 if (!space)
4057 return NULL;
4058 if (!isl_space_is_map(space))
4059 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4060 "not a map space", goto error);
4062 n_in = isl_space_dim(space, isl_dim_in);
4063 if (n_in < 0)
4064 goto error;
4065 space = isl_space_domain_map(space);
4067 ma = isl_multi_aff_alloc(isl_space_copy(space));
4068 if (n_in == 0) {
4069 isl_space_free(space);
4070 return ma;
4073 space = isl_space_domain(space);
4074 ls = isl_local_space_from_space(space);
4075 for (i = 0; i < n_in; ++i) {
4076 isl_aff *aff;
4078 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4079 isl_dim_set, i);
4080 ma = isl_multi_aff_set_aff(ma, i, aff);
4082 isl_local_space_free(ls);
4083 return ma;
4084 error:
4085 isl_space_free(space);
4086 return NULL;
4089 /* This function performs the same operation as isl_multi_aff_domain_map,
4090 * but is considered as a function on an isl_space when exported.
4092 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4093 __isl_take isl_space *space)
4095 return isl_multi_aff_domain_map(space);
4098 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4099 * of the space to its range.
4101 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4103 int i;
4104 isl_size n_in, n_out;
4105 isl_local_space *ls;
4106 isl_multi_aff *ma;
4108 if (!space)
4109 return NULL;
4110 if (!isl_space_is_map(space))
4111 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4112 "not a map space", goto error);
4114 n_in = isl_space_dim(space, isl_dim_in);
4115 n_out = isl_space_dim(space, isl_dim_out);
4116 if (n_in < 0 || n_out < 0)
4117 goto error;
4118 space = isl_space_range_map(space);
4120 ma = isl_multi_aff_alloc(isl_space_copy(space));
4121 if (n_out == 0) {
4122 isl_space_free(space);
4123 return ma;
4126 space = isl_space_domain(space);
4127 ls = isl_local_space_from_space(space);
4128 for (i = 0; i < n_out; ++i) {
4129 isl_aff *aff;
4131 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4132 isl_dim_set, n_in + i);
4133 ma = isl_multi_aff_set_aff(ma, i, aff);
4135 isl_local_space_free(ls);
4136 return ma;
4137 error:
4138 isl_space_free(space);
4139 return NULL;
4142 /* This function performs the same operation as isl_multi_aff_range_map,
4143 * but is considered as a function on an isl_space when exported.
4145 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4146 __isl_take isl_space *space)
4148 return isl_multi_aff_range_map(space);
4151 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4152 * of the space to its domain.
4154 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4155 __isl_take isl_space *space)
4157 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4160 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4161 * but is considered as a function on an isl_space when exported.
4163 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4164 __isl_take isl_space *space)
4166 return isl_pw_multi_aff_domain_map(space);
4169 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4170 * of the space to its range.
4172 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4173 __isl_take isl_space *space)
4175 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4178 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4179 * but is considered as a function on an isl_space when exported.
4181 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4182 __isl_take isl_space *space)
4184 return isl_pw_multi_aff_range_map(space);
4187 /* Given the space of a set and a range of set dimensions,
4188 * construct an isl_multi_aff that projects out those dimensions.
4190 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4191 __isl_take isl_space *space, enum isl_dim_type type,
4192 unsigned first, unsigned n)
4194 int i;
4195 isl_size dim;
4196 isl_local_space *ls;
4197 isl_multi_aff *ma;
4199 if (!space)
4200 return NULL;
4201 if (!isl_space_is_set(space))
4202 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4203 "expecting set space", goto error);
4204 if (type != isl_dim_set)
4205 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4206 "only set dimensions can be projected out", goto error);
4207 if (isl_space_check_range(space, type, first, n) < 0)
4208 goto error;
4210 dim = isl_space_dim(space, isl_dim_set);
4211 if (dim < 0)
4212 goto error;
4214 space = isl_space_from_domain(space);
4215 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4217 if (dim == n)
4218 return isl_multi_aff_alloc(space);
4220 ma = isl_multi_aff_alloc(isl_space_copy(space));
4221 space = isl_space_domain(space);
4222 ls = isl_local_space_from_space(space);
4224 for (i = 0; i < first; ++i) {
4225 isl_aff *aff;
4227 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4228 isl_dim_set, i);
4229 ma = isl_multi_aff_set_aff(ma, i, aff);
4232 for (i = 0; i < dim - (first + n); ++i) {
4233 isl_aff *aff;
4235 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4236 isl_dim_set, first + n + i);
4237 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4240 isl_local_space_free(ls);
4241 return ma;
4242 error:
4243 isl_space_free(space);
4244 return NULL;
4247 /* Given the space of a set and a range of set dimensions,
4248 * construct an isl_pw_multi_aff that projects out those dimensions.
4250 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4251 __isl_take isl_space *space, enum isl_dim_type type,
4252 unsigned first, unsigned n)
4254 isl_multi_aff *ma;
4256 ma = isl_multi_aff_project_out_map(space, type, first, n);
4257 return isl_pw_multi_aff_from_multi_aff(ma);
4260 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4261 * but is considered as a function on an isl_multi_aff when exported.
4263 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4264 __isl_take isl_multi_aff *ma)
4266 return isl_pw_multi_aff_from_multi_aff(ma);
4269 /* Create a piecewise multi-affine expression in the given space that maps each
4270 * input dimension to the corresponding output dimension.
4272 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4273 __isl_take isl_space *space)
4275 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4278 /* Create a piecewise multi expression that maps elements in the given space
4279 * to themselves.
4281 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4282 __isl_take isl_space *space)
4284 isl_multi_aff *ma;
4286 ma = isl_multi_aff_identity_on_domain_space(space);
4287 return isl_pw_multi_aff_from_multi_aff(ma);
4290 /* This function performs the same operation as
4291 * isl_pw_multi_aff_identity_on_domain_space,
4292 * but is considered as a function on an isl_space when exported.
4294 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4295 __isl_take isl_space *space)
4297 return isl_pw_multi_aff_identity_on_domain_space(space);
4300 /* Exploit the equalities in "eq" to simplify the affine expressions.
4302 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4303 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4305 int i;
4307 maff = isl_multi_aff_cow(maff);
4308 if (!maff || !eq)
4309 goto error;
4311 for (i = 0; i < maff->n; ++i) {
4312 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4313 isl_basic_set_copy(eq));
4314 if (!maff->u.p[i])
4315 goto error;
4318 isl_basic_set_free(eq);
4319 return maff;
4320 error:
4321 isl_basic_set_free(eq);
4322 isl_multi_aff_free(maff);
4323 return NULL;
4326 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4327 isl_int f)
4329 int i;
4331 maff = isl_multi_aff_cow(maff);
4332 if (!maff)
4333 return NULL;
4335 for (i = 0; i < maff->n; ++i) {
4336 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4337 if (!maff->u.p[i])
4338 return isl_multi_aff_free(maff);
4341 return maff;
4344 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4345 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4347 maff1 = isl_multi_aff_add(maff1, maff2);
4348 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4349 return maff1;
4352 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4354 if (!maff)
4355 return isl_bool_error;
4357 return isl_bool_false;
4360 /* Return the set of domain elements where "ma1" is lexicographically
4361 * smaller than or equal to "ma2".
4363 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4364 __isl_take isl_multi_aff *ma2)
4366 return isl_multi_aff_lex_ge_set(ma2, ma1);
4369 /* Return the set of domain elements where "ma1" is lexicographically
4370 * smaller than "ma2".
4372 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4373 __isl_take isl_multi_aff *ma2)
4375 return isl_multi_aff_lex_gt_set(ma2, ma1);
4378 /* Return the set of domain elements where "ma1" is lexicographically
4379 * greater than to "ma2". If "equal" is set, then include the domain
4380 * elements where they are equal.
4381 * Do this for the case where there are no entries.
4382 * In this case, "ma1" cannot be greater than "ma2",
4383 * but it is (greater than or) equal to "ma2".
4385 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4386 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4388 isl_space *space;
4390 space = isl_multi_aff_get_domain_space(ma1);
4392 isl_multi_aff_free(ma1);
4393 isl_multi_aff_free(ma2);
4395 if (equal)
4396 return isl_set_universe(space);
4397 else
4398 return isl_set_empty(space);
4401 /* Return the set where entry "i" of "ma1" and "ma2"
4402 * satisfy the relation prescribed by "cmp".
4404 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4405 __isl_keep isl_multi_aff *ma2, int i,
4406 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4407 __isl_take isl_aff *aff2))
4409 isl_aff *aff1, *aff2;
4411 aff1 = isl_multi_aff_get_at(ma1, i);
4412 aff2 = isl_multi_aff_get_at(ma2, i);
4413 return cmp(aff1, aff2);
4416 /* Return the set of domain elements where "ma1" is lexicographically
4417 * greater than to "ma2". If "equal" is set, then include the domain
4418 * elements where they are equal.
4420 * In particular, for all but the final entry,
4421 * include the set of elements where this entry is strictly greater in "ma1"
4422 * and all previous entries are equal.
4423 * The final entry is also allowed to be equal in the two functions
4424 * if "equal" is set.
4426 * The case where there are no entries is handled separately.
4428 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4429 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4431 int i;
4432 isl_size n;
4433 isl_space *space;
4434 isl_set *res;
4435 isl_set *equal_set;
4436 isl_set *gte;
4438 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4439 goto error;
4440 n = isl_multi_aff_size(ma1);
4441 if (n < 0)
4442 goto error;
4443 if (n == 0)
4444 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4446 space = isl_multi_aff_get_domain_space(ma1);
4447 res = isl_set_empty(isl_space_copy(space));
4448 equal_set = isl_set_universe(space);
4450 for (i = 0; i + 1 < n; ++i) {
4451 isl_bool empty;
4452 isl_set *gt, *eq;
4454 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4455 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4456 res = isl_set_union(res, gt);
4457 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4458 equal_set = isl_set_intersect(equal_set, eq);
4460 empty = isl_set_is_empty(equal_set);
4461 if (empty >= 0 && empty)
4462 break;
4465 if (equal)
4466 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4467 else
4468 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4469 isl_multi_aff_free(ma1);
4470 isl_multi_aff_free(ma2);
4472 gte = isl_set_intersect(gte, equal_set);
4473 return isl_set_union(res, gte);
4474 error:
4475 isl_multi_aff_free(ma1);
4476 isl_multi_aff_free(ma2);
4477 return NULL;
4480 /* Return the set of domain elements where "ma1" is lexicographically
4481 * greater than or equal to "ma2".
4483 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4484 __isl_take isl_multi_aff *ma2)
4486 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4489 /* Return the set of domain elements where "ma1" is lexicographically
4490 * greater than "ma2".
4492 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4493 __isl_take isl_multi_aff *ma2)
4495 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4498 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4500 #undef PW
4501 #define PW isl_pw_multi_aff
4502 #undef BASE
4503 #define BASE multi_aff
4504 #undef EL_IS_ZERO
4505 #define EL_IS_ZERO is_empty
4506 #undef ZERO
4507 #define ZERO empty
4508 #undef IS_ZERO
4509 #define IS_ZERO is_empty
4510 #undef FIELD
4511 #define FIELD maff
4512 #undef DEFAULT_IS_ZERO
4513 #define DEFAULT_IS_ZERO 0
4515 #include <isl_pw_templ.c>
4516 #include <isl_pw_add_constant_multi_val_templ.c>
4517 #include <isl_pw_add_constant_val_templ.c>
4518 #include <isl_pw_bind_domain_templ.c>
4519 #include <isl_pw_insert_dims_templ.c>
4520 #include <isl_pw_insert_domain_templ.c>
4521 #include <isl_pw_locals_templ.c>
4522 #include <isl_pw_move_dims_templ.c>
4523 #include <isl_pw_neg_templ.c>
4524 #include <isl_pw_pullback_templ.c>
4525 #include <isl_pw_range_tuple_id_templ.c>
4526 #include <isl_pw_union_opt.c>
4528 #undef BASE
4529 #define BASE pw_multi_aff
4531 #include <isl_union_multi.c>
4532 #include "isl_union_locals_templ.c"
4533 #include <isl_union_neg.c>
4535 #undef BASE
4536 #define BASE multi_aff
4538 #include <isl_union_pw_templ.c>
4540 /* Generic function for extracting a factor from a product "pma".
4541 * "check_space" checks that the space is that of the right kind of product.
4542 * "space_factor" extracts the factor from the space.
4543 * "multi_aff_factor" extracts the factor from the constituent functions.
4545 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4546 __isl_take isl_pw_multi_aff *pma,
4547 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4548 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4549 __isl_give isl_multi_aff *(*multi_aff_factor)(
4550 __isl_take isl_multi_aff *ma))
4552 int i;
4553 isl_space *space;
4555 if (check_space(pma) < 0)
4556 return isl_pw_multi_aff_free(pma);
4558 space = isl_pw_multi_aff_take_space(pma);
4559 space = space_factor(space);
4561 for (i = 0; pma && i < pma->n; ++i) {
4562 isl_multi_aff *ma;
4564 ma = isl_pw_multi_aff_take_base_at(pma, i);
4565 ma = multi_aff_factor(ma);
4566 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4569 pma = isl_pw_multi_aff_restore_space(pma, space);
4571 return pma;
4574 /* Is the range of "pma" a wrapped relation?
4576 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4577 __isl_keep isl_pw_multi_aff *pma)
4579 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4582 /* Check that the range of "pma" is a product.
4584 static isl_stat pw_multi_aff_check_range_product(
4585 __isl_keep isl_pw_multi_aff *pma)
4587 isl_bool wraps;
4589 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4590 if (wraps < 0)
4591 return isl_stat_error;
4592 if (!wraps)
4593 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4594 "range is not a product", return isl_stat_error);
4595 return isl_stat_ok;
4598 /* Given a function A -> [B -> C], extract the function A -> B.
4600 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4601 __isl_take isl_pw_multi_aff *pma)
4603 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4604 &isl_space_range_factor_domain,
4605 &isl_multi_aff_range_factor_domain);
4608 /* Given a function A -> [B -> C], extract the function A -> C.
4610 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4611 __isl_take isl_pw_multi_aff *pma)
4613 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4614 &isl_space_range_factor_range,
4615 &isl_multi_aff_range_factor_range);
4618 /* Given two piecewise multi affine expressions, return a piecewise
4619 * multi-affine expression defined on the union of the definition domains
4620 * of the inputs that is equal to the lexicographic maximum of the two
4621 * inputs on each cell. If only one of the two inputs is defined on
4622 * a given cell, then it is considered to be the maximum.
4624 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4625 __isl_take isl_pw_multi_aff *pma1,
4626 __isl_take isl_pw_multi_aff *pma2)
4628 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4629 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4630 &isl_multi_aff_lex_ge_set);
4633 /* Given two piecewise multi affine expressions, return a piecewise
4634 * multi-affine expression defined on the union of the definition domains
4635 * of the inputs that is equal to the lexicographic minimum of the two
4636 * inputs on each cell. If only one of the two inputs is defined on
4637 * a given cell, then it is considered to be the minimum.
4639 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4640 __isl_take isl_pw_multi_aff *pma1,
4641 __isl_take isl_pw_multi_aff *pma2)
4643 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4644 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4645 &isl_multi_aff_lex_le_set);
4648 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4649 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4651 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4652 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4653 &isl_multi_aff_add);
4656 /* Subtract "pma2" from "pma1" and return the result.
4658 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4659 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4661 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4662 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4663 &isl_multi_aff_sub);
4666 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4667 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4669 return isl_pw_multi_aff_union_add_(pma1, pma2);
4672 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4673 * with the actual sum on the shared domain and
4674 * the defined expression on the symmetric difference of the domains.
4676 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4677 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4679 return isl_union_pw_aff_union_add_(upa1, upa2);
4682 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4683 * with the actual sum on the shared domain and
4684 * the defined expression on the symmetric difference of the domains.
4686 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4687 __isl_take isl_union_pw_multi_aff *upma1,
4688 __isl_take isl_union_pw_multi_aff *upma2)
4690 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4693 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4694 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4696 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4697 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4699 int i, j, n;
4700 isl_space *space;
4701 isl_pw_multi_aff *res;
4703 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4704 goto error;
4706 n = pma1->n * pma2->n;
4707 space = isl_space_product(isl_space_copy(pma1->dim),
4708 isl_space_copy(pma2->dim));
4709 res = isl_pw_multi_aff_alloc_size(space, n);
4711 for (i = 0; i < pma1->n; ++i) {
4712 for (j = 0; j < pma2->n; ++j) {
4713 isl_set *domain;
4714 isl_multi_aff *ma;
4716 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4717 isl_set_copy(pma2->p[j].set));
4718 ma = isl_multi_aff_product(
4719 isl_multi_aff_copy(pma1->p[i].maff),
4720 isl_multi_aff_copy(pma2->p[j].maff));
4721 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4725 isl_pw_multi_aff_free(pma1);
4726 isl_pw_multi_aff_free(pma2);
4727 return res;
4728 error:
4729 isl_pw_multi_aff_free(pma1);
4730 isl_pw_multi_aff_free(pma2);
4731 return NULL;
4734 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4735 * denominator "denom".
4736 * "denom" is allowed to be negative, in which case the actual denominator
4737 * is -denom and the expressions are added instead.
4739 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4740 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4742 int i, first;
4743 int sign;
4744 isl_int d;
4746 first = isl_seq_first_non_zero(c, n);
4747 if (first == -1)
4748 return aff;
4750 sign = isl_int_sgn(denom);
4751 isl_int_init(d);
4752 isl_int_abs(d, denom);
4753 for (i = first; i < n; ++i) {
4754 isl_aff *aff_i;
4756 if (isl_int_is_zero(c[i]))
4757 continue;
4758 aff_i = isl_multi_aff_get_aff(ma, i);
4759 aff_i = isl_aff_scale(aff_i, c[i]);
4760 aff_i = isl_aff_scale_down(aff_i, d);
4761 if (sign >= 0)
4762 aff = isl_aff_sub(aff, aff_i);
4763 else
4764 aff = isl_aff_add(aff, aff_i);
4766 isl_int_clear(d);
4768 return aff;
4771 /* Extract an affine expression that expresses the output dimension "pos"
4772 * of "bmap" in terms of the parameters and input dimensions from
4773 * equality "eq".
4774 * Note that this expression may involve integer divisions defined
4775 * in terms of parameters and input dimensions.
4776 * The equality may also involve references to earlier (but not later)
4777 * output dimensions. These are replaced by the corresponding elements
4778 * in "ma".
4780 * If the equality is of the form
4782 * f(i) + h(j) + a x + g(i) = 0,
4784 * with f(i) a linear combinations of the parameters and input dimensions,
4785 * g(i) a linear combination of integer divisions defined in terms of the same
4786 * and h(j) a linear combinations of earlier output dimensions,
4787 * then the affine expression is
4789 * (-f(i) - g(i))/a - h(j)/a
4791 * If the equality is of the form
4793 * f(i) + h(j) - a x + g(i) = 0,
4795 * then the affine expression is
4797 * (f(i) + g(i))/a - h(j)/(-a)
4800 * If "div" refers to an integer division (i.e., it is smaller than
4801 * the number of integer divisions), then the equality constraint
4802 * does involve an integer division (the one at position "div") that
4803 * is defined in terms of output dimensions. However, this integer
4804 * division can be eliminated by exploiting a pair of constraints
4805 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4806 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4807 * -l + x >= 0.
4808 * In particular, let
4810 * x = e(i) + m floor(...)
4812 * with e(i) the expression derived above and floor(...) the integer
4813 * division involving output dimensions.
4814 * From
4816 * l <= x <= l + n,
4818 * we have
4820 * 0 <= x - l <= n
4822 * This means
4824 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4825 * = (e(i) - l) mod m
4827 * Therefore,
4829 * x - l = (e(i) - l) mod m
4831 * or
4833 * x = ((e(i) - l) mod m) + l
4835 * The variable "shift" below contains the expression -l, which may
4836 * also involve a linear combination of earlier output dimensions.
4838 static __isl_give isl_aff *extract_aff_from_equality(
4839 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4840 __isl_keep isl_multi_aff *ma)
4842 unsigned o_out;
4843 isl_size n_div, n_out;
4844 isl_ctx *ctx;
4845 isl_local_space *ls;
4846 isl_aff *aff, *shift;
4847 isl_val *mod;
4849 ctx = isl_basic_map_get_ctx(bmap);
4850 ls = isl_basic_map_get_local_space(bmap);
4851 ls = isl_local_space_domain(ls);
4852 aff = isl_aff_alloc(isl_local_space_copy(ls));
4853 if (!aff)
4854 goto error;
4855 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4856 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4857 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4858 if (n_out < 0 || n_div < 0)
4859 goto error;
4860 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4861 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4862 isl_seq_cpy(aff->v->el + 1 + o_out,
4863 bmap->eq[eq] + o_out + n_out, n_div);
4864 } else {
4865 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4866 isl_seq_neg(aff->v->el + 1 + o_out,
4867 bmap->eq[eq] + o_out + n_out, n_div);
4869 if (div < n_div)
4870 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4871 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4872 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4873 bmap->eq[eq][o_out + pos]);
4874 if (div < n_div) {
4875 shift = isl_aff_alloc(isl_local_space_copy(ls));
4876 if (!shift)
4877 goto error;
4878 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4879 isl_seq_cpy(shift->v->el + 1 + o_out,
4880 bmap->ineq[ineq] + o_out + n_out, n_div);
4881 isl_int_set_si(shift->v->el[0], 1);
4882 shift = subtract_initial(shift, ma, pos,
4883 bmap->ineq[ineq] + o_out, ctx->negone);
4884 aff = isl_aff_add(aff, isl_aff_copy(shift));
4885 mod = isl_val_int_from_isl_int(ctx,
4886 bmap->eq[eq][o_out + n_out + div]);
4887 mod = isl_val_abs(mod);
4888 aff = isl_aff_mod_val(aff, mod);
4889 aff = isl_aff_sub(aff, shift);
4892 isl_local_space_free(ls);
4893 return aff;
4894 error:
4895 isl_local_space_free(ls);
4896 isl_aff_free(aff);
4897 return NULL;
4900 /* Given a basic map with output dimensions defined
4901 * in terms of the parameters input dimensions and earlier
4902 * output dimensions using an equality (and possibly a pair on inequalities),
4903 * extract an isl_aff that expresses output dimension "pos" in terms
4904 * of the parameters and input dimensions.
4905 * Note that this expression may involve integer divisions defined
4906 * in terms of parameters and input dimensions.
4907 * "ma" contains the expressions corresponding to earlier output dimensions.
4909 * This function shares some similarities with
4910 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4912 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4913 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4915 int eq, div, ineq;
4916 isl_aff *aff;
4918 if (!bmap)
4919 return NULL;
4920 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4921 if (eq >= bmap->n_eq)
4922 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4923 "unable to find suitable equality", return NULL);
4924 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4926 aff = isl_aff_remove_unused_divs(aff);
4927 return aff;
4930 /* Given a basic map where each output dimension is defined
4931 * in terms of the parameters and input dimensions using an equality,
4932 * extract an isl_multi_aff that expresses the output dimensions in terms
4933 * of the parameters and input dimensions.
4935 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4936 __isl_take isl_basic_map *bmap)
4938 int i;
4939 isl_size n_out;
4940 isl_multi_aff *ma;
4942 if (!bmap)
4943 return NULL;
4945 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4946 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4947 if (n_out < 0)
4948 ma = isl_multi_aff_free(ma);
4950 for (i = 0; i < n_out; ++i) {
4951 isl_aff *aff;
4953 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4954 ma = isl_multi_aff_set_aff(ma, i, aff);
4957 isl_basic_map_free(bmap);
4959 return ma;
4962 /* Given a basic set where each set dimension is defined
4963 * in terms of the parameters using an equality,
4964 * extract an isl_multi_aff that expresses the set dimensions in terms
4965 * of the parameters.
4967 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4968 __isl_take isl_basic_set *bset)
4970 return extract_isl_multi_aff_from_basic_map(bset);
4973 /* Create an isl_pw_multi_aff that is equivalent to
4974 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4975 * The given basic map is such that each output dimension is defined
4976 * in terms of the parameters and input dimensions using an equality.
4978 * Since some applications expect the result of isl_pw_multi_aff_from_map
4979 * to only contain integer affine expressions, we compute the floor
4980 * of the expression before returning.
4982 * Remove all constraints involving local variables without
4983 * an explicit representation (resulting in the removal of those
4984 * local variables) prior to the actual extraction to ensure
4985 * that the local spaces in which the resulting affine expressions
4986 * are created do not contain any unknown local variables.
4987 * Removing such constraints is safe because constraints involving
4988 * unknown local variables are not used to determine whether
4989 * a basic map is obviously single-valued.
4991 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4992 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4994 isl_multi_aff *ma;
4996 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4997 ma = extract_isl_multi_aff_from_basic_map(bmap);
4998 ma = isl_multi_aff_floor(ma);
4999 return isl_pw_multi_aff_alloc(domain, ma);
5002 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5003 * This obviously only works if the input "map" is single-valued.
5004 * If so, we compute the lexicographic minimum of the image in the form
5005 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5006 * to its lexicographic minimum.
5007 * If the input is not single-valued, we produce an error.
5009 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
5010 __isl_take isl_map *map)
5012 int i;
5013 int sv;
5014 isl_pw_multi_aff *pma;
5016 sv = isl_map_is_single_valued(map);
5017 if (sv < 0)
5018 goto error;
5019 if (!sv)
5020 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5021 "map is not single-valued", goto error);
5022 map = isl_map_make_disjoint(map);
5023 if (!map)
5024 return NULL;
5026 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5028 for (i = 0; i < map->n; ++i) {
5029 isl_pw_multi_aff *pma_i;
5030 isl_basic_map *bmap;
5031 bmap = isl_basic_map_copy(map->p[i]);
5032 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5033 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5036 isl_map_free(map);
5037 return pma;
5038 error:
5039 isl_map_free(map);
5040 return NULL;
5043 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5044 * taking into account that the output dimension at position "d"
5045 * can be represented as
5047 * x = floor((e(...) + c1) / m)
5049 * given that constraint "i" is of the form
5051 * e(...) + c1 - m x >= 0
5054 * Let "map" be of the form
5056 * A -> B
5058 * We construct a mapping
5060 * A -> [A -> x = floor(...)]
5062 * apply that to the map, obtaining
5064 * [A -> x = floor(...)] -> B
5066 * and equate dimension "d" to x.
5067 * We then compute a isl_pw_multi_aff representation of the resulting map
5068 * and plug in the mapping above.
5070 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5071 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5073 isl_ctx *ctx;
5074 isl_space *space = NULL;
5075 isl_local_space *ls;
5076 isl_multi_aff *ma;
5077 isl_aff *aff;
5078 isl_vec *v;
5079 isl_map *insert;
5080 int offset;
5081 isl_size n;
5082 isl_size n_in;
5083 isl_pw_multi_aff *pma;
5084 isl_bool is_set;
5086 is_set = isl_map_is_set(map);
5087 if (is_set < 0)
5088 goto error;
5090 offset = isl_basic_map_offset(hull, isl_dim_out);
5091 ctx = isl_map_get_ctx(map);
5092 space = isl_space_domain(isl_map_get_space(map));
5093 n_in = isl_space_dim(space, isl_dim_set);
5094 n = isl_space_dim(space, isl_dim_all);
5095 if (n_in < 0 || n < 0)
5096 goto error;
5098 v = isl_vec_alloc(ctx, 1 + 1 + n);
5099 if (v) {
5100 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5101 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5103 isl_basic_map_free(hull);
5105 ls = isl_local_space_from_space(isl_space_copy(space));
5106 aff = isl_aff_alloc_vec(ls, v);
5107 aff = isl_aff_floor(aff);
5108 if (is_set) {
5109 isl_space_free(space);
5110 ma = isl_multi_aff_from_aff(aff);
5111 } else {
5112 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5113 ma = isl_multi_aff_range_product(ma,
5114 isl_multi_aff_from_aff(aff));
5117 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5118 map = isl_map_apply_domain(map, insert);
5119 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5120 pma = isl_pw_multi_aff_from_map(map);
5121 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5123 return pma;
5124 error:
5125 isl_space_free(space);
5126 isl_map_free(map);
5127 isl_basic_map_free(hull);
5128 return NULL;
5131 /* Is constraint "c" of the form
5133 * e(...) + c1 - m x >= 0
5135 * or
5137 * -e(...) + c2 + m x >= 0
5139 * where m > 1 and e only depends on parameters and input dimensions?
5141 * "offset" is the offset of the output dimensions
5142 * "pos" is the position of output dimension x.
5144 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5146 if (isl_int_is_zero(c[offset + d]))
5147 return 0;
5148 if (isl_int_is_one(c[offset + d]))
5149 return 0;
5150 if (isl_int_is_negone(c[offset + d]))
5151 return 0;
5152 if (isl_seq_first_non_zero(c + offset, d) != -1)
5153 return 0;
5154 if (isl_seq_first_non_zero(c + offset + d + 1,
5155 total - (offset + d + 1)) != -1)
5156 return 0;
5157 return 1;
5160 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5162 * As a special case, we first check if there is any pair of constraints,
5163 * shared by all the basic maps in "map" that force a given dimension
5164 * to be equal to the floor of some affine combination of the input dimensions.
5166 * In particular, if we can find two constraints
5168 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5170 * and
5172 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5174 * where m > 1 and e only depends on parameters and input dimensions,
5175 * and such that
5177 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5179 * then we know that we can take
5181 * x = floor((e(...) + c1) / m)
5183 * without having to perform any computation.
5185 * Note that we know that
5187 * c1 + c2 >= 1
5189 * If c1 + c2 were 0, then we would have detected an equality during
5190 * simplification. If c1 + c2 were negative, then we would have detected
5191 * a contradiction.
5193 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5194 __isl_take isl_map *map)
5196 int d;
5197 isl_size dim;
5198 int i, j, n;
5199 int offset;
5200 isl_size total;
5201 isl_int sum;
5202 isl_basic_map *hull;
5204 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5205 dim = isl_map_dim(map, isl_dim_out);
5206 total = isl_basic_map_dim(hull, isl_dim_all);
5207 if (dim < 0 || total < 0)
5208 goto error;
5210 isl_int_init(sum);
5211 offset = isl_basic_map_offset(hull, isl_dim_out);
5212 n = hull->n_ineq;
5213 for (d = 0; d < dim; ++d) {
5214 for (i = 0; i < n; ++i) {
5215 if (!is_potential_div_constraint(hull->ineq[i],
5216 offset, d, 1 + total))
5217 continue;
5218 for (j = i + 1; j < n; ++j) {
5219 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5220 hull->ineq[j] + 1, total))
5221 continue;
5222 isl_int_add(sum, hull->ineq[i][0],
5223 hull->ineq[j][0]);
5224 if (isl_int_abs_lt(sum,
5225 hull->ineq[i][offset + d]))
5226 break;
5229 if (j >= n)
5230 continue;
5231 isl_int_clear(sum);
5232 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5233 j = i;
5234 return pw_multi_aff_from_map_div(map, hull, d, j);
5237 isl_int_clear(sum);
5238 isl_basic_map_free(hull);
5239 return pw_multi_aff_from_map_base(map);
5240 error:
5241 isl_map_free(map);
5242 isl_basic_map_free(hull);
5243 return NULL;
5246 /* Given an affine expression
5248 * [A -> B] -> f(A,B)
5250 * construct an isl_multi_aff
5252 * [A -> B] -> B'
5254 * such that dimension "d" in B' is set to "aff" and the remaining
5255 * dimensions are set equal to the corresponding dimensions in B.
5256 * "n_in" is the dimension of the space A.
5257 * "n_out" is the dimension of the space B.
5259 * If "is_set" is set, then the affine expression is of the form
5261 * [B] -> f(B)
5263 * and we construct an isl_multi_aff
5265 * B -> B'
5267 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5268 unsigned n_in, unsigned n_out, int is_set)
5270 int i;
5271 isl_multi_aff *ma;
5272 isl_space *space, *space2;
5273 isl_local_space *ls;
5275 space = isl_aff_get_domain_space(aff);
5276 ls = isl_local_space_from_space(isl_space_copy(space));
5277 space2 = isl_space_copy(space);
5278 if (!is_set)
5279 space2 = isl_space_range(isl_space_unwrap(space2));
5280 space = isl_space_map_from_domain_and_range(space, space2);
5281 ma = isl_multi_aff_alloc(space);
5282 ma = isl_multi_aff_set_aff(ma, d, aff);
5284 for (i = 0; i < n_out; ++i) {
5285 if (i == d)
5286 continue;
5287 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5288 isl_dim_set, n_in + i);
5289 ma = isl_multi_aff_set_aff(ma, i, aff);
5292 isl_local_space_free(ls);
5294 return ma;
5297 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5298 * taking into account that the dimension at position "d" can be written as
5300 * x = m a + f(..) (1)
5302 * where m is equal to "gcd".
5303 * "i" is the index of the equality in "hull" that defines f(..).
5304 * In particular, the equality is of the form
5306 * f(..) - x + m g(existentials) = 0
5308 * or
5310 * -f(..) + x + m g(existentials) = 0
5312 * We basically plug (1) into "map", resulting in a map with "a"
5313 * in the range instead of "x". The corresponding isl_pw_multi_aff
5314 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5316 * Specifically, given the input map
5318 * A -> B
5320 * We first wrap it into a set
5322 * [A -> B]
5324 * and define (1) on top of the corresponding space, resulting in "aff".
5325 * We use this to create an isl_multi_aff that maps the output position "d"
5326 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5327 * We plug this into the wrapped map, unwrap the result and compute the
5328 * corresponding isl_pw_multi_aff.
5329 * The result is an expression
5331 * A -> T(A)
5333 * We adjust that to
5335 * A -> [A -> T(A)]
5337 * so that we can plug that into "aff", after extending the latter to
5338 * a mapping
5340 * [A -> B] -> B'
5343 * If "map" is actually a set, then there is no "A" space, meaning
5344 * that we do not need to perform any wrapping, and that the result
5345 * of the recursive call is of the form
5347 * [T]
5349 * which is plugged into a mapping of the form
5351 * B -> B'
5353 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5354 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5355 isl_int gcd)
5357 isl_set *set;
5358 isl_space *space;
5359 isl_local_space *ls;
5360 isl_aff *aff;
5361 isl_multi_aff *ma;
5362 isl_pw_multi_aff *pma, *id;
5363 isl_size n_in;
5364 unsigned o_out;
5365 isl_size n_out;
5366 isl_bool is_set;
5368 is_set = isl_map_is_set(map);
5369 if (is_set < 0)
5370 goto error;
5372 n_in = isl_basic_map_dim(hull, isl_dim_in);
5373 n_out = isl_basic_map_dim(hull, isl_dim_out);
5374 if (n_in < 0 || n_out < 0)
5375 goto error;
5376 o_out = isl_basic_map_offset(hull, isl_dim_out);
5378 if (is_set)
5379 set = map;
5380 else
5381 set = isl_map_wrap(map);
5382 space = isl_space_map_from_set(isl_set_get_space(set));
5383 ma = isl_multi_aff_identity(space);
5384 ls = isl_local_space_from_space(isl_set_get_space(set));
5385 aff = isl_aff_alloc(ls);
5386 if (aff) {
5387 isl_int_set_si(aff->v->el[0], 1);
5388 if (isl_int_is_one(hull->eq[i][o_out + d]))
5389 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5390 aff->v->size - 1);
5391 else
5392 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5393 aff->v->size - 1);
5394 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5396 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5397 set = isl_set_preimage_multi_aff(set, ma);
5399 ma = range_map(aff, d, n_in, n_out, is_set);
5401 if (is_set)
5402 map = set;
5403 else
5404 map = isl_set_unwrap(set);
5405 pma = isl_pw_multi_aff_from_map(map);
5407 if (!is_set) {
5408 space = isl_pw_multi_aff_get_domain_space(pma);
5409 space = isl_space_map_from_set(space);
5410 id = isl_pw_multi_aff_identity(space);
5411 pma = isl_pw_multi_aff_range_product(id, pma);
5413 id = isl_pw_multi_aff_from_multi_aff(ma);
5414 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5416 isl_basic_map_free(hull);
5417 return pma;
5418 error:
5419 isl_map_free(map);
5420 isl_basic_map_free(hull);
5421 return NULL;
5424 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5425 * "hull" contains the equalities valid for "map".
5427 * Check if any of the output dimensions is "strided".
5428 * That is, we check if it can be written as
5430 * x = m a + f(..)
5432 * with m greater than 1, a some combination of existentially quantified
5433 * variables and f an expression in the parameters and input dimensions.
5434 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5436 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5437 * special case.
5439 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5440 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5442 int i, j;
5443 isl_size n_out;
5444 unsigned o_out;
5445 isl_size n_div;
5446 unsigned o_div;
5447 isl_int gcd;
5449 n_div = isl_basic_map_dim(hull, isl_dim_div);
5450 n_out = isl_basic_map_dim(hull, isl_dim_out);
5451 if (n_div < 0 || n_out < 0)
5452 goto error;
5454 if (n_div == 0) {
5455 isl_basic_map_free(hull);
5456 return pw_multi_aff_from_map_check_div(map);
5459 isl_int_init(gcd);
5461 o_div = isl_basic_map_offset(hull, isl_dim_div);
5462 o_out = isl_basic_map_offset(hull, isl_dim_out);
5464 for (i = 0; i < n_out; ++i) {
5465 for (j = 0; j < hull->n_eq; ++j) {
5466 isl_int *eq = hull->eq[j];
5467 isl_pw_multi_aff *res;
5469 if (!isl_int_is_one(eq[o_out + i]) &&
5470 !isl_int_is_negone(eq[o_out + i]))
5471 continue;
5472 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5473 continue;
5474 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5475 n_out - (i + 1)) != -1)
5476 continue;
5477 isl_seq_gcd(eq + o_div, n_div, &gcd);
5478 if (isl_int_is_zero(gcd))
5479 continue;
5480 if (isl_int_is_one(gcd))
5481 continue;
5483 res = pw_multi_aff_from_map_stride(map, hull,
5484 i, j, gcd);
5485 isl_int_clear(gcd);
5486 return res;
5490 isl_int_clear(gcd);
5491 isl_basic_map_free(hull);
5492 return pw_multi_aff_from_map_check_div(map);
5493 error:
5494 isl_map_free(map);
5495 isl_basic_map_free(hull);
5496 return NULL;
5499 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5501 * As a special case, we first check if all output dimensions are uniquely
5502 * defined in terms of the parameters and input dimensions over the entire
5503 * domain. If so, we extract the desired isl_pw_multi_aff directly
5504 * from the affine hull of "map" and its domain.
5506 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5507 * special cases.
5509 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5511 isl_bool sv;
5512 isl_size n;
5513 isl_basic_map *hull;
5515 n = isl_map_n_basic_map(map);
5516 if (n < 0)
5517 goto error;
5519 if (n == 1) {
5520 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5521 hull = isl_basic_map_plain_affine_hull(hull);
5522 sv = isl_basic_map_plain_is_single_valued(hull);
5523 if (sv >= 0 && sv)
5524 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5525 hull);
5526 isl_basic_map_free(hull);
5528 map = isl_map_detect_equalities(map);
5529 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5530 sv = isl_basic_map_plain_is_single_valued(hull);
5531 if (sv >= 0 && sv)
5532 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5533 if (sv >= 0)
5534 return pw_multi_aff_from_map_check_strides(map, hull);
5535 isl_basic_map_free(hull);
5536 error:
5537 isl_map_free(map);
5538 return NULL;
5541 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5542 * but is considered as a function on an isl_map when exported.
5544 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5546 return isl_pw_multi_aff_from_map(map);
5549 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5551 return isl_pw_multi_aff_from_map(set);
5554 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5555 * but is considered as a function on an isl_set when exported.
5557 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5559 return isl_pw_multi_aff_from_set(set);
5562 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5563 * add it to *user.
5565 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5567 isl_union_pw_multi_aff **upma = user;
5568 isl_pw_multi_aff *pma;
5570 pma = isl_pw_multi_aff_from_map(map);
5571 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5573 return *upma ? isl_stat_ok : isl_stat_error;
5576 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5577 * domain.
5579 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5580 __isl_take isl_aff *aff)
5582 isl_multi_aff *ma;
5583 isl_pw_multi_aff *pma;
5585 ma = isl_multi_aff_from_aff(aff);
5586 pma = isl_pw_multi_aff_from_multi_aff(ma);
5587 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5590 /* Try and create an isl_union_pw_multi_aff that is equivalent
5591 * to the given isl_union_map.
5592 * The isl_union_map is required to be single-valued in each space.
5593 * Otherwise, an error is produced.
5595 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5596 __isl_take isl_union_map *umap)
5598 isl_space *space;
5599 isl_union_pw_multi_aff *upma;
5601 space = isl_union_map_get_space(umap);
5602 upma = isl_union_pw_multi_aff_empty(space);
5603 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5604 upma = isl_union_pw_multi_aff_free(upma);
5605 isl_union_map_free(umap);
5607 return upma;
5610 /* This function performs the same operation as
5611 * isl_union_pw_multi_aff_from_union_map,
5612 * but is considered as a function on an isl_union_map when exported.
5614 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5615 __isl_take isl_union_map *umap)
5617 return isl_union_pw_multi_aff_from_union_map(umap);
5620 /* Try and create an isl_union_pw_multi_aff that is equivalent
5621 * to the given isl_union_set.
5622 * The isl_union_set is required to be a singleton in each space.
5623 * Otherwise, an error is produced.
5625 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5626 __isl_take isl_union_set *uset)
5628 return isl_union_pw_multi_aff_from_union_map(uset);
5631 /* Return the piecewise affine expression "set ? 1 : 0".
5633 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5635 isl_pw_aff *pa;
5636 isl_space *space = isl_set_get_space(set);
5637 isl_local_space *ls = isl_local_space_from_space(space);
5638 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5639 isl_aff *one = isl_aff_zero_on_domain(ls);
5641 one = isl_aff_add_constant_si(one, 1);
5642 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5643 set = isl_set_complement(set);
5644 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5646 return pa;
5649 /* Plug in "subs" for dimension "type", "pos" of "aff".
5651 * Let i be the dimension to replace and let "subs" be of the form
5653 * f/d
5655 * and "aff" of the form
5657 * (a i + g)/m
5659 * The result is
5661 * (a f + d g')/(m d)
5663 * where g' is the result of plugging in "subs" in each of the integer
5664 * divisions in g.
5666 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5667 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5669 isl_ctx *ctx;
5670 isl_int v;
5671 isl_size n_div;
5673 aff = isl_aff_cow(aff);
5674 if (!aff || !subs)
5675 return isl_aff_free(aff);
5677 ctx = isl_aff_get_ctx(aff);
5678 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5679 isl_die(ctx, isl_error_invalid,
5680 "spaces don't match", return isl_aff_free(aff));
5681 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5682 if (n_div < 0)
5683 return isl_aff_free(aff);
5684 if (n_div != 0)
5685 isl_die(ctx, isl_error_unsupported,
5686 "cannot handle divs yet", return isl_aff_free(aff));
5688 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5689 if (!aff->ls)
5690 return isl_aff_free(aff);
5692 aff->v = isl_vec_cow(aff->v);
5693 if (!aff->v)
5694 return isl_aff_free(aff);
5696 pos += isl_local_space_offset(aff->ls, type);
5698 isl_int_init(v);
5699 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5700 aff->v->size, subs->v->size, v);
5701 isl_int_clear(v);
5703 return aff;
5706 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5707 * expressions in "maff".
5709 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5710 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5711 __isl_keep isl_aff *subs)
5713 int i;
5715 maff = isl_multi_aff_cow(maff);
5716 if (!maff || !subs)
5717 return isl_multi_aff_free(maff);
5719 if (type == isl_dim_in)
5720 type = isl_dim_set;
5722 for (i = 0; i < maff->n; ++i) {
5723 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5724 type, pos, subs);
5725 if (!maff->u.p[i])
5726 return isl_multi_aff_free(maff);
5729 return maff;
5732 /* Plug in "subs" for input dimension "pos" of "pma".
5734 * pma is of the form
5736 * A_i(v) -> M_i(v)
5738 * while subs is of the form
5740 * v' = B_j(v) -> S_j
5742 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5743 * has a contribution in the result, in particular
5745 * C_ij(S_j) -> M_i(S_j)
5747 * Note that plugging in S_j in C_ij may also result in an empty set
5748 * and this contribution should simply be discarded.
5750 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5751 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5752 __isl_keep isl_pw_aff *subs)
5754 int i, j, n;
5755 isl_pw_multi_aff *res;
5757 if (!pma || !subs)
5758 return isl_pw_multi_aff_free(pma);
5760 n = pma->n * subs->n;
5761 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5763 for (i = 0; i < pma->n; ++i) {
5764 for (j = 0; j < subs->n; ++j) {
5765 isl_set *common;
5766 isl_multi_aff *res_ij;
5767 int empty;
5769 common = isl_set_intersect(
5770 isl_set_copy(pma->p[i].set),
5771 isl_set_copy(subs->p[j].set));
5772 common = isl_set_substitute(common,
5773 pos, subs->p[j].aff);
5774 empty = isl_set_plain_is_empty(common);
5775 if (empty < 0 || empty) {
5776 isl_set_free(common);
5777 if (empty < 0)
5778 goto error;
5779 continue;
5782 res_ij = isl_multi_aff_substitute(
5783 isl_multi_aff_copy(pma->p[i].maff),
5784 isl_dim_in, pos, subs->p[j].aff);
5786 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5790 isl_pw_multi_aff_free(pma);
5791 return res;
5792 error:
5793 isl_pw_multi_aff_free(pma);
5794 isl_pw_multi_aff_free(res);
5795 return NULL;
5798 /* Compute the preimage of a range of dimensions in the affine expression "src"
5799 * under "ma" and put the result in "dst". The number of dimensions in "src"
5800 * that precede the range is given by "n_before". The number of dimensions
5801 * in the range is given by the number of output dimensions of "ma".
5802 * The number of dimensions that follow the range is given by "n_after".
5803 * If "has_denom" is set (to one),
5804 * then "src" and "dst" have an extra initial denominator.
5805 * "n_div_ma" is the number of existentials in "ma"
5806 * "n_div_bset" is the number of existentials in "src"
5807 * The resulting "dst" (which is assumed to have been allocated by
5808 * the caller) contains coefficients for both sets of existentials,
5809 * first those in "ma" and then those in "src".
5810 * f, c1, c2 and g are temporary objects that have been initialized
5811 * by the caller.
5813 * Let src represent the expression
5815 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5817 * and let ma represent the expressions
5819 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5821 * We start out with the following expression for dst:
5823 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5825 * with the multiplication factor f initially equal to 1
5826 * and f \sum_i b_i v_i kept separately.
5827 * For each x_i that we substitute, we multiply the numerator
5828 * (and denominator) of dst by c_1 = m_i and add the numerator
5829 * of the x_i expression multiplied by c_2 = f b_i,
5830 * after removing the common factors of c_1 and c_2.
5831 * The multiplication factor f also needs to be multiplied by c_1
5832 * for the next x_j, j > i.
5834 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5835 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5836 int n_div_ma, int n_div_bmap,
5837 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5839 int i;
5840 isl_size n_param, n_in, n_out;
5841 int o_dst, o_src;
5843 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5844 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5845 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5846 if (n_param < 0 || n_in < 0 || n_out < 0)
5847 return isl_stat_error;
5849 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5850 o_dst = o_src = has_denom + 1 + n_param + n_before;
5851 isl_seq_clr(dst + o_dst, n_in);
5852 o_dst += n_in;
5853 o_src += n_out;
5854 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5855 o_dst += n_after;
5856 o_src += n_after;
5857 isl_seq_clr(dst + o_dst, n_div_ma);
5858 o_dst += n_div_ma;
5859 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5861 isl_int_set_si(f, 1);
5863 for (i = 0; i < n_out; ++i) {
5864 int offset = has_denom + 1 + n_param + n_before + i;
5866 if (isl_int_is_zero(src[offset]))
5867 continue;
5868 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5869 isl_int_mul(c2, f, src[offset]);
5870 isl_int_gcd(g, c1, c2);
5871 isl_int_divexact(c1, c1, g);
5872 isl_int_divexact(c2, c2, g);
5874 isl_int_mul(f, f, c1);
5875 o_dst = has_denom;
5876 o_src = 1;
5877 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5878 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5879 o_dst += 1 + n_param;
5880 o_src += 1 + n_param;
5881 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5882 o_dst += n_before;
5883 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5884 c2, ma->u.p[i]->v->el + o_src, n_in);
5885 o_dst += n_in;
5886 o_src += n_in;
5887 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5888 o_dst += n_after;
5889 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5890 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5891 o_dst += n_div_ma;
5892 o_src += n_div_ma;
5893 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5894 if (has_denom)
5895 isl_int_mul(dst[0], dst[0], c1);
5898 return isl_stat_ok;
5901 /* Compute the pullback of "aff" by the function represented by "ma".
5902 * In other words, plug in "ma" in "aff". The result is an affine expression
5903 * defined over the domain space of "ma".
5905 * If "aff" is represented by
5907 * (a(p) + b x + c(divs))/d
5909 * and ma is represented by
5911 * x = D(p) + F(y) + G(divs')
5913 * then the result is
5915 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5917 * The divs in the local space of the input are similarly adjusted
5918 * through a call to isl_local_space_preimage_multi_aff.
5920 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5921 __isl_take isl_multi_aff *ma)
5923 isl_aff *res = NULL;
5924 isl_local_space *ls;
5925 isl_size n_div_aff, n_div_ma;
5926 isl_int f, c1, c2, g;
5928 ma = isl_multi_aff_align_divs(ma);
5929 if (!aff || !ma)
5930 goto error;
5932 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5933 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5934 if (n_div_aff < 0 || n_div_ma < 0)
5935 goto error;
5937 ls = isl_aff_get_domain_local_space(aff);
5938 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5939 res = isl_aff_alloc(ls);
5940 if (!res)
5941 goto error;
5943 isl_int_init(f);
5944 isl_int_init(c1);
5945 isl_int_init(c2);
5946 isl_int_init(g);
5948 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5949 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5950 res = isl_aff_free(res);
5952 isl_int_clear(f);
5953 isl_int_clear(c1);
5954 isl_int_clear(c2);
5955 isl_int_clear(g);
5957 isl_aff_free(aff);
5958 isl_multi_aff_free(ma);
5959 res = isl_aff_normalize(res);
5960 return res;
5961 error:
5962 isl_aff_free(aff);
5963 isl_multi_aff_free(ma);
5964 isl_aff_free(res);
5965 return NULL;
5968 /* Compute the pullback of "aff1" by the function represented by "aff2".
5969 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5970 * defined over the domain space of "aff1".
5972 * The domain of "aff1" should match the range of "aff2", which means
5973 * that it should be single-dimensional.
5975 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5976 __isl_take isl_aff *aff2)
5978 isl_multi_aff *ma;
5980 ma = isl_multi_aff_from_aff(aff2);
5981 return isl_aff_pullback_multi_aff(aff1, ma);
5984 /* Compute the pullback of "ma1" by the function represented by "ma2".
5985 * In other words, plug in "ma2" in "ma1".
5987 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5988 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5990 int i;
5991 isl_space *space = NULL;
5993 isl_multi_aff_align_params_bin(&ma1, &ma2);
5994 ma2 = isl_multi_aff_align_divs(ma2);
5995 ma1 = isl_multi_aff_cow(ma1);
5996 if (!ma1 || !ma2)
5997 goto error;
5999 space = isl_space_join(isl_multi_aff_get_space(ma2),
6000 isl_multi_aff_get_space(ma1));
6002 for (i = 0; i < ma1->n; ++i) {
6003 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
6004 isl_multi_aff_copy(ma2));
6005 if (!ma1->u.p[i])
6006 goto error;
6009 ma1 = isl_multi_aff_reset_space(ma1, space);
6010 isl_multi_aff_free(ma2);
6011 return ma1;
6012 error:
6013 isl_space_free(space);
6014 isl_multi_aff_free(ma2);
6015 isl_multi_aff_free(ma1);
6016 return NULL;
6019 /* Extend the local space of "dst" to include the divs
6020 * in the local space of "src".
6022 * If "src" does not have any divs or if the local spaces of "dst" and
6023 * "src" are the same, then no extension is required.
6025 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6026 __isl_keep isl_aff *src)
6028 isl_ctx *ctx;
6029 isl_size src_n_div, dst_n_div;
6030 int *exp1 = NULL;
6031 int *exp2 = NULL;
6032 isl_bool equal;
6033 isl_mat *div;
6035 if (!src || !dst)
6036 return isl_aff_free(dst);
6038 ctx = isl_aff_get_ctx(src);
6039 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6040 if (equal < 0)
6041 return isl_aff_free(dst);
6042 if (!equal)
6043 isl_die(ctx, isl_error_invalid,
6044 "spaces don't match", goto error);
6046 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6047 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6048 if (src_n_div == 0)
6049 return dst;
6050 equal = isl_local_space_is_equal(src->ls, dst->ls);
6051 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6052 return isl_aff_free(dst);
6053 if (equal)
6054 return dst;
6056 exp1 = isl_alloc_array(ctx, int, src_n_div);
6057 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6058 if (!exp1 || (dst_n_div && !exp2))
6059 goto error;
6061 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6062 dst = isl_aff_expand_divs(dst, div, exp2);
6063 free(exp1);
6064 free(exp2);
6066 return dst;
6067 error:
6068 free(exp1);
6069 free(exp2);
6070 return isl_aff_free(dst);
6073 /* Adjust the local spaces of the affine expressions in "maff"
6074 * such that they all have the save divs.
6076 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6077 __isl_take isl_multi_aff *maff)
6079 int i;
6081 if (!maff)
6082 return NULL;
6083 if (maff->n == 0)
6084 return maff;
6085 maff = isl_multi_aff_cow(maff);
6086 if (!maff)
6087 return NULL;
6089 for (i = 1; i < maff->n; ++i)
6090 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
6091 for (i = 1; i < maff->n; ++i) {
6092 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
6093 if (!maff->u.p[i])
6094 return isl_multi_aff_free(maff);
6097 return maff;
6100 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6102 aff = isl_aff_cow(aff);
6103 if (!aff)
6104 return NULL;
6106 aff->ls = isl_local_space_lift(aff->ls);
6107 if (!aff->ls)
6108 return isl_aff_free(aff);
6110 return aff;
6113 /* Lift "maff" to a space with extra dimensions such that the result
6114 * has no more existentially quantified variables.
6115 * If "ls" is not NULL, then *ls is assigned the local space that lies
6116 * at the basis of the lifting applied to "maff".
6118 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6119 __isl_give isl_local_space **ls)
6121 int i;
6122 isl_space *space;
6123 isl_size n_div;
6125 if (ls)
6126 *ls = NULL;
6128 if (!maff)
6129 return NULL;
6131 if (maff->n == 0) {
6132 if (ls) {
6133 isl_space *space = isl_multi_aff_get_domain_space(maff);
6134 *ls = isl_local_space_from_space(space);
6135 if (!*ls)
6136 return isl_multi_aff_free(maff);
6138 return maff;
6141 maff = isl_multi_aff_cow(maff);
6142 maff = isl_multi_aff_align_divs(maff);
6143 if (!maff)
6144 return NULL;
6146 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
6147 if (n_div < 0)
6148 return isl_multi_aff_free(maff);
6149 space = isl_multi_aff_get_space(maff);
6150 space = isl_space_lift(isl_space_domain(space), n_div);
6151 space = isl_space_extend_domain_with_range(space,
6152 isl_multi_aff_get_space(maff));
6153 if (!space)
6154 return isl_multi_aff_free(maff);
6155 isl_space_free(maff->space);
6156 maff->space = space;
6158 if (ls) {
6159 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
6160 if (!*ls)
6161 return isl_multi_aff_free(maff);
6164 for (i = 0; i < maff->n; ++i) {
6165 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
6166 if (!maff->u.p[i])
6167 goto error;
6170 return maff;
6171 error:
6172 if (ls)
6173 isl_local_space_free(*ls);
6174 return isl_multi_aff_free(maff);
6177 #undef TYPE
6178 #define TYPE isl_pw_multi_aff
6179 static
6180 #include "check_type_range_templ.c"
6182 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6184 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6185 __isl_keep isl_pw_multi_aff *pma, int pos)
6187 int i;
6188 isl_size n_out;
6189 isl_space *space;
6190 isl_pw_aff *pa;
6192 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6193 return NULL;
6195 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6196 if (n_out < 0)
6197 return NULL;
6199 space = isl_pw_multi_aff_get_space(pma);
6200 space = isl_space_drop_dims(space, isl_dim_out,
6201 pos + 1, n_out - pos - 1);
6202 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6204 pa = isl_pw_aff_alloc_size(space, pma->n);
6205 for (i = 0; i < pma->n; ++i) {
6206 isl_aff *aff;
6207 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6208 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6211 return pa;
6214 /* This is an alternative name for the function above.
6216 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6217 __isl_keep isl_pw_multi_aff *pma, int pos)
6219 return isl_pw_multi_aff_get_at(pma, pos);
6222 /* Return an isl_pw_multi_aff with the given "set" as domain and
6223 * an unnamed zero-dimensional range.
6225 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6226 __isl_take isl_set *set)
6228 isl_multi_aff *ma;
6229 isl_space *space;
6231 space = isl_set_get_space(set);
6232 space = isl_space_from_domain(space);
6233 ma = isl_multi_aff_zero(space);
6234 return isl_pw_multi_aff_alloc(set, ma);
6237 /* Add an isl_pw_multi_aff with the given "set" as domain and
6238 * an unnamed zero-dimensional range to *user.
6240 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6241 void *user)
6243 isl_union_pw_multi_aff **upma = user;
6244 isl_pw_multi_aff *pma;
6246 pma = isl_pw_multi_aff_from_domain(set);
6247 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6249 return isl_stat_ok;
6252 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6253 * an unnamed zero-dimensional range.
6255 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6256 __isl_take isl_union_set *uset)
6258 isl_space *space;
6259 isl_union_pw_multi_aff *upma;
6261 if (!uset)
6262 return NULL;
6264 space = isl_union_set_get_space(uset);
6265 upma = isl_union_pw_multi_aff_empty(space);
6267 if (isl_union_set_foreach_set(uset,
6268 &add_pw_multi_aff_from_domain, &upma) < 0)
6269 goto error;
6271 isl_union_set_free(uset);
6272 return upma;
6273 error:
6274 isl_union_set_free(uset);
6275 isl_union_pw_multi_aff_free(upma);
6276 return NULL;
6279 /* Local data for bin_entry and the callback "fn".
6281 struct isl_union_pw_multi_aff_bin_data {
6282 isl_union_pw_multi_aff *upma2;
6283 isl_union_pw_multi_aff *res;
6284 isl_pw_multi_aff *pma;
6285 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6288 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6289 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6291 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6293 struct isl_union_pw_multi_aff_bin_data *data = user;
6294 isl_stat r;
6296 data->pma = pma;
6297 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6298 data->fn, data);
6299 isl_pw_multi_aff_free(pma);
6301 return r;
6304 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6305 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6306 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6307 * as *entry. The callback should adjust data->res if desired.
6309 static __isl_give isl_union_pw_multi_aff *bin_op(
6310 __isl_take isl_union_pw_multi_aff *upma1,
6311 __isl_take isl_union_pw_multi_aff *upma2,
6312 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6314 isl_space *space;
6315 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6317 space = isl_union_pw_multi_aff_get_space(upma2);
6318 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6319 space = isl_union_pw_multi_aff_get_space(upma1);
6320 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6322 if (!upma1 || !upma2)
6323 goto error;
6325 data.upma2 = upma2;
6326 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6327 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6328 &bin_entry, &data) < 0)
6329 goto error;
6331 isl_union_pw_multi_aff_free(upma1);
6332 isl_union_pw_multi_aff_free(upma2);
6333 return data.res;
6334 error:
6335 isl_union_pw_multi_aff_free(upma1);
6336 isl_union_pw_multi_aff_free(upma2);
6337 isl_union_pw_multi_aff_free(data.res);
6338 return NULL;
6341 /* Given two isl_pw_multi_affs A -> B and C -> D,
6342 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6344 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6345 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6347 isl_space *space;
6349 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6350 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6351 isl_pw_multi_aff_get_space(pma2));
6352 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6353 &isl_multi_aff_range_product);
6356 /* Given two isl_pw_multi_affs A -> B and C -> D,
6357 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6359 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6360 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6362 isl_space *space;
6364 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6365 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6366 isl_pw_multi_aff_get_space(pma2));
6367 space = isl_space_flatten_range(space);
6368 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6369 &isl_multi_aff_flat_range_product);
6372 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6373 * to compute some form of range product and add the result to data->res.
6375 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6376 __isl_give isl_pw_multi_aff *(*range_product)(
6377 __isl_take isl_pw_multi_aff *pma1,
6378 __isl_take isl_pw_multi_aff *pma2),
6379 void *user)
6381 struct isl_union_pw_multi_aff_bin_data *data = user;
6382 isl_bool match;
6383 isl_space *space1, *space2;
6385 space1 = isl_pw_multi_aff_peek_space(data->pma);
6386 space2 = isl_pw_multi_aff_peek_space(pma2);
6387 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6388 space2, isl_dim_in);
6389 if (match < 0 || !match) {
6390 isl_pw_multi_aff_free(pma2);
6391 return match < 0 ? isl_stat_error : isl_stat_ok;
6394 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6396 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6398 return isl_stat_ok;
6401 /* If data->pma and "pma2" have the same domain space, then compute
6402 * their flat range product and add the result to data->res.
6404 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6405 void *user)
6407 return gen_range_product_entry(pma2,
6408 &isl_pw_multi_aff_flat_range_product, user);
6411 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6412 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6414 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6415 __isl_take isl_union_pw_multi_aff *upma1,
6416 __isl_take isl_union_pw_multi_aff *upma2)
6418 return bin_op(upma1, upma2, &flat_range_product_entry);
6421 /* If data->pma and "pma2" have the same domain space, then compute
6422 * their range product and add the result to data->res.
6424 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6425 void *user)
6427 return gen_range_product_entry(pma2,
6428 &isl_pw_multi_aff_range_product, user);
6431 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6432 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6434 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6435 __isl_take isl_union_pw_multi_aff *upma1,
6436 __isl_take isl_union_pw_multi_aff *upma2)
6438 return bin_op(upma1, upma2, &range_product_entry);
6441 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6442 * The parameters are assumed to have been aligned.
6444 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6445 * except that it works on two different isl_pw_* types.
6447 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6448 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6449 __isl_take isl_pw_aff *pa)
6451 int i, j, n;
6452 isl_pw_multi_aff *res = NULL;
6454 if (!pma || !pa)
6455 goto error;
6457 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6458 pa->dim, isl_dim_in))
6459 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6460 "domains don't match", goto error);
6461 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6462 goto error;
6464 n = pma->n * pa->n;
6465 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6467 for (i = 0; i < pma->n; ++i) {
6468 for (j = 0; j < pa->n; ++j) {
6469 isl_set *common;
6470 isl_multi_aff *res_ij;
6471 int empty;
6473 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6474 isl_set_copy(pa->p[j].set));
6475 empty = isl_set_plain_is_empty(common);
6476 if (empty < 0 || empty) {
6477 isl_set_free(common);
6478 if (empty < 0)
6479 goto error;
6480 continue;
6483 res_ij = isl_multi_aff_set_aff(
6484 isl_multi_aff_copy(pma->p[i].maff), pos,
6485 isl_aff_copy(pa->p[j].aff));
6486 res_ij = isl_multi_aff_gist(res_ij,
6487 isl_set_copy(common));
6489 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6493 isl_pw_multi_aff_free(pma);
6494 isl_pw_aff_free(pa);
6495 return res;
6496 error:
6497 isl_pw_multi_aff_free(pma);
6498 isl_pw_aff_free(pa);
6499 return isl_pw_multi_aff_free(res);
6502 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6504 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6505 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6506 __isl_take isl_pw_aff *pa)
6508 isl_bool equal_params;
6510 if (!pma || !pa)
6511 goto error;
6512 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6513 if (equal_params < 0)
6514 goto error;
6515 if (equal_params)
6516 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6517 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6518 isl_pw_aff_check_named_params(pa) < 0)
6519 goto error;
6520 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6521 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6522 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6523 error:
6524 isl_pw_multi_aff_free(pma);
6525 isl_pw_aff_free(pa);
6526 return NULL;
6529 /* Do the parameters of "pa" match those of "space"?
6531 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6532 __isl_keep isl_space *space)
6534 isl_space *pa_space;
6535 isl_bool match;
6537 if (!pa || !space)
6538 return isl_bool_error;
6540 pa_space = isl_pw_aff_get_space(pa);
6542 match = isl_space_has_equal_params(space, pa_space);
6544 isl_space_free(pa_space);
6545 return match;
6548 /* Check that the domain space of "pa" matches "space".
6550 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6551 __isl_keep isl_space *space)
6553 isl_space *pa_space;
6554 isl_bool match;
6556 if (!pa || !space)
6557 return isl_stat_error;
6559 pa_space = isl_pw_aff_get_space(pa);
6561 match = isl_space_has_equal_params(space, pa_space);
6562 if (match < 0)
6563 goto error;
6564 if (!match)
6565 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6566 "parameters don't match", goto error);
6567 match = isl_space_tuple_is_equal(space, isl_dim_in,
6568 pa_space, isl_dim_in);
6569 if (match < 0)
6570 goto error;
6571 if (!match)
6572 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6573 "domains don't match", goto error);
6574 isl_space_free(pa_space);
6575 return isl_stat_ok;
6576 error:
6577 isl_space_free(pa_space);
6578 return isl_stat_error;
6581 #undef BASE
6582 #define BASE pw_aff
6583 #undef DOMBASE
6584 #define DOMBASE set
6586 #include <isl_multi_explicit_domain.c>
6587 #include <isl_multi_pw_aff_explicit_domain.c>
6588 #include <isl_multi_templ.c>
6589 #include <isl_multi_add_constant_templ.c>
6590 #include <isl_multi_apply_set.c>
6591 #include <isl_multi_arith_templ.c>
6592 #include <isl_multi_bind_templ.c>
6593 #include <isl_multi_bind_domain_templ.c>
6594 #include <isl_multi_coalesce.c>
6595 #include <isl_multi_domain_templ.c>
6596 #include <isl_multi_dim_id_templ.c>
6597 #include <isl_multi_dims.c>
6598 #include <isl_multi_from_base_templ.c>
6599 #include <isl_multi_gist.c>
6600 #include <isl_multi_hash.c>
6601 #include <isl_multi_identity_templ.c>
6602 #include <isl_multi_align_set.c>
6603 #include <isl_multi_insert_domain_templ.c>
6604 #include <isl_multi_intersect.c>
6605 #include <isl_multi_min_max_templ.c>
6606 #include <isl_multi_move_dims_templ.c>
6607 #include <isl_multi_nan_templ.c>
6608 #include <isl_multi_param_templ.c>
6609 #include <isl_multi_product_templ.c>
6610 #include <isl_multi_splice_templ.c>
6611 #include <isl_multi_tuple_id_templ.c>
6612 #include <isl_multi_union_add_templ.c>
6613 #include <isl_multi_zero_templ.c>
6614 #include <isl_multi_unbind_params_templ.c>
6616 /* Is every element of "mpa" defined over a single universe domain?
6618 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6620 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6623 /* Given that every element of "mpa" is defined over a single universe domain,
6624 * return the corresponding base expressions.
6626 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6627 __isl_take isl_multi_pw_aff *mpa)
6629 int i;
6630 isl_size n;
6631 isl_multi_aff *ma;
6633 n = isl_multi_pw_aff_size(mpa);
6634 if (n < 0)
6635 mpa = isl_multi_pw_aff_free(mpa);
6636 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6637 for (i = 0; i < n; ++i) {
6638 isl_aff *aff;
6640 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6641 ma = isl_multi_aff_set_aff(ma, i, aff);
6643 isl_multi_pw_aff_free(mpa);
6644 return ma;
6647 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6648 * with this explicit domain.
6650 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6651 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6653 isl_set *dom;
6655 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6656 return map;
6658 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6659 map = isl_map_intersect_domain(map, dom);
6661 return map;
6664 /* Are all elements of "mpa" piecewise constants?
6666 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6668 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6671 /* Does "mpa" have a non-trivial explicit domain?
6673 * The explicit domain, if present, is trivial if it represents
6674 * an (obviously) universe set.
6676 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6677 __isl_keep isl_multi_pw_aff *mpa)
6679 if (!mpa)
6680 return isl_bool_error;
6681 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6682 return isl_bool_false;
6683 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6686 #undef BASE
6687 #define BASE set
6689 #include "isl_opt_mpa_templ.c"
6691 /* Compute the minima of the set dimensions as a function of the
6692 * parameters, but independently of the other set dimensions.
6694 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6696 return set_opt_mpa(set, &isl_set_dim_min);
6699 /* Compute the maxima of the set dimensions as a function of the
6700 * parameters, but independently of the other set dimensions.
6702 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6704 return set_opt_mpa(set, &isl_set_dim_max);
6707 #undef BASE
6708 #define BASE map
6710 #include "isl_opt_mpa_templ.c"
6712 /* Compute the minima of the output dimensions as a function of the
6713 * parameters and input dimensions, but independently of
6714 * the other output dimensions.
6716 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6718 return map_opt_mpa(map, &isl_map_dim_min);
6721 /* Compute the maxima of the output dimensions as a function of the
6722 * parameters and input dimensions, but independently of
6723 * the other output dimensions.
6725 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6727 return map_opt_mpa(map, &isl_map_dim_max);
6730 /* Scale the elements of "pma" by the corresponding elements of "mv".
6732 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6733 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6735 int i;
6736 isl_bool equal_params;
6738 pma = isl_pw_multi_aff_cow(pma);
6739 if (!pma || !mv)
6740 goto error;
6741 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6742 mv->space, isl_dim_set))
6743 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6744 "spaces don't match", goto error);
6745 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6746 if (equal_params < 0)
6747 goto error;
6748 if (!equal_params) {
6749 pma = isl_pw_multi_aff_align_params(pma,
6750 isl_multi_val_get_space(mv));
6751 mv = isl_multi_val_align_params(mv,
6752 isl_pw_multi_aff_get_space(pma));
6753 if (!pma || !mv)
6754 goto error;
6757 for (i = 0; i < pma->n; ++i) {
6758 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6759 isl_multi_val_copy(mv));
6760 if (!pma->p[i].maff)
6761 goto error;
6764 isl_multi_val_free(mv);
6765 return pma;
6766 error:
6767 isl_multi_val_free(mv);
6768 isl_pw_multi_aff_free(pma);
6769 return NULL;
6772 /* This function is called for each entry of an isl_union_pw_multi_aff.
6773 * If the space of the entry matches that of data->mv,
6774 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6775 * Otherwise, return an empty isl_pw_multi_aff.
6777 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6778 __isl_take isl_pw_multi_aff *pma, void *user)
6780 isl_multi_val *mv = user;
6782 if (!pma)
6783 return NULL;
6784 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6785 mv->space, isl_dim_set)) {
6786 isl_space *space = isl_pw_multi_aff_get_space(pma);
6787 isl_pw_multi_aff_free(pma);
6788 return isl_pw_multi_aff_empty(space);
6791 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6794 /* Scale the elements of "upma" by the corresponding elements of "mv",
6795 * for those entries that match the space of "mv".
6797 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6798 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6800 struct isl_union_pw_multi_aff_transform_control control = {
6801 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6802 .fn_user = mv,
6805 upma = isl_union_pw_multi_aff_align_params(upma,
6806 isl_multi_val_get_space(mv));
6807 mv = isl_multi_val_align_params(mv,
6808 isl_union_pw_multi_aff_get_space(upma));
6809 if (!upma || !mv)
6810 goto error;
6812 return isl_union_pw_multi_aff_transform(upma, &control);
6814 isl_multi_val_free(mv);
6815 return upma;
6816 error:
6817 isl_multi_val_free(mv);
6818 isl_union_pw_multi_aff_free(upma);
6819 return NULL;
6822 /* Construct and return a piecewise multi affine expression
6823 * in the given space with value zero in each of the output dimensions and
6824 * a universe domain.
6826 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6828 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6831 /* Construct and return a piecewise multi affine expression
6832 * that is equal to the given piecewise affine expression.
6834 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6835 __isl_take isl_pw_aff *pa)
6837 int i;
6838 isl_space *space;
6839 isl_pw_multi_aff *pma;
6841 if (!pa)
6842 return NULL;
6844 space = isl_pw_aff_get_space(pa);
6845 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6847 for (i = 0; i < pa->n; ++i) {
6848 isl_set *set;
6849 isl_multi_aff *ma;
6851 set = isl_set_copy(pa->p[i].set);
6852 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6853 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6856 isl_pw_aff_free(pa);
6857 return pma;
6860 /* Construct and return a piecewise multi affine expression
6861 * that is equal to the given multi piecewise affine expression
6862 * on the shared domain of the piecewise affine expressions,
6863 * in the special case of a 0D multi piecewise affine expression.
6865 * Create a piecewise multi affine expression with the explicit domain of
6866 * the 0D multi piecewise affine expression as domain.
6868 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6869 __isl_take isl_multi_pw_aff *mpa)
6871 isl_space *space;
6872 isl_set *dom;
6873 isl_multi_aff *ma;
6875 space = isl_multi_pw_aff_get_space(mpa);
6876 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6877 isl_multi_pw_aff_free(mpa);
6879 ma = isl_multi_aff_zero(space);
6880 return isl_pw_multi_aff_alloc(dom, ma);
6883 /* Construct and return a piecewise multi affine expression
6884 * that is equal to the given multi piecewise affine expression
6885 * on the shared domain of the piecewise affine expressions.
6887 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6888 __isl_take isl_multi_pw_aff *mpa)
6890 int i;
6891 isl_space *space;
6892 isl_pw_aff *pa;
6893 isl_pw_multi_aff *pma;
6895 if (!mpa)
6896 return NULL;
6898 if (mpa->n == 0)
6899 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6901 space = isl_multi_pw_aff_get_space(mpa);
6902 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6903 pma = isl_pw_multi_aff_from_pw_aff(pa);
6905 for (i = 1; i < mpa->n; ++i) {
6906 isl_pw_multi_aff *pma_i;
6908 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6909 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6910 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6913 pma = isl_pw_multi_aff_reset_space(pma, space);
6915 isl_multi_pw_aff_free(mpa);
6916 return pma;
6919 /* Convenience function that constructs an isl_multi_pw_aff
6920 * directly from an isl_aff.
6922 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6924 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6927 /* Construct and return a multi piecewise affine expression
6928 * that is equal to the given multi affine expression.
6930 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6931 __isl_take isl_multi_aff *ma)
6933 int i;
6934 isl_size n;
6935 isl_multi_pw_aff *mpa;
6937 n = isl_multi_aff_dim(ma, isl_dim_out);
6938 if (n < 0)
6939 ma = isl_multi_aff_free(ma);
6940 if (!ma)
6941 return NULL;
6943 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6945 for (i = 0; i < n; ++i) {
6946 isl_pw_aff *pa;
6948 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6949 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6952 isl_multi_aff_free(ma);
6953 return mpa;
6956 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6957 * but is considered as a function on an isl_multi_aff when exported.
6959 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
6960 __isl_take isl_multi_aff *ma)
6962 return isl_multi_pw_aff_from_multi_aff(ma);
6965 /* Construct and return a multi piecewise affine expression
6966 * that is equal to the given piecewise multi affine expression.
6968 * If the resulting multi piecewise affine expression has
6969 * an explicit domain, then assign it the domain of the input.
6970 * In other cases, the domain is stored in the individual elements.
6972 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6973 __isl_take isl_pw_multi_aff *pma)
6975 int i;
6976 isl_size n;
6977 isl_space *space;
6978 isl_multi_pw_aff *mpa;
6980 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6981 if (n < 0)
6982 pma = isl_pw_multi_aff_free(pma);
6983 space = isl_pw_multi_aff_get_space(pma);
6984 mpa = isl_multi_pw_aff_alloc(space);
6986 for (i = 0; i < n; ++i) {
6987 isl_pw_aff *pa;
6989 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6990 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6992 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6993 isl_set *dom;
6995 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6996 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6999 isl_pw_multi_aff_free(pma);
7000 return mpa;
7003 /* This function performs the same operation as
7004 * isl_multi_pw_aff_from_pw_multi_aff,
7005 * but is considered as a function on an isl_pw_multi_aff when exported.
7007 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
7008 __isl_take isl_pw_multi_aff *pma)
7010 return isl_multi_pw_aff_from_pw_multi_aff(pma);
7013 /* Do "pa1" and "pa2" represent the same function?
7015 * We first check if they are obviously equal.
7016 * If not, we convert them to maps and check if those are equal.
7018 * If "pa1" or "pa2" contain any NaNs, then they are considered
7019 * not to be the same. A NaN is not equal to anything, not even
7020 * to another NaN.
7022 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7023 __isl_keep isl_pw_aff *pa2)
7025 isl_bool equal;
7026 isl_bool has_nan;
7027 isl_map *map1, *map2;
7029 if (!pa1 || !pa2)
7030 return isl_bool_error;
7032 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7033 if (equal < 0 || equal)
7034 return equal;
7035 has_nan = either_involves_nan(pa1, pa2);
7036 if (has_nan < 0)
7037 return isl_bool_error;
7038 if (has_nan)
7039 return isl_bool_false;
7041 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7042 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7043 equal = isl_map_is_equal(map1, map2);
7044 isl_map_free(map1);
7045 isl_map_free(map2);
7047 return equal;
7050 /* Do "mpa1" and "mpa2" represent the same function?
7052 * Note that we cannot convert the entire isl_multi_pw_aff
7053 * to a map because the domains of the piecewise affine expressions
7054 * may not be the same.
7056 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7057 __isl_keep isl_multi_pw_aff *mpa2)
7059 int i;
7060 isl_bool equal, equal_params;
7062 if (!mpa1 || !mpa2)
7063 return isl_bool_error;
7065 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7066 if (equal_params < 0)
7067 return isl_bool_error;
7068 if (!equal_params) {
7069 if (!isl_space_has_named_params(mpa1->space))
7070 return isl_bool_false;
7071 if (!isl_space_has_named_params(mpa2->space))
7072 return isl_bool_false;
7073 mpa1 = isl_multi_pw_aff_copy(mpa1);
7074 mpa2 = isl_multi_pw_aff_copy(mpa2);
7075 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7076 isl_multi_pw_aff_get_space(mpa2));
7077 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7078 isl_multi_pw_aff_get_space(mpa1));
7079 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7080 isl_multi_pw_aff_free(mpa1);
7081 isl_multi_pw_aff_free(mpa2);
7082 return equal;
7085 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7086 if (equal < 0 || !equal)
7087 return equal;
7089 for (i = 0; i < mpa1->n; ++i) {
7090 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7091 if (equal < 0 || !equal)
7092 return equal;
7095 return isl_bool_true;
7098 /* Do "pma1" and "pma2" represent the same function?
7100 * First check if they are obviously equal.
7101 * If not, then convert them to maps and check if those are equal.
7103 * If "pa1" or "pa2" contain any NaNs, then they are considered
7104 * not to be the same. A NaN is not equal to anything, not even
7105 * to another NaN.
7107 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7108 __isl_keep isl_pw_multi_aff *pma2)
7110 isl_bool equal;
7111 isl_bool has_nan;
7112 isl_map *map1, *map2;
7114 if (!pma1 || !pma2)
7115 return isl_bool_error;
7117 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7118 if (equal < 0 || equal)
7119 return equal;
7120 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7121 if (has_nan >= 0 && !has_nan)
7122 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7123 if (has_nan < 0 || has_nan)
7124 return isl_bool_not(has_nan);
7126 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7127 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7128 equal = isl_map_is_equal(map1, map2);
7129 isl_map_free(map1);
7130 isl_map_free(map2);
7132 return equal;
7135 /* Compute the pullback of "mpa" by the function represented by "ma".
7136 * In other words, plug in "ma" in "mpa".
7138 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7140 * If "mpa" has an explicit domain, then it is this domain
7141 * that needs to undergo a pullback, i.e., a preimage.
7143 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
7144 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7146 int i;
7147 isl_space *space = NULL;
7149 mpa = isl_multi_pw_aff_cow(mpa);
7150 if (!mpa || !ma)
7151 goto error;
7153 space = isl_space_join(isl_multi_aff_get_space(ma),
7154 isl_multi_pw_aff_get_space(mpa));
7155 if (!space)
7156 goto error;
7158 for (i = 0; i < mpa->n; ++i) {
7159 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
7160 isl_multi_aff_copy(ma));
7161 if (!mpa->u.p[i])
7162 goto error;
7164 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7165 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
7166 isl_multi_aff_copy(ma));
7167 if (!mpa->u.dom)
7168 goto error;
7171 isl_multi_aff_free(ma);
7172 isl_space_free(mpa->space);
7173 mpa->space = space;
7174 return mpa;
7175 error:
7176 isl_space_free(space);
7177 isl_multi_pw_aff_free(mpa);
7178 isl_multi_aff_free(ma);
7179 return NULL;
7182 /* Compute the pullback of "mpa" by the function represented by "ma".
7183 * In other words, plug in "ma" in "mpa".
7185 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
7186 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7188 isl_bool equal_params;
7190 if (!mpa || !ma)
7191 goto error;
7192 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
7193 if (equal_params < 0)
7194 goto error;
7195 if (equal_params)
7196 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7197 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
7198 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
7199 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7200 error:
7201 isl_multi_pw_aff_free(mpa);
7202 isl_multi_aff_free(ma);
7203 return NULL;
7206 /* Compute the pullback of "mpa" by the function represented by "pma".
7207 * In other words, plug in "pma" in "mpa".
7209 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7211 * If "mpa" has an explicit domain, then it is this domain
7212 * that needs to undergo a pullback, i.e., a preimage.
7214 static __isl_give isl_multi_pw_aff *
7215 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7216 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7218 int i;
7219 isl_space *space = NULL;
7221 mpa = isl_multi_pw_aff_cow(mpa);
7222 if (!mpa || !pma)
7223 goto error;
7225 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
7226 isl_multi_pw_aff_get_space(mpa));
7228 for (i = 0; i < mpa->n; ++i) {
7229 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7230 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
7231 if (!mpa->u.p[i])
7232 goto error;
7234 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7235 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
7236 isl_pw_multi_aff_copy(pma));
7237 if (!mpa->u.dom)
7238 goto error;
7241 isl_pw_multi_aff_free(pma);
7242 isl_space_free(mpa->space);
7243 mpa->space = space;
7244 return mpa;
7245 error:
7246 isl_space_free(space);
7247 isl_multi_pw_aff_free(mpa);
7248 isl_pw_multi_aff_free(pma);
7249 return NULL;
7252 /* Compute the pullback of "mpa" by the function represented by "pma".
7253 * In other words, plug in "pma" in "mpa".
7255 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
7256 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7258 isl_bool equal_params;
7260 if (!mpa || !pma)
7261 goto error;
7262 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
7263 if (equal_params < 0)
7264 goto error;
7265 if (equal_params)
7266 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7267 mpa = isl_multi_pw_aff_align_params(mpa,
7268 isl_pw_multi_aff_get_space(pma));
7269 pma = isl_pw_multi_aff_align_params(pma,
7270 isl_multi_pw_aff_get_space(mpa));
7271 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7272 error:
7273 isl_multi_pw_aff_free(mpa);
7274 isl_pw_multi_aff_free(pma);
7275 return NULL;
7278 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7279 * with the domain of "aff". The domain of the result is the same
7280 * as that of "mpa".
7281 * "mpa" and "aff" are assumed to have been aligned.
7283 * We first extract the parametric constant from "aff", defined
7284 * over the correct domain.
7285 * Then we add the appropriate combinations of the members of "mpa".
7286 * Finally, we add the integer divisions through recursive calls.
7288 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7289 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7291 int i;
7292 isl_size n_in, n_div, n_mpa_in;
7293 isl_space *space;
7294 isl_val *v;
7295 isl_pw_aff *pa;
7296 isl_aff *tmp;
7298 n_in = isl_aff_dim(aff, isl_dim_in);
7299 n_div = isl_aff_dim(aff, isl_dim_div);
7300 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7301 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7302 goto error;
7304 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7305 tmp = isl_aff_copy(aff);
7306 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7307 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7308 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7309 tmp = isl_aff_reset_domain_space(tmp, space);
7310 pa = isl_pw_aff_from_aff(tmp);
7312 for (i = 0; i < n_in; ++i) {
7313 isl_pw_aff *pa_i;
7315 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7316 continue;
7317 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7318 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7319 pa_i = isl_pw_aff_scale_val(pa_i, v);
7320 pa = isl_pw_aff_add(pa, pa_i);
7323 for (i = 0; i < n_div; ++i) {
7324 isl_aff *div;
7325 isl_pw_aff *pa_i;
7327 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7328 continue;
7329 div = isl_aff_get_div(aff, i);
7330 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7331 isl_multi_pw_aff_copy(mpa), div);
7332 pa_i = isl_pw_aff_floor(pa_i);
7333 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7334 pa_i = isl_pw_aff_scale_val(pa_i, v);
7335 pa = isl_pw_aff_add(pa, pa_i);
7338 isl_multi_pw_aff_free(mpa);
7339 isl_aff_free(aff);
7341 return pa;
7342 error:
7343 isl_multi_pw_aff_free(mpa);
7344 isl_aff_free(aff);
7345 return NULL;
7348 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7349 * with the domain of "aff". The domain of the result is the same
7350 * as that of "mpa".
7352 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7353 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7355 isl_bool equal_params;
7357 if (!aff || !mpa)
7358 goto error;
7359 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7360 if (equal_params < 0)
7361 goto error;
7362 if (equal_params)
7363 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7365 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7366 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7368 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7369 error:
7370 isl_aff_free(aff);
7371 isl_multi_pw_aff_free(mpa);
7372 return NULL;
7375 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7376 * with the domain of "pa". The domain of the result is the same
7377 * as that of "mpa".
7378 * "mpa" and "pa" are assumed to have been aligned.
7380 * We consider each piece in turn. Note that the domains of the
7381 * pieces are assumed to be disjoint and they remain disjoint
7382 * after taking the preimage (over the same function).
7384 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7385 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7387 isl_space *space;
7388 isl_pw_aff *res;
7389 int i;
7391 if (!mpa || !pa)
7392 goto error;
7394 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7395 isl_pw_aff_get_space(pa));
7396 res = isl_pw_aff_empty(space);
7398 for (i = 0; i < pa->n; ++i) {
7399 isl_pw_aff *pa_i;
7400 isl_set *domain;
7402 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7403 isl_multi_pw_aff_copy(mpa),
7404 isl_aff_copy(pa->p[i].aff));
7405 domain = isl_set_copy(pa->p[i].set);
7406 domain = isl_set_preimage_multi_pw_aff(domain,
7407 isl_multi_pw_aff_copy(mpa));
7408 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7409 res = isl_pw_aff_add_disjoint(res, pa_i);
7412 isl_pw_aff_free(pa);
7413 isl_multi_pw_aff_free(mpa);
7414 return res;
7415 error:
7416 isl_pw_aff_free(pa);
7417 isl_multi_pw_aff_free(mpa);
7418 return NULL;
7421 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7422 * with the domain of "pa". The domain of the result is the same
7423 * as that of "mpa".
7425 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7426 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7428 isl_bool equal_params;
7430 if (!pa || !mpa)
7431 goto error;
7432 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7433 if (equal_params < 0)
7434 goto error;
7435 if (equal_params)
7436 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7438 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7439 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7441 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7442 error:
7443 isl_pw_aff_free(pa);
7444 isl_multi_pw_aff_free(mpa);
7445 return NULL;
7448 /* Compute the pullback of "pa" by the function represented by "mpa".
7449 * In other words, plug in "mpa" in "pa".
7450 * "pa" and "mpa" are assumed to have been aligned.
7452 * The pullback is computed by applying "pa" to "mpa".
7454 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7455 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7457 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7460 /* Compute the pullback of "pa" by the function represented by "mpa".
7461 * In other words, plug in "mpa" in "pa".
7463 * The pullback is computed by applying "pa" to "mpa".
7465 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7466 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7468 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7471 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7472 * In other words, plug in "mpa2" in "mpa1".
7474 * We pullback each member of "mpa1" in turn.
7476 * If "mpa1" has an explicit domain, then it is this domain
7477 * that needs to undergo a pullback instead, i.e., a preimage.
7479 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7480 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7482 int i;
7483 isl_space *space = NULL;
7485 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7486 mpa1 = isl_multi_pw_aff_cow(mpa1);
7487 if (!mpa1 || !mpa2)
7488 goto error;
7490 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7491 isl_multi_pw_aff_get_space(mpa1));
7493 for (i = 0; i < mpa1->n; ++i) {
7494 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7495 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7496 if (!mpa1->u.p[i])
7497 goto error;
7500 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7501 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7502 isl_multi_pw_aff_copy(mpa2));
7503 if (!mpa1->u.dom)
7504 goto error;
7506 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7508 isl_multi_pw_aff_free(mpa2);
7509 return mpa1;
7510 error:
7511 isl_space_free(space);
7512 isl_multi_pw_aff_free(mpa1);
7513 isl_multi_pw_aff_free(mpa2);
7514 return NULL;
7517 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7518 * of "mpa1" and "mpa2" live in the same space, construct map space
7519 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7520 * with this map space as extract argument.
7522 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7523 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7524 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7525 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7527 int match;
7528 isl_space *space1, *space2;
7529 isl_map *res;
7531 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7532 isl_multi_pw_aff_get_space(mpa2));
7533 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7534 isl_multi_pw_aff_get_space(mpa1));
7535 if (!mpa1 || !mpa2)
7536 goto error;
7537 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7538 mpa2->space, isl_dim_out);
7539 if (match < 0)
7540 goto error;
7541 if (!match)
7542 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7543 "range spaces don't match", goto error);
7544 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7545 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7546 space1 = isl_space_map_from_domain_and_range(space1, space2);
7548 res = order(mpa1, mpa2, space1);
7549 isl_multi_pw_aff_free(mpa1);
7550 isl_multi_pw_aff_free(mpa2);
7551 return res;
7552 error:
7553 isl_multi_pw_aff_free(mpa1);
7554 isl_multi_pw_aff_free(mpa2);
7555 return NULL;
7558 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7559 * where the function values are equal. "space" is the space of the result.
7560 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7562 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7563 * in the sequences are equal.
7565 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7566 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7567 __isl_take isl_space *space)
7569 int i;
7570 isl_size n;
7571 isl_map *res;
7573 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7574 if (n < 0)
7575 space = isl_space_free(space);
7576 res = isl_map_universe(space);
7578 for (i = 0; i < n; ++i) {
7579 isl_pw_aff *pa1, *pa2;
7580 isl_map *map;
7582 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7583 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7584 map = isl_pw_aff_eq_map(pa1, pa2);
7585 res = isl_map_intersect(res, map);
7588 return res;
7591 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7592 * where the function values are equal.
7594 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7595 __isl_take isl_multi_pw_aff *mpa2)
7597 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7598 &isl_multi_pw_aff_eq_map_on_space);
7601 /* Intersect "map" with the result of applying "order"
7602 * on two copies of "mpa".
7604 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7605 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7606 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7607 __isl_take isl_multi_pw_aff *mpa2))
7609 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7612 /* Return the subset of "map" where the domain and the range
7613 * have equal "mpa" values.
7615 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7616 __isl_take isl_multi_pw_aff *mpa)
7618 return isl_map_order_at_multi_pw_aff(map, mpa,
7619 &isl_multi_pw_aff_eq_map);
7622 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7623 * where the function values of "mpa1" lexicographically satisfies
7624 * "strict_base"/"base" compared to that of "mpa2".
7625 * "space" is the space of the result.
7626 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7628 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7629 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7630 * when compared to the i-th element of "mpa2" while all previous elements are
7631 * pairwise equal.
7632 * In particular, if i corresponds to the final elements
7633 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7634 * for other values of i.
7635 * If "base" is a strict order, then "base" and "strict_base" are the same.
7637 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7638 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7639 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7640 __isl_take isl_pw_aff *pa2),
7641 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7642 __isl_take isl_pw_aff *pa2),
7643 __isl_take isl_space *space)
7645 int i;
7646 isl_size n;
7647 isl_map *res, *rest;
7649 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7650 if (n < 0)
7651 space = isl_space_free(space);
7652 res = isl_map_empty(isl_space_copy(space));
7653 rest = isl_map_universe(space);
7655 for (i = 0; i < n; ++i) {
7656 int last;
7657 isl_pw_aff *pa1, *pa2;
7658 isl_map *map;
7660 last = i == n - 1;
7662 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7663 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7664 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7665 map = isl_map_intersect(map, isl_map_copy(rest));
7666 res = isl_map_union(res, map);
7668 if (last)
7669 continue;
7671 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7672 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7673 map = isl_pw_aff_eq_map(pa1, pa2);
7674 rest = isl_map_intersect(rest, map);
7677 isl_map_free(rest);
7678 return res;
7681 #undef ORDER
7682 #define ORDER le
7683 #undef STRICT_ORDER
7684 #define STRICT_ORDER lt
7685 #include "isl_aff_lex_templ.c"
7687 #undef ORDER
7688 #define ORDER lt
7689 #undef STRICT_ORDER
7690 #define STRICT_ORDER lt
7691 #include "isl_aff_lex_templ.c"
7693 #undef ORDER
7694 #define ORDER ge
7695 #undef STRICT_ORDER
7696 #define STRICT_ORDER gt
7697 #include "isl_aff_lex_templ.c"
7699 #undef ORDER
7700 #define ORDER gt
7701 #undef STRICT_ORDER
7702 #define STRICT_ORDER gt
7703 #include "isl_aff_lex_templ.c"
7705 /* Compare two isl_affs.
7707 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7708 * than "aff2" and 0 if they are equal.
7710 * The order is fairly arbitrary. We do consider expressions that only involve
7711 * earlier dimensions as "smaller".
7713 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7715 int cmp;
7716 int last1, last2;
7718 if (aff1 == aff2)
7719 return 0;
7721 if (!aff1)
7722 return -1;
7723 if (!aff2)
7724 return 1;
7726 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7727 if (cmp != 0)
7728 return cmp;
7730 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7731 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7732 if (last1 != last2)
7733 return last1 - last2;
7735 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7738 /* Compare two isl_pw_affs.
7740 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7741 * than "pa2" and 0 if they are equal.
7743 * The order is fairly arbitrary. We do consider expressions that only involve
7744 * earlier dimensions as "smaller".
7746 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7747 __isl_keep isl_pw_aff *pa2)
7749 int i;
7750 int cmp;
7752 if (pa1 == pa2)
7753 return 0;
7755 if (!pa1)
7756 return -1;
7757 if (!pa2)
7758 return 1;
7760 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7761 if (cmp != 0)
7762 return cmp;
7764 if (pa1->n != pa2->n)
7765 return pa1->n - pa2->n;
7767 for (i = 0; i < pa1->n; ++i) {
7768 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7769 if (cmp != 0)
7770 return cmp;
7771 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7772 if (cmp != 0)
7773 return cmp;
7776 return 0;
7779 /* Return a piecewise affine expression that is equal to "v" on "domain".
7781 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7782 __isl_take isl_val *v)
7784 isl_space *space;
7785 isl_local_space *ls;
7786 isl_aff *aff;
7788 space = isl_set_get_space(domain);
7789 ls = isl_local_space_from_space(space);
7790 aff = isl_aff_val_on_domain(ls, v);
7792 return isl_pw_aff_alloc(domain, aff);
7795 /* Return a piecewise affine expression that is equal to the parameter
7796 * with identifier "id" on "domain".
7798 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7799 __isl_take isl_set *domain, __isl_take isl_id *id)
7801 isl_space *space;
7802 isl_aff *aff;
7804 space = isl_set_get_space(domain);
7805 space = isl_space_add_param_id(space, isl_id_copy(id));
7806 domain = isl_set_align_params(domain, isl_space_copy(space));
7807 aff = isl_aff_param_on_domain_space_id(space, id);
7809 return isl_pw_aff_alloc(domain, aff);
7812 /* Return a multi affine expression that is equal to "mv" on domain
7813 * space "space".
7815 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7816 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7818 int i;
7819 isl_size n;
7820 isl_space *space2;
7821 isl_local_space *ls;
7822 isl_multi_aff *ma;
7824 n = isl_multi_val_dim(mv, isl_dim_set);
7825 if (!space || n < 0)
7826 goto error;
7828 space2 = isl_multi_val_get_space(mv);
7829 space2 = isl_space_align_params(space2, isl_space_copy(space));
7830 space = isl_space_align_params(space, isl_space_copy(space2));
7831 space = isl_space_map_from_domain_and_range(space, space2);
7832 ma = isl_multi_aff_alloc(isl_space_copy(space));
7833 ls = isl_local_space_from_space(isl_space_domain(space));
7834 for (i = 0; i < n; ++i) {
7835 isl_val *v;
7836 isl_aff *aff;
7838 v = isl_multi_val_get_val(mv, i);
7839 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7840 ma = isl_multi_aff_set_aff(ma, i, aff);
7842 isl_local_space_free(ls);
7844 isl_multi_val_free(mv);
7845 return ma;
7846 error:
7847 isl_space_free(space);
7848 isl_multi_val_free(mv);
7849 return NULL;
7852 /* This is an alternative name for the function above.
7854 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7855 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7857 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7860 /* This function performs the same operation as
7861 * isl_multi_aff_multi_val_on_domain_space,
7862 * but is considered as a function on an isl_space when exported.
7864 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7865 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7867 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7870 /* Return a piecewise multi-affine expression
7871 * that is equal to "mv" on "domain".
7873 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7874 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7876 isl_space *space;
7877 isl_multi_aff *ma;
7879 space = isl_set_get_space(domain);
7880 ma = isl_multi_aff_multi_val_on_space(space, mv);
7882 return isl_pw_multi_aff_alloc(domain, ma);
7885 /* This function performs the same operation as
7886 * isl_pw_multi_aff_multi_val_on_domain,
7887 * but is considered as a function on an isl_set when exported.
7889 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7890 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7892 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7895 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7896 * mv is the value that should be attained on each domain set
7897 * res collects the results
7899 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7900 isl_multi_val *mv;
7901 isl_union_pw_multi_aff *res;
7904 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7905 * and add it to data->res.
7907 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7908 void *user)
7910 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7911 isl_pw_multi_aff *pma;
7912 isl_multi_val *mv;
7914 mv = isl_multi_val_copy(data->mv);
7915 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7916 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7918 return data->res ? isl_stat_ok : isl_stat_error;
7921 /* Return a union piecewise multi-affine expression
7922 * that is equal to "mv" on "domain".
7924 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7925 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7927 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7928 isl_space *space;
7930 space = isl_union_set_get_space(domain);
7931 data.res = isl_union_pw_multi_aff_empty(space);
7932 data.mv = mv;
7933 if (isl_union_set_foreach_set(domain,
7934 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7935 data.res = isl_union_pw_multi_aff_free(data.res);
7936 isl_union_set_free(domain);
7937 isl_multi_val_free(mv);
7938 return data.res;
7941 /* Compute the pullback of data->pma by the function represented by "pma2",
7942 * provided the spaces match, and add the results to data->res.
7944 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7946 struct isl_union_pw_multi_aff_bin_data *data = user;
7948 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7949 pma2->dim, isl_dim_out)) {
7950 isl_pw_multi_aff_free(pma2);
7951 return isl_stat_ok;
7954 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7955 isl_pw_multi_aff_copy(data->pma), pma2);
7957 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7958 if (!data->res)
7959 return isl_stat_error;
7961 return isl_stat_ok;
7964 /* Compute the pullback of "upma1" by the function represented by "upma2".
7966 __isl_give isl_union_pw_multi_aff *
7967 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7968 __isl_take isl_union_pw_multi_aff *upma1,
7969 __isl_take isl_union_pw_multi_aff *upma2)
7971 return bin_op(upma1, upma2, &pullback_entry);
7974 /* Apply "upma2" to "upma1".
7976 * That is, compute the pullback of "upma2" by "upma1".
7978 __isl_give isl_union_pw_multi_aff *
7979 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7980 __isl_take isl_union_pw_multi_aff *upma1,
7981 __isl_take isl_union_pw_multi_aff *upma2)
7983 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7986 #undef TYPE
7987 #define TYPE isl_pw_multi_aff
7988 static
7989 #include "isl_copy_tuple_id_templ.c"
7991 /* Given a function "pma1" of the form A[B -> C] -> D and
7992 * a function "pma2" of the form E -> B,
7993 * replace the domain of the wrapped relation inside the domain of "pma1"
7994 * by the preimage with respect to "pma2".
7995 * In other words, plug in "pma2" in this nested domain.
7996 * The result is of the form A[E -> C] -> D.
7998 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7999 * plug that into "pma1".
8001 __isl_give isl_pw_multi_aff *
8002 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8003 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
8005 isl_space *pma1_space, *pma2_space;
8006 isl_space *space;
8007 isl_pw_multi_aff *id;
8009 pma1_space = isl_pw_multi_aff_peek_space(pma1);
8010 pma2_space = isl_pw_multi_aff_peek_space(pma2);
8012 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
8013 goto error;
8014 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
8015 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
8016 goto error;
8018 space = isl_space_domain(isl_space_copy(pma1_space));
8019 space = isl_space_range(isl_space_unwrap(space));
8020 id = isl_pw_multi_aff_identity_on_domain_space(space);
8021 pma2 = isl_pw_multi_aff_product(pma2, id);
8023 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
8024 pma1_space, isl_dim_in);
8025 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
8026 pma1_space, isl_dim_in);
8028 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
8029 error:
8030 isl_pw_multi_aff_free(pma1);
8031 isl_pw_multi_aff_free(pma2);
8032 return NULL;
8035 /* If data->pma and "pma2" are such that
8036 * data->pma is of the form A[B -> C] -> D and
8037 * "pma2" is of the form E -> B,
8038 * then replace the domain of the wrapped relation
8039 * inside the domain of data->pma by the preimage with respect to "pma2" and
8040 * add the result to data->res.
8042 static isl_stat preimage_domain_wrapped_domain_entry(
8043 __isl_take isl_pw_multi_aff *pma2, void *user)
8045 struct isl_union_pw_multi_aff_bin_data *data = user;
8046 isl_space *pma1_space, *pma2_space;
8047 isl_bool match;
8049 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
8050 pma2_space = isl_pw_multi_aff_peek_space(pma2);
8052 match = isl_space_domain_is_wrapping(pma1_space);
8053 if (match >= 0 && match)
8054 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
8055 isl_dim_in, pma2_space, isl_dim_out);
8056 if (match < 0 || !match) {
8057 isl_pw_multi_aff_free(pma2);
8058 return match < 0 ? isl_stat_error : isl_stat_ok;
8061 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8062 isl_pw_multi_aff_copy(data->pma), pma2);
8064 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
8066 return isl_stat_non_null(data->res);
8069 /* For each pair of functions A[B -> C] -> D in "upma1" and
8070 * E -> B in "upma2",
8071 * replace the domain of the wrapped relation inside the domain of the first
8072 * by the preimage with respect to the second and collect the results.
8073 * In other words, plug in the second function in this nested domain.
8074 * The results are of the form A[E -> C] -> D.
8076 __isl_give isl_union_pw_multi_aff *
8077 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8078 __isl_take isl_union_pw_multi_aff *upma1,
8079 __isl_take isl_union_pw_multi_aff *upma2)
8081 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
8084 /* Check that the domain space of "upa" matches "space".
8086 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8087 * can in principle never fail since the space "space" is that
8088 * of the isl_multi_union_pw_aff and is a set space such that
8089 * there is no domain space to match.
8091 * We check the parameters and double-check that "space" is
8092 * indeed that of a set.
8094 static isl_stat isl_union_pw_aff_check_match_domain_space(
8095 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8097 isl_space *upa_space;
8098 isl_bool match;
8100 if (!upa || !space)
8101 return isl_stat_error;
8103 match = isl_space_is_set(space);
8104 if (match < 0)
8105 return isl_stat_error;
8106 if (!match)
8107 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8108 "expecting set space", return isl_stat_error);
8110 upa_space = isl_union_pw_aff_get_space(upa);
8111 match = isl_space_has_equal_params(space, upa_space);
8112 if (match < 0)
8113 goto error;
8114 if (!match)
8115 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8116 "parameters don't match", goto error);
8118 isl_space_free(upa_space);
8119 return isl_stat_ok;
8120 error:
8121 isl_space_free(upa_space);
8122 return isl_stat_error;
8125 /* Do the parameters of "upa" match those of "space"?
8127 static isl_bool isl_union_pw_aff_matching_params(
8128 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8130 isl_space *upa_space;
8131 isl_bool match;
8133 if (!upa || !space)
8134 return isl_bool_error;
8136 upa_space = isl_union_pw_aff_get_space(upa);
8138 match = isl_space_has_equal_params(space, upa_space);
8140 isl_space_free(upa_space);
8141 return match;
8144 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8145 * space represents the new parameters.
8146 * res collects the results.
8148 struct isl_union_pw_aff_reset_params_data {
8149 isl_space *space;
8150 isl_union_pw_aff *res;
8153 /* Replace the parameters of "pa" by data->space and
8154 * add the result to data->res.
8156 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8158 struct isl_union_pw_aff_reset_params_data *data = user;
8159 isl_space *space;
8161 space = isl_pw_aff_get_space(pa);
8162 space = isl_space_replace_params(space, data->space);
8163 pa = isl_pw_aff_reset_space(pa, space);
8164 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8166 return data->res ? isl_stat_ok : isl_stat_error;
8169 /* Replace the domain space of "upa" by "space".
8170 * Since a union expression does not have a (single) domain space,
8171 * "space" is necessarily a parameter space.
8173 * Since the order and the names of the parameters determine
8174 * the hash value, we need to create a new hash table.
8176 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8177 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8179 struct isl_union_pw_aff_reset_params_data data = { space };
8180 isl_bool match;
8182 match = isl_union_pw_aff_matching_params(upa, space);
8183 if (match < 0)
8184 upa = isl_union_pw_aff_free(upa);
8185 else if (match) {
8186 isl_space_free(space);
8187 return upa;
8190 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8191 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8192 data.res = isl_union_pw_aff_free(data.res);
8194 isl_union_pw_aff_free(upa);
8195 isl_space_free(space);
8196 return data.res;
8199 /* Return the floor of "pa".
8201 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8203 return isl_pw_aff_floor(pa);
8206 /* Given f, return floor(f).
8208 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8209 __isl_take isl_union_pw_aff *upa)
8211 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8214 /* Compute
8216 * upa mod m = upa - m * floor(upa/m)
8218 * with m an integer value.
8220 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8221 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8223 isl_union_pw_aff *res;
8225 if (!upa || !m)
8226 goto error;
8228 if (!isl_val_is_int(m))
8229 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8230 "expecting integer modulo", goto error);
8231 if (!isl_val_is_pos(m))
8232 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8233 "expecting positive modulo", goto error);
8235 res = isl_union_pw_aff_copy(upa);
8236 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8237 upa = isl_union_pw_aff_floor(upa);
8238 upa = isl_union_pw_aff_scale_val(upa, m);
8239 res = isl_union_pw_aff_sub(res, upa);
8241 return res;
8242 error:
8243 isl_val_free(m);
8244 isl_union_pw_aff_free(upa);
8245 return NULL;
8248 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8249 * pos is the output position that needs to be extracted.
8250 * res collects the results.
8252 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8253 int pos;
8254 isl_union_pw_aff *res;
8257 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8258 * (assuming it has such a dimension) and add it to data->res.
8260 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8262 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8263 isl_size n_out;
8264 isl_pw_aff *pa;
8266 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8267 if (n_out < 0)
8268 return isl_stat_error;
8269 if (data->pos >= n_out) {
8270 isl_pw_multi_aff_free(pma);
8271 return isl_stat_ok;
8274 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8275 isl_pw_multi_aff_free(pma);
8277 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8279 return data->res ? isl_stat_ok : isl_stat_error;
8282 /* Extract an isl_union_pw_aff corresponding to
8283 * output dimension "pos" of "upma".
8285 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8286 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8288 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8289 isl_space *space;
8291 if (!upma)
8292 return NULL;
8294 if (pos < 0)
8295 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8296 "cannot extract at negative position", return NULL);
8298 space = isl_union_pw_multi_aff_get_space(upma);
8299 data.res = isl_union_pw_aff_empty(space);
8300 data.pos = pos;
8301 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8302 &get_union_pw_aff, &data) < 0)
8303 data.res = isl_union_pw_aff_free(data.res);
8305 return data.res;
8308 /* Return a union piecewise affine expression
8309 * that is equal to "aff" on "domain".
8311 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8312 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8314 isl_pw_aff *pa;
8316 pa = isl_pw_aff_from_aff(aff);
8317 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8320 /* Return a union piecewise affine expression
8321 * that is equal to the parameter identified by "id" on "domain".
8323 * Make sure the parameter appears in the space passed to
8324 * isl_aff_param_on_domain_space_id.
8326 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8327 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8329 isl_space *space;
8330 isl_aff *aff;
8332 space = isl_union_set_get_space(domain);
8333 space = isl_space_add_param_id(space, isl_id_copy(id));
8334 aff = isl_aff_param_on_domain_space_id(space, id);
8335 return isl_union_pw_aff_aff_on_domain(domain, aff);
8338 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8339 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8340 * needs to attain.
8341 * "res" collects the results.
8343 struct isl_union_pw_aff_pw_aff_on_domain_data {
8344 isl_pw_aff *pa;
8345 isl_union_pw_aff *res;
8348 /* Construct a piecewise affine expression that is equal to data->pa
8349 * on "domain" and add the result to data->res.
8351 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8353 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8354 isl_pw_aff *pa;
8355 isl_size dim;
8357 pa = isl_pw_aff_copy(data->pa);
8358 dim = isl_set_dim(domain, isl_dim_set);
8359 if (dim < 0)
8360 pa = isl_pw_aff_free(pa);
8361 pa = isl_pw_aff_from_range(pa);
8362 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8363 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8364 pa = isl_pw_aff_intersect_domain(pa, domain);
8365 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8367 return data->res ? isl_stat_ok : isl_stat_error;
8370 /* Return a union piecewise affine expression
8371 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8372 * have been aligned.
8374 * Construct an isl_pw_aff on each of the sets in "domain" and
8375 * collect the results.
8377 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8378 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8380 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8381 isl_space *space;
8383 space = isl_union_set_get_space(domain);
8384 data.res = isl_union_pw_aff_empty(space);
8385 data.pa = pa;
8386 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8387 data.res = isl_union_pw_aff_free(data.res);
8388 isl_union_set_free(domain);
8389 isl_pw_aff_free(pa);
8390 return data.res;
8393 /* Return a union piecewise affine expression
8394 * that is equal to "pa" on "domain".
8396 * Check that "pa" is a parametric expression,
8397 * align the parameters if needed and call
8398 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8400 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8401 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8403 isl_bool is_set;
8404 isl_bool equal_params;
8405 isl_space *domain_space, *pa_space;
8407 pa_space = isl_pw_aff_peek_space(pa);
8408 is_set = isl_space_is_set(pa_space);
8409 if (is_set < 0)
8410 goto error;
8411 if (!is_set)
8412 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8413 "expecting parametric expression", goto error);
8415 domain_space = isl_union_set_get_space(domain);
8416 pa_space = isl_pw_aff_get_space(pa);
8417 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8418 if (equal_params >= 0 && !equal_params) {
8419 isl_space *space;
8421 space = isl_space_align_params(domain_space, pa_space);
8422 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8423 domain = isl_union_set_align_params(domain, space);
8424 } else {
8425 isl_space_free(domain_space);
8426 isl_space_free(pa_space);
8429 if (equal_params < 0)
8430 goto error;
8431 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8432 error:
8433 isl_union_set_free(domain);
8434 isl_pw_aff_free(pa);
8435 return NULL;
8438 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8439 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8440 * "res" collects the results.
8442 struct isl_union_pw_aff_val_on_domain_data {
8443 isl_val *v;
8444 isl_union_pw_aff *res;
8447 /* Construct a piecewise affine expression that is equal to data->v
8448 * on "domain" and add the result to data->res.
8450 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8452 struct isl_union_pw_aff_val_on_domain_data *data = user;
8453 isl_pw_aff *pa;
8454 isl_val *v;
8456 v = isl_val_copy(data->v);
8457 pa = isl_pw_aff_val_on_domain(domain, v);
8458 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8460 return data->res ? isl_stat_ok : isl_stat_error;
8463 /* Return a union piecewise affine expression
8464 * that is equal to "v" on "domain".
8466 * Construct an isl_pw_aff on each of the sets in "domain" and
8467 * collect the results.
8469 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8470 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8472 struct isl_union_pw_aff_val_on_domain_data data;
8473 isl_space *space;
8475 space = isl_union_set_get_space(domain);
8476 data.res = isl_union_pw_aff_empty(space);
8477 data.v = v;
8478 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8479 data.res = isl_union_pw_aff_free(data.res);
8480 isl_union_set_free(domain);
8481 isl_val_free(v);
8482 return data.res;
8485 /* Construct a piecewise multi affine expression
8486 * that is equal to "pa" and add it to upma.
8488 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8489 void *user)
8491 isl_union_pw_multi_aff **upma = user;
8492 isl_pw_multi_aff *pma;
8494 pma = isl_pw_multi_aff_from_pw_aff(pa);
8495 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8497 return *upma ? isl_stat_ok : isl_stat_error;
8500 /* Construct and return a union piecewise multi affine expression
8501 * that is equal to the given union piecewise affine expression.
8503 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8504 __isl_take isl_union_pw_aff *upa)
8506 isl_space *space;
8507 isl_union_pw_multi_aff *upma;
8509 if (!upa)
8510 return NULL;
8512 space = isl_union_pw_aff_get_space(upa);
8513 upma = isl_union_pw_multi_aff_empty(space);
8515 if (isl_union_pw_aff_foreach_pw_aff(upa,
8516 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8517 upma = isl_union_pw_multi_aff_free(upma);
8519 isl_union_pw_aff_free(upa);
8520 return upma;
8523 /* Compute the set of elements in the domain of "pa" where it is zero and
8524 * add this set to "uset".
8526 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8528 isl_union_set **uset = (isl_union_set **)user;
8530 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8532 return *uset ? isl_stat_ok : isl_stat_error;
8535 /* Return a union set containing those elements in the domain
8536 * of "upa" where it is zero.
8538 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8539 __isl_take isl_union_pw_aff *upa)
8541 isl_union_set *zero;
8543 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8544 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8545 zero = isl_union_set_free(zero);
8547 isl_union_pw_aff_free(upa);
8548 return zero;
8551 /* Internal data structure for isl_union_pw_aff_bind_id,
8552 * storing the parameter that needs to be bound and
8553 * the accumulated results.
8555 struct isl_bind_id_data {
8556 isl_id *id;
8557 isl_union_set *bound;
8560 /* Bind the piecewise affine function "pa" to the parameter data->id,
8561 * adding the resulting elements in the domain where the expression
8562 * is equal to the parameter to data->bound.
8564 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8566 struct isl_bind_id_data *data = user;
8567 isl_set *bound;
8569 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8570 data->bound = isl_union_set_add_set(data->bound, bound);
8572 return data->bound ? isl_stat_ok : isl_stat_error;
8575 /* Bind the union piecewise affine function "upa" to the parameter "id",
8576 * returning the elements in the domain where the expression
8577 * is equal to the parameter.
8579 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8580 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8582 struct isl_bind_id_data data = { id };
8584 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8585 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8586 data.bound = isl_union_set_free(data.bound);
8588 isl_union_pw_aff_free(upa);
8589 isl_id_free(id);
8590 return data.bound;
8593 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8594 * upma is the function that is plugged in.
8595 * pa is the current part of the function in which upma is plugged in.
8596 * res collects the results.
8598 struct isl_union_pw_aff_pullback_upma_data {
8599 isl_union_pw_multi_aff *upma;
8600 isl_pw_aff *pa;
8601 isl_union_pw_aff *res;
8604 /* Check if "pma" can be plugged into data->pa.
8605 * If so, perform the pullback and add the result to data->res.
8607 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8609 struct isl_union_pw_aff_pullback_upma_data *data = user;
8610 isl_pw_aff *pa;
8612 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8613 pma->dim, isl_dim_out)) {
8614 isl_pw_multi_aff_free(pma);
8615 return isl_stat_ok;
8618 pa = isl_pw_aff_copy(data->pa);
8619 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8621 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8623 return data->res ? isl_stat_ok : isl_stat_error;
8626 /* Check if any of the elements of data->upma can be plugged into pa,
8627 * add if so add the result to data->res.
8629 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8631 struct isl_union_pw_aff_pullback_upma_data *data = user;
8632 isl_stat r;
8634 data->pa = pa;
8635 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8636 &pa_pb_pma, data);
8637 isl_pw_aff_free(pa);
8639 return r;
8642 /* Compute the pullback of "upa" by the function represented by "upma".
8643 * In other words, plug in "upma" in "upa". The result contains
8644 * expressions defined over the domain space of "upma".
8646 * Run over all pairs of elements in "upa" and "upma", perform
8647 * the pullback when appropriate and collect the results.
8648 * If the hash value were based on the domain space rather than
8649 * the function space, then we could run through all elements
8650 * of "upma" and directly pick out the corresponding element of "upa".
8652 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8653 __isl_take isl_union_pw_aff *upa,
8654 __isl_take isl_union_pw_multi_aff *upma)
8656 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8657 isl_space *space;
8659 space = isl_union_pw_multi_aff_get_space(upma);
8660 upa = isl_union_pw_aff_align_params(upa, space);
8661 space = isl_union_pw_aff_get_space(upa);
8662 upma = isl_union_pw_multi_aff_align_params(upma, space);
8664 if (!upa || !upma)
8665 goto error;
8667 data.upma = upma;
8668 data.res = isl_union_pw_aff_alloc_same_size(upa);
8669 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8670 data.res = isl_union_pw_aff_free(data.res);
8672 isl_union_pw_aff_free(upa);
8673 isl_union_pw_multi_aff_free(upma);
8674 return data.res;
8675 error:
8676 isl_union_pw_aff_free(upa);
8677 isl_union_pw_multi_aff_free(upma);
8678 return NULL;
8681 #undef BASE
8682 #define BASE union_pw_aff
8683 #undef DOMBASE
8684 #define DOMBASE union_set
8686 #include <isl_multi_explicit_domain.c>
8687 #include <isl_multi_union_pw_aff_explicit_domain.c>
8688 #include <isl_multi_templ.c>
8689 #include <isl_multi_apply_set.c>
8690 #include <isl_multi_apply_union_set.c>
8691 #include <isl_multi_arith_templ.c>
8692 #include <isl_multi_bind_templ.c>
8693 #include <isl_multi_coalesce.c>
8694 #include <isl_multi_dim_id_templ.c>
8695 #include <isl_multi_floor.c>
8696 #include <isl_multi_from_base_templ.c>
8697 #include <isl_multi_gist.c>
8698 #include <isl_multi_align_set.c>
8699 #include <isl_multi_align_union_set.c>
8700 #include <isl_multi_intersect.c>
8701 #include <isl_multi_nan_templ.c>
8702 #include <isl_multi_tuple_id_templ.c>
8703 #include <isl_multi_union_add_templ.c>
8704 #include <isl_multi_zero_space_templ.c>
8706 /* Does "mupa" have a non-trivial explicit domain?
8708 * The explicit domain, if present, is trivial if it represents
8709 * an (obviously) universe parameter set.
8711 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8712 __isl_keep isl_multi_union_pw_aff *mupa)
8714 isl_bool is_params, trivial;
8715 isl_set *set;
8717 if (!mupa)
8718 return isl_bool_error;
8719 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8720 return isl_bool_false;
8721 is_params = isl_union_set_is_params(mupa->u.dom);
8722 if (is_params < 0 || !is_params)
8723 return isl_bool_not(is_params);
8724 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8725 trivial = isl_set_plain_is_universe(set);
8726 isl_set_free(set);
8727 return isl_bool_not(trivial);
8730 /* Construct a multiple union piecewise affine expression
8731 * in the given space with value zero in each of the output dimensions.
8733 * Since there is no canonical zero value for
8734 * a union piecewise affine expression, we can only construct
8735 * a zero-dimensional "zero" value.
8737 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8738 __isl_take isl_space *space)
8740 isl_bool params;
8741 isl_size dim;
8743 if (!space)
8744 return NULL;
8746 params = isl_space_is_params(space);
8747 if (params < 0)
8748 goto error;
8749 if (params)
8750 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8751 "expecting proper set space", goto error);
8752 if (!isl_space_is_set(space))
8753 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8754 "expecting set space", goto error);
8755 dim = isl_space_dim(space, isl_dim_out);
8756 if (dim < 0)
8757 goto error;
8758 if (dim != 0)
8759 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8760 "expecting 0D space", goto error);
8762 return isl_multi_union_pw_aff_alloc(space);
8763 error:
8764 isl_space_free(space);
8765 return NULL;
8768 /* Construct and return a multi union piecewise affine expression
8769 * that is equal to the given multi affine expression.
8771 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8772 __isl_take isl_multi_aff *ma)
8774 isl_multi_pw_aff *mpa;
8776 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8777 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8780 /* This function performs the same operation as
8781 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8782 * isl_multi_aff when exported.
8784 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8785 __isl_take isl_multi_aff *ma)
8787 return isl_multi_union_pw_aff_from_multi_aff(ma);
8790 /* Construct and return a multi union piecewise affine expression
8791 * that is equal to the given multi piecewise affine expression.
8793 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8794 __isl_take isl_multi_pw_aff *mpa)
8796 int i;
8797 isl_size n;
8798 isl_space *space;
8799 isl_multi_union_pw_aff *mupa;
8801 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8802 if (n < 0)
8803 mpa = isl_multi_pw_aff_free(mpa);
8804 if (!mpa)
8805 return NULL;
8807 space = isl_multi_pw_aff_get_space(mpa);
8808 space = isl_space_range(space);
8809 mupa = isl_multi_union_pw_aff_alloc(space);
8811 for (i = 0; i < n; ++i) {
8812 isl_pw_aff *pa;
8813 isl_union_pw_aff *upa;
8815 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8816 upa = isl_union_pw_aff_from_pw_aff(pa);
8817 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8820 isl_multi_pw_aff_free(mpa);
8822 return mupa;
8825 /* Extract the range space of "pma" and assign it to *space.
8826 * If *space has already been set (through a previous call to this function),
8827 * then check that the range space is the same.
8829 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8831 isl_space **space = user;
8832 isl_space *pma_space;
8833 isl_bool equal;
8835 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8836 isl_pw_multi_aff_free(pma);
8838 if (!pma_space)
8839 return isl_stat_error;
8840 if (!*space) {
8841 *space = pma_space;
8842 return isl_stat_ok;
8845 equal = isl_space_is_equal(pma_space, *space);
8846 isl_space_free(pma_space);
8848 if (equal < 0)
8849 return isl_stat_error;
8850 if (!equal)
8851 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8852 "range spaces not the same", return isl_stat_error);
8853 return isl_stat_ok;
8856 /* Construct and return a multi union piecewise affine expression
8857 * that is equal to the given union piecewise multi affine expression.
8859 * In order to be able to perform the conversion, the input
8860 * needs to be non-empty and may only involve a single range space.
8862 * If the resulting multi union piecewise affine expression has
8863 * an explicit domain, then assign it the domain of the input.
8864 * In other cases, the domain is stored in the individual elements.
8866 __isl_give isl_multi_union_pw_aff *
8867 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8868 __isl_take isl_union_pw_multi_aff *upma)
8870 isl_space *space = NULL;
8871 isl_multi_union_pw_aff *mupa;
8872 int i;
8873 isl_size n;
8875 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8876 if (n < 0)
8877 goto error;
8878 if (n == 0)
8879 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8880 "cannot extract range space from empty input",
8881 goto error);
8882 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8883 &space) < 0)
8884 goto error;
8886 if (!space)
8887 goto error;
8889 n = isl_space_dim(space, isl_dim_set);
8890 if (n < 0)
8891 space = isl_space_free(space);
8892 mupa = isl_multi_union_pw_aff_alloc(space);
8894 for (i = 0; i < n; ++i) {
8895 isl_union_pw_aff *upa;
8897 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8898 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8900 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8901 isl_union_set *dom;
8902 isl_union_pw_multi_aff *copy;
8904 copy = isl_union_pw_multi_aff_copy(upma);
8905 dom = isl_union_pw_multi_aff_domain(copy);
8906 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8909 isl_union_pw_multi_aff_free(upma);
8910 return mupa;
8911 error:
8912 isl_space_free(space);
8913 isl_union_pw_multi_aff_free(upma);
8914 return NULL;
8917 /* This function performs the same operation as
8918 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8919 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8921 __isl_give isl_multi_union_pw_aff *
8922 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8923 __isl_take isl_union_pw_multi_aff *upma)
8925 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8928 /* Try and create an isl_multi_union_pw_aff that is equivalent
8929 * to the given isl_union_map.
8930 * The isl_union_map is required to be single-valued in each space.
8931 * Moreover, it cannot be empty and all range spaces need to be the same.
8932 * Otherwise, an error is produced.
8934 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8935 __isl_take isl_union_map *umap)
8937 isl_union_pw_multi_aff *upma;
8939 upma = isl_union_pw_multi_aff_from_union_map(umap);
8940 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8943 /* This function performs the same operation as
8944 * isl_multi_union_pw_aff_from_union_map,
8945 * but is considered as a function on an isl_union_map when exported.
8947 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8948 __isl_take isl_union_map *umap)
8950 return isl_multi_union_pw_aff_from_union_map(umap);
8953 /* Return a multiple union piecewise affine expression
8954 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8955 * have been aligned.
8957 * If the resulting multi union piecewise affine expression has
8958 * an explicit domain, then assign it the input domain.
8959 * In other cases, the domain is stored in the individual elements.
8961 static __isl_give isl_multi_union_pw_aff *
8962 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8963 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8965 int i;
8966 isl_size n;
8967 isl_space *space;
8968 isl_multi_union_pw_aff *mupa;
8970 n = isl_multi_val_dim(mv, isl_dim_set);
8971 if (!domain || n < 0)
8972 goto error;
8974 space = isl_multi_val_get_space(mv);
8975 mupa = isl_multi_union_pw_aff_alloc(space);
8976 for (i = 0; i < n; ++i) {
8977 isl_val *v;
8978 isl_union_pw_aff *upa;
8980 v = isl_multi_val_get_val(mv, i);
8981 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8983 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8985 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8986 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8987 isl_union_set_copy(domain));
8989 isl_union_set_free(domain);
8990 isl_multi_val_free(mv);
8991 return mupa;
8992 error:
8993 isl_union_set_free(domain);
8994 isl_multi_val_free(mv);
8995 return NULL;
8998 /* Return a multiple union piecewise affine expression
8999 * that is equal to "mv" on "domain".
9001 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
9002 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
9004 isl_bool equal_params;
9006 if (!domain || !mv)
9007 goto error;
9008 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
9009 if (equal_params < 0)
9010 goto error;
9011 if (equal_params)
9012 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9013 domain, mv);
9014 domain = isl_union_set_align_params(domain,
9015 isl_multi_val_get_space(mv));
9016 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
9017 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
9018 error:
9019 isl_union_set_free(domain);
9020 isl_multi_val_free(mv);
9021 return NULL;
9024 /* Return a multiple union piecewise affine expression
9025 * that is equal to "ma" on "domain".
9027 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
9028 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
9030 isl_pw_multi_aff *pma;
9032 pma = isl_pw_multi_aff_from_multi_aff(ma);
9033 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
9036 /* Return a multiple union piecewise affine expression
9037 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9038 * have been aligned.
9040 * If the resulting multi union piecewise affine expression has
9041 * an explicit domain, then assign it the input domain.
9042 * In other cases, the domain is stored in the individual elements.
9044 static __isl_give isl_multi_union_pw_aff *
9045 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9046 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
9048 int i;
9049 isl_size n;
9050 isl_space *space;
9051 isl_multi_union_pw_aff *mupa;
9053 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
9054 if (!domain || n < 0)
9055 goto error;
9056 space = isl_pw_multi_aff_get_space(pma);
9057 mupa = isl_multi_union_pw_aff_alloc(space);
9058 for (i = 0; i < n; ++i) {
9059 isl_pw_aff *pa;
9060 isl_union_pw_aff *upa;
9062 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9063 upa = isl_union_pw_aff_pw_aff_on_domain(
9064 isl_union_set_copy(domain), pa);
9065 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9067 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9068 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
9069 isl_union_set_copy(domain));
9071 isl_union_set_free(domain);
9072 isl_pw_multi_aff_free(pma);
9073 return mupa;
9074 error:
9075 isl_union_set_free(domain);
9076 isl_pw_multi_aff_free(pma);
9077 return NULL;
9080 /* Return a multiple union piecewise affine expression
9081 * that is equal to "pma" on "domain".
9083 __isl_give isl_multi_union_pw_aff *
9084 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
9085 __isl_take isl_pw_multi_aff *pma)
9087 isl_bool equal_params;
9088 isl_space *space;
9090 space = isl_pw_multi_aff_peek_space(pma);
9091 equal_params = isl_union_set_space_has_equal_params(domain, space);
9092 if (equal_params < 0)
9093 goto error;
9094 if (equal_params)
9095 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9096 domain, pma);
9097 domain = isl_union_set_align_params(domain,
9098 isl_pw_multi_aff_get_space(pma));
9099 pma = isl_pw_multi_aff_align_params(pma,
9100 isl_union_set_get_space(domain));
9101 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
9102 pma);
9103 error:
9104 isl_union_set_free(domain);
9105 isl_pw_multi_aff_free(pma);
9106 return NULL;
9109 /* Return a union set containing those elements in the domains
9110 * of the elements of "mupa" where they are all zero.
9112 * If there are no elements, then simply return the entire domain.
9114 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
9115 __isl_take isl_multi_union_pw_aff *mupa)
9117 int i;
9118 isl_size n;
9119 isl_union_pw_aff *upa;
9120 isl_union_set *zero;
9122 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9123 if (n < 0)
9124 mupa = isl_multi_union_pw_aff_free(mupa);
9125 if (!mupa)
9126 return NULL;
9128 if (n == 0)
9129 return isl_multi_union_pw_aff_domain(mupa);
9131 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9132 zero = isl_union_pw_aff_zero_union_set(upa);
9134 for (i = 1; i < n; ++i) {
9135 isl_union_set *zero_i;
9137 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9138 zero_i = isl_union_pw_aff_zero_union_set(upa);
9140 zero = isl_union_set_intersect(zero, zero_i);
9143 isl_multi_union_pw_aff_free(mupa);
9144 return zero;
9147 /* Construct a union map mapping the shared domain
9148 * of the union piecewise affine expressions to the range of "mupa"
9149 * in the special case of a 0D multi union piecewise affine expression.
9151 * Construct a map between the explicit domain of "mupa" and
9152 * the range space.
9153 * Note that this assumes that the domain consists of explicit elements.
9155 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9156 __isl_take isl_multi_union_pw_aff *mupa)
9158 isl_bool is_params;
9159 isl_space *space;
9160 isl_union_set *dom, *ran;
9162 space = isl_multi_union_pw_aff_get_space(mupa);
9163 dom = isl_multi_union_pw_aff_domain(mupa);
9164 ran = isl_union_set_from_set(isl_set_universe(space));
9166 is_params = isl_union_set_is_params(dom);
9167 if (is_params < 0)
9168 dom = isl_union_set_free(dom);
9169 else if (is_params)
9170 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9171 "cannot create union map from expression without "
9172 "explicit domain elements",
9173 dom = isl_union_set_free(dom));
9175 return isl_union_map_from_domain_and_range(dom, ran);
9178 /* Construct a union map mapping the shared domain
9179 * of the union piecewise affine expressions to the range of "mupa"
9180 * with each dimension in the range equated to the
9181 * corresponding union piecewise affine expression.
9183 * If the input is zero-dimensional, then construct a mapping
9184 * from its explicit domain.
9186 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9187 __isl_take isl_multi_union_pw_aff *mupa)
9189 int i;
9190 isl_size n;
9191 isl_space *space;
9192 isl_union_map *umap;
9193 isl_union_pw_aff *upa;
9195 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9196 if (n < 0)
9197 mupa = isl_multi_union_pw_aff_free(mupa);
9198 if (!mupa)
9199 return NULL;
9201 if (n == 0)
9202 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9204 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9205 umap = isl_union_map_from_union_pw_aff(upa);
9207 for (i = 1; i < n; ++i) {
9208 isl_union_map *umap_i;
9210 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9211 umap_i = isl_union_map_from_union_pw_aff(upa);
9212 umap = isl_union_map_flat_range_product(umap, umap_i);
9215 space = isl_multi_union_pw_aff_get_space(mupa);
9216 umap = isl_union_map_reset_range_space(umap, space);
9218 isl_multi_union_pw_aff_free(mupa);
9219 return umap;
9222 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9223 * "range" is the space from which to set the range space.
9224 * "res" collects the results.
9226 struct isl_union_pw_multi_aff_reset_range_space_data {
9227 isl_space *range;
9228 isl_union_pw_multi_aff *res;
9231 /* Replace the range space of "pma" by the range space of data->range and
9232 * add the result to data->res.
9234 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9236 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9237 isl_space *space;
9239 space = isl_pw_multi_aff_get_space(pma);
9240 space = isl_space_domain(space);
9241 space = isl_space_extend_domain_with_range(space,
9242 isl_space_copy(data->range));
9243 pma = isl_pw_multi_aff_reset_space(pma, space);
9244 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9246 return data->res ? isl_stat_ok : isl_stat_error;
9249 /* Replace the range space of all the piecewise affine expressions in "upma" by
9250 * the range space of "space".
9252 * This assumes that all these expressions have the same output dimension.
9254 * Since the spaces of the expressions change, so do their hash values.
9255 * We therefore need to create a new isl_union_pw_multi_aff.
9256 * Note that the hash value is currently computed based on the entire
9257 * space even though there can only be a single expression with a given
9258 * domain space.
9260 static __isl_give isl_union_pw_multi_aff *
9261 isl_union_pw_multi_aff_reset_range_space(
9262 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9264 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9265 isl_space *space_upma;
9267 space_upma = isl_union_pw_multi_aff_get_space(upma);
9268 data.res = isl_union_pw_multi_aff_empty(space_upma);
9269 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9270 &reset_range_space, &data) < 0)
9271 data.res = isl_union_pw_multi_aff_free(data.res);
9273 isl_space_free(space);
9274 isl_union_pw_multi_aff_free(upma);
9275 return data.res;
9278 /* Construct and return a union piecewise multi affine expression
9279 * that is equal to the given multi union piecewise affine expression,
9280 * in the special case of a 0D multi union piecewise affine expression.
9282 * Construct a union piecewise multi affine expression
9283 * on top of the explicit domain of the input.
9285 __isl_give isl_union_pw_multi_aff *
9286 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9287 __isl_take isl_multi_union_pw_aff *mupa)
9289 isl_space *space;
9290 isl_multi_val *mv;
9291 isl_union_set *domain;
9293 space = isl_multi_union_pw_aff_get_space(mupa);
9294 mv = isl_multi_val_zero(space);
9295 domain = isl_multi_union_pw_aff_domain(mupa);
9296 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9299 /* Construct and return a union piecewise multi affine expression
9300 * that is equal to the given multi union piecewise affine expression.
9302 * If the input is zero-dimensional, then
9303 * construct a union piecewise multi affine expression
9304 * on top of the explicit domain of the input.
9306 __isl_give isl_union_pw_multi_aff *
9307 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9308 __isl_take isl_multi_union_pw_aff *mupa)
9310 int i;
9311 isl_size n;
9312 isl_space *space;
9313 isl_union_pw_multi_aff *upma;
9314 isl_union_pw_aff *upa;
9316 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9317 if (n < 0)
9318 mupa = isl_multi_union_pw_aff_free(mupa);
9319 if (!mupa)
9320 return NULL;
9322 if (n == 0)
9323 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9325 space = isl_multi_union_pw_aff_get_space(mupa);
9326 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9327 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9329 for (i = 1; i < n; ++i) {
9330 isl_union_pw_multi_aff *upma_i;
9332 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9333 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9334 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9337 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9339 isl_multi_union_pw_aff_free(mupa);
9340 return upma;
9343 /* Intersect the range of "mupa" with "range",
9344 * in the special case where "mupa" is 0D.
9346 * Intersect the domain of "mupa" with the constraints on the parameters
9347 * of "range".
9349 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9350 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9352 range = isl_set_params(range);
9353 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9354 return mupa;
9357 /* Intersect the range of "mupa" with "range".
9358 * That is, keep only those domain elements that have a function value
9359 * in "range".
9361 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9362 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9364 isl_union_pw_multi_aff *upma;
9365 isl_union_set *domain;
9366 isl_space *space;
9367 isl_size n;
9368 int match;
9370 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9371 if (n < 0 || !range)
9372 goto error;
9374 space = isl_set_get_space(range);
9375 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9376 space, isl_dim_set);
9377 isl_space_free(space);
9378 if (match < 0)
9379 goto error;
9380 if (!match)
9381 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9382 "space don't match", goto error);
9383 if (n == 0)
9384 return mupa_intersect_range_0D(mupa, range);
9386 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9387 isl_multi_union_pw_aff_copy(mupa));
9388 domain = isl_union_set_from_set(range);
9389 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9390 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9392 return mupa;
9393 error:
9394 isl_multi_union_pw_aff_free(mupa);
9395 isl_set_free(range);
9396 return NULL;
9399 /* Return the shared domain of the elements of "mupa",
9400 * in the special case where "mupa" is zero-dimensional.
9402 * Return the explicit domain of "mupa".
9403 * Note that this domain may be a parameter set, either
9404 * because "mupa" is meant to live in a set space or
9405 * because no explicit domain has been set.
9407 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9408 __isl_take isl_multi_union_pw_aff *mupa)
9410 isl_union_set *dom;
9412 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9413 isl_multi_union_pw_aff_free(mupa);
9415 return dom;
9418 /* Return the shared domain of the elements of "mupa".
9420 * If "mupa" is zero-dimensional, then return its explicit domain.
9422 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9423 __isl_take isl_multi_union_pw_aff *mupa)
9425 int i;
9426 isl_size n;
9427 isl_union_pw_aff *upa;
9428 isl_union_set *dom;
9430 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9431 if (n < 0)
9432 mupa = isl_multi_union_pw_aff_free(mupa);
9433 if (!mupa)
9434 return NULL;
9436 if (n == 0)
9437 return isl_multi_union_pw_aff_domain_0D(mupa);
9439 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9440 dom = isl_union_pw_aff_domain(upa);
9441 for (i = 1; i < n; ++i) {
9442 isl_union_set *dom_i;
9444 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9445 dom_i = isl_union_pw_aff_domain(upa);
9446 dom = isl_union_set_intersect(dom, dom_i);
9449 isl_multi_union_pw_aff_free(mupa);
9450 return dom;
9453 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9454 * In particular, the spaces have been aligned.
9455 * The result is defined over the shared domain of the elements of "mupa"
9457 * We first extract the parametric constant part of "aff" and
9458 * define that over the shared domain.
9459 * Then we iterate over all input dimensions of "aff" and add the corresponding
9460 * multiples of the elements of "mupa".
9461 * Finally, we consider the integer divisions, calling the function
9462 * recursively to obtain an isl_union_pw_aff corresponding to the
9463 * integer division argument.
9465 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9466 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9468 int i;
9469 isl_size n_in, n_div;
9470 isl_union_pw_aff *upa;
9471 isl_union_set *uset;
9472 isl_val *v;
9473 isl_aff *cst;
9475 n_in = isl_aff_dim(aff, isl_dim_in);
9476 n_div = isl_aff_dim(aff, isl_dim_div);
9477 if (n_in < 0 || n_div < 0)
9478 goto error;
9480 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9481 cst = isl_aff_copy(aff);
9482 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9483 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9484 cst = isl_aff_project_domain_on_params(cst);
9485 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9487 for (i = 0; i < n_in; ++i) {
9488 isl_union_pw_aff *upa_i;
9490 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9491 continue;
9492 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9493 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9494 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9495 upa = isl_union_pw_aff_add(upa, upa_i);
9498 for (i = 0; i < n_div; ++i) {
9499 isl_aff *div;
9500 isl_union_pw_aff *upa_i;
9502 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9503 continue;
9504 div = isl_aff_get_div(aff, i);
9505 upa_i = multi_union_pw_aff_apply_aff(
9506 isl_multi_union_pw_aff_copy(mupa), div);
9507 upa_i = isl_union_pw_aff_floor(upa_i);
9508 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9509 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9510 upa = isl_union_pw_aff_add(upa, upa_i);
9513 isl_multi_union_pw_aff_free(mupa);
9514 isl_aff_free(aff);
9516 return upa;
9517 error:
9518 isl_multi_union_pw_aff_free(mupa);
9519 isl_aff_free(aff);
9520 return NULL;
9523 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9524 * with the domain of "aff".
9525 * Furthermore, the dimension of this space needs to be greater than zero.
9526 * The result is defined over the shared domain of the elements of "mupa"
9528 * We perform these checks and then hand over control to
9529 * multi_union_pw_aff_apply_aff.
9531 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9532 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9534 isl_size dim;
9535 isl_space *space1, *space2;
9536 isl_bool equal;
9538 mupa = isl_multi_union_pw_aff_align_params(mupa,
9539 isl_aff_get_space(aff));
9540 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9541 if (!mupa || !aff)
9542 goto error;
9544 space1 = isl_multi_union_pw_aff_get_space(mupa);
9545 space2 = isl_aff_get_domain_space(aff);
9546 equal = isl_space_is_equal(space1, space2);
9547 isl_space_free(space1);
9548 isl_space_free(space2);
9549 if (equal < 0)
9550 goto error;
9551 if (!equal)
9552 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9553 "spaces don't match", goto error);
9554 dim = isl_aff_dim(aff, isl_dim_in);
9555 if (dim < 0)
9556 goto error;
9557 if (dim == 0)
9558 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9559 "cannot determine domains", goto error);
9561 return multi_union_pw_aff_apply_aff(mupa, aff);
9562 error:
9563 isl_multi_union_pw_aff_free(mupa);
9564 isl_aff_free(aff);
9565 return NULL;
9568 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9569 * The space of "mupa" is known to be compatible with the domain of "ma".
9571 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9572 * on the domain of "mupa".
9574 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9575 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9577 isl_union_set *dom;
9579 dom = isl_multi_union_pw_aff_domain(mupa);
9580 ma = isl_multi_aff_project_domain_on_params(ma);
9582 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9585 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9586 * with the domain of "ma".
9587 * The result is defined over the shared domain of the elements of "mupa"
9589 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9590 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9592 isl_space *space1, *space2;
9593 isl_multi_union_pw_aff *res;
9594 isl_bool equal;
9595 int i;
9596 isl_size n_in, n_out;
9598 mupa = isl_multi_union_pw_aff_align_params(mupa,
9599 isl_multi_aff_get_space(ma));
9600 ma = isl_multi_aff_align_params(ma,
9601 isl_multi_union_pw_aff_get_space(mupa));
9602 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9603 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9604 if (!mupa || n_in < 0 || n_out < 0)
9605 goto error;
9607 space1 = isl_multi_union_pw_aff_get_space(mupa);
9608 space2 = isl_multi_aff_get_domain_space(ma);
9609 equal = isl_space_is_equal(space1, space2);
9610 isl_space_free(space1);
9611 isl_space_free(space2);
9612 if (equal < 0)
9613 goto error;
9614 if (!equal)
9615 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9616 "spaces don't match", goto error);
9617 if (n_in == 0)
9618 return mupa_apply_multi_aff_0D(mupa, ma);
9620 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9621 res = isl_multi_union_pw_aff_alloc(space1);
9623 for (i = 0; i < n_out; ++i) {
9624 isl_aff *aff;
9625 isl_union_pw_aff *upa;
9627 aff = isl_multi_aff_get_aff(ma, i);
9628 upa = multi_union_pw_aff_apply_aff(
9629 isl_multi_union_pw_aff_copy(mupa), aff);
9630 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9633 isl_multi_aff_free(ma);
9634 isl_multi_union_pw_aff_free(mupa);
9635 return res;
9636 error:
9637 isl_multi_union_pw_aff_free(mupa);
9638 isl_multi_aff_free(ma);
9639 return NULL;
9642 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9643 * The space of "mupa" is known to be compatible with the domain of "pa".
9645 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9646 * on the domain of "mupa".
9648 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9649 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9651 isl_union_set *dom;
9653 dom = isl_multi_union_pw_aff_domain(mupa);
9654 pa = isl_pw_aff_project_domain_on_params(pa);
9656 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9659 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9660 * with the domain of "pa".
9661 * Furthermore, the dimension of this space needs to be greater than zero.
9662 * The result is defined over the shared domain of the elements of "mupa"
9664 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9665 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9667 int i;
9668 isl_bool equal;
9669 isl_size n_in;
9670 isl_space *space, *space2;
9671 isl_union_pw_aff *upa;
9673 mupa = isl_multi_union_pw_aff_align_params(mupa,
9674 isl_pw_aff_get_space(pa));
9675 pa = isl_pw_aff_align_params(pa,
9676 isl_multi_union_pw_aff_get_space(mupa));
9677 if (!mupa || !pa)
9678 goto error;
9680 space = isl_multi_union_pw_aff_get_space(mupa);
9681 space2 = isl_pw_aff_get_domain_space(pa);
9682 equal = isl_space_is_equal(space, space2);
9683 isl_space_free(space);
9684 isl_space_free(space2);
9685 if (equal < 0)
9686 goto error;
9687 if (!equal)
9688 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9689 "spaces don't match", goto error);
9690 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9691 if (n_in < 0)
9692 goto error;
9693 if (n_in == 0)
9694 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9696 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9697 upa = isl_union_pw_aff_empty(space);
9699 for (i = 0; i < pa->n; ++i) {
9700 isl_aff *aff;
9701 isl_set *domain;
9702 isl_multi_union_pw_aff *mupa_i;
9703 isl_union_pw_aff *upa_i;
9705 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9706 domain = isl_set_copy(pa->p[i].set);
9707 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9708 aff = isl_aff_copy(pa->p[i].aff);
9709 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9710 upa = isl_union_pw_aff_union_add(upa, upa_i);
9713 isl_multi_union_pw_aff_free(mupa);
9714 isl_pw_aff_free(pa);
9715 return upa;
9716 error:
9717 isl_multi_union_pw_aff_free(mupa);
9718 isl_pw_aff_free(pa);
9719 return NULL;
9722 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9723 * The space of "mupa" is known to be compatible with the domain of "pma".
9725 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9726 * on the domain of "mupa".
9728 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9729 __isl_take isl_multi_union_pw_aff *mupa,
9730 __isl_take isl_pw_multi_aff *pma)
9732 isl_union_set *dom;
9734 dom = isl_multi_union_pw_aff_domain(mupa);
9735 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9737 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9740 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9741 * with the domain of "pma".
9742 * The result is defined over the shared domain of the elements of "mupa"
9744 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9745 __isl_take isl_multi_union_pw_aff *mupa,
9746 __isl_take isl_pw_multi_aff *pma)
9748 isl_space *space1, *space2;
9749 isl_multi_union_pw_aff *res;
9750 isl_bool equal;
9751 int i;
9752 isl_size n_in, n_out;
9754 mupa = isl_multi_union_pw_aff_align_params(mupa,
9755 isl_pw_multi_aff_get_space(pma));
9756 pma = isl_pw_multi_aff_align_params(pma,
9757 isl_multi_union_pw_aff_get_space(mupa));
9758 if (!mupa || !pma)
9759 goto error;
9761 space1 = isl_multi_union_pw_aff_get_space(mupa);
9762 space2 = isl_pw_multi_aff_get_domain_space(pma);
9763 equal = isl_space_is_equal(space1, space2);
9764 isl_space_free(space1);
9765 isl_space_free(space2);
9766 if (equal < 0)
9767 goto error;
9768 if (!equal)
9769 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9770 "spaces don't match", goto error);
9771 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9772 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9773 if (n_in < 0 || n_out < 0)
9774 goto error;
9775 if (n_in == 0)
9776 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9778 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9779 res = isl_multi_union_pw_aff_alloc(space1);
9781 for (i = 0; i < n_out; ++i) {
9782 isl_pw_aff *pa;
9783 isl_union_pw_aff *upa;
9785 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9786 upa = isl_multi_union_pw_aff_apply_pw_aff(
9787 isl_multi_union_pw_aff_copy(mupa), pa);
9788 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9791 isl_pw_multi_aff_free(pma);
9792 isl_multi_union_pw_aff_free(mupa);
9793 return res;
9794 error:
9795 isl_multi_union_pw_aff_free(mupa);
9796 isl_pw_multi_aff_free(pma);
9797 return NULL;
9800 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9801 * If the explicit domain only keeps track of constraints on the parameters,
9802 * then only update those constraints.
9804 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9805 __isl_take isl_multi_union_pw_aff *mupa,
9806 __isl_keep isl_union_pw_multi_aff *upma)
9808 isl_bool is_params;
9810 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9811 return isl_multi_union_pw_aff_free(mupa);
9813 mupa = isl_multi_union_pw_aff_cow(mupa);
9814 if (!mupa)
9815 return NULL;
9817 is_params = isl_union_set_is_params(mupa->u.dom);
9818 if (is_params < 0)
9819 return isl_multi_union_pw_aff_free(mupa);
9821 upma = isl_union_pw_multi_aff_copy(upma);
9822 if (is_params)
9823 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9824 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9825 else
9826 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9827 mupa->u.dom, upma);
9828 if (!mupa->u.dom)
9829 return isl_multi_union_pw_aff_free(mupa);
9830 return mupa;
9833 /* Compute the pullback of "mupa" by the function represented by "upma".
9834 * In other words, plug in "upma" in "mupa". The result contains
9835 * expressions defined over the domain space of "upma".
9837 * Run over all elements of "mupa" and plug in "upma" in each of them.
9839 * If "mupa" has an explicit domain, then it is this domain
9840 * that needs to undergo a pullback instead, i.e., a preimage.
9842 __isl_give isl_multi_union_pw_aff *
9843 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9844 __isl_take isl_multi_union_pw_aff *mupa,
9845 __isl_take isl_union_pw_multi_aff *upma)
9847 int i;
9848 isl_size n;
9850 mupa = isl_multi_union_pw_aff_align_params(mupa,
9851 isl_union_pw_multi_aff_get_space(upma));
9852 upma = isl_union_pw_multi_aff_align_params(upma,
9853 isl_multi_union_pw_aff_get_space(mupa));
9854 mupa = isl_multi_union_pw_aff_cow(mupa);
9855 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9856 if (n < 0 || !upma)
9857 goto error;
9859 for (i = 0; i < n; ++i) {
9860 isl_union_pw_aff *upa;
9862 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9863 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9864 isl_union_pw_multi_aff_copy(upma));
9865 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9868 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9869 mupa = preimage_explicit_domain(mupa, upma);
9871 isl_union_pw_multi_aff_free(upma);
9872 return mupa;
9873 error:
9874 isl_multi_union_pw_aff_free(mupa);
9875 isl_union_pw_multi_aff_free(upma);
9876 return NULL;
9879 /* Extract the sequence of elements in "mupa" with domain space "space"
9880 * (ignoring parameters).
9882 * For the elements of "mupa" that are not defined on the specified space,
9883 * the corresponding element in the result is empty.
9885 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9886 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9888 int i;
9889 isl_size n;
9890 isl_space *space_mpa;
9891 isl_multi_pw_aff *mpa;
9893 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9894 if (n < 0 || !space)
9895 goto error;
9897 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9898 space = isl_space_replace_params(space, space_mpa);
9899 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9900 space_mpa);
9901 mpa = isl_multi_pw_aff_alloc(space_mpa);
9903 space = isl_space_from_domain(space);
9904 space = isl_space_add_dims(space, isl_dim_out, 1);
9905 for (i = 0; i < n; ++i) {
9906 isl_union_pw_aff *upa;
9907 isl_pw_aff *pa;
9909 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9910 pa = isl_union_pw_aff_extract_pw_aff(upa,
9911 isl_space_copy(space));
9912 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9913 isl_union_pw_aff_free(upa);
9916 isl_space_free(space);
9917 return mpa;
9918 error:
9919 isl_space_free(space);
9920 return NULL;
9923 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9924 * should modify the base expressions in the input.
9926 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9927 * are taken into account.
9928 * "fn" is applied to each entry in the input.
9930 struct isl_union_pw_multi_aff_un_op_control {
9931 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9932 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9935 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9936 * a second argument) for use as an isl_union_pw_multi_aff_transform
9937 * filter function (which does take a second argument).
9938 * Simply call control->filter without the second argument.
9940 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9941 __isl_take isl_pw_multi_aff *pma, void *user)
9943 struct isl_union_pw_multi_aff_un_op_control *control = user;
9945 return control->filter(pma);
9948 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9949 * a second argument) for use as an isl_union_pw_multi_aff_transform
9950 * base function (which does take a second argument).
9951 * Simply call control->fn without the second argument.
9953 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9954 __isl_take isl_pw_multi_aff *pma, void *user)
9956 struct isl_union_pw_multi_aff_un_op_control *control = user;
9958 return control->fn(pma);
9961 /* Construct an isl_union_pw_multi_aff that is obtained by
9962 * modifying "upma" according to "control".
9964 * isl_union_pw_multi_aff_transform performs essentially
9965 * the same operation, but takes a filter and a callback function
9966 * of a different form (with an extra argument).
9967 * Call isl_union_pw_multi_aff_transform with wrappers
9968 * that remove this extra argument.
9970 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9971 __isl_take isl_union_pw_multi_aff *upma,
9972 struct isl_union_pw_multi_aff_un_op_control *control)
9974 struct isl_union_pw_multi_aff_transform_control t_control = {
9975 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9976 .filter_user = control,
9977 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9978 .fn_user = control,
9981 return isl_union_pw_multi_aff_transform(upma, &t_control);
9984 /* For each function in "upma" of the form A -> [B -> C],
9985 * extract the function A -> B and collect the results.
9987 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9988 __isl_take isl_union_pw_multi_aff *upma)
9990 struct isl_union_pw_multi_aff_un_op_control control = {
9991 .filter = &isl_pw_multi_aff_range_is_wrapping,
9992 .fn = &isl_pw_multi_aff_range_factor_domain,
9994 return isl_union_pw_multi_aff_un_op(upma, &control);
9997 /* For each function in "upma" of the form A -> [B -> C],
9998 * extract the function A -> C and collect the results.
10000 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
10001 __isl_take isl_union_pw_multi_aff *upma)
10003 struct isl_union_pw_multi_aff_un_op_control control = {
10004 .filter = &isl_pw_multi_aff_range_is_wrapping,
10005 .fn = &isl_pw_multi_aff_range_factor_range,
10007 return isl_union_pw_multi_aff_un_op(upma, &control);
10010 /* Evaluate the affine function "aff" in the void point "pnt".
10011 * In particular, return the value NaN.
10013 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
10014 __isl_take isl_point *pnt)
10016 isl_ctx *ctx;
10018 ctx = isl_point_get_ctx(pnt);
10019 isl_aff_free(aff);
10020 isl_point_free(pnt);
10021 return isl_val_nan(ctx);
10024 /* Evaluate the affine expression "aff"
10025 * in the coordinates (with denominator) "pnt".
10027 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
10028 __isl_keep isl_vec *pnt)
10030 isl_int n, d;
10031 isl_ctx *ctx;
10032 isl_val *v;
10034 if (!aff || !pnt)
10035 return NULL;
10037 ctx = isl_vec_get_ctx(aff);
10038 isl_int_init(n);
10039 isl_int_init(d);
10040 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
10041 isl_int_mul(d, aff->el[0], pnt->el[0]);
10042 v = isl_val_rat_from_isl_int(ctx, n, d);
10043 v = isl_val_normalize(v);
10044 isl_int_clear(n);
10045 isl_int_clear(d);
10047 return v;
10050 /* Check that the domain space of "aff" is equal to "space".
10052 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
10053 __isl_keep isl_space *space)
10055 isl_bool ok;
10057 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
10058 if (ok < 0)
10059 return isl_stat_error;
10060 if (!ok)
10061 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
10062 "incompatible spaces", return isl_stat_error);
10063 return isl_stat_ok;
10066 /* Evaluate the affine function "aff" in "pnt".
10068 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
10069 __isl_take isl_point *pnt)
10071 isl_bool is_void;
10072 isl_val *v;
10073 isl_local_space *ls;
10075 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
10076 goto error;
10077 is_void = isl_point_is_void(pnt);
10078 if (is_void < 0)
10079 goto error;
10080 if (is_void)
10081 return eval_void(aff, pnt);
10083 ls = isl_aff_get_domain_local_space(aff);
10084 pnt = isl_local_space_lift_point(ls, pnt);
10086 v = eval(aff->v, isl_point_peek_vec(pnt));
10088 isl_aff_free(aff);
10089 isl_point_free(pnt);
10091 return v;
10092 error:
10093 isl_aff_free(aff);
10094 isl_point_free(pnt);
10095 return NULL;