add exported isl_aff_domain_reverse
[isl.git] / isl_aff.c
blob937abb66282ca82e32de088af0cdff6b455ec4d7
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018,2020 Cerebras Systems
8 * Copyright 2021 Sven Verdoolaege
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
14 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
15 * 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_union_map_private.h>
26 #include <isl_aff_private.h>
27 #include <isl_space_private.h>
28 #include <isl_local_space_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_mat_private.h>
31 #include <isl_id_private.h>
32 #include <isl/constraint.h>
33 #include <isl_seq.h>
34 #include <isl/set.h>
35 #include <isl_val_private.h>
36 #include <isl_point_private.h>
37 #include <isl_config.h>
39 #undef EL_BASE
40 #define EL_BASE aff
42 #include <isl_list_templ.c>
43 #include <isl_list_read_templ.c>
45 #undef EL_BASE
46 #define EL_BASE pw_aff
48 #include <isl_list_templ.c>
49 #include <isl_list_read_templ.c>
51 #undef EL_BASE
52 #define EL_BASE pw_multi_aff
54 #include <isl_list_templ.c>
55 #include <isl_list_read_templ.c>
57 #undef EL_BASE
58 #define EL_BASE union_pw_aff
60 #include <isl_list_templ.c>
61 #include <isl_list_read_templ.c>
63 #undef EL_BASE
64 #define EL_BASE union_pw_multi_aff
66 #include <isl_list_templ.c>
68 /* Construct an isl_aff from the given domain local space "ls" and
69 * coefficients "v", where the local space is known to be valid
70 * for an affine expression.
72 static __isl_give isl_aff *isl_aff_alloc_vec_validated(
73 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
75 isl_aff *aff;
77 if (!ls || !v)
78 goto error;
80 aff = isl_calloc_type(v->ctx, struct isl_aff);
81 if (!aff)
82 goto error;
84 aff->ref = 1;
85 aff->ls = ls;
86 aff->v = v;
88 return aff;
89 error:
90 isl_local_space_free(ls);
91 isl_vec_free(v);
92 return NULL;
95 /* Construct an isl_aff from the given domain local space "ls" and
96 * coefficients "v".
98 * First check that "ls" is a valid domain local space
99 * for an affine expression.
101 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
102 __isl_take isl_vec *v)
104 isl_ctx *ctx;
106 if (!ls)
107 return NULL;
109 ctx = isl_local_space_get_ctx(ls);
110 if (!isl_local_space_divs_known(ls))
111 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
112 goto error);
113 if (!isl_local_space_is_set(ls))
114 isl_die(ctx, isl_error_invalid,
115 "domain of affine expression should be a set",
116 goto error);
117 return isl_aff_alloc_vec_validated(ls, v);
118 error:
119 isl_local_space_free(ls);
120 isl_vec_free(v);
121 return NULL;
124 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
126 isl_ctx *ctx;
127 isl_vec *v;
128 isl_size total;
130 if (!ls)
131 return NULL;
133 ctx = isl_local_space_get_ctx(ls);
135 total = isl_local_space_dim(ls, isl_dim_all);
136 if (total < 0)
137 goto error;
138 v = isl_vec_alloc(ctx, 1 + 1 + total);
139 return isl_aff_alloc_vec(ls, v);
140 error:
141 isl_local_space_free(ls);
142 return NULL;
145 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
147 if (!aff)
148 return NULL;
150 aff->ref++;
151 return aff;
154 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
156 if (!aff)
157 return NULL;
159 return isl_aff_alloc_vec_validated(isl_local_space_copy(aff->ls),
160 isl_vec_copy(aff->v));
163 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
165 if (!aff)
166 return NULL;
168 if (aff->ref == 1)
169 return aff;
170 aff->ref--;
171 return isl_aff_dup(aff);
174 /* Return a copy of the rational affine expression of "aff".
176 static __isl_give isl_vec *isl_aff_get_rat_aff(__isl_keep isl_aff *aff)
178 if (!aff)
179 return NULL;
180 return isl_vec_copy(aff->v);
183 /* Return the rational affine expression of "aff".
184 * This may be either a copy or the expression itself
185 * if there is only one reference to "aff".
186 * This allows the expression to be modified inplace
187 * if both the "aff" and its expression have only a single reference.
188 * The caller is not allowed to modify "aff" between this call and
189 * a subsequent call to isl_aff_restore_rat_aff.
190 * The only exception is that isl_aff_free can be called instead.
192 static __isl_give isl_vec *isl_aff_take_rat_aff(__isl_keep isl_aff *aff)
194 isl_vec *v;
196 if (!aff)
197 return NULL;
198 if (aff->ref != 1)
199 return isl_aff_get_rat_aff(aff);
200 v = aff->v;
201 aff->v = NULL;
202 return v;
205 /* Set the rational affine expression of "aff" to "v",
206 * where the rational affine expression of "aff" may be missing
207 * due to a preceding call to isl_aff_take_rat_aff.
208 * However, in this case, "aff" only has a single reference and
209 * then the call to isl_aff_cow has no effect.
211 static __isl_give isl_aff *isl_aff_restore_rat_aff(__isl_keep isl_aff *aff,
212 __isl_take isl_vec *v)
214 if (!aff || !v)
215 goto error;
217 if (aff->v == v) {
218 isl_vec_free(v);
219 return aff;
222 aff = isl_aff_cow(aff);
223 if (!aff)
224 goto error;
225 isl_vec_free(aff->v);
226 aff->v = v;
228 return aff;
229 error:
230 isl_aff_free(aff);
231 isl_vec_free(v);
232 return NULL;
235 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
237 isl_aff *aff;
239 aff = isl_aff_alloc(ls);
240 if (!aff)
241 return NULL;
243 isl_int_set_si(aff->v->el[0], 1);
244 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
246 return aff;
249 /* Return an affine expression that is equal to zero on domain space "space".
251 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
253 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
256 /* This function performs the same operation as isl_aff_zero_on_domain_space,
257 * but is considered as a function on an isl_space when exported.
259 __isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
261 return isl_aff_zero_on_domain_space(space);
264 /* Return a piecewise affine expression defined on the specified domain
265 * that is equal to zero.
267 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
269 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
272 /* Change "aff" into a NaN.
274 * Note that this function gets called from isl_aff_nan_on_domain,
275 * so "aff" may not have been initialized yet.
277 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
279 isl_vec *v;
281 v = isl_aff_take_rat_aff(aff);
282 v = isl_vec_clr(v);
283 aff = isl_aff_restore_rat_aff(aff, v);
285 return aff;
288 /* Return an affine expression defined on the specified domain
289 * that represents NaN.
291 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
293 isl_aff *aff;
295 aff = isl_aff_alloc(ls);
296 return isl_aff_set_nan(aff);
299 /* Return an affine expression defined on the specified domain space
300 * that represents NaN.
302 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
304 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
307 /* Return a piecewise affine expression defined on the specified domain space
308 * that represents NaN.
310 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
311 __isl_take isl_space *space)
313 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
316 /* Return a piecewise affine expression defined on the specified domain
317 * that represents NaN.
319 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
321 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
324 /* Return an affine expression that is equal to "val" on
325 * domain local space "ls".
327 * Note that the encoding for the special value NaN
328 * is the same in isl_val and isl_aff, so this does not need
329 * to be treated in any special way.
331 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
332 __isl_take isl_val *val)
334 isl_aff *aff;
336 if (!ls || !val)
337 goto error;
338 if (!isl_val_is_rat(val) && !isl_val_is_nan(val))
339 isl_die(isl_val_get_ctx(val), isl_error_invalid,
340 "expecting rational value or NaN", goto error);
342 aff = isl_aff_alloc(isl_local_space_copy(ls));
343 if (!aff)
344 goto error;
346 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
347 isl_int_set(aff->v->el[1], val->n);
348 isl_int_set(aff->v->el[0], val->d);
350 isl_local_space_free(ls);
351 isl_val_free(val);
352 return aff;
353 error:
354 isl_local_space_free(ls);
355 isl_val_free(val);
356 return NULL;
359 /* Return an affine expression that is equal to "val" on domain space "space".
361 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
362 __isl_take isl_val *val)
364 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
367 /* Return an affine expression that is equal to the specified dimension
368 * in "ls".
370 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
371 enum isl_dim_type type, unsigned pos)
373 isl_space *space;
374 isl_aff *aff;
376 if (!ls)
377 return NULL;
379 space = isl_local_space_get_space(ls);
380 if (!space)
381 goto error;
382 if (isl_space_is_map(space))
383 isl_die(isl_space_get_ctx(space), isl_error_invalid,
384 "expecting (parameter) set space", goto error);
385 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
386 goto error;
388 isl_space_free(space);
389 aff = isl_aff_alloc(ls);
390 if (!aff)
391 return NULL;
393 pos += isl_local_space_offset(aff->ls, type);
395 isl_int_set_si(aff->v->el[0], 1);
396 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
397 isl_int_set_si(aff->v->el[1 + pos], 1);
399 return aff;
400 error:
401 isl_local_space_free(ls);
402 isl_space_free(space);
403 return NULL;
406 /* Return a piecewise affine expression that is equal to
407 * the specified dimension in "ls".
409 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
410 enum isl_dim_type type, unsigned pos)
412 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
415 /* Return an affine expression that is equal to the parameter
416 * in the domain space "space" with identifier "id".
418 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
419 __isl_take isl_space *space, __isl_take isl_id *id)
421 int pos;
422 isl_local_space *ls;
424 if (!space || !id)
425 goto error;
426 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
427 if (pos < 0)
428 isl_die(isl_space_get_ctx(space), isl_error_invalid,
429 "parameter not found in space", goto error);
430 isl_id_free(id);
431 ls = isl_local_space_from_space(space);
432 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
433 error:
434 isl_space_free(space);
435 isl_id_free(id);
436 return NULL;
439 /* This function performs the same operation as
440 * isl_aff_param_on_domain_space_id,
441 * but is considered as a function on an isl_space when exported.
443 __isl_give isl_aff *isl_space_param_aff_on_domain_id(
444 __isl_take isl_space *space, __isl_take isl_id *id)
446 return isl_aff_param_on_domain_space_id(space, id);
449 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
451 if (!aff)
452 return NULL;
454 if (--aff->ref > 0)
455 return NULL;
457 isl_local_space_free(aff->ls);
458 isl_vec_free(aff->v);
460 free(aff);
462 return NULL;
465 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
467 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
470 /* Return a hash value that digests "aff".
472 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
474 uint32_t hash, ls_hash, v_hash;
476 if (!aff)
477 return 0;
479 hash = isl_hash_init();
480 ls_hash = isl_local_space_get_hash(aff->ls);
481 isl_hash_hash(hash, ls_hash);
482 v_hash = isl_vec_get_hash(aff->v);
483 isl_hash_hash(hash, v_hash);
485 return hash;
488 /* Return the domain local space of "aff".
490 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
491 __isl_keep isl_aff *aff)
493 return aff ? aff->ls : NULL;
496 /* Return the number of variables of the given type in the domain of "aff".
498 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
500 isl_local_space *ls;
502 ls = isl_aff_peek_domain_local_space(aff);
503 return isl_local_space_dim(ls, type);
506 /* Externally, an isl_aff has a map space, but internally, the
507 * ls field corresponds to the domain of that space.
509 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
511 if (!aff)
512 return isl_size_error;
513 if (type == isl_dim_out)
514 return 1;
515 if (type == isl_dim_in)
516 type = isl_dim_set;
517 return isl_aff_domain_dim(aff, type);
520 /* Return the offset of the first coefficient of type "type" in
521 * the domain of "aff".
523 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
525 isl_local_space *ls;
527 ls = isl_aff_peek_domain_local_space(aff);
528 return isl_local_space_offset(ls, type);
531 /* Return the position of the dimension of the given type and name
532 * in "aff".
533 * Return -1 if no such dimension can be found.
535 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
536 const char *name)
538 if (!aff)
539 return -1;
540 if (type == isl_dim_out)
541 return -1;
542 if (type == isl_dim_in)
543 type = isl_dim_set;
544 return isl_local_space_find_dim_by_name(aff->ls, type, name);
547 /* Return the domain space of "aff".
549 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
551 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
554 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
556 return isl_space_copy(isl_aff_peek_domain_space(aff));
559 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
561 isl_space *space;
562 if (!aff)
563 return NULL;
564 space = isl_local_space_get_space(aff->ls);
565 space = isl_space_from_domain(space);
566 space = isl_space_add_dims(space, isl_dim_out, 1);
567 return space;
570 /* Return a copy of the domain space of "aff".
572 __isl_give isl_local_space *isl_aff_get_domain_local_space(
573 __isl_keep isl_aff *aff)
575 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
578 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
580 isl_local_space *ls;
581 if (!aff)
582 return NULL;
583 ls = isl_local_space_copy(aff->ls);
584 ls = isl_local_space_from_domain(ls);
585 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
586 return ls;
589 /* Return the local space of the domain of "aff".
590 * This may be either a copy or the local space itself
591 * if there is only one reference to "aff".
592 * This allows the local space to be modified inplace
593 * if both the expression and its local space have only a single reference.
594 * The caller is not allowed to modify "aff" between this call and
595 * a subsequent call to isl_aff_restore_domain_local_space.
596 * The only exception is that isl_aff_free can be called instead.
598 __isl_give isl_local_space *isl_aff_take_domain_local_space(
599 __isl_keep isl_aff *aff)
601 isl_local_space *ls;
603 if (!aff)
604 return NULL;
605 if (aff->ref != 1)
606 return isl_aff_get_domain_local_space(aff);
607 ls = aff->ls;
608 aff->ls = NULL;
609 return ls;
612 /* Set the local space of the domain of "aff" to "ls",
613 * where the local space of "aff" may be missing
614 * due to a preceding call to isl_aff_take_domain_local_space.
615 * However, in this case, "aff" only has a single reference and
616 * then the call to isl_aff_cow has no effect.
618 __isl_give isl_aff *isl_aff_restore_domain_local_space(
619 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
621 if (!aff || !ls)
622 goto error;
624 if (aff->ls == ls) {
625 isl_local_space_free(ls);
626 return aff;
629 aff = isl_aff_cow(aff);
630 if (!aff)
631 goto error;
632 isl_local_space_free(aff->ls);
633 aff->ls = ls;
635 return aff;
636 error:
637 isl_aff_free(aff);
638 isl_local_space_free(ls);
639 return NULL;
642 /* Externally, an isl_aff has a map space, but internally, the
643 * ls field corresponds to the domain of that space.
645 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
646 enum isl_dim_type type, unsigned pos)
648 if (!aff)
649 return NULL;
650 if (type == isl_dim_out)
651 return NULL;
652 if (type == isl_dim_in)
653 type = isl_dim_set;
654 return isl_local_space_get_dim_name(aff->ls, type, pos);
657 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
658 __isl_take isl_space *space)
660 aff = isl_aff_cow(aff);
661 if (!aff || !space)
662 goto error;
664 aff->ls = isl_local_space_reset_space(aff->ls, space);
665 if (!aff->ls)
666 return isl_aff_free(aff);
668 return aff;
669 error:
670 isl_aff_free(aff);
671 isl_space_free(space);
672 return NULL;
675 /* Reset the space of "aff". This function is called from isl_pw_templ.c
676 * and doesn't know if the space of an element object is represented
677 * directly or through its domain. It therefore passes along both.
679 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
680 __isl_take isl_space *space, __isl_take isl_space *domain)
682 isl_space_free(space);
683 return isl_aff_reset_domain_space(aff, domain);
686 /* Reorder the dimensions of the domain of "aff" according
687 * to the given reordering.
689 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
690 __isl_take isl_reordering *r)
692 aff = isl_aff_cow(aff);
693 if (!aff)
694 goto error;
696 r = isl_reordering_extend(r, aff->ls->div->n_row);
697 aff->v = isl_vec_reorder(aff->v, 2, isl_reordering_copy(r));
698 aff->ls = isl_local_space_realign(aff->ls, r);
700 if (!aff->v || !aff->ls)
701 return isl_aff_free(aff);
703 return aff;
704 error:
705 isl_aff_free(aff);
706 isl_reordering_free(r);
707 return NULL;
710 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
711 __isl_take isl_space *model)
713 isl_space *domain_space;
714 isl_bool equal_params;
716 domain_space = isl_aff_peek_domain_space(aff);
717 equal_params = isl_space_has_equal_params(domain_space, model);
718 if (equal_params < 0)
719 goto error;
720 if (!equal_params) {
721 isl_reordering *exp;
723 exp = isl_parameter_alignment_reordering(domain_space, model);
724 aff = isl_aff_realign_domain(aff, exp);
727 isl_space_free(model);
728 return aff;
729 error:
730 isl_space_free(model);
731 isl_aff_free(aff);
732 return NULL;
735 #undef TYPE
736 #define TYPE isl_aff
737 #include "isl_unbind_params_templ.c"
739 /* Is "aff" obviously equal to zero?
741 * If the denominator is zero, then "aff" is not equal to zero.
743 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
745 int pos;
747 if (!aff)
748 return isl_bool_error;
750 if (isl_int_is_zero(aff->v->el[0]))
751 return isl_bool_false;
752 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
753 return isl_bool_ok(pos < 0);
756 /* Does "aff" represent NaN?
758 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
760 if (!aff)
761 return isl_bool_error;
763 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
766 /* Are "aff1" and "aff2" obviously equal?
768 * NaN is not equal to anything, not even to another NaN.
770 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
771 __isl_keep isl_aff *aff2)
773 isl_bool equal;
775 if (!aff1 || !aff2)
776 return isl_bool_error;
778 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
779 return isl_bool_false;
781 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
782 if (equal < 0 || !equal)
783 return equal;
785 return isl_vec_is_equal(aff1->v, aff2->v);
788 /* Return the common denominator of "aff" in "v".
790 * We cannot return anything meaningful in case of a NaN.
792 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
794 if (!aff)
795 return isl_stat_error;
796 if (isl_aff_is_nan(aff))
797 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
798 "cannot get denominator of NaN", return isl_stat_error);
799 isl_int_set(*v, aff->v->el[0]);
800 return isl_stat_ok;
803 /* Return the common denominator of "aff".
805 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
807 isl_ctx *ctx;
809 if (!aff)
810 return NULL;
812 ctx = isl_aff_get_ctx(aff);
813 if (isl_aff_is_nan(aff))
814 return isl_val_nan(ctx);
815 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
818 /* Return the constant term of "aff".
820 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
822 isl_ctx *ctx;
823 isl_val *v;
825 if (!aff)
826 return NULL;
828 ctx = isl_aff_get_ctx(aff);
829 if (isl_aff_is_nan(aff))
830 return isl_val_nan(ctx);
831 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
832 return isl_val_normalize(v);
835 /* Return the coefficient of the variable of type "type" at position "pos"
836 * of "aff".
838 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
839 enum isl_dim_type type, int pos)
841 isl_ctx *ctx;
842 isl_val *v;
844 if (!aff)
845 return NULL;
847 ctx = isl_aff_get_ctx(aff);
848 if (type == isl_dim_out)
849 isl_die(ctx, isl_error_invalid,
850 "output/set dimension does not have a coefficient",
851 return NULL);
852 if (type == isl_dim_in)
853 type = isl_dim_set;
855 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
856 return NULL;
858 if (isl_aff_is_nan(aff))
859 return isl_val_nan(ctx);
860 pos += isl_local_space_offset(aff->ls, type);
861 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
862 return isl_val_normalize(v);
865 /* Return the sign of the coefficient of the variable of type "type"
866 * at position "pos" of "aff".
868 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
869 int pos)
871 isl_ctx *ctx;
873 if (!aff)
874 return 0;
876 ctx = isl_aff_get_ctx(aff);
877 if (type == isl_dim_out)
878 isl_die(ctx, isl_error_invalid,
879 "output/set dimension does not have a coefficient",
880 return 0);
881 if (type == isl_dim_in)
882 type = isl_dim_set;
884 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
885 return 0;
887 pos += isl_local_space_offset(aff->ls, type);
888 return isl_int_sgn(aff->v->el[1 + pos]);
891 /* Replace the numerator of the constant term of "aff" by "v".
893 * A NaN is unaffected by this operation.
895 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
897 if (!aff)
898 return NULL;
899 if (isl_aff_is_nan(aff))
900 return aff;
901 aff = isl_aff_cow(aff);
902 if (!aff)
903 return NULL;
905 aff->v = isl_vec_cow(aff->v);
906 if (!aff->v)
907 return isl_aff_free(aff);
909 isl_int_set(aff->v->el[1], v);
911 return aff;
914 /* Replace the constant term of "aff" by "v".
916 * A NaN is unaffected by this operation.
918 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
919 __isl_take isl_val *v)
921 if (!aff || !v)
922 goto error;
924 if (isl_aff_is_nan(aff)) {
925 isl_val_free(v);
926 return aff;
929 if (!isl_val_is_rat(v))
930 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
931 "expecting rational value", goto error);
933 if (isl_int_eq(aff->v->el[1], v->n) &&
934 isl_int_eq(aff->v->el[0], v->d)) {
935 isl_val_free(v);
936 return aff;
939 aff = isl_aff_cow(aff);
940 if (!aff)
941 goto error;
942 aff->v = isl_vec_cow(aff->v);
943 if (!aff->v)
944 goto error;
946 if (isl_int_eq(aff->v->el[0], v->d)) {
947 isl_int_set(aff->v->el[1], v->n);
948 } else if (isl_int_is_one(v->d)) {
949 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
950 } else {
951 isl_seq_scale(aff->v->el + 1,
952 aff->v->el + 1, v->d, aff->v->size - 1);
953 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
954 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
955 aff->v = isl_vec_normalize(aff->v);
956 if (!aff->v)
957 goto error;
960 isl_val_free(v);
961 return aff;
962 error:
963 isl_aff_free(aff);
964 isl_val_free(v);
965 return NULL;
968 /* Add "v" to the constant term of "aff".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
974 if (isl_int_is_zero(v))
975 return aff;
977 if (!aff)
978 return NULL;
979 if (isl_aff_is_nan(aff))
980 return aff;
981 aff = isl_aff_cow(aff);
982 if (!aff)
983 return NULL;
985 aff->v = isl_vec_cow(aff->v);
986 if (!aff->v)
987 return isl_aff_free(aff);
989 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
991 return aff;
994 /* Add "v" to the constant term of "aff",
995 * in case "aff" is a rational expression.
997 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
998 __isl_take isl_val *v)
1000 aff = isl_aff_cow(aff);
1001 if (!aff)
1002 goto error;
1004 aff->v = isl_vec_cow(aff->v);
1005 if (!aff->v)
1006 goto error;
1008 if (isl_int_is_one(v->d)) {
1009 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
1010 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1011 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
1012 aff->v = isl_vec_normalize(aff->v);
1013 if (!aff->v)
1014 goto error;
1015 } else {
1016 isl_seq_scale(aff->v->el + 1,
1017 aff->v->el + 1, v->d, aff->v->size - 1);
1018 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
1019 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1020 aff->v = isl_vec_normalize(aff->v);
1021 if (!aff->v)
1022 goto error;
1025 isl_val_free(v);
1026 return aff;
1027 error:
1028 isl_aff_free(aff);
1029 isl_val_free(v);
1030 return NULL;
1033 /* Return the first argument and free the second.
1035 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
1036 __isl_take isl_val *v)
1038 isl_val_free(v);
1039 return aff;
1042 /* Replace the first argument by NaN and free the second argument.
1044 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
1045 __isl_take isl_val *v)
1047 isl_val_free(v);
1048 return isl_aff_set_nan(aff);
1051 /* Add "v" to the constant term of "aff".
1053 * A NaN is unaffected by this operation.
1054 * Conversely, adding a NaN turns "aff" into a NaN.
1056 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
1057 __isl_take isl_val *v)
1059 isl_bool is_nan, is_zero, is_rat;
1061 is_nan = isl_aff_is_nan(aff);
1062 is_zero = isl_val_is_zero(v);
1063 if (is_nan < 0 || is_zero < 0)
1064 goto error;
1065 if (is_nan || is_zero)
1066 return pick_free(aff, v);
1068 is_nan = isl_val_is_nan(v);
1069 is_rat = isl_val_is_rat(v);
1070 if (is_nan < 0 || is_rat < 0)
1071 goto error;
1072 if (is_nan)
1073 return set_nan_free_val(aff, v);
1074 if (!is_rat)
1075 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1076 "expecting rational value or NaN", goto error);
1078 return isl_aff_add_rat_constant_val(aff, v);
1079 error:
1080 isl_aff_free(aff);
1081 isl_val_free(v);
1082 return NULL;
1085 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1087 isl_int t;
1089 isl_int_init(t);
1090 isl_int_set_si(t, v);
1091 aff = isl_aff_add_constant(aff, t);
1092 isl_int_clear(t);
1094 return aff;
1097 /* Add "v" to the numerator of the constant term of "aff".
1099 * A NaN is unaffected by this operation.
1101 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1103 if (isl_int_is_zero(v))
1104 return aff;
1106 if (!aff)
1107 return NULL;
1108 if (isl_aff_is_nan(aff))
1109 return aff;
1110 aff = isl_aff_cow(aff);
1111 if (!aff)
1112 return NULL;
1114 aff->v = isl_vec_cow(aff->v);
1115 if (!aff->v)
1116 return isl_aff_free(aff);
1118 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1120 return aff;
1123 /* Add "v" to the numerator of the constant term of "aff".
1125 * A NaN is unaffected by this operation.
1127 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1129 isl_int t;
1131 if (v == 0)
1132 return aff;
1134 isl_int_init(t);
1135 isl_int_set_si(t, v);
1136 aff = isl_aff_add_constant_num(aff, t);
1137 isl_int_clear(t);
1139 return aff;
1142 /* Replace the numerator of the constant term of "aff" by "v".
1144 * A NaN is unaffected by this operation.
1146 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1148 if (!aff)
1149 return NULL;
1150 if (isl_aff_is_nan(aff))
1151 return aff;
1152 aff = isl_aff_cow(aff);
1153 if (!aff)
1154 return NULL;
1156 aff->v = isl_vec_cow(aff->v);
1157 if (!aff->v)
1158 return isl_aff_free(aff);
1160 isl_int_set_si(aff->v->el[1], v);
1162 return aff;
1165 /* Replace the numerator of the coefficient of the variable of type "type"
1166 * at position "pos" of "aff" by "v".
1168 * A NaN is unaffected by this operation.
1170 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1171 enum isl_dim_type type, int pos, isl_int v)
1173 if (!aff)
1174 return NULL;
1176 if (type == isl_dim_out)
1177 isl_die(aff->v->ctx, isl_error_invalid,
1178 "output/set dimension does not have a coefficient",
1179 return isl_aff_free(aff));
1180 if (type == isl_dim_in)
1181 type = isl_dim_set;
1183 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1184 return isl_aff_free(aff);
1186 if (isl_aff_is_nan(aff))
1187 return aff;
1188 aff = isl_aff_cow(aff);
1189 if (!aff)
1190 return NULL;
1192 aff->v = isl_vec_cow(aff->v);
1193 if (!aff->v)
1194 return isl_aff_free(aff);
1196 pos += isl_local_space_offset(aff->ls, type);
1197 isl_int_set(aff->v->el[1 + pos], v);
1199 return aff;
1202 /* Replace the numerator of the coefficient of the variable of type "type"
1203 * at position "pos" of "aff" by "v".
1205 * A NaN is unaffected by this operation.
1207 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1208 enum isl_dim_type type, int pos, int v)
1210 if (!aff)
1211 return NULL;
1213 if (type == isl_dim_out)
1214 isl_die(aff->v->ctx, isl_error_invalid,
1215 "output/set dimension does not have a coefficient",
1216 return isl_aff_free(aff));
1217 if (type == isl_dim_in)
1218 type = isl_dim_set;
1220 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1221 return isl_aff_free(aff);
1223 if (isl_aff_is_nan(aff))
1224 return aff;
1225 pos += isl_local_space_offset(aff->ls, type);
1226 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1227 return aff;
1229 aff = isl_aff_cow(aff);
1230 if (!aff)
1231 return NULL;
1233 aff->v = isl_vec_cow(aff->v);
1234 if (!aff->v)
1235 return isl_aff_free(aff);
1237 isl_int_set_si(aff->v->el[1 + pos], v);
1239 return aff;
1242 /* Replace the coefficient of the variable of type "type" at position "pos"
1243 * of "aff" by "v".
1245 * A NaN is unaffected by this operation.
1247 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1248 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1250 if (!aff || !v)
1251 goto error;
1253 if (type == isl_dim_out)
1254 isl_die(aff->v->ctx, isl_error_invalid,
1255 "output/set dimension does not have a coefficient",
1256 goto error);
1257 if (type == isl_dim_in)
1258 type = isl_dim_set;
1260 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1261 return isl_aff_free(aff);
1263 if (isl_aff_is_nan(aff)) {
1264 isl_val_free(v);
1265 return aff;
1267 if (!isl_val_is_rat(v))
1268 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1269 "expecting rational value", goto error);
1271 pos += isl_local_space_offset(aff->ls, type);
1272 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1273 isl_int_eq(aff->v->el[0], v->d)) {
1274 isl_val_free(v);
1275 return aff;
1278 aff = isl_aff_cow(aff);
1279 if (!aff)
1280 goto error;
1281 aff->v = isl_vec_cow(aff->v);
1282 if (!aff->v)
1283 goto error;
1285 if (isl_int_eq(aff->v->el[0], v->d)) {
1286 isl_int_set(aff->v->el[1 + pos], v->n);
1287 } else if (isl_int_is_one(v->d)) {
1288 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1289 } else {
1290 isl_seq_scale(aff->v->el + 1,
1291 aff->v->el + 1, v->d, aff->v->size - 1);
1292 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1293 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1294 aff->v = isl_vec_normalize(aff->v);
1295 if (!aff->v)
1296 goto error;
1299 isl_val_free(v);
1300 return aff;
1301 error:
1302 isl_aff_free(aff);
1303 isl_val_free(v);
1304 return NULL;
1307 /* Add "v" to the coefficient of the variable of type "type"
1308 * at position "pos" of "aff".
1310 * A NaN is unaffected by this operation.
1312 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1313 enum isl_dim_type type, int pos, isl_int v)
1315 if (!aff)
1316 return NULL;
1318 if (type == isl_dim_out)
1319 isl_die(aff->v->ctx, isl_error_invalid,
1320 "output/set dimension does not have a coefficient",
1321 return isl_aff_free(aff));
1322 if (type == isl_dim_in)
1323 type = isl_dim_set;
1325 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1326 return isl_aff_free(aff);
1328 if (isl_aff_is_nan(aff))
1329 return aff;
1330 aff = isl_aff_cow(aff);
1331 if (!aff)
1332 return NULL;
1334 aff->v = isl_vec_cow(aff->v);
1335 if (!aff->v)
1336 return isl_aff_free(aff);
1338 pos += isl_local_space_offset(aff->ls, type);
1339 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1341 return aff;
1344 /* Add "v" to the coefficient of the variable of type "type"
1345 * at position "pos" of "aff".
1347 * A NaN is unaffected by this operation.
1349 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1350 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1352 if (!aff || !v)
1353 goto error;
1355 if (isl_val_is_zero(v)) {
1356 isl_val_free(v);
1357 return aff;
1360 if (type == isl_dim_out)
1361 isl_die(aff->v->ctx, isl_error_invalid,
1362 "output/set dimension does not have a coefficient",
1363 goto error);
1364 if (type == isl_dim_in)
1365 type = isl_dim_set;
1367 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1368 goto error;
1370 if (isl_aff_is_nan(aff)) {
1371 isl_val_free(v);
1372 return aff;
1374 if (!isl_val_is_rat(v))
1375 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1376 "expecting rational value", goto error);
1378 aff = isl_aff_cow(aff);
1379 if (!aff)
1380 goto error;
1382 aff->v = isl_vec_cow(aff->v);
1383 if (!aff->v)
1384 goto error;
1386 pos += isl_local_space_offset(aff->ls, type);
1387 if (isl_int_is_one(v->d)) {
1388 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1389 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1390 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1391 aff->v = isl_vec_normalize(aff->v);
1392 if (!aff->v)
1393 goto error;
1394 } else {
1395 isl_seq_scale(aff->v->el + 1,
1396 aff->v->el + 1, v->d, aff->v->size - 1);
1397 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1398 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1399 aff->v = isl_vec_normalize(aff->v);
1400 if (!aff->v)
1401 goto error;
1404 isl_val_free(v);
1405 return aff;
1406 error:
1407 isl_aff_free(aff);
1408 isl_val_free(v);
1409 return NULL;
1412 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1413 enum isl_dim_type type, int pos, int v)
1415 isl_int t;
1417 isl_int_init(t);
1418 isl_int_set_si(t, v);
1419 aff = isl_aff_add_coefficient(aff, type, pos, t);
1420 isl_int_clear(t);
1422 return aff;
1425 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1427 if (!aff)
1428 return NULL;
1430 return isl_local_space_get_div(aff->ls, pos);
1433 /* Return the negation of "aff".
1435 * As a special case, -NaN = NaN.
1437 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1439 if (!aff)
1440 return NULL;
1441 if (isl_aff_is_nan(aff))
1442 return aff;
1443 aff = isl_aff_cow(aff);
1444 if (!aff)
1445 return NULL;
1446 aff->v = isl_vec_cow(aff->v);
1447 if (!aff->v)
1448 return isl_aff_free(aff);
1450 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1452 return aff;
1455 /* Remove divs from the local space that do not appear in the affine
1456 * expression.
1457 * We currently only remove divs at the end.
1458 * Some intermediate divs may also not appear directly in the affine
1459 * expression, but we would also need to check that no other divs are
1460 * defined in terms of them.
1462 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1464 int pos;
1465 isl_size off;
1466 isl_size n;
1468 n = isl_aff_domain_dim(aff, isl_dim_div);
1469 off = isl_aff_domain_offset(aff, isl_dim_div);
1470 if (n < 0 || off < 0)
1471 return isl_aff_free(aff);
1473 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1474 if (pos == n)
1475 return aff;
1477 aff = isl_aff_cow(aff);
1478 if (!aff)
1479 return NULL;
1481 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1482 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1483 if (!aff->ls || !aff->v)
1484 return isl_aff_free(aff);
1486 return aff;
1489 /* Look for any divs in the aff->ls with a denominator equal to one
1490 * and plug them into the affine expression and any subsequent divs
1491 * that may reference the div.
1493 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1495 int i;
1496 isl_size n;
1497 int len;
1498 isl_int v;
1499 isl_vec *vec;
1500 isl_local_space *ls;
1501 isl_size off;
1503 n = isl_aff_domain_dim(aff, isl_dim_div);
1504 off = isl_aff_domain_offset(aff, isl_dim_div);
1505 if (n < 0 || off < 0)
1506 return isl_aff_free(aff);
1507 len = aff->v->size;
1508 for (i = 0; i < n; ++i) {
1509 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1510 continue;
1511 ls = isl_local_space_copy(aff->ls);
1512 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1513 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1514 vec = isl_vec_copy(aff->v);
1515 vec = isl_vec_cow(vec);
1516 if (!ls || !vec)
1517 goto error;
1519 isl_int_init(v);
1521 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1522 len, len, v);
1524 isl_int_clear(v);
1526 isl_vec_free(aff->v);
1527 aff->v = vec;
1528 isl_local_space_free(aff->ls);
1529 aff->ls = ls;
1532 return aff;
1533 error:
1534 isl_vec_free(vec);
1535 isl_local_space_free(ls);
1536 return isl_aff_free(aff);
1539 /* Look for any divs j that appear with a unit coefficient inside
1540 * the definitions of other divs i and plug them into the definitions
1541 * of the divs i.
1543 * In particular, an expression of the form
1545 * floor((f(..) + floor(g(..)/n))/m)
1547 * is simplified to
1549 * floor((n * f(..) + g(..))/(n * m))
1551 * This simplification is correct because we can move the expression
1552 * f(..) into the inner floor in the original expression to obtain
1554 * floor(floor((n * f(..) + g(..))/n)/m)
1556 * from which we can derive the simplified expression.
1558 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1560 int i, j;
1561 isl_size n;
1562 isl_size off;
1564 n = isl_aff_domain_dim(aff, isl_dim_div);
1565 off = isl_aff_domain_offset(aff, isl_dim_div);
1566 if (n < 0 || off < 0)
1567 return isl_aff_free(aff);
1568 for (i = 1; i < n; ++i) {
1569 for (j = 0; j < i; ++j) {
1570 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1571 continue;
1572 aff->ls = isl_local_space_substitute_seq(aff->ls,
1573 isl_dim_div, j, aff->ls->div->row[j],
1574 aff->v->size, i, 1);
1575 if (!aff->ls)
1576 return isl_aff_free(aff);
1580 return aff;
1583 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1585 * Even though this function is only called on isl_affs with a single
1586 * reference, we are careful to only change aff->v and aff->ls together.
1588 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1590 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1591 isl_local_space *ls;
1592 isl_vec *v;
1594 if (off < 0)
1595 return isl_aff_free(aff);
1597 ls = isl_local_space_copy(aff->ls);
1598 ls = isl_local_space_swap_div(ls, a, b);
1599 v = isl_vec_copy(aff->v);
1600 v = isl_vec_cow(v);
1601 if (!ls || !v)
1602 goto error;
1604 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1605 isl_vec_free(aff->v);
1606 aff->v = v;
1607 isl_local_space_free(aff->ls);
1608 aff->ls = ls;
1610 return aff;
1611 error:
1612 isl_vec_free(v);
1613 isl_local_space_free(ls);
1614 return isl_aff_free(aff);
1617 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1619 * We currently do not actually remove div "b", but simply add its
1620 * coefficient to that of "a" and then zero it out.
1622 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1624 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1626 if (off < 0)
1627 return isl_aff_free(aff);
1629 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1630 return aff;
1632 aff->v = isl_vec_cow(aff->v);
1633 if (!aff->v)
1634 return isl_aff_free(aff);
1636 isl_int_add(aff->v->el[1 + off + a],
1637 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1638 isl_int_set_si(aff->v->el[1 + off + b], 0);
1640 return aff;
1643 /* Sort the divs in the local space of "aff" according to
1644 * the comparison function "cmp_row" in isl_local_space.c,
1645 * combining the coefficients of identical divs.
1647 * Reordering divs does not change the semantics of "aff",
1648 * so there is no need to call isl_aff_cow.
1649 * Moreover, this function is currently only called on isl_affs
1650 * with a single reference.
1652 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1654 isl_size n;
1655 int i, j;
1657 n = isl_aff_dim(aff, isl_dim_div);
1658 if (n < 0)
1659 return isl_aff_free(aff);
1660 for (i = 1; i < n; ++i) {
1661 for (j = i - 1; j >= 0; --j) {
1662 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1663 if (cmp < 0)
1664 break;
1665 if (cmp == 0)
1666 aff = merge_divs(aff, j, j + 1);
1667 else
1668 aff = swap_div(aff, j, j + 1);
1669 if (!aff)
1670 return NULL;
1674 return aff;
1677 /* Normalize the representation of "aff".
1679 * This function should only be called on "new" isl_affs, i.e.,
1680 * with only a single reference. We therefore do not need to
1681 * worry about affecting other instances.
1683 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1685 if (!aff)
1686 return NULL;
1687 aff->v = isl_vec_normalize(aff->v);
1688 if (!aff->v)
1689 return isl_aff_free(aff);
1690 aff = plug_in_integral_divs(aff);
1691 aff = plug_in_unit_divs(aff);
1692 aff = sort_divs(aff);
1693 aff = isl_aff_remove_unused_divs(aff);
1694 return aff;
1697 /* Given f, return floor(f).
1698 * If f is an integer expression, then just return f.
1699 * If f is a constant, then return the constant floor(f).
1700 * Otherwise, if f = g/m, write g = q m + r,
1701 * create a new div d = [r/m] and return the expression q + d.
1702 * The coefficients in r are taken to lie between -m/2 and m/2.
1704 * reduce_div_coefficients performs the same normalization.
1706 * As a special case, floor(NaN) = NaN.
1708 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1710 int i;
1711 int size;
1712 isl_ctx *ctx;
1713 isl_vec *div;
1715 if (!aff)
1716 return NULL;
1718 if (isl_aff_is_nan(aff))
1719 return aff;
1720 if (isl_int_is_one(aff->v->el[0]))
1721 return aff;
1723 aff = isl_aff_cow(aff);
1724 if (!aff)
1725 return NULL;
1727 aff->v = isl_vec_cow(aff->v);
1728 if (!aff->v)
1729 return isl_aff_free(aff);
1731 if (isl_aff_is_cst(aff)) {
1732 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1733 isl_int_set_si(aff->v->el[0], 1);
1734 return aff;
1737 div = isl_vec_copy(aff->v);
1738 div = isl_vec_cow(div);
1739 if (!div)
1740 return isl_aff_free(aff);
1742 ctx = isl_aff_get_ctx(aff);
1743 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1744 for (i = 1; i < aff->v->size; ++i) {
1745 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1746 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1747 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1748 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1749 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1753 aff->ls = isl_local_space_add_div(aff->ls, div);
1754 if (!aff->ls)
1755 return isl_aff_free(aff);
1757 size = aff->v->size;
1758 aff->v = isl_vec_extend(aff->v, size + 1);
1759 if (!aff->v)
1760 return isl_aff_free(aff);
1761 isl_int_set_si(aff->v->el[0], 1);
1762 isl_int_set_si(aff->v->el[size], 1);
1764 aff = isl_aff_normalize(aff);
1766 return aff;
1769 /* Compute
1771 * aff mod m = aff - m * floor(aff/m)
1773 * with m an integer value.
1775 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1776 __isl_take isl_val *m)
1778 isl_aff *res;
1780 if (!aff || !m)
1781 goto error;
1783 if (!isl_val_is_int(m))
1784 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1785 "expecting integer modulo", goto error);
1787 res = isl_aff_copy(aff);
1788 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1789 aff = isl_aff_floor(aff);
1790 aff = isl_aff_scale_val(aff, m);
1791 res = isl_aff_sub(res, aff);
1793 return res;
1794 error:
1795 isl_aff_free(aff);
1796 isl_val_free(m);
1797 return NULL;
1800 /* Compute
1802 * pwaff mod m = pwaff - m * floor(pwaff/m)
1804 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1806 isl_pw_aff *res;
1808 res = isl_pw_aff_copy(pwaff);
1809 pwaff = isl_pw_aff_scale_down(pwaff, m);
1810 pwaff = isl_pw_aff_floor(pwaff);
1811 pwaff = isl_pw_aff_scale(pwaff, m);
1812 res = isl_pw_aff_sub(res, pwaff);
1814 return res;
1817 /* Compute
1819 * pa mod m = pa - m * floor(pa/m)
1821 * with m an integer value.
1823 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1824 __isl_take isl_val *m)
1826 if (!pa || !m)
1827 goto error;
1828 if (!isl_val_is_int(m))
1829 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1830 "expecting integer modulo", goto error);
1831 pa = isl_pw_aff_mod(pa, m->n);
1832 isl_val_free(m);
1833 return pa;
1834 error:
1835 isl_pw_aff_free(pa);
1836 isl_val_free(m);
1837 return NULL;
1840 /* Given f, return ceil(f).
1841 * If f is an integer expression, then just return f.
1842 * Otherwise, let f be the expression
1844 * e/m
1846 * then return
1848 * floor((e + m - 1)/m)
1850 * As a special case, ceil(NaN) = NaN.
1852 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1854 if (!aff)
1855 return NULL;
1857 if (isl_aff_is_nan(aff))
1858 return aff;
1859 if (isl_int_is_one(aff->v->el[0]))
1860 return aff;
1862 aff = isl_aff_cow(aff);
1863 if (!aff)
1864 return NULL;
1865 aff->v = isl_vec_cow(aff->v);
1866 if (!aff->v)
1867 return isl_aff_free(aff);
1869 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1870 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1871 aff = isl_aff_floor(aff);
1873 return aff;
1876 /* Apply the expansion computed by isl_merge_divs.
1877 * The expansion itself is given by "exp" while the resulting
1878 * list of divs is given by "div".
1880 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1881 __isl_take isl_mat *div, int *exp)
1883 isl_size old_n_div;
1884 isl_size new_n_div;
1885 isl_size offset;
1887 aff = isl_aff_cow(aff);
1889 offset = isl_aff_domain_offset(aff, isl_dim_div);
1890 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1891 new_n_div = isl_mat_rows(div);
1892 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1893 goto error;
1895 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1896 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1897 if (!aff->v || !aff->ls)
1898 return isl_aff_free(aff);
1899 return aff;
1900 error:
1901 isl_aff_free(aff);
1902 isl_mat_free(div);
1903 return NULL;
1906 /* Add two affine expressions that live in the same local space.
1908 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1909 __isl_take isl_aff *aff2)
1911 isl_int gcd, f;
1913 aff1 = isl_aff_cow(aff1);
1914 if (!aff1 || !aff2)
1915 goto error;
1917 aff1->v = isl_vec_cow(aff1->v);
1918 if (!aff1->v)
1919 goto error;
1921 isl_int_init(gcd);
1922 isl_int_init(f);
1923 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1924 isl_int_divexact(f, aff2->v->el[0], gcd);
1925 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1926 isl_int_divexact(f, aff1->v->el[0], gcd);
1927 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1928 isl_int_divexact(f, aff2->v->el[0], gcd);
1929 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1930 isl_int_clear(f);
1931 isl_int_clear(gcd);
1933 isl_aff_free(aff2);
1934 aff1 = isl_aff_normalize(aff1);
1935 return aff1;
1936 error:
1937 isl_aff_free(aff1);
1938 isl_aff_free(aff2);
1939 return NULL;
1942 /* Replace one of the arguments by a NaN and free the other one.
1944 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1945 __isl_take isl_aff *aff2)
1947 isl_aff_free(aff2);
1948 return isl_aff_set_nan(aff1);
1951 /* Return the sum of "aff1" and "aff2".
1953 * If either of the two is NaN, then the result is NaN.
1955 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1956 __isl_take isl_aff *aff2)
1958 isl_ctx *ctx;
1959 int *exp1 = NULL;
1960 int *exp2 = NULL;
1961 isl_mat *div;
1962 isl_size n_div1, n_div2;
1964 if (!aff1 || !aff2)
1965 goto error;
1967 ctx = isl_aff_get_ctx(aff1);
1968 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1969 isl_die(ctx, isl_error_invalid,
1970 "spaces don't match", goto error);
1972 if (isl_aff_is_nan(aff1)) {
1973 isl_aff_free(aff2);
1974 return aff1;
1976 if (isl_aff_is_nan(aff2)) {
1977 isl_aff_free(aff1);
1978 return aff2;
1981 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1982 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1983 if (n_div1 < 0 || n_div2 < 0)
1984 goto error;
1985 if (n_div1 == 0 && n_div2 == 0)
1986 return add_expanded(aff1, aff2);
1988 exp1 = isl_alloc_array(ctx, int, n_div1);
1989 exp2 = isl_alloc_array(ctx, int, n_div2);
1990 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1991 goto error;
1993 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1994 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1995 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1996 free(exp1);
1997 free(exp2);
1999 return add_expanded(aff1, aff2);
2000 error:
2001 free(exp1);
2002 free(exp2);
2003 isl_aff_free(aff1);
2004 isl_aff_free(aff2);
2005 return NULL;
2008 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
2009 __isl_take isl_aff *aff2)
2011 return isl_aff_add(aff1, isl_aff_neg(aff2));
2014 /* Return the result of scaling "aff" by a factor of "f".
2016 * As a special case, f * NaN = NaN.
2018 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
2020 isl_int gcd;
2022 if (!aff)
2023 return NULL;
2024 if (isl_aff_is_nan(aff))
2025 return aff;
2027 if (isl_int_is_one(f))
2028 return aff;
2030 aff = isl_aff_cow(aff);
2031 if (!aff)
2032 return NULL;
2033 aff->v = isl_vec_cow(aff->v);
2034 if (!aff->v)
2035 return isl_aff_free(aff);
2037 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
2038 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
2039 return aff;
2042 isl_int_init(gcd);
2043 isl_int_gcd(gcd, aff->v->el[0], f);
2044 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
2045 isl_int_divexact(gcd, f, gcd);
2046 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2047 isl_int_clear(gcd);
2049 return aff;
2052 /* Multiple "aff" by "v".
2054 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
2055 __isl_take isl_val *v)
2057 if (!aff || !v)
2058 goto error;
2060 if (isl_val_is_one(v)) {
2061 isl_val_free(v);
2062 return aff;
2065 if (!isl_val_is_rat(v))
2066 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2067 "expecting rational factor", goto error);
2069 aff = isl_aff_scale(aff, v->n);
2070 aff = isl_aff_scale_down(aff, v->d);
2072 isl_val_free(v);
2073 return aff;
2074 error:
2075 isl_aff_free(aff);
2076 isl_val_free(v);
2077 return NULL;
2080 /* Return the result of scaling "aff" down by a factor of "f".
2082 * As a special case, NaN/f = NaN.
2084 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2086 isl_int gcd;
2088 if (!aff)
2089 return NULL;
2090 if (isl_aff_is_nan(aff))
2091 return aff;
2093 if (isl_int_is_one(f))
2094 return aff;
2096 aff = isl_aff_cow(aff);
2097 if (!aff)
2098 return NULL;
2100 if (isl_int_is_zero(f))
2101 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2102 "cannot scale down by zero", return isl_aff_free(aff));
2104 aff->v = isl_vec_cow(aff->v);
2105 if (!aff->v)
2106 return isl_aff_free(aff);
2108 isl_int_init(gcd);
2109 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2110 isl_int_gcd(gcd, gcd, f);
2111 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2112 isl_int_divexact(gcd, f, gcd);
2113 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2114 isl_int_clear(gcd);
2116 return aff;
2119 /* Divide "aff" by "v".
2121 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2122 __isl_take isl_val *v)
2124 if (!aff || !v)
2125 goto error;
2127 if (isl_val_is_one(v)) {
2128 isl_val_free(v);
2129 return aff;
2132 if (!isl_val_is_rat(v))
2133 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2134 "expecting rational factor", goto error);
2135 if (!isl_val_is_pos(v))
2136 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2137 "factor needs to be positive", goto error);
2139 aff = isl_aff_scale(aff, v->d);
2140 aff = isl_aff_scale_down(aff, v->n);
2142 isl_val_free(v);
2143 return aff;
2144 error:
2145 isl_aff_free(aff);
2146 isl_val_free(v);
2147 return NULL;
2150 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2152 isl_int v;
2154 if (f == 1)
2155 return aff;
2157 isl_int_init(v);
2158 isl_int_set_ui(v, f);
2159 aff = isl_aff_scale_down(aff, v);
2160 isl_int_clear(v);
2162 return aff;
2165 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2166 enum isl_dim_type type, unsigned pos, const char *s)
2168 aff = isl_aff_cow(aff);
2169 if (!aff)
2170 return NULL;
2171 if (type == isl_dim_out)
2172 isl_die(aff->v->ctx, isl_error_invalid,
2173 "cannot set name of output/set dimension",
2174 return isl_aff_free(aff));
2175 if (type == isl_dim_in)
2176 type = isl_dim_set;
2177 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2178 if (!aff->ls)
2179 return isl_aff_free(aff);
2181 return aff;
2184 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2185 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2187 aff = isl_aff_cow(aff);
2188 if (!aff)
2189 goto error;
2190 if (type == isl_dim_out)
2191 isl_die(aff->v->ctx, isl_error_invalid,
2192 "cannot set name of output/set dimension",
2193 goto error);
2194 if (type == isl_dim_in)
2195 type = isl_dim_set;
2196 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2197 if (!aff->ls)
2198 return isl_aff_free(aff);
2200 return aff;
2201 error:
2202 isl_id_free(id);
2203 isl_aff_free(aff);
2204 return NULL;
2207 /* Replace the identifier of the input tuple of "aff" by "id".
2208 * type is currently required to be equal to isl_dim_in
2210 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2211 enum isl_dim_type type, __isl_take isl_id *id)
2213 aff = isl_aff_cow(aff);
2214 if (!aff)
2215 goto error;
2216 if (type != isl_dim_in)
2217 isl_die(aff->v->ctx, isl_error_invalid,
2218 "cannot only set id of input tuple", goto error);
2219 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2220 if (!aff->ls)
2221 return isl_aff_free(aff);
2223 return aff;
2224 error:
2225 isl_id_free(id);
2226 isl_aff_free(aff);
2227 return NULL;
2230 /* Exploit the equalities in "eq" to simplify the affine expression
2231 * and the expressions of the integer divisions in the local space.
2232 * The integer divisions in this local space are assumed to appear
2233 * as regular dimensions in "eq".
2235 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2236 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2238 int i, j;
2239 unsigned o_div;
2240 unsigned n_div;
2242 if (!eq)
2243 goto error;
2244 if (eq->n_eq == 0) {
2245 isl_basic_set_free(eq);
2246 return aff;
2249 aff = isl_aff_cow(aff);
2250 if (!aff)
2251 goto error;
2253 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2254 isl_basic_set_copy(eq));
2255 aff->v = isl_vec_cow(aff->v);
2256 if (!aff->ls || !aff->v)
2257 goto error;
2259 o_div = isl_basic_set_offset(eq, isl_dim_div);
2260 n_div = eq->n_div;
2261 for (i = 0; i < eq->n_eq; ++i) {
2262 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2263 if (j < 0 || j == 0 || j >= o_div)
2264 continue;
2266 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2267 &aff->v->el[0]);
2270 isl_basic_set_free(eq);
2271 aff = isl_aff_normalize(aff);
2272 return aff;
2273 error:
2274 isl_basic_set_free(eq);
2275 isl_aff_free(aff);
2276 return NULL;
2279 /* Exploit the equalities in "eq" to simplify the affine expression
2280 * and the expressions of the integer divisions in the local space.
2282 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2283 __isl_take isl_basic_set *eq)
2285 isl_size n_div;
2287 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2288 if (n_div < 0)
2289 goto error;
2290 if (n_div > 0)
2291 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2292 return isl_aff_substitute_equalities_lifted(aff, eq);
2293 error:
2294 isl_basic_set_free(eq);
2295 isl_aff_free(aff);
2296 return NULL;
2299 /* Look for equalities among the variables shared by context and aff
2300 * and the integer divisions of aff, if any.
2301 * The equalities are then used to eliminate coefficients and/or integer
2302 * divisions from aff.
2304 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2305 __isl_take isl_set *context)
2307 isl_local_space *ls;
2308 isl_basic_set *hull;
2310 ls = isl_aff_get_domain_local_space(aff);
2311 context = isl_local_space_lift_set(ls, context);
2313 hull = isl_set_affine_hull(context);
2314 return isl_aff_substitute_equalities_lifted(aff, hull);
2317 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2318 __isl_take isl_set *context)
2320 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2321 dom_context = isl_set_intersect_params(dom_context, context);
2322 return isl_aff_gist(aff, dom_context);
2325 /* Return a basic set containing those elements in the space
2326 * of aff where it is positive. "rational" should not be set.
2328 * If "aff" is NaN, then it is not positive.
2330 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2331 int rational, void *user)
2333 isl_constraint *ineq;
2334 isl_basic_set *bset;
2335 isl_val *c;
2337 if (!aff)
2338 return NULL;
2339 if (isl_aff_is_nan(aff)) {
2340 isl_space *space = isl_aff_get_domain_space(aff);
2341 isl_aff_free(aff);
2342 return isl_basic_set_empty(space);
2344 if (rational)
2345 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2346 "rational sets not supported", goto error);
2348 ineq = isl_inequality_from_aff(aff);
2349 c = isl_constraint_get_constant_val(ineq);
2350 c = isl_val_sub_ui(c, 1);
2351 ineq = isl_constraint_set_constant_val(ineq, c);
2353 bset = isl_basic_set_from_constraint(ineq);
2354 bset = isl_basic_set_simplify(bset);
2355 return bset;
2356 error:
2357 isl_aff_free(aff);
2358 return NULL;
2361 /* Return a basic set containing those elements in the space
2362 * of aff where it is non-negative.
2363 * If "rational" is set, then return a rational basic set.
2365 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2367 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2368 __isl_take isl_aff *aff, int rational, void *user)
2370 isl_constraint *ineq;
2371 isl_basic_set *bset;
2373 if (!aff)
2374 return NULL;
2375 if (isl_aff_is_nan(aff)) {
2376 isl_space *space = isl_aff_get_domain_space(aff);
2377 isl_aff_free(aff);
2378 return isl_basic_set_empty(space);
2381 ineq = isl_inequality_from_aff(aff);
2383 bset = isl_basic_set_from_constraint(ineq);
2384 if (rational)
2385 bset = isl_basic_set_set_rational(bset);
2386 bset = isl_basic_set_simplify(bset);
2387 return bset;
2390 /* Return a basic set containing those elements in the space
2391 * of aff where it is non-negative.
2393 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2395 return aff_nonneg_basic_set(aff, 0, NULL);
2398 /* Return a basic set containing those elements in the domain space
2399 * of "aff" where it is positive.
2401 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2403 aff = isl_aff_add_constant_num_si(aff, -1);
2404 return isl_aff_nonneg_basic_set(aff);
2407 /* Return a basic set containing those elements in the domain space
2408 * of aff where it is negative.
2410 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2412 aff = isl_aff_neg(aff);
2413 return isl_aff_pos_basic_set(aff);
2416 /* Return a basic set containing those elements in the space
2417 * of aff where it is zero.
2418 * If "rational" is set, then return a rational basic set.
2420 * If "aff" is NaN, then it is not zero.
2422 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2423 int rational, void *user)
2425 isl_constraint *ineq;
2426 isl_basic_set *bset;
2428 if (!aff)
2429 return NULL;
2430 if (isl_aff_is_nan(aff)) {
2431 isl_space *space = isl_aff_get_domain_space(aff);
2432 isl_aff_free(aff);
2433 return isl_basic_set_empty(space);
2436 ineq = isl_equality_from_aff(aff);
2438 bset = isl_basic_set_from_constraint(ineq);
2439 if (rational)
2440 bset = isl_basic_set_set_rational(bset);
2441 bset = isl_basic_set_simplify(bset);
2442 return bset;
2445 /* Return a basic set containing those elements in the space
2446 * of aff where it is zero.
2448 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2450 return aff_zero_basic_set(aff, 0, NULL);
2453 /* Return a basic set containing those elements in the shared space
2454 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2456 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2457 __isl_take isl_aff *aff2)
2459 aff1 = isl_aff_sub(aff1, aff2);
2461 return isl_aff_nonneg_basic_set(aff1);
2464 /* Return a basic set containing those elements in the shared domain space
2465 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2467 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2468 __isl_take isl_aff *aff2)
2470 aff1 = isl_aff_sub(aff1, aff2);
2472 return isl_aff_pos_basic_set(aff1);
2475 /* Return a set containing those elements in the shared space
2476 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2478 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2479 __isl_take isl_aff *aff2)
2481 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2484 /* Return a set containing those elements in the shared domain space
2485 * of aff1 and aff2 where aff1 is greater than aff2.
2487 * If either of the two inputs is NaN, then the result is empty,
2488 * as comparisons with NaN always return false.
2490 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2491 __isl_take isl_aff *aff2)
2493 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2496 /* Return a basic set containing those elements in the shared space
2497 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2499 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2500 __isl_take isl_aff *aff2)
2502 return isl_aff_ge_basic_set(aff2, aff1);
2505 /* Return a basic set containing those elements in the shared domain space
2506 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2508 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2509 __isl_take isl_aff *aff2)
2511 return isl_aff_gt_basic_set(aff2, aff1);
2514 /* Return a set containing those elements in the shared space
2515 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2517 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2518 __isl_take isl_aff *aff2)
2520 return isl_aff_ge_set(aff2, aff1);
2523 /* Return a set containing those elements in the shared domain space
2524 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2526 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2527 __isl_take isl_aff *aff2)
2529 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2532 /* Return a basic set containing those elements in the shared space
2533 * of aff1 and aff2 where aff1 and aff2 are equal.
2535 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2536 __isl_take isl_aff *aff2)
2538 aff1 = isl_aff_sub(aff1, aff2);
2540 return isl_aff_zero_basic_set(aff1);
2543 /* Return a set containing those elements in the shared space
2544 * of aff1 and aff2 where aff1 and aff2 are equal.
2546 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2547 __isl_take isl_aff *aff2)
2549 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2552 /* Return a set containing those elements in the shared domain space
2553 * of aff1 and aff2 where aff1 and aff2 are not equal.
2555 * If either of the two inputs is NaN, then the result is empty,
2556 * as comparisons with NaN always return false.
2558 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2559 __isl_take isl_aff *aff2)
2561 isl_set *set_lt, *set_gt;
2563 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2564 isl_aff_copy(aff2));
2565 set_gt = isl_aff_gt_set(aff1, aff2);
2566 return isl_set_union_disjoint(set_lt, set_gt);
2569 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2570 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2572 aff1 = isl_aff_add(aff1, aff2);
2573 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2574 return aff1;
2577 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2579 if (!aff)
2580 return isl_bool_error;
2582 return isl_bool_false;
2585 #undef TYPE
2586 #define TYPE isl_aff
2587 static
2588 #include "check_type_range_templ.c"
2590 /* Check whether the given affine expression has non-zero coefficient
2591 * for any dimension in the given range or if any of these dimensions
2592 * appear with non-zero coefficients in any of the integer divisions
2593 * involved in the affine expression.
2595 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2596 enum isl_dim_type type, unsigned first, unsigned n)
2598 int i;
2599 int *active = NULL;
2600 isl_bool involves = isl_bool_false;
2602 if (!aff)
2603 return isl_bool_error;
2604 if (n == 0)
2605 return isl_bool_false;
2606 if (isl_aff_check_range(aff, type, first, n) < 0)
2607 return isl_bool_error;
2609 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2610 if (!active)
2611 goto error;
2613 first += isl_local_space_offset(aff->ls, type) - 1;
2614 for (i = 0; i < n; ++i)
2615 if (active[first + i]) {
2616 involves = isl_bool_true;
2617 break;
2620 free(active);
2622 return involves;
2623 error:
2624 free(active);
2625 return isl_bool_error;
2628 /* Does "aff" involve any local variables, i.e., integer divisions?
2630 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2632 isl_size n;
2634 n = isl_aff_dim(aff, isl_dim_div);
2635 if (n < 0)
2636 return isl_bool_error;
2637 return isl_bool_ok(n > 0);
2640 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2641 enum isl_dim_type type, unsigned first, unsigned n)
2643 if (!aff)
2644 return NULL;
2645 if (type == isl_dim_out)
2646 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2647 "cannot drop output/set dimension",
2648 return isl_aff_free(aff));
2649 if (type == isl_dim_in)
2650 type = isl_dim_set;
2651 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2652 return aff;
2654 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2655 return isl_aff_free(aff);
2657 aff = isl_aff_cow(aff);
2658 if (!aff)
2659 return NULL;
2661 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2662 if (!aff->ls)
2663 return isl_aff_free(aff);
2665 first += 1 + isl_local_space_offset(aff->ls, type);
2666 aff->v = isl_vec_drop_els(aff->v, first, n);
2667 if (!aff->v)
2668 return isl_aff_free(aff);
2670 return aff;
2673 /* Is the domain of "aff" a product?
2675 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2677 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2680 #undef TYPE
2681 #define TYPE isl_aff
2682 #include <isl_domain_factor_templ.c>
2684 /* Project the domain of the affine expression onto its parameter space.
2685 * The affine expression may not involve any of the domain dimensions.
2687 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2689 isl_space *space;
2690 isl_size n;
2692 n = isl_aff_dim(aff, isl_dim_in);
2693 if (n < 0)
2694 return isl_aff_free(aff);
2695 aff = isl_aff_drop_domain(aff, 0, n);
2696 space = isl_aff_get_domain_space(aff);
2697 space = isl_space_params(space);
2698 aff = isl_aff_reset_domain_space(aff, space);
2699 return aff;
2702 /* Convert an affine expression defined over a parameter domain
2703 * into one that is defined over a zero-dimensional set.
2705 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2707 isl_local_space *ls;
2709 ls = isl_aff_take_domain_local_space(aff);
2710 ls = isl_local_space_set_from_params(ls);
2711 aff = isl_aff_restore_domain_local_space(aff, ls);
2713 return aff;
2716 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2717 enum isl_dim_type type, unsigned first, unsigned n)
2719 if (!aff)
2720 return NULL;
2721 if (type == isl_dim_out)
2722 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2723 "cannot insert output/set dimensions",
2724 return isl_aff_free(aff));
2725 if (type == isl_dim_in)
2726 type = isl_dim_set;
2727 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2728 return aff;
2730 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2731 return isl_aff_free(aff);
2733 aff = isl_aff_cow(aff);
2734 if (!aff)
2735 return NULL;
2737 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2738 if (!aff->ls)
2739 return isl_aff_free(aff);
2741 first += 1 + isl_local_space_offset(aff->ls, type);
2742 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2743 if (!aff->v)
2744 return isl_aff_free(aff);
2746 return aff;
2749 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2750 enum isl_dim_type type, unsigned n)
2752 isl_size pos;
2754 pos = isl_aff_dim(aff, type);
2755 if (pos < 0)
2756 return isl_aff_free(aff);
2758 return isl_aff_insert_dims(aff, type, pos, n);
2761 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2762 * to dimensions of "dst_type" at "dst_pos".
2764 * We only support moving input dimensions to parameters and vice versa.
2766 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2767 enum isl_dim_type dst_type, unsigned dst_pos,
2768 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2770 unsigned g_dst_pos;
2771 unsigned g_src_pos;
2772 isl_size src_off, dst_off;
2774 if (!aff)
2775 return NULL;
2776 if (n == 0 &&
2777 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2778 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2779 return aff;
2781 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2782 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2783 "cannot move output/set dimension",
2784 return isl_aff_free(aff));
2785 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2786 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2787 "cannot move divs", return isl_aff_free(aff));
2788 if (dst_type == isl_dim_in)
2789 dst_type = isl_dim_set;
2790 if (src_type == isl_dim_in)
2791 src_type = isl_dim_set;
2793 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2794 return isl_aff_free(aff);
2795 if (dst_type == src_type)
2796 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2797 "moving dims within the same type not supported",
2798 return isl_aff_free(aff));
2800 aff = isl_aff_cow(aff);
2801 src_off = isl_aff_domain_offset(aff, src_type);
2802 dst_off = isl_aff_domain_offset(aff, dst_type);
2803 if (src_off < 0 || dst_off < 0)
2804 return isl_aff_free(aff);
2806 g_src_pos = 1 + src_off + src_pos;
2807 g_dst_pos = 1 + dst_off + dst_pos;
2808 if (dst_type > src_type)
2809 g_dst_pos -= n;
2811 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2812 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2813 src_type, src_pos, n);
2814 if (!aff->v || !aff->ls)
2815 return isl_aff_free(aff);
2817 aff = sort_divs(aff);
2819 return aff;
2822 /* Given an affine function on a domain (A -> B),
2823 * interchange A and B in the wrapped domain
2824 * to obtain a function on the domain (B -> A).
2826 * Since this may change the position of some variables,
2827 * it may also change the normalized order of the local variables.
2828 * Restore this order. Since sort_divs assumes the input
2829 * has a single reference, an explicit isl_aff_cow is required.
2831 __isl_give isl_aff *isl_aff_domain_reverse(__isl_take isl_aff *aff)
2833 isl_space *space;
2834 isl_local_space *ls;
2835 isl_vec *v;
2836 isl_size n_in, n_out;
2837 unsigned offset;
2839 space = isl_aff_peek_domain_space(aff);
2840 offset = isl_space_offset(space, isl_dim_set);
2841 n_in = isl_space_wrapped_dim(space, isl_dim_set, isl_dim_in);
2842 n_out = isl_space_wrapped_dim(space, isl_dim_set, isl_dim_out);
2843 if (n_in < 0 || n_out < 0)
2844 return isl_aff_free(aff);
2846 v = isl_aff_take_rat_aff(aff);
2847 v = isl_vec_move_els(v, 1 + 1 + offset, 1 + 1 + offset + n_in, n_out);
2848 aff = isl_aff_restore_rat_aff(aff, v);
2850 ls = isl_aff_take_domain_local_space(aff);
2851 ls = isl_local_space_wrapped_reverse(ls);
2852 aff = isl_aff_restore_domain_local_space(aff, ls);
2854 aff = isl_aff_cow(aff);
2855 aff = sort_divs(aff);
2857 return aff;
2860 /* Return a zero isl_aff in the given space.
2862 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2863 * interface over all piecewise types.
2865 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2867 isl_local_space *ls;
2869 ls = isl_local_space_from_space(isl_space_domain(space));
2870 return isl_aff_zero_on_domain(ls);
2873 #define isl_aff_involves_nan isl_aff_is_nan
2875 #undef PW
2876 #define PW isl_pw_aff
2877 #undef BASE
2878 #define BASE aff
2879 #undef EL_IS_ZERO
2880 #define EL_IS_ZERO is_empty
2881 #undef ZERO
2882 #define ZERO empty
2883 #undef IS_ZERO
2884 #define IS_ZERO is_empty
2885 #undef FIELD
2886 #define FIELD aff
2887 #undef DEFAULT_IS_ZERO
2888 #define DEFAULT_IS_ZERO 0
2890 #include <isl_pw_templ.c>
2891 #include <isl_pw_un_op_templ.c>
2892 #include <isl_pw_add_constant_val_templ.c>
2893 #include <isl_pw_add_disjoint_templ.c>
2894 #include <isl_pw_bind_domain_templ.c>
2895 #include <isl_pw_eval.c>
2896 #include <isl_pw_hash.c>
2897 #include <isl_pw_fix_templ.c>
2898 #include <isl_pw_from_range_templ.c>
2899 #include <isl_pw_insert_dims_templ.c>
2900 #include <isl_pw_insert_domain_templ.c>
2901 #include <isl_pw_move_dims_templ.c>
2902 #include <isl_pw_neg_templ.c>
2903 #include <isl_pw_pullback_templ.c>
2904 #include <isl_pw_scale_templ.c>
2905 #include <isl_pw_sub_templ.c>
2906 #include <isl_pw_union_opt.c>
2908 #undef BASE
2909 #define BASE pw_aff
2911 #include <isl_union_single.c>
2912 #include <isl_union_neg.c>
2913 #include <isl_union_sub_templ.c>
2915 #undef BASE
2916 #define BASE aff
2918 #include <isl_union_pw_templ.c>
2920 /* Compute a piecewise quasi-affine expression with a domain that
2921 * is the union of those of pwaff1 and pwaff2 and such that on each
2922 * cell, the quasi-affine expression is the maximum of those of pwaff1
2923 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2924 * cell, then the associated expression is the defined one.
2926 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2927 __isl_take isl_pw_aff *pwaff2)
2929 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2930 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2933 /* Compute a piecewise quasi-affine expression with a domain that
2934 * is the union of those of pwaff1 and pwaff2 and such that on each
2935 * cell, the quasi-affine expression is the minimum of those of pwaff1
2936 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2937 * cell, then the associated expression is the defined one.
2939 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2940 __isl_take isl_pw_aff *pwaff2)
2942 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2943 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2946 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2947 __isl_take isl_pw_aff *pwaff2, int max)
2949 if (max)
2950 return isl_pw_aff_union_max(pwaff1, pwaff2);
2951 else
2952 return isl_pw_aff_union_min(pwaff1, pwaff2);
2955 /* Is the domain of "pa" a product?
2957 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2959 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2962 #undef TYPE
2963 #define TYPE isl_pw_aff
2964 #include <isl_domain_factor_templ.c>
2966 /* Return a set containing those elements in the domain
2967 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2968 * does not satisfy "fn" (if complement is 1).
2970 * The pieces with a NaN never belong to the result since
2971 * NaN does not satisfy any property.
2973 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2974 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2975 void *user),
2976 int complement, void *user)
2978 int i;
2979 isl_set *set;
2981 if (!pwaff)
2982 return NULL;
2984 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2986 for (i = 0; i < pwaff->n; ++i) {
2987 isl_basic_set *bset;
2988 isl_set *set_i, *locus;
2989 isl_bool rational;
2991 if (isl_aff_is_nan(pwaff->p[i].aff))
2992 continue;
2994 rational = isl_set_has_rational(pwaff->p[i].set);
2995 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2996 locus = isl_set_from_basic_set(bset);
2997 set_i = isl_set_copy(pwaff->p[i].set);
2998 if (complement)
2999 set_i = isl_set_subtract(set_i, locus);
3000 else
3001 set_i = isl_set_intersect(set_i, locus);
3002 set = isl_set_union_disjoint(set, set_i);
3005 isl_pw_aff_free(pwaff);
3007 return set;
3010 /* Return a set containing those elements in the domain
3011 * of "pa" where it is positive.
3013 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
3015 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
3018 /* Return a set containing those elements in the domain
3019 * of pwaff where it is non-negative.
3021 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
3023 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
3026 /* Return a set containing those elements in the domain
3027 * of pwaff where it is zero.
3029 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
3031 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
3034 /* Return a set containing those elements in the domain
3035 * of pwaff where it is not zero.
3037 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
3039 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
3042 /* Bind the affine function "aff" to the parameter "id",
3043 * returning the elements in the domain where the affine expression
3044 * is equal to the parameter.
3046 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
3047 __isl_take isl_id *id)
3049 isl_space *space;
3050 isl_aff *aff_id;
3052 space = isl_aff_get_domain_space(aff);
3053 space = isl_space_add_param_id(space, isl_id_copy(id));
3055 aff = isl_aff_align_params(aff, isl_space_copy(space));
3056 aff_id = isl_aff_param_on_domain_space_id(space, id);
3058 return isl_aff_eq_basic_set(aff, aff_id);
3061 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
3062 * "rational" should not be set.
3064 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
3065 int rational, void *user)
3067 isl_id *id = user;
3069 if (!aff)
3070 return NULL;
3071 if (rational)
3072 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
3073 "rational binding not supported", goto error);
3074 return isl_aff_bind_id(aff, isl_id_copy(id));
3075 error:
3076 isl_aff_free(aff);
3077 return NULL;
3080 /* Bind the piecewise affine function "pa" to the parameter "id",
3081 * returning the elements in the domain where the expression
3082 * is equal to the parameter.
3084 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
3085 __isl_take isl_id *id)
3087 isl_set *bound;
3089 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
3090 isl_id_free(id);
3092 return bound;
3095 /* Return a set containing those elements in the shared domain
3096 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3098 * We compute the difference on the shared domain and then construct
3099 * the set of values where this difference is non-negative.
3100 * If strict is set, we first subtract 1 from the difference.
3101 * If equal is set, we only return the elements where pwaff1 and pwaff2
3102 * are equal.
3104 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3105 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3107 isl_set *set1, *set2;
3109 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3110 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3111 set1 = isl_set_intersect(set1, set2);
3112 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3113 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3114 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3116 if (strict) {
3117 isl_space *space = isl_set_get_space(set1);
3118 isl_aff *aff;
3119 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3120 aff = isl_aff_add_constant_si(aff, -1);
3121 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3122 } else
3123 isl_set_free(set1);
3125 if (equal)
3126 return isl_pw_aff_zero_set(pwaff1);
3127 return isl_pw_aff_nonneg_set(pwaff1);
3130 /* Return a set containing those elements in the shared domain
3131 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3133 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3134 __isl_take isl_pw_aff *pwaff2)
3136 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3137 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3140 /* Return a set containing those elements in the shared domain
3141 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3143 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3144 __isl_take isl_pw_aff *pwaff2)
3146 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3147 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3150 /* Return a set containing those elements in the shared domain
3151 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3153 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3154 __isl_take isl_pw_aff *pwaff2)
3156 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3157 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3160 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3161 __isl_take isl_pw_aff *pwaff2)
3163 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3166 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3167 __isl_take isl_pw_aff *pwaff2)
3169 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3172 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3173 * where the function values are ordered in the same way as "order",
3174 * which returns a set in the shared domain of its two arguments.
3176 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3177 * We first pull back the two functions such that they are defined on
3178 * the domain [A -> B]. Then we apply "order", resulting in a set
3179 * in the space [A -> B]. Finally, we unwrap this set to obtain
3180 * a map in the space A -> B.
3182 static __isl_give isl_map *isl_pw_aff_order_map(
3183 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3184 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3185 __isl_take isl_pw_aff *pa2))
3187 isl_space *space1, *space2;
3188 isl_multi_aff *ma;
3189 isl_set *set;
3191 isl_pw_aff_align_params_bin(&pa1, &pa2);
3192 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3193 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3194 space1 = isl_space_map_from_domain_and_range(space1, space2);
3195 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3196 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3197 ma = isl_multi_aff_range_map(space1);
3198 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3199 set = order(pa1, pa2);
3201 return isl_set_unwrap(set);
3204 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3205 * where the function values are equal.
3207 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3208 __isl_take isl_pw_aff *pa2)
3210 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3213 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3214 * where the function value of "pa1" is less than or equal to
3215 * the function value of "pa2".
3217 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3218 __isl_take isl_pw_aff *pa2)
3220 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3223 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3224 * where the function value of "pa1" is less than the function value of "pa2".
3226 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3227 __isl_take isl_pw_aff *pa2)
3229 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3232 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3233 * where the function value of "pa1" is greater than or equal to
3234 * the function value of "pa2".
3236 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3237 __isl_take isl_pw_aff *pa2)
3239 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3242 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3243 * where the function value of "pa1" is greater than the function value
3244 * of "pa2".
3246 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3247 __isl_take isl_pw_aff *pa2)
3249 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3252 /* Return a set containing those elements in the shared domain
3253 * of the elements of list1 and list2 where each element in list1
3254 * has the relation specified by "fn" with each element in list2.
3256 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3257 __isl_take isl_pw_aff_list *list2,
3258 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3259 __isl_take isl_pw_aff *pwaff2))
3261 int i, j;
3262 isl_ctx *ctx;
3263 isl_set *set;
3265 if (!list1 || !list2)
3266 goto error;
3268 ctx = isl_pw_aff_list_get_ctx(list1);
3269 if (list1->n < 1 || list2->n < 1)
3270 isl_die(ctx, isl_error_invalid,
3271 "list should contain at least one element", goto error);
3273 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3274 for (i = 0; i < list1->n; ++i)
3275 for (j = 0; j < list2->n; ++j) {
3276 isl_set *set_ij;
3278 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3279 isl_pw_aff_copy(list2->p[j]));
3280 set = isl_set_intersect(set, set_ij);
3283 isl_pw_aff_list_free(list1);
3284 isl_pw_aff_list_free(list2);
3285 return set;
3286 error:
3287 isl_pw_aff_list_free(list1);
3288 isl_pw_aff_list_free(list2);
3289 return NULL;
3292 /* Return a set containing those elements in the shared domain
3293 * of the elements of list1 and list2 where each element in list1
3294 * is equal to each element in list2.
3296 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3297 __isl_take isl_pw_aff_list *list2)
3299 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3302 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3303 __isl_take isl_pw_aff_list *list2)
3305 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3308 /* Return a set containing those elements in the shared domain
3309 * of the elements of list1 and list2 where each element in list1
3310 * is less than or equal to each element in list2.
3312 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3313 __isl_take isl_pw_aff_list *list2)
3315 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3318 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3319 __isl_take isl_pw_aff_list *list2)
3321 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3324 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3325 __isl_take isl_pw_aff_list *list2)
3327 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3330 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3331 __isl_take isl_pw_aff_list *list2)
3333 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3337 /* Return a set containing those elements in the shared domain
3338 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3340 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3341 __isl_take isl_pw_aff *pwaff2)
3343 isl_set *set_lt, *set_gt;
3345 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3346 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3347 isl_pw_aff_copy(pwaff2));
3348 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3349 return isl_set_union_disjoint(set_lt, set_gt);
3352 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3353 isl_int v)
3355 int i;
3357 if (isl_int_is_one(v))
3358 return pwaff;
3359 if (!isl_int_is_pos(v))
3360 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3361 "factor needs to be positive",
3362 return isl_pw_aff_free(pwaff));
3363 pwaff = isl_pw_aff_cow(pwaff);
3364 if (!pwaff)
3365 return NULL;
3366 if (pwaff->n == 0)
3367 return pwaff;
3369 for (i = 0; i < pwaff->n; ++i) {
3370 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3371 if (!pwaff->p[i].aff)
3372 return isl_pw_aff_free(pwaff);
3375 return pwaff;
3378 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3380 return isl_pw_aff_un_op(pwaff, &isl_aff_floor);
3383 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3385 return isl_pw_aff_un_op(pwaff, &isl_aff_ceil);
3388 /* Assuming that "cond1" and "cond2" are disjoint,
3389 * return an affine expression that is equal to pwaff1 on cond1
3390 * and to pwaff2 on cond2.
3392 static __isl_give isl_pw_aff *isl_pw_aff_select(
3393 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3394 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3396 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3397 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3399 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3402 /* Return an affine expression that is equal to pwaff_true for elements
3403 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3404 * is zero.
3405 * That is, return cond ? pwaff_true : pwaff_false;
3407 * If "cond" involves and NaN, then we conservatively return a NaN
3408 * on its entire domain. In principle, we could consider the pieces
3409 * where it is NaN separately from those where it is not.
3411 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3412 * then only use the domain of "cond" to restrict the domain.
3414 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3415 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3417 isl_set *cond_true, *cond_false;
3418 isl_bool equal;
3420 if (!cond)
3421 goto error;
3422 if (isl_pw_aff_involves_nan(cond)) {
3423 isl_space *space = isl_pw_aff_get_domain_space(cond);
3424 isl_local_space *ls = isl_local_space_from_space(space);
3425 isl_pw_aff_free(cond);
3426 isl_pw_aff_free(pwaff_true);
3427 isl_pw_aff_free(pwaff_false);
3428 return isl_pw_aff_nan_on_domain(ls);
3431 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3432 isl_pw_aff_get_space(pwaff_false));
3433 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3434 isl_pw_aff_get_space(pwaff_true));
3435 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3436 if (equal < 0)
3437 goto error;
3438 if (equal) {
3439 isl_set *dom;
3441 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3442 isl_pw_aff_free(pwaff_false);
3443 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3446 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3447 cond_false = isl_pw_aff_zero_set(cond);
3448 return isl_pw_aff_select(cond_true, pwaff_true,
3449 cond_false, pwaff_false);
3450 error:
3451 isl_pw_aff_free(cond);
3452 isl_pw_aff_free(pwaff_true);
3453 isl_pw_aff_free(pwaff_false);
3454 return NULL;
3457 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3459 int pos;
3461 if (!aff)
3462 return isl_bool_error;
3464 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3465 return isl_bool_ok(pos == -1);
3468 /* Check whether pwaff is a piecewise constant.
3470 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3472 int i;
3474 if (!pwaff)
3475 return isl_bool_error;
3477 for (i = 0; i < pwaff->n; ++i) {
3478 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3479 if (is_cst < 0 || !is_cst)
3480 return is_cst;
3483 return isl_bool_true;
3486 /* Return the product of "aff1" and "aff2".
3488 * If either of the two is NaN, then the result is NaN.
3490 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3492 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3493 __isl_take isl_aff *aff2)
3495 if (!aff1 || !aff2)
3496 goto error;
3498 if (isl_aff_is_nan(aff1)) {
3499 isl_aff_free(aff2);
3500 return aff1;
3502 if (isl_aff_is_nan(aff2)) {
3503 isl_aff_free(aff1);
3504 return aff2;
3507 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3508 return isl_aff_mul(aff2, aff1);
3510 if (!isl_aff_is_cst(aff2))
3511 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3512 "at least one affine expression should be constant",
3513 goto error);
3515 aff1 = isl_aff_cow(aff1);
3516 if (!aff1 || !aff2)
3517 goto error;
3519 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3520 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3522 isl_aff_free(aff2);
3523 return aff1;
3524 error:
3525 isl_aff_free(aff1);
3526 isl_aff_free(aff2);
3527 return NULL;
3530 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3532 * If either of the two is NaN, then the result is NaN.
3533 * A division by zero also results in NaN.
3535 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3536 __isl_take isl_aff *aff2)
3538 isl_bool is_cst, is_zero;
3539 int neg;
3541 if (!aff1 || !aff2)
3542 goto error;
3544 if (isl_aff_is_nan(aff1)) {
3545 isl_aff_free(aff2);
3546 return aff1;
3548 if (isl_aff_is_nan(aff2)) {
3549 isl_aff_free(aff1);
3550 return aff2;
3553 is_cst = isl_aff_is_cst(aff2);
3554 if (is_cst < 0)
3555 goto error;
3556 if (!is_cst)
3557 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3558 "second argument should be a constant", goto error);
3559 is_zero = isl_aff_plain_is_zero(aff2);
3560 if (is_zero < 0)
3561 goto error;
3562 if (is_zero)
3563 return set_nan_free(aff1, aff2);
3565 neg = isl_int_is_neg(aff2->v->el[1]);
3566 if (neg) {
3567 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3568 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3571 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3572 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3574 if (neg) {
3575 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3576 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3579 isl_aff_free(aff2);
3580 return aff1;
3581 error:
3582 isl_aff_free(aff1);
3583 isl_aff_free(aff2);
3584 return NULL;
3587 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3588 __isl_take isl_pw_aff *pwaff2)
3590 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3591 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3594 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3595 __isl_take isl_pw_aff *pwaff2)
3597 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3598 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3601 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3603 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3604 __isl_take isl_pw_aff *pa2)
3606 int is_cst;
3608 is_cst = isl_pw_aff_is_cst(pa2);
3609 if (is_cst < 0)
3610 goto error;
3611 if (!is_cst)
3612 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3613 "second argument should be a piecewise constant",
3614 goto error);
3615 isl_pw_aff_align_params_bin(&pa1, &pa2);
3616 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3617 error:
3618 isl_pw_aff_free(pa1);
3619 isl_pw_aff_free(pa2);
3620 return NULL;
3623 /* Compute the quotient of the integer division of "pa1" by "pa2"
3624 * with rounding towards zero.
3625 * "pa2" is assumed to be a piecewise constant.
3627 * In particular, return
3629 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3632 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3633 __isl_take isl_pw_aff *pa2)
3635 int is_cst;
3636 isl_set *cond;
3637 isl_pw_aff *f, *c;
3639 is_cst = isl_pw_aff_is_cst(pa2);
3640 if (is_cst < 0)
3641 goto error;
3642 if (!is_cst)
3643 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3644 "second argument should be a piecewise constant",
3645 goto error);
3647 pa1 = isl_pw_aff_div(pa1, pa2);
3649 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3650 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3651 c = isl_pw_aff_ceil(pa1);
3652 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3653 error:
3654 isl_pw_aff_free(pa1);
3655 isl_pw_aff_free(pa2);
3656 return NULL;
3659 /* Compute the remainder of the integer division of "pa1" by "pa2"
3660 * with rounding towards zero.
3661 * "pa2" is assumed to be a piecewise constant.
3663 * In particular, return
3665 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3668 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3669 __isl_take isl_pw_aff *pa2)
3671 int is_cst;
3672 isl_pw_aff *res;
3674 is_cst = isl_pw_aff_is_cst(pa2);
3675 if (is_cst < 0)
3676 goto error;
3677 if (!is_cst)
3678 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3679 "second argument should be a piecewise constant",
3680 goto error);
3681 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3682 res = isl_pw_aff_mul(pa2, res);
3683 res = isl_pw_aff_sub(pa1, res);
3684 return res;
3685 error:
3686 isl_pw_aff_free(pa1);
3687 isl_pw_aff_free(pa2);
3688 return NULL;
3691 /* Does either of "pa1" or "pa2" involve any NaN?
3693 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3694 __isl_keep isl_pw_aff *pa2)
3696 isl_bool has_nan;
3698 has_nan = isl_pw_aff_involves_nan(pa1);
3699 if (has_nan < 0 || has_nan)
3700 return has_nan;
3701 return isl_pw_aff_involves_nan(pa2);
3704 /* Return a piecewise affine expression defined on the specified domain
3705 * that represents NaN.
3707 static __isl_give isl_pw_aff *nan_on_domain_set(__isl_take isl_set *dom)
3709 isl_local_space *ls;
3710 isl_pw_aff *pa;
3712 ls = isl_local_space_from_space(isl_set_get_space(dom));
3713 pa = isl_pw_aff_nan_on_domain(ls);
3714 pa = isl_pw_aff_intersect_domain(pa, dom);
3716 return pa;
3719 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3720 * by a NaN on their shared domain.
3722 * In principle, the result could be refined to only being NaN
3723 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3725 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3726 __isl_take isl_pw_aff *pa2)
3728 isl_set *dom;
3730 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3731 return nan_on_domain_set(dom);
3734 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3735 __isl_take isl_pw_aff *pwaff2)
3737 isl_set *le;
3738 isl_set *dom;
3740 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3741 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3742 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3743 isl_pw_aff_copy(pwaff2));
3744 dom = isl_set_subtract(dom, isl_set_copy(le));
3745 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3748 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3749 __isl_take isl_pw_aff *pwaff2)
3751 isl_set *ge;
3752 isl_set *dom;
3754 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3755 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3756 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3757 isl_pw_aff_copy(pwaff2));
3758 dom = isl_set_subtract(dom, isl_set_copy(ge));
3759 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3762 /* Return an expression for the minimum (if "max" is not set) or
3763 * the maximum (if "max" is set) of "pa1" and "pa2".
3764 * If either expression involves any NaN, then return a NaN
3765 * on the shared domain as result.
3767 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3768 __isl_take isl_pw_aff *pa2, int max)
3770 isl_bool has_nan;
3772 has_nan = either_involves_nan(pa1, pa2);
3773 if (has_nan < 0)
3774 pa1 = isl_pw_aff_free(pa1);
3775 else if (has_nan)
3776 return replace_by_nan(pa1, pa2);
3778 isl_pw_aff_align_params_bin(&pa1, &pa2);
3779 if (max)
3780 return pw_aff_max(pa1, pa2);
3781 else
3782 return pw_aff_min(pa1, pa2);
3785 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3787 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3788 __isl_take isl_pw_aff *pwaff2)
3790 return pw_aff_min_max(pwaff1, pwaff2, 0);
3793 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3795 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3796 __isl_take isl_pw_aff *pwaff2)
3798 return pw_aff_min_max(pwaff1, pwaff2, 1);
3801 /* Does "pa" not involve any NaN?
3803 static isl_bool pw_aff_no_nan(__isl_keep isl_pw_aff *pa, void *user)
3805 return isl_bool_not(isl_pw_aff_involves_nan(pa));
3808 /* Does any element of "list" involve any NaN?
3810 * That is, is it not the case that every element does not involve any NaN?
3812 static isl_bool isl_pw_aff_list_involves_nan(__isl_keep isl_pw_aff_list *list)
3814 return isl_bool_not(isl_pw_aff_list_every(list, &pw_aff_no_nan, NULL));
3817 /* Replace "list" (consisting of "n" elements, of which
3818 * at least one element involves a NaN)
3819 * by a NaN on the shared domain of the elements.
3821 * In principle, the result could be refined to only being NaN
3822 * on the parts of this domain where at least one of the elements is NaN.
3824 static __isl_give isl_pw_aff *replace_list_by_nan(
3825 __isl_take isl_pw_aff_list *list, int n)
3827 int i;
3828 isl_set *dom;
3830 dom = isl_pw_aff_domain(isl_pw_aff_list_get_at(list, 0));
3831 for (i = 1; i < n; ++i) {
3832 isl_set *dom_i;
3834 dom_i = isl_pw_aff_domain(isl_pw_aff_list_get_at(list, i));
3835 dom = isl_set_intersect(dom, dom_i);
3838 isl_pw_aff_list_free(list);
3839 return nan_on_domain_set(dom);
3842 /* Return the set where the element at "pos1" of "list" is less than or
3843 * equal to the element at "pos2".
3844 * Equality is only allowed if "pos1" is smaller than "pos2".
3846 static __isl_give isl_set *less(__isl_keep isl_pw_aff_list *list,
3847 int pos1, int pos2)
3849 isl_pw_aff *pa1, *pa2;
3851 pa1 = isl_pw_aff_list_get_at(list, pos1);
3852 pa2 = isl_pw_aff_list_get_at(list, pos2);
3854 if (pos1 < pos2)
3855 return isl_pw_aff_le_set(pa1, pa2);
3856 else
3857 return isl_pw_aff_lt_set(pa1, pa2);
3860 /* Return an isl_pw_aff that maps each element in the intersection of the
3861 * domains of the piecewise affine expressions in "list"
3862 * to the maximal (if "max" is set) or minimal (if "max" is not set)
3863 * expression in "list" at that element.
3864 * If any expression involves any NaN, then return a NaN
3865 * on the shared domain as result.
3867 * If "list" has n elements, then the result consists of n pieces,
3868 * where, in the case of a minimum, each piece has as value expression
3869 * the value expression of one of the elements and as domain
3870 * the set of elements where that value expression
3871 * is less than (or equal) to the other value expressions.
3872 * In the case of a maximum, the condition is
3873 * that all the other value expressions are less than (or equal)
3874 * to the given value expression.
3876 * In order to produce disjoint pieces, a pair of elements
3877 * in the original domain is only allowed to be equal to each other
3878 * on exactly one of the two pieces corresponding to the two elements.
3879 * The position in the list is used to break ties.
3880 * In particular, in the case of a minimum,
3881 * in the piece corresponding to a given element,
3882 * this element is allowed to be equal to any later element in the list,
3883 * but not to any earlier element in the list.
3885 static __isl_give isl_pw_aff *isl_pw_aff_list_opt(
3886 __isl_take isl_pw_aff_list *list, int max)
3888 int i, j;
3889 isl_bool has_nan;
3890 isl_size n;
3891 isl_space *space;
3892 isl_pw_aff *pa, *res;
3894 n = isl_pw_aff_list_size(list);
3895 if (n < 0)
3896 goto error;
3897 if (n < 1)
3898 isl_die(isl_pw_aff_list_get_ctx(list), isl_error_invalid,
3899 "list should contain at least one element", goto error);
3901 has_nan = isl_pw_aff_list_involves_nan(list);
3902 if (has_nan < 0)
3903 goto error;
3904 if (has_nan)
3905 return replace_list_by_nan(list, n);
3907 pa = isl_pw_aff_list_get_at(list, 0);
3908 space = isl_pw_aff_get_space(pa);
3909 isl_pw_aff_free(pa);
3910 res = isl_pw_aff_empty(space);
3912 for (i = 0; i < n; ++i) {
3913 pa = isl_pw_aff_list_get_at(list, i);
3914 for (j = 0; j < n; ++j) {
3915 isl_set *dom;
3917 if (j == i)
3918 continue;
3919 if (max)
3920 dom = less(list, j, i);
3921 else
3922 dom = less(list, i, j);
3924 pa = isl_pw_aff_intersect_domain(pa, dom);
3926 res = isl_pw_aff_add_disjoint(res, pa);
3929 isl_pw_aff_list_free(list);
3930 return res;
3931 error:
3932 isl_pw_aff_list_free(list);
3933 return NULL;
3936 /* Return an isl_pw_aff that maps each element in the intersection of the
3937 * domains of the elements of list to the minimal corresponding affine
3938 * expression.
3940 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3942 return isl_pw_aff_list_opt(list, 0);
3945 /* Return an isl_pw_aff that maps each element in the intersection of the
3946 * domains of the elements of list to the maximal corresponding affine
3947 * expression.
3949 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3951 return isl_pw_aff_list_opt(list, 1);
3954 /* Mark the domains of "pwaff" as rational.
3956 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3958 int i;
3960 pwaff = isl_pw_aff_cow(pwaff);
3961 if (!pwaff)
3962 return NULL;
3963 if (pwaff->n == 0)
3964 return pwaff;
3966 for (i = 0; i < pwaff->n; ++i) {
3967 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3968 if (!pwaff->p[i].set)
3969 return isl_pw_aff_free(pwaff);
3972 return pwaff;
3975 /* Mark the domains of the elements of "list" as rational.
3977 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3978 __isl_take isl_pw_aff_list *list)
3980 int i, n;
3982 if (!list)
3983 return NULL;
3984 if (list->n == 0)
3985 return list;
3987 n = list->n;
3988 for (i = 0; i < n; ++i) {
3989 isl_pw_aff *pa;
3991 pa = isl_pw_aff_list_get_pw_aff(list, i);
3992 pa = isl_pw_aff_set_rational(pa);
3993 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3996 return list;
3999 /* Do the parameters of "aff" match those of "space"?
4001 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
4002 __isl_keep isl_space *space)
4004 isl_space *aff_space;
4005 isl_bool match;
4007 if (!aff || !space)
4008 return isl_bool_error;
4010 aff_space = isl_aff_get_domain_space(aff);
4012 match = isl_space_has_equal_params(space, aff_space);
4014 isl_space_free(aff_space);
4015 return match;
4018 /* Check that the domain space of "aff" matches "space".
4020 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
4021 __isl_keep isl_space *space)
4023 isl_space *aff_space;
4024 isl_bool match;
4026 if (!aff || !space)
4027 return isl_stat_error;
4029 aff_space = isl_aff_get_domain_space(aff);
4031 match = isl_space_has_equal_params(space, aff_space);
4032 if (match < 0)
4033 goto error;
4034 if (!match)
4035 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
4036 "parameters don't match", goto error);
4037 match = isl_space_tuple_is_equal(space, isl_dim_in,
4038 aff_space, isl_dim_set);
4039 if (match < 0)
4040 goto error;
4041 if (!match)
4042 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
4043 "domains don't match", goto error);
4044 isl_space_free(aff_space);
4045 return isl_stat_ok;
4046 error:
4047 isl_space_free(aff_space);
4048 return isl_stat_error;
4051 /* Return the shared (universe) domain of the elements of "ma".
4053 * Since an isl_multi_aff (and an isl_aff) is always total,
4054 * the domain is always the universe set in its domain space.
4055 * This is a helper function for use in the generic isl_multi_*_bind.
4057 static __isl_give isl_basic_set *isl_multi_aff_domain(
4058 __isl_take isl_multi_aff *ma)
4060 isl_space *space;
4062 space = isl_multi_aff_get_space(ma);
4063 isl_multi_aff_free(ma);
4065 return isl_basic_set_universe(isl_space_domain(space));
4068 #undef BASE
4069 #define BASE aff
4071 #include <isl_multi_no_explicit_domain.c>
4072 #include <isl_multi_templ.c>
4073 #include <isl_multi_un_op_templ.c>
4074 #include <isl_multi_bin_val_templ.c>
4075 #include <isl_multi_add_constant_templ.c>
4076 #include <isl_multi_align_set.c>
4077 #include <isl_multi_arith_templ.c>
4078 #include <isl_multi_bind_domain_templ.c>
4079 #include <isl_multi_cmp.c>
4080 #include <isl_multi_dim_id_templ.c>
4081 #include <isl_multi_dims.c>
4082 #include <isl_multi_floor.c>
4083 #include <isl_multi_from_base_templ.c>
4084 #include <isl_multi_identity_templ.c>
4085 #include <isl_multi_insert_domain_templ.c>
4086 #include <isl_multi_locals_templ.c>
4087 #include <isl_multi_move_dims_templ.c>
4088 #include <isl_multi_nan_templ.c>
4089 #include <isl_multi_product_templ.c>
4090 #include <isl_multi_splice_templ.c>
4091 #include <isl_multi_tuple_id_templ.c>
4092 #include <isl_multi_unbind_params_templ.c>
4093 #include <isl_multi_zero_templ.c>
4095 #undef DOMBASE
4096 #define DOMBASE set
4097 #include <isl_multi_check_domain_templ.c>
4098 #include <isl_multi_apply_set_no_explicit_domain_templ.c>
4099 #include <isl_multi_gist.c>
4101 #undef DOMBASE
4102 #define DOMBASE basic_set
4103 #include <isl_multi_bind_templ.c>
4105 /* Construct an isl_multi_aff living in "space" that corresponds
4106 * to the affine transformation matrix "mat".
4108 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
4109 __isl_take isl_space *space, __isl_take isl_mat *mat)
4111 isl_ctx *ctx;
4112 isl_local_space *ls = NULL;
4113 isl_multi_aff *ma = NULL;
4114 isl_size n_row, n_col, n_out, total;
4115 int i;
4117 if (!space || !mat)
4118 goto error;
4120 ctx = isl_mat_get_ctx(mat);
4122 n_row = isl_mat_rows(mat);
4123 n_col = isl_mat_cols(mat);
4124 n_out = isl_space_dim(space, isl_dim_out);
4125 total = isl_space_dim(space, isl_dim_all);
4126 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
4127 goto error;
4128 if (n_row < 1)
4129 isl_die(ctx, isl_error_invalid,
4130 "insufficient number of rows", goto error);
4131 if (n_col < 1)
4132 isl_die(ctx, isl_error_invalid,
4133 "insufficient number of columns", goto error);
4134 if (1 + n_out != n_row || 2 + total != n_row + n_col)
4135 isl_die(ctx, isl_error_invalid,
4136 "dimension mismatch", goto error);
4138 ma = isl_multi_aff_zero(isl_space_copy(space));
4139 space = isl_space_domain(space);
4140 ls = isl_local_space_from_space(isl_space_copy(space));
4142 for (i = 0; i < n_row - 1; ++i) {
4143 isl_vec *v;
4144 isl_aff *aff;
4146 v = isl_vec_alloc(ctx, 1 + n_col);
4147 if (!v)
4148 goto error;
4149 isl_int_set(v->el[0], mat->row[0][0]);
4150 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
4151 v = isl_vec_normalize(v);
4152 aff = isl_aff_alloc_vec_validated(isl_local_space_copy(ls), v);
4153 ma = isl_multi_aff_set_aff(ma, i, aff);
4156 isl_space_free(space);
4157 isl_local_space_free(ls);
4158 isl_mat_free(mat);
4159 return ma;
4160 error:
4161 isl_space_free(space);
4162 isl_local_space_free(ls);
4163 isl_mat_free(mat);
4164 isl_multi_aff_free(ma);
4165 return NULL;
4168 /* Return the constant terms of the affine expressions of "ma".
4170 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
4171 __isl_keep isl_multi_aff *ma)
4173 int i;
4174 isl_size n;
4175 isl_space *space;
4176 isl_multi_val *mv;
4178 n = isl_multi_aff_size(ma);
4179 if (n < 0)
4180 return NULL;
4181 space = isl_space_range(isl_multi_aff_get_space(ma));
4182 space = isl_space_drop_all_params(space);
4183 mv = isl_multi_val_zero(space);
4185 for (i = 0; i < n; ++i) {
4186 isl_aff *aff;
4187 isl_val *val;
4189 aff = isl_multi_aff_get_at(ma, i);
4190 val = isl_aff_get_constant_val(aff);
4191 isl_aff_free(aff);
4192 mv = isl_multi_val_set_at(mv, i, val);
4195 return mv;
4198 /* Remove any internal structure of the domain of "ma".
4199 * If there is any such internal structure in the input,
4200 * then the name of the corresponding space is also removed.
4202 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4203 __isl_take isl_multi_aff *ma)
4205 isl_space *space;
4207 if (!ma)
4208 return NULL;
4210 if (!ma->space->nested[0])
4211 return ma;
4213 space = isl_multi_aff_get_space(ma);
4214 space = isl_space_flatten_domain(space);
4215 ma = isl_multi_aff_reset_space(ma, space);
4217 return ma;
4220 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4221 * of the space to its domain.
4223 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4225 int i;
4226 isl_size n_in;
4227 isl_local_space *ls;
4228 isl_multi_aff *ma;
4230 if (!space)
4231 return NULL;
4232 if (!isl_space_is_map(space))
4233 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4234 "not a map space", goto error);
4236 n_in = isl_space_dim(space, isl_dim_in);
4237 if (n_in < 0)
4238 goto error;
4239 space = isl_space_domain_map(space);
4241 ma = isl_multi_aff_alloc(isl_space_copy(space));
4242 if (n_in == 0) {
4243 isl_space_free(space);
4244 return ma;
4247 space = isl_space_domain(space);
4248 ls = isl_local_space_from_space(space);
4249 for (i = 0; i < n_in; ++i) {
4250 isl_aff *aff;
4252 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4253 isl_dim_set, i);
4254 ma = isl_multi_aff_set_aff(ma, i, aff);
4256 isl_local_space_free(ls);
4257 return ma;
4258 error:
4259 isl_space_free(space);
4260 return NULL;
4263 /* This function performs the same operation as isl_multi_aff_domain_map,
4264 * but is considered as a function on an isl_space when exported.
4266 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4267 __isl_take isl_space *space)
4269 return isl_multi_aff_domain_map(space);
4272 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4273 * of the space to its range.
4275 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4277 int i;
4278 isl_size n_in, n_out;
4279 isl_local_space *ls;
4280 isl_multi_aff *ma;
4282 if (!space)
4283 return NULL;
4284 if (!isl_space_is_map(space))
4285 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4286 "not a map space", goto error);
4288 n_in = isl_space_dim(space, isl_dim_in);
4289 n_out = isl_space_dim(space, isl_dim_out);
4290 if (n_in < 0 || n_out < 0)
4291 goto error;
4292 space = isl_space_range_map(space);
4294 ma = isl_multi_aff_alloc(isl_space_copy(space));
4295 if (n_out == 0) {
4296 isl_space_free(space);
4297 return ma;
4300 space = isl_space_domain(space);
4301 ls = isl_local_space_from_space(space);
4302 for (i = 0; i < n_out; ++i) {
4303 isl_aff *aff;
4305 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4306 isl_dim_set, n_in + i);
4307 ma = isl_multi_aff_set_aff(ma, i, aff);
4309 isl_local_space_free(ls);
4310 return ma;
4311 error:
4312 isl_space_free(space);
4313 return NULL;
4316 /* This function performs the same operation as isl_multi_aff_range_map,
4317 * but is considered as a function on an isl_space when exported.
4319 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4320 __isl_take isl_space *space)
4322 return isl_multi_aff_range_map(space);
4325 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4326 * of the space to its domain.
4328 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4329 __isl_take isl_space *space)
4331 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4334 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4335 * but is considered as a function on an isl_space when exported.
4337 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4338 __isl_take isl_space *space)
4340 return isl_pw_multi_aff_domain_map(space);
4343 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4344 * of the space to its range.
4346 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4347 __isl_take isl_space *space)
4349 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4352 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4353 * but is considered as a function on an isl_space when exported.
4355 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4356 __isl_take isl_space *space)
4358 return isl_pw_multi_aff_range_map(space);
4361 /* Given the space of a set and a range of set dimensions,
4362 * construct an isl_multi_aff that projects out those dimensions.
4364 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4365 __isl_take isl_space *space, enum isl_dim_type type,
4366 unsigned first, unsigned n)
4368 int i;
4369 isl_size dim;
4370 isl_local_space *ls;
4371 isl_multi_aff *ma;
4373 if (!space)
4374 return NULL;
4375 if (!isl_space_is_set(space))
4376 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4377 "expecting set space", goto error);
4378 if (type != isl_dim_set)
4379 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4380 "only set dimensions can be projected out", goto error);
4381 if (isl_space_check_range(space, type, first, n) < 0)
4382 goto error;
4384 dim = isl_space_dim(space, isl_dim_set);
4385 if (dim < 0)
4386 goto error;
4388 space = isl_space_from_domain(space);
4389 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4391 if (dim == n)
4392 return isl_multi_aff_alloc(space);
4394 ma = isl_multi_aff_alloc(isl_space_copy(space));
4395 space = isl_space_domain(space);
4396 ls = isl_local_space_from_space(space);
4398 for (i = 0; i < first; ++i) {
4399 isl_aff *aff;
4401 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4402 isl_dim_set, i);
4403 ma = isl_multi_aff_set_aff(ma, i, aff);
4406 for (i = 0; i < dim - (first + n); ++i) {
4407 isl_aff *aff;
4409 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4410 isl_dim_set, first + n + i);
4411 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4414 isl_local_space_free(ls);
4415 return ma;
4416 error:
4417 isl_space_free(space);
4418 return NULL;
4421 /* Given the space of a set and a range of set dimensions,
4422 * construct an isl_pw_multi_aff that projects out those dimensions.
4424 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4425 __isl_take isl_space *space, enum isl_dim_type type,
4426 unsigned first, unsigned n)
4428 isl_multi_aff *ma;
4430 ma = isl_multi_aff_project_out_map(space, type, first, n);
4431 return isl_pw_multi_aff_from_multi_aff(ma);
4434 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4435 * but is considered as a function on an isl_multi_aff when exported.
4437 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4438 __isl_take isl_multi_aff *ma)
4440 return isl_pw_multi_aff_from_multi_aff(ma);
4443 /* Create a piecewise multi-affine expression in the given space that maps each
4444 * input dimension to the corresponding output dimension.
4446 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4447 __isl_take isl_space *space)
4449 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4452 /* Create a piecewise multi expression that maps elements in the given space
4453 * to themselves.
4455 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4456 __isl_take isl_space *space)
4458 isl_multi_aff *ma;
4460 ma = isl_multi_aff_identity_on_domain_space(space);
4461 return isl_pw_multi_aff_from_multi_aff(ma);
4464 /* This function performs the same operation as
4465 * isl_pw_multi_aff_identity_on_domain_space,
4466 * but is considered as a function on an isl_space when exported.
4468 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4469 __isl_take isl_space *space)
4471 return isl_pw_multi_aff_identity_on_domain_space(space);
4474 /* Exploit the equalities in "eq" to simplify the affine expressions.
4476 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4477 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4479 isl_size n;
4480 int i;
4482 n = isl_multi_aff_size(maff);
4483 if (n < 0 || !eq)
4484 goto error;
4486 for (i = 0; i < n; ++i) {
4487 isl_aff *aff;
4489 aff = isl_multi_aff_take_at(maff, i);
4490 aff = isl_aff_substitute_equalities(aff,
4491 isl_basic_set_copy(eq));
4492 maff = isl_multi_aff_restore_at(maff, i, aff);
4495 isl_basic_set_free(eq);
4496 return maff;
4497 error:
4498 isl_basic_set_free(eq);
4499 isl_multi_aff_free(maff);
4500 return NULL;
4503 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4504 isl_int f)
4506 isl_size n;
4507 int i;
4509 n = isl_multi_aff_size(maff);
4510 if (n < 0)
4511 return isl_multi_aff_free(maff);
4513 for (i = 0; i < n; ++i) {
4514 isl_aff *aff;
4516 aff = isl_multi_aff_take_at(maff, i);
4517 aff = isl_aff_scale(aff, f);
4518 maff = isl_multi_aff_restore_at(maff, i, aff);
4521 return maff;
4524 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4525 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4527 maff1 = isl_multi_aff_add(maff1, maff2);
4528 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4529 return maff1;
4532 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4534 if (!maff)
4535 return isl_bool_error;
4537 return isl_bool_false;
4540 /* Return the set of domain elements where "ma1" is lexicographically
4541 * smaller than or equal to "ma2".
4543 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4544 __isl_take isl_multi_aff *ma2)
4546 return isl_multi_aff_lex_ge_set(ma2, ma1);
4549 /* Return the set of domain elements where "ma1" is lexicographically
4550 * smaller than "ma2".
4552 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4553 __isl_take isl_multi_aff *ma2)
4555 return isl_multi_aff_lex_gt_set(ma2, ma1);
4558 /* Return the set of domain elements where "ma1" is lexicographically
4559 * greater than to "ma2". If "equal" is set, then include the domain
4560 * elements where they are equal.
4561 * Do this for the case where there are no entries.
4562 * In this case, "ma1" cannot be greater than "ma2",
4563 * but it is (greater than or) equal to "ma2".
4565 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4566 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4568 isl_space *space;
4570 space = isl_multi_aff_get_domain_space(ma1);
4572 isl_multi_aff_free(ma1);
4573 isl_multi_aff_free(ma2);
4575 if (equal)
4576 return isl_set_universe(space);
4577 else
4578 return isl_set_empty(space);
4581 /* Return the set where entry "i" of "ma1" and "ma2"
4582 * satisfy the relation prescribed by "cmp".
4584 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4585 __isl_keep isl_multi_aff *ma2, int i,
4586 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4587 __isl_take isl_aff *aff2))
4589 isl_aff *aff1, *aff2;
4591 aff1 = isl_multi_aff_get_at(ma1, i);
4592 aff2 = isl_multi_aff_get_at(ma2, i);
4593 return cmp(aff1, aff2);
4596 /* Return the set of domain elements where "ma1" is lexicographically
4597 * greater than to "ma2". If "equal" is set, then include the domain
4598 * elements where they are equal.
4600 * In particular, for all but the final entry,
4601 * include the set of elements where this entry is strictly greater in "ma1"
4602 * and all previous entries are equal.
4603 * The final entry is also allowed to be equal in the two functions
4604 * if "equal" is set.
4606 * The case where there are no entries is handled separately.
4608 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4609 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4611 int i;
4612 isl_size n;
4613 isl_space *space;
4614 isl_set *res;
4615 isl_set *equal_set;
4616 isl_set *gte;
4618 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4619 goto error;
4620 n = isl_multi_aff_size(ma1);
4621 if (n < 0)
4622 goto error;
4623 if (n == 0)
4624 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4626 space = isl_multi_aff_get_domain_space(ma1);
4627 res = isl_set_empty(isl_space_copy(space));
4628 equal_set = isl_set_universe(space);
4630 for (i = 0; i + 1 < n; ++i) {
4631 isl_bool empty;
4632 isl_set *gt, *eq;
4634 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4635 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4636 res = isl_set_union(res, gt);
4637 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4638 equal_set = isl_set_intersect(equal_set, eq);
4640 empty = isl_set_is_empty(equal_set);
4641 if (empty >= 0 && empty)
4642 break;
4645 if (equal)
4646 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4647 else
4648 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4649 isl_multi_aff_free(ma1);
4650 isl_multi_aff_free(ma2);
4652 gte = isl_set_intersect(gte, equal_set);
4653 return isl_set_union(res, gte);
4654 error:
4655 isl_multi_aff_free(ma1);
4656 isl_multi_aff_free(ma2);
4657 return NULL;
4660 /* Return the set of domain elements where "ma1" is lexicographically
4661 * greater than or equal to "ma2".
4663 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4664 __isl_take isl_multi_aff *ma2)
4666 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4669 /* Return the set of domain elements where "ma1" is lexicographically
4670 * greater than "ma2".
4672 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4673 __isl_take isl_multi_aff *ma2)
4675 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4678 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4680 #undef PW
4681 #define PW isl_pw_multi_aff
4682 #undef BASE
4683 #define BASE multi_aff
4684 #undef EL_IS_ZERO
4685 #define EL_IS_ZERO is_empty
4686 #undef ZERO
4687 #define ZERO empty
4688 #undef IS_ZERO
4689 #define IS_ZERO is_empty
4690 #undef FIELD
4691 #define FIELD maff
4692 #undef DEFAULT_IS_ZERO
4693 #define DEFAULT_IS_ZERO 0
4695 #include <isl_pw_templ.c>
4696 #include <isl_pw_un_op_templ.c>
4697 #include <isl_pw_add_constant_multi_val_templ.c>
4698 #include <isl_pw_add_constant_val_templ.c>
4699 #include <isl_pw_add_disjoint_templ.c>
4700 #include <isl_pw_bind_domain_templ.c>
4701 #include <isl_pw_fix_templ.c>
4702 #include <isl_pw_from_range_templ.c>
4703 #include <isl_pw_insert_dims_templ.c>
4704 #include <isl_pw_insert_domain_templ.c>
4705 #include <isl_pw_locals_templ.c>
4706 #include <isl_pw_move_dims_templ.c>
4707 #include <isl_pw_neg_templ.c>
4708 #include <isl_pw_pullback_templ.c>
4709 #include <isl_pw_range_tuple_id_templ.c>
4710 #include <isl_pw_union_opt.c>
4712 #undef BASE
4713 #define BASE pw_multi_aff
4715 #include <isl_union_multi.c>
4716 #include "isl_union_locals_templ.c"
4717 #include <isl_union_neg.c>
4718 #include <isl_union_sub_templ.c>
4720 #undef BASE
4721 #define BASE multi_aff
4723 #include <isl_union_pw_templ.c>
4725 /* Generic function for extracting a factor from a product "pma".
4726 * "check_space" checks that the space is that of the right kind of product.
4727 * "space_factor" extracts the factor from the space.
4728 * "multi_aff_factor" extracts the factor from the constituent functions.
4730 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4731 __isl_take isl_pw_multi_aff *pma,
4732 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4733 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4734 __isl_give isl_multi_aff *(*multi_aff_factor)(
4735 __isl_take isl_multi_aff *ma))
4737 int i;
4738 isl_space *space;
4740 if (check_space(pma) < 0)
4741 return isl_pw_multi_aff_free(pma);
4743 space = isl_pw_multi_aff_take_space(pma);
4744 space = space_factor(space);
4746 for (i = 0; pma && i < pma->n; ++i) {
4747 isl_multi_aff *ma;
4749 ma = isl_pw_multi_aff_take_base_at(pma, i);
4750 ma = multi_aff_factor(ma);
4751 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4754 pma = isl_pw_multi_aff_restore_space(pma, space);
4756 return pma;
4759 /* Is the range of "pma" a wrapped relation?
4761 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4762 __isl_keep isl_pw_multi_aff *pma)
4764 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4767 /* Check that the range of "pma" is a product.
4769 static isl_stat pw_multi_aff_check_range_product(
4770 __isl_keep isl_pw_multi_aff *pma)
4772 isl_bool wraps;
4774 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4775 if (wraps < 0)
4776 return isl_stat_error;
4777 if (!wraps)
4778 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4779 "range is not a product", return isl_stat_error);
4780 return isl_stat_ok;
4783 /* Given a function A -> [B -> C], extract the function A -> B.
4785 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4786 __isl_take isl_pw_multi_aff *pma)
4788 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4789 &isl_space_range_factor_domain,
4790 &isl_multi_aff_range_factor_domain);
4793 /* Given a function A -> [B -> C], extract the function A -> C.
4795 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4796 __isl_take isl_pw_multi_aff *pma)
4798 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4799 &isl_space_range_factor_range,
4800 &isl_multi_aff_range_factor_range);
4803 /* Given two piecewise multi affine expressions, return a piecewise
4804 * multi-affine expression defined on the union of the definition domains
4805 * of the inputs that is equal to the lexicographic maximum of the two
4806 * inputs on each cell. If only one of the two inputs is defined on
4807 * a given cell, then it is considered to be the maximum.
4809 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4810 __isl_take isl_pw_multi_aff *pma1,
4811 __isl_take isl_pw_multi_aff *pma2)
4813 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4814 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4815 &isl_multi_aff_lex_ge_set);
4818 /* Given two piecewise multi affine expressions, return a piecewise
4819 * multi-affine expression defined on the union of the definition domains
4820 * of the inputs that is equal to the lexicographic minimum of the two
4821 * inputs on each cell. If only one of the two inputs is defined on
4822 * a given cell, then it is considered to be the minimum.
4824 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4825 __isl_take isl_pw_multi_aff *pma1,
4826 __isl_take isl_pw_multi_aff *pma2)
4828 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4829 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4830 &isl_multi_aff_lex_le_set);
4833 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4834 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4836 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4837 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4838 &isl_multi_aff_add);
4841 /* Subtract "pma2" from "pma1" and return the result.
4843 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4844 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4846 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4847 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4848 &isl_multi_aff_sub);
4851 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4852 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4854 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4855 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4857 int i, j, n;
4858 isl_space *space;
4859 isl_pw_multi_aff *res;
4861 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4862 goto error;
4864 n = pma1->n * pma2->n;
4865 space = isl_space_product(isl_space_copy(pma1->dim),
4866 isl_space_copy(pma2->dim));
4867 res = isl_pw_multi_aff_alloc_size(space, n);
4869 for (i = 0; i < pma1->n; ++i) {
4870 for (j = 0; j < pma2->n; ++j) {
4871 isl_set *domain;
4872 isl_multi_aff *ma;
4874 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4875 isl_set_copy(pma2->p[j].set));
4876 ma = isl_multi_aff_product(
4877 isl_multi_aff_copy(pma1->p[i].maff),
4878 isl_multi_aff_copy(pma2->p[j].maff));
4879 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4883 isl_pw_multi_aff_free(pma1);
4884 isl_pw_multi_aff_free(pma2);
4885 return res;
4886 error:
4887 isl_pw_multi_aff_free(pma1);
4888 isl_pw_multi_aff_free(pma2);
4889 return NULL;
4892 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4893 * denominator "denom".
4894 * "denom" is allowed to be negative, in which case the actual denominator
4895 * is -denom and the expressions are added instead.
4897 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4898 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4900 int i, first;
4901 int sign;
4902 isl_int d;
4904 first = isl_seq_first_non_zero(c, n);
4905 if (first == -1)
4906 return aff;
4908 sign = isl_int_sgn(denom);
4909 isl_int_init(d);
4910 isl_int_abs(d, denom);
4911 for (i = first; i < n; ++i) {
4912 isl_aff *aff_i;
4914 if (isl_int_is_zero(c[i]))
4915 continue;
4916 aff_i = isl_multi_aff_get_aff(ma, i);
4917 aff_i = isl_aff_scale(aff_i, c[i]);
4918 aff_i = isl_aff_scale_down(aff_i, d);
4919 if (sign >= 0)
4920 aff = isl_aff_sub(aff, aff_i);
4921 else
4922 aff = isl_aff_add(aff, aff_i);
4924 isl_int_clear(d);
4926 return aff;
4929 /* Extract an affine expression that expresses the output dimension "pos"
4930 * of "bmap" in terms of the parameters and input dimensions from
4931 * equality "eq".
4932 * Note that this expression may involve integer divisions defined
4933 * in terms of parameters and input dimensions.
4934 * The equality may also involve references to earlier (but not later)
4935 * output dimensions. These are replaced by the corresponding elements
4936 * in "ma".
4938 * If the equality is of the form
4940 * f(i) + h(j) + a x + g(i) = 0,
4942 * with f(i) a linear combinations of the parameters and input dimensions,
4943 * g(i) a linear combination of integer divisions defined in terms of the same
4944 * and h(j) a linear combinations of earlier output dimensions,
4945 * then the affine expression is
4947 * (-f(i) - g(i))/a - h(j)/a
4949 * If the equality is of the form
4951 * f(i) + h(j) - a x + g(i) = 0,
4953 * then the affine expression is
4955 * (f(i) + g(i))/a - h(j)/(-a)
4958 * If "div" refers to an integer division (i.e., it is smaller than
4959 * the number of integer divisions), then the equality constraint
4960 * does involve an integer division (the one at position "div") that
4961 * is defined in terms of output dimensions. However, this integer
4962 * division can be eliminated by exploiting a pair of constraints
4963 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4964 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4965 * -l + x >= 0.
4966 * In particular, let
4968 * x = e(i) + m floor(...)
4970 * with e(i) the expression derived above and floor(...) the integer
4971 * division involving output dimensions.
4972 * From
4974 * l <= x <= l + n,
4976 * we have
4978 * 0 <= x - l <= n
4980 * This means
4982 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4983 * = (e(i) - l) mod m
4985 * Therefore,
4987 * x - l = (e(i) - l) mod m
4989 * or
4991 * x = ((e(i) - l) mod m) + l
4993 * The variable "shift" below contains the expression -l, which may
4994 * also involve a linear combination of earlier output dimensions.
4996 static __isl_give isl_aff *extract_aff_from_equality(
4997 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4998 __isl_keep isl_multi_aff *ma)
5000 unsigned o_out;
5001 isl_size n_div, n_out;
5002 isl_ctx *ctx;
5003 isl_local_space *ls;
5004 isl_aff *aff, *shift;
5005 isl_val *mod;
5007 ctx = isl_basic_map_get_ctx(bmap);
5008 ls = isl_basic_map_get_local_space(bmap);
5009 ls = isl_local_space_domain(ls);
5010 aff = isl_aff_alloc(isl_local_space_copy(ls));
5011 if (!aff)
5012 goto error;
5013 o_out = isl_basic_map_offset(bmap, isl_dim_out);
5014 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5015 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5016 if (n_out < 0 || n_div < 0)
5017 goto error;
5018 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
5019 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
5020 isl_seq_cpy(aff->v->el + 1 + o_out,
5021 bmap->eq[eq] + o_out + n_out, n_div);
5022 } else {
5023 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
5024 isl_seq_neg(aff->v->el + 1 + o_out,
5025 bmap->eq[eq] + o_out + n_out, n_div);
5027 if (div < n_div)
5028 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
5029 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
5030 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
5031 bmap->eq[eq][o_out + pos]);
5032 if (div < n_div) {
5033 shift = isl_aff_alloc(isl_local_space_copy(ls));
5034 if (!shift)
5035 goto error;
5036 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
5037 isl_seq_cpy(shift->v->el + 1 + o_out,
5038 bmap->ineq[ineq] + o_out + n_out, n_div);
5039 isl_int_set_si(shift->v->el[0], 1);
5040 shift = subtract_initial(shift, ma, pos,
5041 bmap->ineq[ineq] + o_out, ctx->negone);
5042 aff = isl_aff_add(aff, isl_aff_copy(shift));
5043 mod = isl_val_int_from_isl_int(ctx,
5044 bmap->eq[eq][o_out + n_out + div]);
5045 mod = isl_val_abs(mod);
5046 aff = isl_aff_mod_val(aff, mod);
5047 aff = isl_aff_sub(aff, shift);
5050 isl_local_space_free(ls);
5051 return aff;
5052 error:
5053 isl_local_space_free(ls);
5054 isl_aff_free(aff);
5055 return NULL;
5058 /* Given a basic map with output dimensions defined
5059 * in terms of the parameters input dimensions and earlier
5060 * output dimensions using an equality (and possibly a pair on inequalities),
5061 * extract an isl_aff that expresses output dimension "pos" in terms
5062 * of the parameters and input dimensions.
5063 * Note that this expression may involve integer divisions defined
5064 * in terms of parameters and input dimensions.
5065 * "ma" contains the expressions corresponding to earlier output dimensions.
5067 * This function shares some similarities with
5068 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
5070 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
5071 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
5073 int eq, div, ineq;
5074 isl_aff *aff;
5076 if (!bmap)
5077 return NULL;
5078 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
5079 if (eq >= bmap->n_eq)
5080 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5081 "unable to find suitable equality", return NULL);
5082 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
5084 aff = isl_aff_remove_unused_divs(aff);
5085 return aff;
5088 /* Given a basic map where each output dimension is defined
5089 * in terms of the parameters and input dimensions using an equality,
5090 * extract an isl_multi_aff that expresses the output dimensions in terms
5091 * of the parameters and input dimensions.
5093 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
5094 __isl_take isl_basic_map *bmap)
5096 int i;
5097 isl_size n_out;
5098 isl_multi_aff *ma;
5100 if (!bmap)
5101 return NULL;
5103 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
5104 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5105 if (n_out < 0)
5106 ma = isl_multi_aff_free(ma);
5108 for (i = 0; i < n_out; ++i) {
5109 isl_aff *aff;
5111 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
5112 ma = isl_multi_aff_set_aff(ma, i, aff);
5115 isl_basic_map_free(bmap);
5117 return ma;
5120 /* Given a basic set where each set dimension is defined
5121 * in terms of the parameters using an equality,
5122 * extract an isl_multi_aff that expresses the set dimensions in terms
5123 * of the parameters.
5125 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
5126 __isl_take isl_basic_set *bset)
5128 return extract_isl_multi_aff_from_basic_map(bset);
5131 /* Create an isl_pw_multi_aff that is equivalent to
5132 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
5133 * The given basic map is such that each output dimension is defined
5134 * in terms of the parameters and input dimensions using an equality.
5136 * Since some applications expect the result of isl_pw_multi_aff_from_map
5137 * to only contain integer affine expressions, we compute the floor
5138 * of the expression before returning.
5140 * Remove all constraints involving local variables without
5141 * an explicit representation (resulting in the removal of those
5142 * local variables) prior to the actual extraction to ensure
5143 * that the local spaces in which the resulting affine expressions
5144 * are created do not contain any unknown local variables.
5145 * Removing such constraints is safe because constraints involving
5146 * unknown local variables are not used to determine whether
5147 * a basic map is obviously single-valued.
5149 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
5150 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
5152 isl_multi_aff *ma;
5154 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
5155 ma = extract_isl_multi_aff_from_basic_map(bmap);
5156 ma = isl_multi_aff_floor(ma);
5157 return isl_pw_multi_aff_alloc(domain, ma);
5160 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5161 * This obviously only works if the input "map" is single-valued.
5162 * If so, we compute the lexicographic minimum of the image in the form
5163 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5164 * to its lexicographic minimum.
5165 * If the input is not single-valued, we produce an error.
5167 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
5168 __isl_take isl_map *map)
5170 int i;
5171 int sv;
5172 isl_pw_multi_aff *pma;
5174 sv = isl_map_is_single_valued(map);
5175 if (sv < 0)
5176 goto error;
5177 if (!sv)
5178 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5179 "map is not single-valued", goto error);
5180 map = isl_map_make_disjoint(map);
5181 if (!map)
5182 return NULL;
5184 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5186 for (i = 0; i < map->n; ++i) {
5187 isl_pw_multi_aff *pma_i;
5188 isl_basic_map *bmap;
5189 bmap = isl_basic_map_copy(map->p[i]);
5190 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5191 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5194 isl_map_free(map);
5195 return pma;
5196 error:
5197 isl_map_free(map);
5198 return NULL;
5201 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5202 * taking into account that the output dimension at position "d"
5203 * can be represented as
5205 * x = floor((e(...) + c1) / m)
5207 * given that constraint "i" is of the form
5209 * e(...) + c1 - m x >= 0
5212 * Let "map" be of the form
5214 * A -> B
5216 * We construct a mapping
5218 * A -> [A -> x = floor(...)]
5220 * apply that to the map, obtaining
5222 * [A -> x = floor(...)] -> B
5224 * and equate dimension "d" to x.
5225 * We then compute a isl_pw_multi_aff representation of the resulting map
5226 * and plug in the mapping above.
5228 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5229 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5231 isl_ctx *ctx;
5232 isl_space *space = NULL;
5233 isl_local_space *ls;
5234 isl_multi_aff *ma;
5235 isl_aff *aff;
5236 isl_vec *v;
5237 isl_map *insert;
5238 int offset;
5239 isl_size n;
5240 isl_size n_in;
5241 isl_pw_multi_aff *pma;
5242 isl_bool is_set;
5244 is_set = isl_map_is_set(map);
5245 if (is_set < 0)
5246 goto error;
5248 offset = isl_basic_map_offset(hull, isl_dim_out);
5249 ctx = isl_map_get_ctx(map);
5250 space = isl_space_domain(isl_map_get_space(map));
5251 n_in = isl_space_dim(space, isl_dim_set);
5252 n = isl_space_dim(space, isl_dim_all);
5253 if (n_in < 0 || n < 0)
5254 goto error;
5256 v = isl_vec_alloc(ctx, 1 + 1 + n);
5257 if (v) {
5258 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5259 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5261 isl_basic_map_free(hull);
5263 ls = isl_local_space_from_space(isl_space_copy(space));
5264 aff = isl_aff_alloc_vec_validated(ls, v);
5265 aff = isl_aff_floor(aff);
5266 if (is_set) {
5267 isl_space_free(space);
5268 ma = isl_multi_aff_from_aff(aff);
5269 } else {
5270 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5271 ma = isl_multi_aff_range_product(ma,
5272 isl_multi_aff_from_aff(aff));
5275 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5276 map = isl_map_apply_domain(map, insert);
5277 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5278 pma = isl_pw_multi_aff_from_map(map);
5279 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5281 return pma;
5282 error:
5283 isl_space_free(space);
5284 isl_map_free(map);
5285 isl_basic_map_free(hull);
5286 return NULL;
5289 /* Is constraint "c" of the form
5291 * e(...) + c1 - m x >= 0
5293 * or
5295 * -e(...) + c2 + m x >= 0
5297 * where m > 1 and e only depends on parameters and input dimensions?
5299 * "offset" is the offset of the output dimensions
5300 * "pos" is the position of output dimension x.
5302 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5304 if (isl_int_is_zero(c[offset + d]))
5305 return 0;
5306 if (isl_int_is_one(c[offset + d]))
5307 return 0;
5308 if (isl_int_is_negone(c[offset + d]))
5309 return 0;
5310 if (isl_seq_first_non_zero(c + offset, d) != -1)
5311 return 0;
5312 if (isl_seq_first_non_zero(c + offset + d + 1,
5313 total - (offset + d + 1)) != -1)
5314 return 0;
5315 return 1;
5318 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5320 * As a special case, we first check if there is any pair of constraints,
5321 * shared by all the basic maps in "map" that force a given dimension
5322 * to be equal to the floor of some affine combination of the input dimensions.
5324 * In particular, if we can find two constraints
5326 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5328 * and
5330 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5332 * where m > 1 and e only depends on parameters and input dimensions,
5333 * and such that
5335 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5337 * then we know that we can take
5339 * x = floor((e(...) + c1) / m)
5341 * without having to perform any computation.
5343 * Note that we know that
5345 * c1 + c2 >= 1
5347 * If c1 + c2 were 0, then we would have detected an equality during
5348 * simplification. If c1 + c2 were negative, then we would have detected
5349 * a contradiction.
5351 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5352 __isl_take isl_map *map)
5354 int d;
5355 isl_size dim;
5356 int i, j, n;
5357 int offset;
5358 isl_size total;
5359 isl_int sum;
5360 isl_basic_map *hull;
5362 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5363 dim = isl_map_dim(map, isl_dim_out);
5364 total = isl_basic_map_dim(hull, isl_dim_all);
5365 if (dim < 0 || total < 0)
5366 goto error;
5368 isl_int_init(sum);
5369 offset = isl_basic_map_offset(hull, isl_dim_out);
5370 n = hull->n_ineq;
5371 for (d = 0; d < dim; ++d) {
5372 for (i = 0; i < n; ++i) {
5373 if (!is_potential_div_constraint(hull->ineq[i],
5374 offset, d, 1 + total))
5375 continue;
5376 for (j = i + 1; j < n; ++j) {
5377 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5378 hull->ineq[j] + 1, total))
5379 continue;
5380 isl_int_add(sum, hull->ineq[i][0],
5381 hull->ineq[j][0]);
5382 if (isl_int_abs_lt(sum,
5383 hull->ineq[i][offset + d]))
5384 break;
5387 if (j >= n)
5388 continue;
5389 isl_int_clear(sum);
5390 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5391 j = i;
5392 return pw_multi_aff_from_map_div(map, hull, d, j);
5395 isl_int_clear(sum);
5396 isl_basic_map_free(hull);
5397 return pw_multi_aff_from_map_base(map);
5398 error:
5399 isl_map_free(map);
5400 isl_basic_map_free(hull);
5401 return NULL;
5404 /* Given an affine expression
5406 * [A -> B] -> f(A,B)
5408 * construct an isl_multi_aff
5410 * [A -> B] -> B'
5412 * such that dimension "d" in B' is set to "aff" and the remaining
5413 * dimensions are set equal to the corresponding dimensions in B.
5414 * "n_in" is the dimension of the space A.
5415 * "n_out" is the dimension of the space B.
5417 * If "is_set" is set, then the affine expression is of the form
5419 * [B] -> f(B)
5421 * and we construct an isl_multi_aff
5423 * B -> B'
5425 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5426 unsigned n_in, unsigned n_out, int is_set)
5428 int i;
5429 isl_multi_aff *ma;
5430 isl_space *space, *space2;
5431 isl_local_space *ls;
5433 space = isl_aff_get_domain_space(aff);
5434 ls = isl_local_space_from_space(isl_space_copy(space));
5435 space2 = isl_space_copy(space);
5436 if (!is_set)
5437 space2 = isl_space_range(isl_space_unwrap(space2));
5438 space = isl_space_map_from_domain_and_range(space, space2);
5439 ma = isl_multi_aff_alloc(space);
5440 ma = isl_multi_aff_set_aff(ma, d, aff);
5442 for (i = 0; i < n_out; ++i) {
5443 if (i == d)
5444 continue;
5445 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5446 isl_dim_set, n_in + i);
5447 ma = isl_multi_aff_set_aff(ma, i, aff);
5450 isl_local_space_free(ls);
5452 return ma;
5455 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5456 * taking into account that the dimension at position "d" can be written as
5458 * x = m a + f(..) (1)
5460 * where m is equal to "gcd".
5461 * "i" is the index of the equality in "hull" that defines f(..).
5462 * In particular, the equality is of the form
5464 * f(..) - x + m g(existentials) = 0
5466 * or
5468 * -f(..) + x + m g(existentials) = 0
5470 * We basically plug (1) into "map", resulting in a map with "a"
5471 * in the range instead of "x". The corresponding isl_pw_multi_aff
5472 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5474 * Specifically, given the input map
5476 * A -> B
5478 * We first wrap it into a set
5480 * [A -> B]
5482 * and define (1) on top of the corresponding space, resulting in "aff".
5483 * We use this to create an isl_multi_aff that maps the output position "d"
5484 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5485 * We plug this into the wrapped map, unwrap the result and compute the
5486 * corresponding isl_pw_multi_aff.
5487 * The result is an expression
5489 * A -> T(A)
5491 * We adjust that to
5493 * A -> [A -> T(A)]
5495 * so that we can plug that into "aff", after extending the latter to
5496 * a mapping
5498 * [A -> B] -> B'
5501 * If "map" is actually a set, then there is no "A" space, meaning
5502 * that we do not need to perform any wrapping, and that the result
5503 * of the recursive call is of the form
5505 * [T]
5507 * which is plugged into a mapping of the form
5509 * B -> B'
5511 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5512 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5513 isl_int gcd)
5515 isl_set *set;
5516 isl_space *space;
5517 isl_local_space *ls;
5518 isl_aff *aff;
5519 isl_multi_aff *ma;
5520 isl_pw_multi_aff *pma, *id;
5521 isl_size n_in;
5522 unsigned o_out;
5523 isl_size n_out;
5524 isl_bool is_set;
5526 is_set = isl_map_is_set(map);
5527 if (is_set < 0)
5528 goto error;
5530 n_in = isl_basic_map_dim(hull, isl_dim_in);
5531 n_out = isl_basic_map_dim(hull, isl_dim_out);
5532 if (n_in < 0 || n_out < 0)
5533 goto error;
5534 o_out = isl_basic_map_offset(hull, isl_dim_out);
5536 if (is_set)
5537 set = map;
5538 else
5539 set = isl_map_wrap(map);
5540 space = isl_space_map_from_set(isl_set_get_space(set));
5541 ma = isl_multi_aff_identity(space);
5542 ls = isl_local_space_from_space(isl_set_get_space(set));
5543 aff = isl_aff_alloc(ls);
5544 if (aff) {
5545 isl_int_set_si(aff->v->el[0], 1);
5546 if (isl_int_is_one(hull->eq[i][o_out + d]))
5547 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5548 aff->v->size - 1);
5549 else
5550 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5551 aff->v->size - 1);
5552 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5554 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5555 set = isl_set_preimage_multi_aff(set, ma);
5557 ma = range_map(aff, d, n_in, n_out, is_set);
5559 if (is_set)
5560 map = set;
5561 else
5562 map = isl_set_unwrap(set);
5563 pma = isl_pw_multi_aff_from_map(map);
5565 if (!is_set) {
5566 space = isl_pw_multi_aff_get_domain_space(pma);
5567 space = isl_space_map_from_set(space);
5568 id = isl_pw_multi_aff_identity(space);
5569 pma = isl_pw_multi_aff_range_product(id, pma);
5571 id = isl_pw_multi_aff_from_multi_aff(ma);
5572 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5574 isl_basic_map_free(hull);
5575 return pma;
5576 error:
5577 isl_map_free(map);
5578 isl_basic_map_free(hull);
5579 return NULL;
5582 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5583 * "hull" contains the equalities valid for "map".
5585 * Check if any of the output dimensions is "strided".
5586 * That is, we check if it can be written as
5588 * x = m a + f(..)
5590 * with m greater than 1, a some combination of existentially quantified
5591 * variables and f an expression in the parameters and input dimensions.
5592 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5594 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5595 * special case.
5597 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5598 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5600 int i, j;
5601 isl_size n_out;
5602 unsigned o_out;
5603 isl_size n_div;
5604 unsigned o_div;
5605 isl_int gcd;
5607 n_div = isl_basic_map_dim(hull, isl_dim_div);
5608 n_out = isl_basic_map_dim(hull, isl_dim_out);
5609 if (n_div < 0 || n_out < 0)
5610 goto error;
5612 if (n_div == 0) {
5613 isl_basic_map_free(hull);
5614 return pw_multi_aff_from_map_check_div(map);
5617 isl_int_init(gcd);
5619 o_div = isl_basic_map_offset(hull, isl_dim_div);
5620 o_out = isl_basic_map_offset(hull, isl_dim_out);
5622 for (i = 0; i < n_out; ++i) {
5623 for (j = 0; j < hull->n_eq; ++j) {
5624 isl_int *eq = hull->eq[j];
5625 isl_pw_multi_aff *res;
5627 if (!isl_int_is_one(eq[o_out + i]) &&
5628 !isl_int_is_negone(eq[o_out + i]))
5629 continue;
5630 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5631 continue;
5632 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5633 n_out - (i + 1)) != -1)
5634 continue;
5635 isl_seq_gcd(eq + o_div, n_div, &gcd);
5636 if (isl_int_is_zero(gcd))
5637 continue;
5638 if (isl_int_is_one(gcd))
5639 continue;
5641 res = pw_multi_aff_from_map_stride(map, hull,
5642 i, j, gcd);
5643 isl_int_clear(gcd);
5644 return res;
5648 isl_int_clear(gcd);
5649 isl_basic_map_free(hull);
5650 return pw_multi_aff_from_map_check_div(map);
5651 error:
5652 isl_map_free(map);
5653 isl_basic_map_free(hull);
5654 return NULL;
5657 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5659 * As a special case, we first check if all output dimensions are uniquely
5660 * defined in terms of the parameters and input dimensions over the entire
5661 * domain. If so, we extract the desired isl_pw_multi_aff directly
5662 * from the affine hull of "map" and its domain.
5664 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5665 * special cases.
5667 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5669 isl_bool sv;
5670 isl_size n;
5671 isl_basic_map *hull;
5673 n = isl_map_n_basic_map(map);
5674 if (n < 0)
5675 goto error;
5677 if (n == 1) {
5678 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5679 hull = isl_basic_map_plain_affine_hull(hull);
5680 sv = isl_basic_map_plain_is_single_valued(hull);
5681 if (sv >= 0 && sv)
5682 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5683 hull);
5684 isl_basic_map_free(hull);
5686 map = isl_map_detect_equalities(map);
5687 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5688 sv = isl_basic_map_plain_is_single_valued(hull);
5689 if (sv >= 0 && sv)
5690 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5691 if (sv >= 0)
5692 return pw_multi_aff_from_map_check_strides(map, hull);
5693 isl_basic_map_free(hull);
5694 error:
5695 isl_map_free(map);
5696 return NULL;
5699 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5700 * but is considered as a function on an isl_map when exported.
5702 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5704 return isl_pw_multi_aff_from_map(map);
5707 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5709 return isl_pw_multi_aff_from_map(set);
5712 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5713 * but is considered as a function on an isl_set when exported.
5715 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5717 return isl_pw_multi_aff_from_set(set);
5720 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5721 * add it to *user.
5723 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5725 isl_union_pw_multi_aff **upma = user;
5726 isl_pw_multi_aff *pma;
5728 pma = isl_pw_multi_aff_from_map(map);
5729 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5731 return *upma ? isl_stat_ok : isl_stat_error;
5734 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5735 * domain.
5737 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5738 __isl_take isl_aff *aff)
5740 isl_multi_aff *ma;
5741 isl_pw_multi_aff *pma;
5743 ma = isl_multi_aff_from_aff(aff);
5744 pma = isl_pw_multi_aff_from_multi_aff(ma);
5745 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5748 /* Try and create an isl_union_pw_multi_aff that is equivalent
5749 * to the given isl_union_map.
5750 * The isl_union_map is required to be single-valued in each space.
5751 * Otherwise, an error is produced.
5753 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5754 __isl_take isl_union_map *umap)
5756 isl_space *space;
5757 isl_union_pw_multi_aff *upma;
5759 space = isl_union_map_get_space(umap);
5760 upma = isl_union_pw_multi_aff_empty(space);
5761 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5762 upma = isl_union_pw_multi_aff_free(upma);
5763 isl_union_map_free(umap);
5765 return upma;
5768 /* This function performs the same operation as
5769 * isl_union_pw_multi_aff_from_union_map,
5770 * but is considered as a function on an isl_union_map when exported.
5772 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5773 __isl_take isl_union_map *umap)
5775 return isl_union_pw_multi_aff_from_union_map(umap);
5778 /* Try and create an isl_union_pw_multi_aff that is equivalent
5779 * to the given isl_union_set.
5780 * The isl_union_set is required to be a singleton in each space.
5781 * Otherwise, an error is produced.
5783 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5784 __isl_take isl_union_set *uset)
5786 return isl_union_pw_multi_aff_from_union_map(uset);
5789 /* Return the piecewise affine expression "set ? 1 : 0".
5791 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5793 isl_pw_aff *pa;
5794 isl_space *space = isl_set_get_space(set);
5795 isl_local_space *ls = isl_local_space_from_space(space);
5796 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5797 isl_aff *one = isl_aff_zero_on_domain(ls);
5799 one = isl_aff_add_constant_si(one, 1);
5800 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5801 set = isl_set_complement(set);
5802 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5804 return pa;
5807 /* Plug in "subs" for dimension "type", "pos" of "aff".
5809 * Let i be the dimension to replace and let "subs" be of the form
5811 * f/d
5813 * and "aff" of the form
5815 * (a i + g)/m
5817 * The result is
5819 * (a f + d g')/(m d)
5821 * where g' is the result of plugging in "subs" in each of the integer
5822 * divisions in g.
5824 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5825 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5827 isl_ctx *ctx;
5828 isl_int v;
5829 isl_size n_div;
5831 aff = isl_aff_cow(aff);
5832 if (!aff || !subs)
5833 return isl_aff_free(aff);
5835 ctx = isl_aff_get_ctx(aff);
5836 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5837 isl_die(ctx, isl_error_invalid,
5838 "spaces don't match", return isl_aff_free(aff));
5839 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5840 if (n_div < 0)
5841 return isl_aff_free(aff);
5842 if (n_div != 0)
5843 isl_die(ctx, isl_error_unsupported,
5844 "cannot handle divs yet", return isl_aff_free(aff));
5846 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5847 if (!aff->ls)
5848 return isl_aff_free(aff);
5850 aff->v = isl_vec_cow(aff->v);
5851 if (!aff->v)
5852 return isl_aff_free(aff);
5854 pos += isl_local_space_offset(aff->ls, type);
5856 isl_int_init(v);
5857 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5858 aff->v->size, subs->v->size, v);
5859 isl_int_clear(v);
5861 return aff;
5864 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5865 * expressions in "maff".
5867 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5868 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5869 __isl_keep isl_aff *subs)
5871 isl_size n;
5872 int i;
5874 n = isl_multi_aff_size(maff);
5875 if (n < 0 || !subs)
5876 return isl_multi_aff_free(maff);
5878 if (type == isl_dim_in)
5879 type = isl_dim_set;
5881 for (i = 0; i < n; ++i) {
5882 isl_aff *aff;
5884 aff = isl_multi_aff_take_at(maff, i);
5885 aff = isl_aff_substitute(aff, type, pos, subs);
5886 maff = isl_multi_aff_restore_at(maff, i, aff);
5889 return maff;
5892 /* Plug in "subs" for input dimension "pos" of "pma".
5894 * pma is of the form
5896 * A_i(v) -> M_i(v)
5898 * while subs is of the form
5900 * v' = B_j(v) -> S_j
5902 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5903 * has a contribution in the result, in particular
5905 * C_ij(S_j) -> M_i(S_j)
5907 * Note that plugging in S_j in C_ij may also result in an empty set
5908 * and this contribution should simply be discarded.
5910 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5911 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5912 __isl_keep isl_pw_aff *subs)
5914 int i, j, n;
5915 isl_pw_multi_aff *res;
5917 if (!pma || !subs)
5918 return isl_pw_multi_aff_free(pma);
5920 n = pma->n * subs->n;
5921 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5923 for (i = 0; i < pma->n; ++i) {
5924 for (j = 0; j < subs->n; ++j) {
5925 isl_set *common;
5926 isl_multi_aff *res_ij;
5927 int empty;
5929 common = isl_set_intersect(
5930 isl_set_copy(pma->p[i].set),
5931 isl_set_copy(subs->p[j].set));
5932 common = isl_set_substitute(common,
5933 pos, subs->p[j].aff);
5934 empty = isl_set_plain_is_empty(common);
5935 if (empty < 0 || empty) {
5936 isl_set_free(common);
5937 if (empty < 0)
5938 goto error;
5939 continue;
5942 res_ij = isl_multi_aff_substitute(
5943 isl_multi_aff_copy(pma->p[i].maff),
5944 isl_dim_in, pos, subs->p[j].aff);
5946 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5950 isl_pw_multi_aff_free(pma);
5951 return res;
5952 error:
5953 isl_pw_multi_aff_free(pma);
5954 isl_pw_multi_aff_free(res);
5955 return NULL;
5958 /* Compute the preimage of a range of dimensions in the affine expression "src"
5959 * under "ma" and put the result in "dst". The number of dimensions in "src"
5960 * that precede the range is given by "n_before". The number of dimensions
5961 * in the range is given by the number of output dimensions of "ma".
5962 * The number of dimensions that follow the range is given by "n_after".
5963 * If "has_denom" is set (to one),
5964 * then "src" and "dst" have an extra initial denominator.
5965 * "n_div_ma" is the number of existentials in "ma"
5966 * "n_div_bset" is the number of existentials in "src"
5967 * The resulting "dst" (which is assumed to have been allocated by
5968 * the caller) contains coefficients for both sets of existentials,
5969 * first those in "ma" and then those in "src".
5970 * f, c1, c2 and g are temporary objects that have been initialized
5971 * by the caller.
5973 * Let src represent the expression
5975 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5977 * and let ma represent the expressions
5979 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5981 * We start out with the following expression for dst:
5983 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5985 * with the multiplication factor f initially equal to 1
5986 * and f \sum_i b_i v_i kept separately.
5987 * For each x_i that we substitute, we multiply the numerator
5988 * (and denominator) of dst by c_1 = m_i and add the numerator
5989 * of the x_i expression multiplied by c_2 = f b_i,
5990 * after removing the common factors of c_1 and c_2.
5991 * The multiplication factor f also needs to be multiplied by c_1
5992 * for the next x_j, j > i.
5994 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5995 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5996 int n_div_ma, int n_div_bmap,
5997 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5999 int i;
6000 isl_size n_param, n_in, n_out;
6001 int o_dst, o_src;
6003 n_param = isl_multi_aff_dim(ma, isl_dim_param);
6004 n_in = isl_multi_aff_dim(ma, isl_dim_in);
6005 n_out = isl_multi_aff_dim(ma, isl_dim_out);
6006 if (n_param < 0 || n_in < 0 || n_out < 0)
6007 return isl_stat_error;
6009 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
6010 o_dst = o_src = has_denom + 1 + n_param + n_before;
6011 isl_seq_clr(dst + o_dst, n_in);
6012 o_dst += n_in;
6013 o_src += n_out;
6014 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
6015 o_dst += n_after;
6016 o_src += n_after;
6017 isl_seq_clr(dst + o_dst, n_div_ma);
6018 o_dst += n_div_ma;
6019 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
6021 isl_int_set_si(f, 1);
6023 for (i = 0; i < n_out; ++i) {
6024 int offset = has_denom + 1 + n_param + n_before + i;
6026 if (isl_int_is_zero(src[offset]))
6027 continue;
6028 isl_int_set(c1, ma->u.p[i]->v->el[0]);
6029 isl_int_mul(c2, f, src[offset]);
6030 isl_int_gcd(g, c1, c2);
6031 isl_int_divexact(c1, c1, g);
6032 isl_int_divexact(c2, c2, g);
6034 isl_int_mul(f, f, c1);
6035 o_dst = has_denom;
6036 o_src = 1;
6037 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
6038 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
6039 o_dst += 1 + n_param;
6040 o_src += 1 + n_param;
6041 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
6042 o_dst += n_before;
6043 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
6044 c2, ma->u.p[i]->v->el + o_src, n_in);
6045 o_dst += n_in;
6046 o_src += n_in;
6047 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
6048 o_dst += n_after;
6049 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
6050 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
6051 o_dst += n_div_ma;
6052 o_src += n_div_ma;
6053 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
6054 if (has_denom)
6055 isl_int_mul(dst[0], dst[0], c1);
6058 return isl_stat_ok;
6061 /* Compute the pullback of "aff" by the function represented by "ma".
6062 * In other words, plug in "ma" in "aff". The result is an affine expression
6063 * defined over the domain space of "ma".
6065 * If "aff" is represented by
6067 * (a(p) + b x + c(divs))/d
6069 * and ma is represented by
6071 * x = D(p) + F(y) + G(divs')
6073 * then the result is
6075 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
6077 * The divs in the local space of the input are similarly adjusted
6078 * through a call to isl_local_space_preimage_multi_aff.
6080 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
6081 __isl_take isl_multi_aff *ma)
6083 isl_aff *res = NULL;
6084 isl_local_space *ls;
6085 isl_size n_div_aff, n_div_ma;
6086 isl_int f, c1, c2, g;
6088 ma = isl_multi_aff_align_divs(ma);
6089 if (!aff || !ma)
6090 goto error;
6092 n_div_aff = isl_aff_dim(aff, isl_dim_div);
6093 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
6094 if (n_div_aff < 0 || n_div_ma < 0)
6095 goto error;
6097 ls = isl_aff_get_domain_local_space(aff);
6098 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
6099 res = isl_aff_alloc(ls);
6100 if (!res)
6101 goto error;
6103 isl_int_init(f);
6104 isl_int_init(c1);
6105 isl_int_init(c2);
6106 isl_int_init(g);
6108 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
6109 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
6110 res = isl_aff_free(res);
6112 isl_int_clear(f);
6113 isl_int_clear(c1);
6114 isl_int_clear(c2);
6115 isl_int_clear(g);
6117 isl_aff_free(aff);
6118 isl_multi_aff_free(ma);
6119 res = isl_aff_normalize(res);
6120 return res;
6121 error:
6122 isl_aff_free(aff);
6123 isl_multi_aff_free(ma);
6124 isl_aff_free(res);
6125 return NULL;
6128 /* Compute the pullback of "aff1" by the function represented by "aff2".
6129 * In other words, plug in "aff2" in "aff1". The result is an affine expression
6130 * defined over the domain space of "aff1".
6132 * The domain of "aff1" should match the range of "aff2", which means
6133 * that it should be single-dimensional.
6135 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
6136 __isl_take isl_aff *aff2)
6138 isl_multi_aff *ma;
6140 ma = isl_multi_aff_from_aff(aff2);
6141 return isl_aff_pullback_multi_aff(aff1, ma);
6144 /* Compute the pullback of "ma1" by the function represented by "ma2".
6145 * In other words, plug in "ma2" in "ma1".
6147 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
6148 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
6150 int i;
6151 isl_size n;
6152 isl_space *space = NULL;
6154 isl_multi_aff_align_params_bin(&ma1, &ma2);
6155 ma2 = isl_multi_aff_align_divs(ma2);
6156 n = isl_multi_aff_size(ma1);
6157 if (n < 0 || !ma2)
6158 goto error;
6160 space = isl_space_join(isl_multi_aff_get_space(ma2),
6161 isl_multi_aff_get_space(ma1));
6163 for (i = 0; i < n; ++i) {
6164 isl_aff *aff;
6166 aff = isl_multi_aff_take_at(ma1, i);
6167 aff = isl_aff_pullback_multi_aff(aff, isl_multi_aff_copy(ma2));
6168 ma1 = isl_multi_aff_restore_at(ma1, i, aff);
6171 ma1 = isl_multi_aff_reset_space(ma1, space);
6172 isl_multi_aff_free(ma2);
6173 return ma1;
6174 error:
6175 isl_space_free(space);
6176 isl_multi_aff_free(ma2);
6177 isl_multi_aff_free(ma1);
6178 return NULL;
6181 /* Extend the local space of "dst" to include the divs
6182 * in the local space of "src".
6184 * If "src" does not have any divs or if the local spaces of "dst" and
6185 * "src" are the same, then no extension is required.
6187 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6188 __isl_keep isl_aff *src)
6190 isl_ctx *ctx;
6191 isl_size src_n_div, dst_n_div;
6192 int *exp1 = NULL;
6193 int *exp2 = NULL;
6194 isl_bool equal;
6195 isl_mat *div;
6197 if (!src || !dst)
6198 return isl_aff_free(dst);
6200 ctx = isl_aff_get_ctx(src);
6201 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6202 if (equal < 0)
6203 return isl_aff_free(dst);
6204 if (!equal)
6205 isl_die(ctx, isl_error_invalid,
6206 "spaces don't match", goto error);
6208 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6209 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6210 if (src_n_div == 0)
6211 return dst;
6212 equal = isl_local_space_is_equal(src->ls, dst->ls);
6213 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6214 return isl_aff_free(dst);
6215 if (equal)
6216 return dst;
6218 exp1 = isl_alloc_array(ctx, int, src_n_div);
6219 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6220 if (!exp1 || (dst_n_div && !exp2))
6221 goto error;
6223 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6224 dst = isl_aff_expand_divs(dst, div, exp2);
6225 free(exp1);
6226 free(exp2);
6228 return dst;
6229 error:
6230 free(exp1);
6231 free(exp2);
6232 return isl_aff_free(dst);
6235 /* Adjust the local spaces of the affine expressions in "maff"
6236 * such that they all have the save divs.
6238 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6239 __isl_take isl_multi_aff *maff)
6241 isl_aff *aff_0;
6242 isl_size n;
6243 int i;
6245 n = isl_multi_aff_size(maff);
6246 if (n < 0)
6247 return isl_multi_aff_free(maff);
6248 if (n <= 1)
6249 return maff;
6251 aff_0 = isl_multi_aff_take_at(maff, 0);
6252 for (i = 1; i < n; ++i) {
6253 isl_aff *aff_i;
6255 aff_i = isl_multi_aff_peek_at(maff, i);
6256 aff_0 = isl_aff_align_divs(aff_0, aff_i);
6258 maff = isl_multi_aff_restore_at(maff, 0, aff_0);
6260 aff_0 = isl_multi_aff_peek_at(maff, 0);
6261 for (i = 1; i < n; ++i) {
6262 isl_aff *aff_i;
6264 aff_i = isl_multi_aff_take_at(maff, i);
6265 aff_i = isl_aff_align_divs(aff_i, aff_0);
6266 maff = isl_multi_aff_restore_at(maff, i, aff_i);
6269 return maff;
6272 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6274 aff = isl_aff_cow(aff);
6275 if (!aff)
6276 return NULL;
6278 aff->ls = isl_local_space_lift(aff->ls);
6279 if (!aff->ls)
6280 return isl_aff_free(aff);
6282 return aff;
6285 /* Lift "maff" to a space with extra dimensions such that the result
6286 * has no more existentially quantified variables.
6287 * If "ls" is not NULL, then *ls is assigned the local space that lies
6288 * at the basis of the lifting applied to "maff".
6290 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6291 __isl_give isl_local_space **ls)
6293 int i;
6294 isl_space *space;
6295 isl_aff *aff;
6296 isl_size n, n_div;
6298 if (ls)
6299 *ls = NULL;
6301 n = isl_multi_aff_size(maff);
6302 if (n < 0)
6303 return isl_multi_aff_free(maff);
6305 if (n == 0) {
6306 if (ls) {
6307 isl_space *space = isl_multi_aff_get_domain_space(maff);
6308 *ls = isl_local_space_from_space(space);
6309 if (!*ls)
6310 return isl_multi_aff_free(maff);
6312 return maff;
6315 maff = isl_multi_aff_align_divs(maff);
6317 aff = isl_multi_aff_peek_at(maff, 0);
6318 n_div = isl_aff_dim(aff, isl_dim_div);
6319 if (n_div < 0)
6320 return isl_multi_aff_free(maff);
6321 space = isl_multi_aff_get_space(maff);
6322 space = isl_space_lift(isl_space_domain(space), n_div);
6323 space = isl_space_extend_domain_with_range(space,
6324 isl_multi_aff_get_space(maff));
6325 maff = isl_multi_aff_restore_space(maff, space);
6327 if (ls) {
6328 aff = isl_multi_aff_peek_at(maff, 0);
6329 *ls = isl_aff_get_domain_local_space(aff);
6330 if (!*ls)
6331 return isl_multi_aff_free(maff);
6334 for (i = 0; i < n; ++i) {
6335 aff = isl_multi_aff_take_at(maff, i);
6336 aff = isl_aff_lift(aff);
6337 maff = isl_multi_aff_restore_at(maff, i, aff);
6340 return maff;
6343 #undef TYPE
6344 #define TYPE isl_pw_multi_aff
6345 static
6346 #include "check_type_range_templ.c"
6348 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6350 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6351 __isl_keep isl_pw_multi_aff *pma, int pos)
6353 int i;
6354 isl_size n_out;
6355 isl_space *space;
6356 isl_pw_aff *pa;
6358 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6359 return NULL;
6361 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6362 if (n_out < 0)
6363 return NULL;
6365 space = isl_pw_multi_aff_get_space(pma);
6366 space = isl_space_drop_dims(space, isl_dim_out,
6367 pos + 1, n_out - pos - 1);
6368 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6370 pa = isl_pw_aff_alloc_size(space, pma->n);
6371 for (i = 0; i < pma->n; ++i) {
6372 isl_aff *aff;
6373 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6374 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6377 return pa;
6380 /* This is an alternative name for the function above.
6382 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6383 __isl_keep isl_pw_multi_aff *pma, int pos)
6385 return isl_pw_multi_aff_get_at(pma, pos);
6388 /* Return an isl_pw_multi_aff with the given "set" as domain and
6389 * an unnamed zero-dimensional range.
6391 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6392 __isl_take isl_set *set)
6394 isl_multi_aff *ma;
6395 isl_space *space;
6397 space = isl_set_get_space(set);
6398 space = isl_space_from_domain(space);
6399 ma = isl_multi_aff_zero(space);
6400 return isl_pw_multi_aff_alloc(set, ma);
6403 /* Add an isl_pw_multi_aff with the given "set" as domain and
6404 * an unnamed zero-dimensional range to *user.
6406 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6407 void *user)
6409 isl_union_pw_multi_aff **upma = user;
6410 isl_pw_multi_aff *pma;
6412 pma = isl_pw_multi_aff_from_domain(set);
6413 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6415 return isl_stat_ok;
6418 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6419 * an unnamed zero-dimensional range.
6421 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6422 __isl_take isl_union_set *uset)
6424 isl_space *space;
6425 isl_union_pw_multi_aff *upma;
6427 if (!uset)
6428 return NULL;
6430 space = isl_union_set_get_space(uset);
6431 upma = isl_union_pw_multi_aff_empty(space);
6433 if (isl_union_set_foreach_set(uset,
6434 &add_pw_multi_aff_from_domain, &upma) < 0)
6435 goto error;
6437 isl_union_set_free(uset);
6438 return upma;
6439 error:
6440 isl_union_set_free(uset);
6441 isl_union_pw_multi_aff_free(upma);
6442 return NULL;
6445 /* Local data for bin_entry and the callback "fn".
6447 struct isl_union_pw_multi_aff_bin_data {
6448 isl_union_pw_multi_aff *upma2;
6449 isl_union_pw_multi_aff *res;
6450 isl_pw_multi_aff *pma;
6451 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6454 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6455 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6457 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6459 struct isl_union_pw_multi_aff_bin_data *data = user;
6460 isl_stat r;
6462 data->pma = pma;
6463 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6464 data->fn, data);
6465 isl_pw_multi_aff_free(pma);
6467 return r;
6470 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6471 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6472 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6473 * as *entry. The callback should adjust data->res if desired.
6475 static __isl_give isl_union_pw_multi_aff *bin_op(
6476 __isl_take isl_union_pw_multi_aff *upma1,
6477 __isl_take isl_union_pw_multi_aff *upma2,
6478 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6480 isl_space *space;
6481 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6483 space = isl_union_pw_multi_aff_get_space(upma2);
6484 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6485 space = isl_union_pw_multi_aff_get_space(upma1);
6486 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6488 if (!upma1 || !upma2)
6489 goto error;
6491 data.upma2 = upma2;
6492 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6493 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6494 &bin_entry, &data) < 0)
6495 goto error;
6497 isl_union_pw_multi_aff_free(upma1);
6498 isl_union_pw_multi_aff_free(upma2);
6499 return data.res;
6500 error:
6501 isl_union_pw_multi_aff_free(upma1);
6502 isl_union_pw_multi_aff_free(upma2);
6503 isl_union_pw_multi_aff_free(data.res);
6504 return NULL;
6507 /* Given two isl_pw_multi_affs A -> B and C -> D,
6508 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6510 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6511 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6513 isl_space *space;
6515 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6516 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6517 isl_pw_multi_aff_get_space(pma2));
6518 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6519 &isl_multi_aff_range_product);
6522 /* Given two isl_pw_multi_affs A -> B and C -> D,
6523 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6525 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6526 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6528 isl_space *space;
6530 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6531 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6532 isl_pw_multi_aff_get_space(pma2));
6533 space = isl_space_flatten_range(space);
6534 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6535 &isl_multi_aff_flat_range_product);
6538 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6539 * to compute some form of range product and add the result to data->res.
6541 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6542 __isl_give isl_pw_multi_aff *(*range_product)(
6543 __isl_take isl_pw_multi_aff *pma1,
6544 __isl_take isl_pw_multi_aff *pma2),
6545 void *user)
6547 struct isl_union_pw_multi_aff_bin_data *data = user;
6548 isl_bool match;
6549 isl_space *space1, *space2;
6551 space1 = isl_pw_multi_aff_peek_space(data->pma);
6552 space2 = isl_pw_multi_aff_peek_space(pma2);
6553 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6554 space2, isl_dim_in);
6555 if (match < 0 || !match) {
6556 isl_pw_multi_aff_free(pma2);
6557 return match < 0 ? isl_stat_error : isl_stat_ok;
6560 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6562 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6564 return isl_stat_ok;
6567 /* If data->pma and "pma2" have the same domain space, then compute
6568 * their flat range product and add the result to data->res.
6570 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6571 void *user)
6573 return gen_range_product_entry(pma2,
6574 &isl_pw_multi_aff_flat_range_product, user);
6577 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6578 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6580 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6581 __isl_take isl_union_pw_multi_aff *upma1,
6582 __isl_take isl_union_pw_multi_aff *upma2)
6584 return bin_op(upma1, upma2, &flat_range_product_entry);
6587 /* If data->pma and "pma2" have the same domain space, then compute
6588 * their range product and add the result to data->res.
6590 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6591 void *user)
6593 return gen_range_product_entry(pma2,
6594 &isl_pw_multi_aff_range_product, user);
6597 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6598 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6600 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6601 __isl_take isl_union_pw_multi_aff *upma1,
6602 __isl_take isl_union_pw_multi_aff *upma2)
6604 return bin_op(upma1, upma2, &range_product_entry);
6607 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6608 * The parameters are assumed to have been aligned.
6610 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6611 * except that it works on two different isl_pw_* types.
6613 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6614 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6615 __isl_take isl_pw_aff *pa)
6617 int i, j, n;
6618 isl_pw_multi_aff *res = NULL;
6620 if (!pma || !pa)
6621 goto error;
6623 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6624 pa->dim, isl_dim_in))
6625 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6626 "domains don't match", goto error);
6627 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6628 goto error;
6630 n = pma->n * pa->n;
6631 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6633 for (i = 0; i < pma->n; ++i) {
6634 for (j = 0; j < pa->n; ++j) {
6635 isl_set *common;
6636 isl_multi_aff *res_ij;
6637 int empty;
6639 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6640 isl_set_copy(pa->p[j].set));
6641 empty = isl_set_plain_is_empty(common);
6642 if (empty < 0 || empty) {
6643 isl_set_free(common);
6644 if (empty < 0)
6645 goto error;
6646 continue;
6649 res_ij = isl_multi_aff_set_aff(
6650 isl_multi_aff_copy(pma->p[i].maff), pos,
6651 isl_aff_copy(pa->p[j].aff));
6652 res_ij = isl_multi_aff_gist(res_ij,
6653 isl_set_copy(common));
6655 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6659 isl_pw_multi_aff_free(pma);
6660 isl_pw_aff_free(pa);
6661 return res;
6662 error:
6663 isl_pw_multi_aff_free(pma);
6664 isl_pw_aff_free(pa);
6665 return isl_pw_multi_aff_free(res);
6668 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6670 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6671 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6672 __isl_take isl_pw_aff *pa)
6674 isl_bool equal_params;
6676 if (!pma || !pa)
6677 goto error;
6678 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6679 if (equal_params < 0)
6680 goto error;
6681 if (equal_params)
6682 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6683 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6684 isl_pw_aff_check_named_params(pa) < 0)
6685 goto error;
6686 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6687 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6688 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6689 error:
6690 isl_pw_multi_aff_free(pma);
6691 isl_pw_aff_free(pa);
6692 return NULL;
6695 /* Do the parameters of "pa" match those of "space"?
6697 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6698 __isl_keep isl_space *space)
6700 isl_space *pa_space;
6701 isl_bool match;
6703 if (!pa || !space)
6704 return isl_bool_error;
6706 pa_space = isl_pw_aff_get_space(pa);
6708 match = isl_space_has_equal_params(space, pa_space);
6710 isl_space_free(pa_space);
6711 return match;
6714 /* Check that the domain space of "pa" matches "space".
6716 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6717 __isl_keep isl_space *space)
6719 isl_space *pa_space;
6720 isl_bool match;
6722 if (!pa || !space)
6723 return isl_stat_error;
6725 pa_space = isl_pw_aff_get_space(pa);
6727 match = isl_space_has_equal_params(space, pa_space);
6728 if (match < 0)
6729 goto error;
6730 if (!match)
6731 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6732 "parameters don't match", goto error);
6733 match = isl_space_tuple_is_equal(space, isl_dim_in,
6734 pa_space, isl_dim_in);
6735 if (match < 0)
6736 goto error;
6737 if (!match)
6738 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6739 "domains don't match", goto error);
6740 isl_space_free(pa_space);
6741 return isl_stat_ok;
6742 error:
6743 isl_space_free(pa_space);
6744 return isl_stat_error;
6747 #undef BASE
6748 #define BASE pw_aff
6749 #undef DOMBASE
6750 #define DOMBASE set
6752 #include <isl_multi_explicit_domain.c>
6753 #include <isl_multi_pw_aff_explicit_domain.c>
6754 #include <isl_multi_templ.c>
6755 #include <isl_multi_un_op_templ.c>
6756 #include <isl_multi_bin_val_templ.c>
6757 #include <isl_multi_add_constant_templ.c>
6758 #include <isl_multi_align_set.c>
6759 #include <isl_multi_apply_set_explicit_domain_templ.c>
6760 #include <isl_multi_arith_templ.c>
6761 #include <isl_multi_bind_templ.c>
6762 #include <isl_multi_bind_domain_templ.c>
6763 #include <isl_multi_coalesce.c>
6764 #include <isl_multi_domain_templ.c>
6765 #include <isl_multi_dim_id_templ.c>
6766 #include <isl_multi_dims.c>
6767 #include <isl_multi_from_base_templ.c>
6768 #include <isl_multi_check_domain_templ.c>
6769 #include <isl_multi_gist.c>
6770 #include <isl_multi_hash.c>
6771 #include <isl_multi_identity_templ.c>
6772 #include <isl_multi_insert_domain_templ.c>
6773 #include <isl_multi_intersect.c>
6774 #include <isl_multi_min_max_templ.c>
6775 #include <isl_multi_move_dims_templ.c>
6776 #include <isl_multi_nan_templ.c>
6777 #include <isl_multi_param_templ.c>
6778 #include <isl_multi_product_templ.c>
6779 #include <isl_multi_splice_templ.c>
6780 #include <isl_multi_tuple_id_templ.c>
6781 #include <isl_multi_union_add_templ.c>
6782 #include <isl_multi_zero_templ.c>
6783 #include <isl_multi_unbind_params_templ.c>
6785 /* Is every element of "mpa" defined over a single universe domain?
6787 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6789 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6792 /* Given that every element of "mpa" is defined over a single universe domain,
6793 * return the corresponding base expressions.
6795 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6796 __isl_take isl_multi_pw_aff *mpa)
6798 int i;
6799 isl_size n;
6800 isl_multi_aff *ma;
6802 n = isl_multi_pw_aff_size(mpa);
6803 if (n < 0)
6804 mpa = isl_multi_pw_aff_free(mpa);
6805 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6806 for (i = 0; i < n; ++i) {
6807 isl_aff *aff;
6809 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6810 ma = isl_multi_aff_set_aff(ma, i, aff);
6812 isl_multi_pw_aff_free(mpa);
6813 return ma;
6816 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6817 * with this explicit domain.
6819 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6820 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6822 isl_set *dom;
6824 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6825 return map;
6827 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6828 map = isl_map_intersect_domain(map, dom);
6830 return map;
6833 /* Are all elements of "mpa" piecewise constants?
6835 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6837 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6840 /* Does "mpa" have a non-trivial explicit domain?
6842 * The explicit domain, if present, is trivial if it represents
6843 * an (obviously) universe set.
6845 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6846 __isl_keep isl_multi_pw_aff *mpa)
6848 if (!mpa)
6849 return isl_bool_error;
6850 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6851 return isl_bool_false;
6852 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6855 #undef BASE
6856 #define BASE set
6858 #include "isl_opt_mpa_templ.c"
6860 /* Compute the minima of the set dimensions as a function of the
6861 * parameters, but independently of the other set dimensions.
6863 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6865 return set_opt_mpa(set, &isl_set_dim_min);
6868 /* Compute the maxima of the set dimensions as a function of the
6869 * parameters, but independently of the other set dimensions.
6871 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6873 return set_opt_mpa(set, &isl_set_dim_max);
6876 #undef BASE
6877 #define BASE map
6879 #include "isl_opt_mpa_templ.c"
6881 /* Compute the minima of the output dimensions as a function of the
6882 * parameters and input dimensions, but independently of
6883 * the other output dimensions.
6885 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6887 return map_opt_mpa(map, &isl_map_dim_min);
6890 /* Compute the maxima of the output dimensions as a function of the
6891 * parameters and input dimensions, but independently of
6892 * the other output dimensions.
6894 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6896 return map_opt_mpa(map, &isl_map_dim_max);
6899 #undef TYPE
6900 #define TYPE isl_pw_multi_aff
6901 #include "isl_type_check_match_range_multi_val.c"
6903 /* Apply "fn" to the base expressions of "pma" and "mv".
6905 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_op_multi_val(
6906 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv,
6907 __isl_give isl_multi_aff *(*fn)(__isl_take isl_multi_aff *ma,
6908 __isl_take isl_multi_val *mv))
6910 int i;
6911 isl_size n;
6913 if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
6914 goto error;
6916 n = isl_pw_multi_aff_n_piece(pma);
6917 if (n < 0)
6918 goto error;
6920 for (i = 0; i < n; ++i) {
6921 isl_multi_aff *ma;
6923 ma = isl_pw_multi_aff_take_base_at(pma, i);
6924 ma = fn(ma, isl_multi_val_copy(mv));
6925 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
6928 isl_multi_val_free(mv);
6929 return pma;
6930 error:
6931 isl_multi_val_free(mv);
6932 isl_pw_multi_aff_free(pma);
6933 return NULL;
6936 /* Scale the elements of "pma" by the corresponding elements of "mv".
6938 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6939 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6941 return isl_pw_multi_aff_op_multi_val(pma, mv,
6942 &isl_multi_aff_scale_multi_val);
6945 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6947 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
6948 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6950 return isl_pw_multi_aff_op_multi_val(pma, mv,
6951 &isl_multi_aff_scale_down_multi_val);
6954 /* This function is called for each entry of an isl_union_pw_multi_aff.
6955 * If the space of the entry matches that of data->mv,
6956 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6957 * Otherwise, return an empty isl_pw_multi_aff.
6959 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6960 __isl_take isl_pw_multi_aff *pma, void *user)
6962 isl_bool equal;
6963 isl_multi_val *mv = user;
6965 equal = isl_pw_multi_aff_match_range_multi_val(pma, mv);
6966 if (equal < 0)
6967 return isl_pw_multi_aff_free(pma);
6968 if (!equal) {
6969 isl_space *space = isl_pw_multi_aff_get_space(pma);
6970 isl_pw_multi_aff_free(pma);
6971 return isl_pw_multi_aff_empty(space);
6974 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6977 /* Scale the elements of "upma" by the corresponding elements of "mv",
6978 * for those entries that match the space of "mv".
6980 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6981 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6983 struct isl_union_pw_multi_aff_transform_control control = {
6984 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6985 .fn_user = mv,
6988 upma = isl_union_pw_multi_aff_align_params(upma,
6989 isl_multi_val_get_space(mv));
6990 mv = isl_multi_val_align_params(mv,
6991 isl_union_pw_multi_aff_get_space(upma));
6992 if (!upma || !mv)
6993 goto error;
6995 return isl_union_pw_multi_aff_transform(upma, &control);
6997 isl_multi_val_free(mv);
6998 return upma;
6999 error:
7000 isl_multi_val_free(mv);
7001 isl_union_pw_multi_aff_free(upma);
7002 return NULL;
7005 /* Construct and return a piecewise multi affine expression
7006 * in the given space with value zero in each of the output dimensions and
7007 * a universe domain.
7009 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
7011 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
7014 /* Construct and return a piecewise multi affine expression
7015 * that is equal to the given piecewise affine expression.
7017 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
7018 __isl_take isl_pw_aff *pa)
7020 int i;
7021 isl_space *space;
7022 isl_pw_multi_aff *pma;
7024 if (!pa)
7025 return NULL;
7027 space = isl_pw_aff_get_space(pa);
7028 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
7030 for (i = 0; i < pa->n; ++i) {
7031 isl_set *set;
7032 isl_multi_aff *ma;
7034 set = isl_set_copy(pa->p[i].set);
7035 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
7036 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
7039 isl_pw_aff_free(pa);
7040 return pma;
7043 /* Construct and return a piecewise multi affine expression
7044 * that is equal to the given multi piecewise affine expression
7045 * on the shared domain of the piecewise affine expressions,
7046 * in the special case of a 0D multi piecewise affine expression.
7048 * Create a piecewise multi affine expression with the explicit domain of
7049 * the 0D multi piecewise affine expression as domain.
7051 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
7052 __isl_take isl_multi_pw_aff *mpa)
7054 isl_space *space;
7055 isl_set *dom;
7056 isl_multi_aff *ma;
7058 space = isl_multi_pw_aff_get_space(mpa);
7059 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
7060 isl_multi_pw_aff_free(mpa);
7062 ma = isl_multi_aff_zero(space);
7063 return isl_pw_multi_aff_alloc(dom, ma);
7066 /* Construct and return a piecewise multi affine expression
7067 * that is equal to the given multi piecewise affine expression
7068 * on the shared domain of the piecewise affine expressions.
7070 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
7071 __isl_take isl_multi_pw_aff *mpa)
7073 int i;
7074 isl_space *space;
7075 isl_pw_aff *pa;
7076 isl_pw_multi_aff *pma;
7078 if (!mpa)
7079 return NULL;
7081 if (mpa->n == 0)
7082 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
7084 space = isl_multi_pw_aff_get_space(mpa);
7085 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
7086 pma = isl_pw_multi_aff_from_pw_aff(pa);
7088 for (i = 1; i < mpa->n; ++i) {
7089 isl_pw_multi_aff *pma_i;
7091 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7092 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
7093 pma = isl_pw_multi_aff_range_product(pma, pma_i);
7096 pma = isl_pw_multi_aff_reset_space(pma, space);
7098 isl_multi_pw_aff_free(mpa);
7099 return pma;
7102 /* Convenience function that constructs an isl_multi_pw_aff
7103 * directly from an isl_aff.
7105 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
7107 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
7110 /* Construct and return a multi piecewise affine expression
7111 * that is equal to the given multi affine expression.
7113 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
7114 __isl_take isl_multi_aff *ma)
7116 int i;
7117 isl_size n;
7118 isl_multi_pw_aff *mpa;
7120 n = isl_multi_aff_dim(ma, isl_dim_out);
7121 if (n < 0)
7122 ma = isl_multi_aff_free(ma);
7123 if (!ma)
7124 return NULL;
7126 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
7128 for (i = 0; i < n; ++i) {
7129 isl_pw_aff *pa;
7131 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
7132 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
7135 isl_multi_aff_free(ma);
7136 return mpa;
7139 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
7140 * but is considered as a function on an isl_multi_aff when exported.
7142 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
7143 __isl_take isl_multi_aff *ma)
7145 return isl_multi_pw_aff_from_multi_aff(ma);
7148 /* Construct and return a multi piecewise affine expression
7149 * that is equal to the given piecewise multi affine expression.
7151 * If the resulting multi piecewise affine expression has
7152 * an explicit domain, then assign it the domain of the input.
7153 * In other cases, the domain is stored in the individual elements.
7155 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
7156 __isl_take isl_pw_multi_aff *pma)
7158 int i;
7159 isl_size n;
7160 isl_space *space;
7161 isl_multi_pw_aff *mpa;
7163 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
7164 if (n < 0)
7165 pma = isl_pw_multi_aff_free(pma);
7166 space = isl_pw_multi_aff_get_space(pma);
7167 mpa = isl_multi_pw_aff_alloc(space);
7169 for (i = 0; i < n; ++i) {
7170 isl_pw_aff *pa;
7172 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
7173 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
7175 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7176 isl_set *dom;
7178 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
7179 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
7182 isl_pw_multi_aff_free(pma);
7183 return mpa;
7186 /* This function performs the same operation as
7187 * isl_multi_pw_aff_from_pw_multi_aff,
7188 * but is considered as a function on an isl_pw_multi_aff when exported.
7190 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
7191 __isl_take isl_pw_multi_aff *pma)
7193 return isl_multi_pw_aff_from_pw_multi_aff(pma);
7196 /* Do "pa1" and "pa2" represent the same function?
7198 * We first check if they are obviously equal.
7199 * If not, we convert them to maps and check if those are equal.
7201 * If "pa1" or "pa2" contain any NaNs, then they are considered
7202 * not to be the same. A NaN is not equal to anything, not even
7203 * to another NaN.
7205 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7206 __isl_keep isl_pw_aff *pa2)
7208 isl_bool equal;
7209 isl_bool has_nan;
7210 isl_map *map1, *map2;
7212 if (!pa1 || !pa2)
7213 return isl_bool_error;
7215 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7216 if (equal < 0 || equal)
7217 return equal;
7218 has_nan = either_involves_nan(pa1, pa2);
7219 if (has_nan < 0)
7220 return isl_bool_error;
7221 if (has_nan)
7222 return isl_bool_false;
7224 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7225 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7226 equal = isl_map_is_equal(map1, map2);
7227 isl_map_free(map1);
7228 isl_map_free(map2);
7230 return equal;
7233 /* Do "mpa1" and "mpa2" represent the same function?
7235 * Note that we cannot convert the entire isl_multi_pw_aff
7236 * to a map because the domains of the piecewise affine expressions
7237 * may not be the same.
7239 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7240 __isl_keep isl_multi_pw_aff *mpa2)
7242 int i;
7243 isl_bool equal, equal_params;
7245 if (!mpa1 || !mpa2)
7246 return isl_bool_error;
7248 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7249 if (equal_params < 0)
7250 return isl_bool_error;
7251 if (!equal_params) {
7252 if (!isl_space_has_named_params(mpa1->space))
7253 return isl_bool_false;
7254 if (!isl_space_has_named_params(mpa2->space))
7255 return isl_bool_false;
7256 mpa1 = isl_multi_pw_aff_copy(mpa1);
7257 mpa2 = isl_multi_pw_aff_copy(mpa2);
7258 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7259 isl_multi_pw_aff_get_space(mpa2));
7260 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7261 isl_multi_pw_aff_get_space(mpa1));
7262 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7263 isl_multi_pw_aff_free(mpa1);
7264 isl_multi_pw_aff_free(mpa2);
7265 return equal;
7268 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7269 if (equal < 0 || !equal)
7270 return equal;
7272 for (i = 0; i < mpa1->n; ++i) {
7273 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7274 if (equal < 0 || !equal)
7275 return equal;
7278 return isl_bool_true;
7281 /* Do "pma1" and "pma2" represent the same function?
7283 * First check if they are obviously equal.
7284 * If not, then convert them to maps and check if those are equal.
7286 * If "pa1" or "pa2" contain any NaNs, then they are considered
7287 * not to be the same. A NaN is not equal to anything, not even
7288 * to another NaN.
7290 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7291 __isl_keep isl_pw_multi_aff *pma2)
7293 isl_bool equal;
7294 isl_bool has_nan;
7295 isl_map *map1, *map2;
7297 if (!pma1 || !pma2)
7298 return isl_bool_error;
7300 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7301 if (equal < 0 || equal)
7302 return equal;
7303 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7304 if (has_nan >= 0 && !has_nan)
7305 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7306 if (has_nan < 0 || has_nan)
7307 return isl_bool_not(has_nan);
7309 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7310 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7311 equal = isl_map_is_equal(map1, map2);
7312 isl_map_free(map1);
7313 isl_map_free(map2);
7315 return equal;
7318 #undef BASE
7319 #define BASE multi_aff
7321 #include "isl_multi_pw_aff_pullback_templ.c"
7323 #undef BASE
7324 #define BASE pw_multi_aff
7326 #include "isl_multi_pw_aff_pullback_templ.c"
7328 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7329 * with the domain of "aff". The domain of the result is the same
7330 * as that of "mpa".
7331 * "mpa" and "aff" are assumed to have been aligned.
7333 * We first extract the parametric constant from "aff", defined
7334 * over the correct domain.
7335 * Then we add the appropriate combinations of the members of "mpa".
7336 * Finally, we add the integer divisions through recursive calls.
7338 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7339 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7341 int i;
7342 isl_size n_in, n_div, n_mpa_in;
7343 isl_space *space;
7344 isl_val *v;
7345 isl_pw_aff *pa;
7346 isl_aff *tmp;
7348 n_in = isl_aff_dim(aff, isl_dim_in);
7349 n_div = isl_aff_dim(aff, isl_dim_div);
7350 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7351 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7352 goto error;
7354 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7355 tmp = isl_aff_copy(aff);
7356 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7357 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7358 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7359 tmp = isl_aff_reset_domain_space(tmp, space);
7360 pa = isl_pw_aff_from_aff(tmp);
7362 for (i = 0; i < n_in; ++i) {
7363 isl_pw_aff *pa_i;
7365 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7366 continue;
7367 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7368 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7369 pa_i = isl_pw_aff_scale_val(pa_i, v);
7370 pa = isl_pw_aff_add(pa, pa_i);
7373 for (i = 0; i < n_div; ++i) {
7374 isl_aff *div;
7375 isl_pw_aff *pa_i;
7377 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7378 continue;
7379 div = isl_aff_get_div(aff, i);
7380 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7381 isl_multi_pw_aff_copy(mpa), div);
7382 pa_i = isl_pw_aff_floor(pa_i);
7383 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7384 pa_i = isl_pw_aff_scale_val(pa_i, v);
7385 pa = isl_pw_aff_add(pa, pa_i);
7388 isl_multi_pw_aff_free(mpa);
7389 isl_aff_free(aff);
7391 return pa;
7392 error:
7393 isl_multi_pw_aff_free(mpa);
7394 isl_aff_free(aff);
7395 return NULL;
7398 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7399 * with the domain of "aff". The domain of the result is the same
7400 * as that of "mpa".
7402 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7403 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7405 isl_bool equal_params;
7407 if (!aff || !mpa)
7408 goto error;
7409 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7410 if (equal_params < 0)
7411 goto error;
7412 if (equal_params)
7413 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7415 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7416 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7418 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7419 error:
7420 isl_aff_free(aff);
7421 isl_multi_pw_aff_free(mpa);
7422 return NULL;
7425 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7426 * with the domain of "pa". The domain of the result is the same
7427 * as that of "mpa".
7428 * "mpa" and "pa" are assumed to have been aligned.
7430 * We consider each piece in turn. Note that the domains of the
7431 * pieces are assumed to be disjoint and they remain disjoint
7432 * after taking the preimage (over the same function).
7434 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7435 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7437 isl_space *space;
7438 isl_pw_aff *res;
7439 int i;
7441 if (!mpa || !pa)
7442 goto error;
7444 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7445 isl_pw_aff_get_space(pa));
7446 res = isl_pw_aff_empty(space);
7448 for (i = 0; i < pa->n; ++i) {
7449 isl_pw_aff *pa_i;
7450 isl_set *domain;
7452 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7453 isl_multi_pw_aff_copy(mpa),
7454 isl_aff_copy(pa->p[i].aff));
7455 domain = isl_set_copy(pa->p[i].set);
7456 domain = isl_set_preimage_multi_pw_aff(domain,
7457 isl_multi_pw_aff_copy(mpa));
7458 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7459 res = isl_pw_aff_add_disjoint(res, pa_i);
7462 isl_pw_aff_free(pa);
7463 isl_multi_pw_aff_free(mpa);
7464 return res;
7465 error:
7466 isl_pw_aff_free(pa);
7467 isl_multi_pw_aff_free(mpa);
7468 return NULL;
7471 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7472 * with the domain of "pa". The domain of the result is the same
7473 * as that of "mpa".
7475 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7476 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7478 isl_bool equal_params;
7480 if (!pa || !mpa)
7481 goto error;
7482 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7483 if (equal_params < 0)
7484 goto error;
7485 if (equal_params)
7486 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7488 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7489 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7491 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7492 error:
7493 isl_pw_aff_free(pa);
7494 isl_multi_pw_aff_free(mpa);
7495 return NULL;
7498 /* Compute the pullback of "pa" by the function represented by "mpa".
7499 * In other words, plug in "mpa" in "pa".
7501 * The pullback is computed by applying "pa" to "mpa".
7503 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7504 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7506 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7509 #undef BASE
7510 #define BASE multi_pw_aff
7512 #include "isl_multi_pw_aff_pullback_templ.c"
7514 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7515 * of "mpa1" and "mpa2" live in the same space, construct map space
7516 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7517 * with this map space as extract argument.
7519 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7520 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7521 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7522 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7524 int match;
7525 isl_space *space1, *space2;
7526 isl_map *res;
7528 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7529 isl_multi_pw_aff_get_space(mpa2));
7530 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7531 isl_multi_pw_aff_get_space(mpa1));
7532 if (!mpa1 || !mpa2)
7533 goto error;
7534 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7535 mpa2->space, isl_dim_out);
7536 if (match < 0)
7537 goto error;
7538 if (!match)
7539 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7540 "range spaces don't match", goto error);
7541 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7542 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7543 space1 = isl_space_map_from_domain_and_range(space1, space2);
7545 res = order(mpa1, mpa2, space1);
7546 isl_multi_pw_aff_free(mpa1);
7547 isl_multi_pw_aff_free(mpa2);
7548 return res;
7549 error:
7550 isl_multi_pw_aff_free(mpa1);
7551 isl_multi_pw_aff_free(mpa2);
7552 return NULL;
7555 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7556 * where the function values are equal. "space" is the space of the result.
7557 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7559 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7560 * in the sequences are equal.
7562 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7563 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7564 __isl_take isl_space *space)
7566 int i;
7567 isl_size n;
7568 isl_map *res;
7570 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7571 if (n < 0)
7572 space = isl_space_free(space);
7573 res = isl_map_universe(space);
7575 for (i = 0; i < n; ++i) {
7576 isl_pw_aff *pa1, *pa2;
7577 isl_map *map;
7579 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7580 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7581 map = isl_pw_aff_eq_map(pa1, pa2);
7582 res = isl_map_intersect(res, map);
7585 return res;
7588 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7589 * where the function values are equal.
7591 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7592 __isl_take isl_multi_pw_aff *mpa2)
7594 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7595 &isl_multi_pw_aff_eq_map_on_space);
7598 /* Intersect "map" with the result of applying "order"
7599 * on two copies of "mpa".
7601 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7602 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7603 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7604 __isl_take isl_multi_pw_aff *mpa2))
7606 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7609 /* Return the subset of "map" where the domain and the range
7610 * have equal "mpa" values.
7612 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7613 __isl_take isl_multi_pw_aff *mpa)
7615 return isl_map_order_at_multi_pw_aff(map, mpa,
7616 &isl_multi_pw_aff_eq_map);
7619 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7620 * where the function values of "mpa1" lexicographically satisfies
7621 * "strict_base"/"base" compared to that of "mpa2".
7622 * "space" is the space of the result.
7623 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7625 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7626 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7627 * when compared to the i-th element of "mpa2" while all previous elements are
7628 * pairwise equal.
7629 * In particular, if i corresponds to the final elements
7630 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7631 * for other values of i.
7632 * If "base" is a strict order, then "base" and "strict_base" are the same.
7634 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7635 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7636 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7637 __isl_take isl_pw_aff *pa2),
7638 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7639 __isl_take isl_pw_aff *pa2),
7640 __isl_take isl_space *space)
7642 int i;
7643 isl_size n;
7644 isl_map *res, *rest;
7646 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7647 if (n < 0)
7648 space = isl_space_free(space);
7649 res = isl_map_empty(isl_space_copy(space));
7650 rest = isl_map_universe(space);
7652 for (i = 0; i < n; ++i) {
7653 int last;
7654 isl_pw_aff *pa1, *pa2;
7655 isl_map *map;
7657 last = i == n - 1;
7659 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7660 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7661 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7662 map = isl_map_intersect(map, isl_map_copy(rest));
7663 res = isl_map_union(res, map);
7665 if (last)
7666 continue;
7668 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7669 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7670 map = isl_pw_aff_eq_map(pa1, pa2);
7671 rest = isl_map_intersect(rest, map);
7674 isl_map_free(rest);
7675 return res;
7678 #undef ORDER
7679 #define ORDER le
7680 #undef STRICT_ORDER
7681 #define STRICT_ORDER lt
7682 #include "isl_aff_lex_templ.c"
7684 #undef ORDER
7685 #define ORDER lt
7686 #undef STRICT_ORDER
7687 #define STRICT_ORDER lt
7688 #include "isl_aff_lex_templ.c"
7690 #undef ORDER
7691 #define ORDER ge
7692 #undef STRICT_ORDER
7693 #define STRICT_ORDER gt
7694 #include "isl_aff_lex_templ.c"
7696 #undef ORDER
7697 #define ORDER gt
7698 #undef STRICT_ORDER
7699 #define STRICT_ORDER gt
7700 #include "isl_aff_lex_templ.c"
7702 /* Compare two isl_affs.
7704 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7705 * than "aff2" and 0 if they are equal.
7707 * The order is fairly arbitrary. We do consider expressions that only involve
7708 * earlier dimensions as "smaller".
7710 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7712 int cmp;
7713 int last1, last2;
7715 if (aff1 == aff2)
7716 return 0;
7718 if (!aff1)
7719 return -1;
7720 if (!aff2)
7721 return 1;
7723 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7724 if (cmp != 0)
7725 return cmp;
7727 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7728 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7729 if (last1 != last2)
7730 return last1 - last2;
7732 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7735 /* Compare two isl_pw_affs.
7737 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7738 * than "pa2" and 0 if they are equal.
7740 * The order is fairly arbitrary. We do consider expressions that only involve
7741 * earlier dimensions as "smaller".
7743 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7744 __isl_keep isl_pw_aff *pa2)
7746 int i;
7747 int cmp;
7749 if (pa1 == pa2)
7750 return 0;
7752 if (!pa1)
7753 return -1;
7754 if (!pa2)
7755 return 1;
7757 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7758 if (cmp != 0)
7759 return cmp;
7761 if (pa1->n != pa2->n)
7762 return pa1->n - pa2->n;
7764 for (i = 0; i < pa1->n; ++i) {
7765 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7766 if (cmp != 0)
7767 return cmp;
7768 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7769 if (cmp != 0)
7770 return cmp;
7773 return 0;
7776 /* Return a piecewise affine expression that is equal to "v" on "domain".
7778 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7779 __isl_take isl_val *v)
7781 isl_space *space;
7782 isl_local_space *ls;
7783 isl_aff *aff;
7785 space = isl_set_get_space(domain);
7786 ls = isl_local_space_from_space(space);
7787 aff = isl_aff_val_on_domain(ls, v);
7789 return isl_pw_aff_alloc(domain, aff);
7792 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7793 * but is considered as a function on an isl_set when exported.
7795 __isl_give isl_pw_aff *isl_set_pw_aff_on_domain_val(__isl_take isl_set *domain,
7796 __isl_take isl_val *v)
7798 return isl_pw_aff_val_on_domain(domain, v);
7801 /* Return a piecewise affine expression that is equal to the parameter
7802 * with identifier "id" on "domain".
7804 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7805 __isl_take isl_set *domain, __isl_take isl_id *id)
7807 isl_space *space;
7808 isl_aff *aff;
7810 space = isl_set_get_space(domain);
7811 space = isl_space_add_param_id(space, isl_id_copy(id));
7812 domain = isl_set_align_params(domain, isl_space_copy(space));
7813 aff = isl_aff_param_on_domain_space_id(space, id);
7815 return isl_pw_aff_alloc(domain, aff);
7818 /* This function performs the same operation as
7819 * isl_pw_aff_param_on_domain_id,
7820 * but is considered as a function on an isl_set when exported.
7822 __isl_give isl_pw_aff *isl_set_param_pw_aff_on_domain_id(
7823 __isl_take isl_set *domain, __isl_take isl_id *id)
7825 return isl_pw_aff_param_on_domain_id(domain, id);
7828 /* Return a multi affine expression that is equal to "mv" on domain
7829 * space "space".
7831 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7832 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7834 int i;
7835 isl_size n;
7836 isl_space *space2;
7837 isl_local_space *ls;
7838 isl_multi_aff *ma;
7840 n = isl_multi_val_dim(mv, isl_dim_set);
7841 if (!space || n < 0)
7842 goto error;
7844 space2 = isl_multi_val_get_space(mv);
7845 space2 = isl_space_align_params(space2, isl_space_copy(space));
7846 space = isl_space_align_params(space, isl_space_copy(space2));
7847 space = isl_space_map_from_domain_and_range(space, space2);
7848 ma = isl_multi_aff_alloc(isl_space_copy(space));
7849 ls = isl_local_space_from_space(isl_space_domain(space));
7850 for (i = 0; i < n; ++i) {
7851 isl_val *v;
7852 isl_aff *aff;
7854 v = isl_multi_val_get_val(mv, i);
7855 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7856 ma = isl_multi_aff_set_aff(ma, i, aff);
7858 isl_local_space_free(ls);
7860 isl_multi_val_free(mv);
7861 return ma;
7862 error:
7863 isl_space_free(space);
7864 isl_multi_val_free(mv);
7865 return NULL;
7868 /* This is an alternative name for the function above.
7870 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7871 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7873 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7876 /* This function performs the same operation as
7877 * isl_multi_aff_multi_val_on_domain_space,
7878 * but is considered as a function on an isl_space when exported.
7880 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7881 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7883 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7886 /* Return a piecewise multi-affine expression
7887 * that is equal to "mv" on "domain".
7889 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7890 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7892 isl_space *space;
7893 isl_multi_aff *ma;
7895 space = isl_set_get_space(domain);
7896 ma = isl_multi_aff_multi_val_on_space(space, mv);
7898 return isl_pw_multi_aff_alloc(domain, ma);
7901 /* This function performs the same operation as
7902 * isl_pw_multi_aff_multi_val_on_domain,
7903 * but is considered as a function on an isl_set when exported.
7905 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7906 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7908 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7911 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7912 * mv is the value that should be attained on each domain set
7913 * res collects the results
7915 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7916 isl_multi_val *mv;
7917 isl_union_pw_multi_aff *res;
7920 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7921 * and add it to data->res.
7923 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7924 void *user)
7926 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7927 isl_pw_multi_aff *pma;
7928 isl_multi_val *mv;
7930 mv = isl_multi_val_copy(data->mv);
7931 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7932 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7934 return data->res ? isl_stat_ok : isl_stat_error;
7937 /* Return a union piecewise multi-affine expression
7938 * that is equal to "mv" on "domain".
7940 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7941 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7943 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7944 isl_space *space;
7946 space = isl_union_set_get_space(domain);
7947 data.res = isl_union_pw_multi_aff_empty(space);
7948 data.mv = mv;
7949 if (isl_union_set_foreach_set(domain,
7950 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7951 data.res = isl_union_pw_multi_aff_free(data.res);
7952 isl_union_set_free(domain);
7953 isl_multi_val_free(mv);
7954 return data.res;
7957 /* Compute the pullback of data->pma by the function represented by "pma2",
7958 * provided the spaces match, and add the results to data->res.
7960 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7962 struct isl_union_pw_multi_aff_bin_data *data = user;
7964 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7965 pma2->dim, isl_dim_out)) {
7966 isl_pw_multi_aff_free(pma2);
7967 return isl_stat_ok;
7970 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7971 isl_pw_multi_aff_copy(data->pma), pma2);
7973 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7974 if (!data->res)
7975 return isl_stat_error;
7977 return isl_stat_ok;
7980 /* Compute the pullback of "upma1" by the function represented by "upma2".
7982 __isl_give isl_union_pw_multi_aff *
7983 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7984 __isl_take isl_union_pw_multi_aff *upma1,
7985 __isl_take isl_union_pw_multi_aff *upma2)
7987 return bin_op(upma1, upma2, &pullback_entry);
7990 /* Apply "upma2" to "upma1".
7992 * That is, compute the pullback of "upma2" by "upma1".
7994 __isl_give isl_union_pw_multi_aff *
7995 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7996 __isl_take isl_union_pw_multi_aff *upma1,
7997 __isl_take isl_union_pw_multi_aff *upma2)
7999 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
8002 #undef TYPE
8003 #define TYPE isl_pw_multi_aff
8004 static
8005 #include "isl_copy_tuple_id_templ.c"
8007 /* Given a function "pma1" of the form A[B -> C] -> D and
8008 * a function "pma2" of the form E -> B,
8009 * replace the domain of the wrapped relation inside the domain of "pma1"
8010 * by the preimage with respect to "pma2".
8011 * In other words, plug in "pma2" in this nested domain.
8012 * The result is of the form A[E -> C] -> D.
8014 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
8015 * plug that into "pma1".
8017 __isl_give isl_pw_multi_aff *
8018 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8019 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
8021 isl_space *pma1_space, *pma2_space;
8022 isl_space *space;
8023 isl_pw_multi_aff *id;
8025 pma1_space = isl_pw_multi_aff_peek_space(pma1);
8026 pma2_space = isl_pw_multi_aff_peek_space(pma2);
8028 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
8029 goto error;
8030 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
8031 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
8032 goto error;
8034 space = isl_space_domain(isl_space_copy(pma1_space));
8035 space = isl_space_range(isl_space_unwrap(space));
8036 id = isl_pw_multi_aff_identity_on_domain_space(space);
8037 pma2 = isl_pw_multi_aff_product(pma2, id);
8039 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
8040 pma1_space, isl_dim_in);
8041 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
8042 pma1_space, isl_dim_in);
8044 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
8045 error:
8046 isl_pw_multi_aff_free(pma1);
8047 isl_pw_multi_aff_free(pma2);
8048 return NULL;
8051 /* If data->pma and "pma2" are such that
8052 * data->pma is of the form A[B -> C] -> D and
8053 * "pma2" is of the form E -> B,
8054 * then replace the domain of the wrapped relation
8055 * inside the domain of data->pma by the preimage with respect to "pma2" and
8056 * add the result to data->res.
8058 static isl_stat preimage_domain_wrapped_domain_entry(
8059 __isl_take isl_pw_multi_aff *pma2, void *user)
8061 struct isl_union_pw_multi_aff_bin_data *data = user;
8062 isl_space *pma1_space, *pma2_space;
8063 isl_bool match;
8065 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
8066 pma2_space = isl_pw_multi_aff_peek_space(pma2);
8068 match = isl_space_domain_is_wrapping(pma1_space);
8069 if (match >= 0 && match)
8070 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
8071 isl_dim_in, pma2_space, isl_dim_out);
8072 if (match < 0 || !match) {
8073 isl_pw_multi_aff_free(pma2);
8074 return match < 0 ? isl_stat_error : isl_stat_ok;
8077 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8078 isl_pw_multi_aff_copy(data->pma), pma2);
8080 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
8082 return isl_stat_non_null(data->res);
8085 /* For each pair of functions A[B -> C] -> D in "upma1" and
8086 * E -> B in "upma2",
8087 * replace the domain of the wrapped relation inside the domain of the first
8088 * by the preimage with respect to the second and collect the results.
8089 * In other words, plug in the second function in this nested domain.
8090 * The results are of the form A[E -> C] -> D.
8092 __isl_give isl_union_pw_multi_aff *
8093 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8094 __isl_take isl_union_pw_multi_aff *upma1,
8095 __isl_take isl_union_pw_multi_aff *upma2)
8097 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
8100 /* Check that the domain space of "upa" matches "space".
8102 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8103 * can in principle never fail since the space "space" is that
8104 * of the isl_multi_union_pw_aff and is a set space such that
8105 * there is no domain space to match.
8107 * We check the parameters and double-check that "space" is
8108 * indeed that of a set.
8110 static isl_stat isl_union_pw_aff_check_match_domain_space(
8111 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8113 isl_space *upa_space;
8114 isl_bool match;
8116 if (!upa || !space)
8117 return isl_stat_error;
8119 match = isl_space_is_set(space);
8120 if (match < 0)
8121 return isl_stat_error;
8122 if (!match)
8123 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8124 "expecting set space", return isl_stat_error);
8126 upa_space = isl_union_pw_aff_get_space(upa);
8127 match = isl_space_has_equal_params(space, upa_space);
8128 if (match < 0)
8129 goto error;
8130 if (!match)
8131 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8132 "parameters don't match", goto error);
8134 isl_space_free(upa_space);
8135 return isl_stat_ok;
8136 error:
8137 isl_space_free(upa_space);
8138 return isl_stat_error;
8141 /* Do the parameters of "upa" match those of "space"?
8143 static isl_bool isl_union_pw_aff_matching_params(
8144 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8146 isl_space *upa_space;
8147 isl_bool match;
8149 if (!upa || !space)
8150 return isl_bool_error;
8152 upa_space = isl_union_pw_aff_get_space(upa);
8154 match = isl_space_has_equal_params(space, upa_space);
8156 isl_space_free(upa_space);
8157 return match;
8160 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8161 * space represents the new parameters.
8162 * res collects the results.
8164 struct isl_union_pw_aff_reset_params_data {
8165 isl_space *space;
8166 isl_union_pw_aff *res;
8169 /* Replace the parameters of "pa" by data->space and
8170 * add the result to data->res.
8172 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8174 struct isl_union_pw_aff_reset_params_data *data = user;
8175 isl_space *space;
8177 space = isl_pw_aff_get_space(pa);
8178 space = isl_space_replace_params(space, data->space);
8179 pa = isl_pw_aff_reset_space(pa, space);
8180 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8182 return data->res ? isl_stat_ok : isl_stat_error;
8185 /* Replace the domain space of "upa" by "space".
8186 * Since a union expression does not have a (single) domain space,
8187 * "space" is necessarily a parameter space.
8189 * Since the order and the names of the parameters determine
8190 * the hash value, we need to create a new hash table.
8192 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8193 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8195 struct isl_union_pw_aff_reset_params_data data = { space };
8196 isl_bool match;
8198 match = isl_union_pw_aff_matching_params(upa, space);
8199 if (match < 0)
8200 upa = isl_union_pw_aff_free(upa);
8201 else if (match) {
8202 isl_space_free(space);
8203 return upa;
8206 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8207 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8208 data.res = isl_union_pw_aff_free(data.res);
8210 isl_union_pw_aff_free(upa);
8211 isl_space_free(space);
8212 return data.res;
8215 /* Return the floor of "pa".
8217 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8219 return isl_pw_aff_floor(pa);
8222 /* Given f, return floor(f).
8224 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8225 __isl_take isl_union_pw_aff *upa)
8227 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8230 /* Compute
8232 * upa mod m = upa - m * floor(upa/m)
8234 * with m an integer value.
8236 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8237 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8239 isl_union_pw_aff *res;
8241 if (!upa || !m)
8242 goto error;
8244 if (!isl_val_is_int(m))
8245 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8246 "expecting integer modulo", goto error);
8247 if (!isl_val_is_pos(m))
8248 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8249 "expecting positive modulo", goto error);
8251 res = isl_union_pw_aff_copy(upa);
8252 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8253 upa = isl_union_pw_aff_floor(upa);
8254 upa = isl_union_pw_aff_scale_val(upa, m);
8255 res = isl_union_pw_aff_sub(res, upa);
8257 return res;
8258 error:
8259 isl_val_free(m);
8260 isl_union_pw_aff_free(upa);
8261 return NULL;
8264 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8265 * pos is the output position that needs to be extracted.
8266 * res collects the results.
8268 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8269 int pos;
8270 isl_union_pw_aff *res;
8273 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8274 * (assuming it has such a dimension) and add it to data->res.
8276 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8278 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8279 isl_size n_out;
8280 isl_pw_aff *pa;
8282 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8283 if (n_out < 0)
8284 return isl_stat_error;
8285 if (data->pos >= n_out) {
8286 isl_pw_multi_aff_free(pma);
8287 return isl_stat_ok;
8290 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8291 isl_pw_multi_aff_free(pma);
8293 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8295 return data->res ? isl_stat_ok : isl_stat_error;
8298 /* Extract an isl_union_pw_aff corresponding to
8299 * output dimension "pos" of "upma".
8301 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8302 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8304 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8305 isl_space *space;
8307 if (!upma)
8308 return NULL;
8310 if (pos < 0)
8311 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8312 "cannot extract at negative position", return NULL);
8314 space = isl_union_pw_multi_aff_get_space(upma);
8315 data.res = isl_union_pw_aff_empty(space);
8316 data.pos = pos;
8317 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8318 &get_union_pw_aff, &data) < 0)
8319 data.res = isl_union_pw_aff_free(data.res);
8321 return data.res;
8324 /* Return a union piecewise affine expression
8325 * that is equal to "aff" on "domain".
8327 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8328 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8330 isl_pw_aff *pa;
8332 pa = isl_pw_aff_from_aff(aff);
8333 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8336 /* Return a union piecewise affine expression
8337 * that is equal to the parameter identified by "id" on "domain".
8339 * Make sure the parameter appears in the space passed to
8340 * isl_aff_param_on_domain_space_id.
8342 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8343 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8345 isl_space *space;
8346 isl_aff *aff;
8348 space = isl_union_set_get_space(domain);
8349 space = isl_space_add_param_id(space, isl_id_copy(id));
8350 aff = isl_aff_param_on_domain_space_id(space, id);
8351 return isl_union_pw_aff_aff_on_domain(domain, aff);
8354 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8355 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8356 * needs to attain.
8357 * "res" collects the results.
8359 struct isl_union_pw_aff_pw_aff_on_domain_data {
8360 isl_pw_aff *pa;
8361 isl_union_pw_aff *res;
8364 /* Construct a piecewise affine expression that is equal to data->pa
8365 * on "domain" and add the result to data->res.
8367 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8369 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8370 isl_pw_aff *pa;
8371 isl_size dim;
8373 pa = isl_pw_aff_copy(data->pa);
8374 dim = isl_set_dim(domain, isl_dim_set);
8375 if (dim < 0)
8376 pa = isl_pw_aff_free(pa);
8377 pa = isl_pw_aff_from_range(pa);
8378 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8379 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8380 pa = isl_pw_aff_intersect_domain(pa, domain);
8381 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8383 return data->res ? isl_stat_ok : isl_stat_error;
8386 /* Return a union piecewise affine expression
8387 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8388 * have been aligned.
8390 * Construct an isl_pw_aff on each of the sets in "domain" and
8391 * collect the results.
8393 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8394 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8396 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8397 isl_space *space;
8399 space = isl_union_set_get_space(domain);
8400 data.res = isl_union_pw_aff_empty(space);
8401 data.pa = pa;
8402 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8403 data.res = isl_union_pw_aff_free(data.res);
8404 isl_union_set_free(domain);
8405 isl_pw_aff_free(pa);
8406 return data.res;
8409 /* Return a union piecewise affine expression
8410 * that is equal to "pa" on "domain".
8412 * Check that "pa" is a parametric expression,
8413 * align the parameters if needed and call
8414 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8416 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8417 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8419 isl_bool is_set;
8420 isl_bool equal_params;
8421 isl_space *domain_space, *pa_space;
8423 pa_space = isl_pw_aff_peek_space(pa);
8424 is_set = isl_space_is_set(pa_space);
8425 if (is_set < 0)
8426 goto error;
8427 if (!is_set)
8428 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8429 "expecting parametric expression", goto error);
8431 domain_space = isl_union_set_get_space(domain);
8432 pa_space = isl_pw_aff_get_space(pa);
8433 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8434 if (equal_params >= 0 && !equal_params) {
8435 isl_space *space;
8437 space = isl_space_align_params(domain_space, pa_space);
8438 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8439 domain = isl_union_set_align_params(domain, space);
8440 } else {
8441 isl_space_free(domain_space);
8442 isl_space_free(pa_space);
8445 if (equal_params < 0)
8446 goto error;
8447 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8448 error:
8449 isl_union_set_free(domain);
8450 isl_pw_aff_free(pa);
8451 return NULL;
8454 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8455 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8456 * "res" collects the results.
8458 struct isl_union_pw_aff_val_on_domain_data {
8459 isl_val *v;
8460 isl_union_pw_aff *res;
8463 /* Construct a piecewise affine expression that is equal to data->v
8464 * on "domain" and add the result to data->res.
8466 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8468 struct isl_union_pw_aff_val_on_domain_data *data = user;
8469 isl_pw_aff *pa;
8470 isl_val *v;
8472 v = isl_val_copy(data->v);
8473 pa = isl_pw_aff_val_on_domain(domain, v);
8474 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8476 return data->res ? isl_stat_ok : isl_stat_error;
8479 /* Return a union piecewise affine expression
8480 * that is equal to "v" on "domain".
8482 * Construct an isl_pw_aff on each of the sets in "domain" and
8483 * collect the results.
8485 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8486 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8488 struct isl_union_pw_aff_val_on_domain_data data;
8489 isl_space *space;
8491 space = isl_union_set_get_space(domain);
8492 data.res = isl_union_pw_aff_empty(space);
8493 data.v = v;
8494 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8495 data.res = isl_union_pw_aff_free(data.res);
8496 isl_union_set_free(domain);
8497 isl_val_free(v);
8498 return data.res;
8501 /* Construct a piecewise multi affine expression
8502 * that is equal to "pa" and add it to upma.
8504 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8505 void *user)
8507 isl_union_pw_multi_aff **upma = user;
8508 isl_pw_multi_aff *pma;
8510 pma = isl_pw_multi_aff_from_pw_aff(pa);
8511 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8513 return *upma ? isl_stat_ok : isl_stat_error;
8516 /* Construct and return a union piecewise multi affine expression
8517 * that is equal to the given union piecewise affine expression.
8519 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8520 __isl_take isl_union_pw_aff *upa)
8522 isl_space *space;
8523 isl_union_pw_multi_aff *upma;
8525 if (!upa)
8526 return NULL;
8528 space = isl_union_pw_aff_get_space(upa);
8529 upma = isl_union_pw_multi_aff_empty(space);
8531 if (isl_union_pw_aff_foreach_pw_aff(upa,
8532 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8533 upma = isl_union_pw_multi_aff_free(upma);
8535 isl_union_pw_aff_free(upa);
8536 return upma;
8539 /* Compute the set of elements in the domain of "pa" where it is zero and
8540 * add this set to "uset".
8542 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8544 isl_union_set **uset = (isl_union_set **)user;
8546 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8548 return *uset ? isl_stat_ok : isl_stat_error;
8551 /* Return a union set containing those elements in the domain
8552 * of "upa" where it is zero.
8554 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8555 __isl_take isl_union_pw_aff *upa)
8557 isl_union_set *zero;
8559 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8560 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8561 zero = isl_union_set_free(zero);
8563 isl_union_pw_aff_free(upa);
8564 return zero;
8567 /* Internal data structure for isl_union_pw_aff_bind_id,
8568 * storing the parameter that needs to be bound and
8569 * the accumulated results.
8571 struct isl_bind_id_data {
8572 isl_id *id;
8573 isl_union_set *bound;
8576 /* Bind the piecewise affine function "pa" to the parameter data->id,
8577 * adding the resulting elements in the domain where the expression
8578 * is equal to the parameter to data->bound.
8580 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8582 struct isl_bind_id_data *data = user;
8583 isl_set *bound;
8585 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8586 data->bound = isl_union_set_add_set(data->bound, bound);
8588 return data->bound ? isl_stat_ok : isl_stat_error;
8591 /* Bind the union piecewise affine function "upa" to the parameter "id",
8592 * returning the elements in the domain where the expression
8593 * is equal to the parameter.
8595 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8596 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8598 struct isl_bind_id_data data = { id };
8600 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8601 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8602 data.bound = isl_union_set_free(data.bound);
8604 isl_union_pw_aff_free(upa);
8605 isl_id_free(id);
8606 return data.bound;
8609 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8610 * upma is the function that is plugged in.
8611 * pa is the current part of the function in which upma is plugged in.
8612 * res collects the results.
8614 struct isl_union_pw_aff_pullback_upma_data {
8615 isl_union_pw_multi_aff *upma;
8616 isl_pw_aff *pa;
8617 isl_union_pw_aff *res;
8620 /* Check if "pma" can be plugged into data->pa.
8621 * If so, perform the pullback and add the result to data->res.
8623 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8625 struct isl_union_pw_aff_pullback_upma_data *data = user;
8626 isl_pw_aff *pa;
8628 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8629 pma->dim, isl_dim_out)) {
8630 isl_pw_multi_aff_free(pma);
8631 return isl_stat_ok;
8634 pa = isl_pw_aff_copy(data->pa);
8635 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8637 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8639 return data->res ? isl_stat_ok : isl_stat_error;
8642 /* Check if any of the elements of data->upma can be plugged into pa,
8643 * add if so add the result to data->res.
8645 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8647 struct isl_union_pw_aff_pullback_upma_data *data = user;
8648 isl_stat r;
8650 data->pa = pa;
8651 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8652 &pa_pb_pma, data);
8653 isl_pw_aff_free(pa);
8655 return r;
8658 /* Compute the pullback of "upa" by the function represented by "upma".
8659 * In other words, plug in "upma" in "upa". The result contains
8660 * expressions defined over the domain space of "upma".
8662 * Run over all pairs of elements in "upa" and "upma", perform
8663 * the pullback when appropriate and collect the results.
8664 * If the hash value were based on the domain space rather than
8665 * the function space, then we could run through all elements
8666 * of "upma" and directly pick out the corresponding element of "upa".
8668 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8669 __isl_take isl_union_pw_aff *upa,
8670 __isl_take isl_union_pw_multi_aff *upma)
8672 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8673 isl_space *space;
8675 space = isl_union_pw_multi_aff_get_space(upma);
8676 upa = isl_union_pw_aff_align_params(upa, space);
8677 space = isl_union_pw_aff_get_space(upa);
8678 upma = isl_union_pw_multi_aff_align_params(upma, space);
8680 if (!upa || !upma)
8681 goto error;
8683 data.upma = upma;
8684 data.res = isl_union_pw_aff_alloc_same_size(upa);
8685 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8686 data.res = isl_union_pw_aff_free(data.res);
8688 isl_union_pw_aff_free(upa);
8689 isl_union_pw_multi_aff_free(upma);
8690 return data.res;
8691 error:
8692 isl_union_pw_aff_free(upa);
8693 isl_union_pw_multi_aff_free(upma);
8694 return NULL;
8697 #undef BASE
8698 #define BASE union_pw_aff
8699 #undef DOMBASE
8700 #define DOMBASE union_set
8702 #include <isl_multi_explicit_domain.c>
8703 #include <isl_multi_union_pw_aff_explicit_domain.c>
8704 #include <isl_multi_templ.c>
8705 #include <isl_multi_un_op_templ.c>
8706 #include <isl_multi_bin_val_templ.c>
8707 #include <isl_multi_align_set.c>
8708 #include <isl_multi_align_union_set.c>
8709 #include <isl_multi_apply_set_explicit_domain_templ.c>
8710 #include <isl_multi_apply_union_set_explicit_domain_templ.c>
8711 #include <isl_multi_arith_templ.c>
8712 #include <isl_multi_bind_templ.c>
8713 #include <isl_multi_coalesce.c>
8714 #include <isl_multi_dim_id_templ.c>
8715 #include <isl_multi_floor.c>
8716 #include <isl_multi_from_base_templ.c>
8717 #include <isl_multi_check_domain_templ.c>
8718 #include <isl_multi_gist.c>
8719 #include <isl_multi_intersect.c>
8720 #include <isl_multi_nan_templ.c>
8721 #include <isl_multi_tuple_id_templ.c>
8722 #include <isl_multi_union_add_templ.c>
8723 #include <isl_multi_zero_space_templ.c>
8725 /* Does "mupa" have a non-trivial explicit domain?
8727 * The explicit domain, if present, is trivial if it represents
8728 * an (obviously) universe parameter set.
8730 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8731 __isl_keep isl_multi_union_pw_aff *mupa)
8733 isl_bool is_params, trivial;
8734 isl_set *set;
8736 if (!mupa)
8737 return isl_bool_error;
8738 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8739 return isl_bool_false;
8740 is_params = isl_union_set_is_params(mupa->u.dom);
8741 if (is_params < 0 || !is_params)
8742 return isl_bool_not(is_params);
8743 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8744 trivial = isl_set_plain_is_universe(set);
8745 isl_set_free(set);
8746 return isl_bool_not(trivial);
8749 /* Construct a multiple union piecewise affine expression
8750 * in the given space with value zero in each of the output dimensions.
8752 * Since there is no canonical zero value for
8753 * a union piecewise affine expression, we can only construct
8754 * a zero-dimensional "zero" value.
8756 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8757 __isl_take isl_space *space)
8759 isl_bool params;
8760 isl_size dim;
8762 if (!space)
8763 return NULL;
8765 params = isl_space_is_params(space);
8766 if (params < 0)
8767 goto error;
8768 if (params)
8769 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8770 "expecting proper set space", goto error);
8771 if (!isl_space_is_set(space))
8772 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8773 "expecting set space", goto error);
8774 dim = isl_space_dim(space, isl_dim_out);
8775 if (dim < 0)
8776 goto error;
8777 if (dim != 0)
8778 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8779 "expecting 0D space", goto error);
8781 return isl_multi_union_pw_aff_alloc(space);
8782 error:
8783 isl_space_free(space);
8784 return NULL;
8787 /* Construct and return a multi union piecewise affine expression
8788 * that is equal to the given multi affine expression.
8790 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8791 __isl_take isl_multi_aff *ma)
8793 isl_multi_pw_aff *mpa;
8795 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8796 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8799 /* This function performs the same operation as
8800 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8801 * isl_multi_aff when exported.
8803 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8804 __isl_take isl_multi_aff *ma)
8806 return isl_multi_union_pw_aff_from_multi_aff(ma);
8809 /* Construct and return a multi union piecewise affine expression
8810 * that is equal to the given multi piecewise affine expression.
8812 * If the resulting multi union piecewise affine expression has
8813 * an explicit domain, then assign it the domain of the input.
8814 * In other cases, the domain is stored in the individual elements.
8816 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8817 __isl_take isl_multi_pw_aff *mpa)
8819 int i;
8820 isl_size n;
8821 isl_space *space;
8822 isl_multi_union_pw_aff *mupa;
8824 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8825 if (n < 0)
8826 mpa = isl_multi_pw_aff_free(mpa);
8827 if (!mpa)
8828 return NULL;
8830 space = isl_multi_pw_aff_get_space(mpa);
8831 space = isl_space_range(space);
8832 mupa = isl_multi_union_pw_aff_alloc(space);
8834 for (i = 0; i < n; ++i) {
8835 isl_pw_aff *pa;
8836 isl_union_pw_aff *upa;
8838 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8839 upa = isl_union_pw_aff_from_pw_aff(pa);
8840 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8842 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8843 isl_union_set *dom;
8844 isl_multi_pw_aff *copy;
8846 copy = isl_multi_pw_aff_copy(mpa);
8847 dom = isl_union_set_from_set(isl_multi_pw_aff_domain(copy));
8848 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8851 isl_multi_pw_aff_free(mpa);
8853 return mupa;
8856 /* Extract the range space of "pma" and assign it to *space.
8857 * If *space has already been set (through a previous call to this function),
8858 * then check that the range space is the same.
8860 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8862 isl_space **space = user;
8863 isl_space *pma_space;
8864 isl_bool equal;
8866 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8867 isl_pw_multi_aff_free(pma);
8869 if (!pma_space)
8870 return isl_stat_error;
8871 if (!*space) {
8872 *space = pma_space;
8873 return isl_stat_ok;
8876 equal = isl_space_is_equal(pma_space, *space);
8877 isl_space_free(pma_space);
8879 if (equal < 0)
8880 return isl_stat_error;
8881 if (!equal)
8882 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8883 "range spaces not the same", return isl_stat_error);
8884 return isl_stat_ok;
8887 /* Construct and return a multi union piecewise affine expression
8888 * that is equal to the given union piecewise multi affine expression.
8890 * In order to be able to perform the conversion, the input
8891 * needs to be non-empty and may only involve a single range space.
8893 * If the resulting multi union piecewise affine expression has
8894 * an explicit domain, then assign it the domain of the input.
8895 * In other cases, the domain is stored in the individual elements.
8897 __isl_give isl_multi_union_pw_aff *
8898 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8899 __isl_take isl_union_pw_multi_aff *upma)
8901 isl_space *space = NULL;
8902 isl_multi_union_pw_aff *mupa;
8903 int i;
8904 isl_size n;
8906 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8907 if (n < 0)
8908 goto error;
8909 if (n == 0)
8910 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8911 "cannot extract range space from empty input",
8912 goto error);
8913 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8914 &space) < 0)
8915 goto error;
8917 if (!space)
8918 goto error;
8920 n = isl_space_dim(space, isl_dim_set);
8921 if (n < 0)
8922 space = isl_space_free(space);
8923 mupa = isl_multi_union_pw_aff_alloc(space);
8925 for (i = 0; i < n; ++i) {
8926 isl_union_pw_aff *upa;
8928 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8929 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8931 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8932 isl_union_set *dom;
8933 isl_union_pw_multi_aff *copy;
8935 copy = isl_union_pw_multi_aff_copy(upma);
8936 dom = isl_union_pw_multi_aff_domain(copy);
8937 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8940 isl_union_pw_multi_aff_free(upma);
8941 return mupa;
8942 error:
8943 isl_space_free(space);
8944 isl_union_pw_multi_aff_free(upma);
8945 return NULL;
8948 /* This function performs the same operation as
8949 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8950 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8952 __isl_give isl_multi_union_pw_aff *
8953 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8954 __isl_take isl_union_pw_multi_aff *upma)
8956 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8959 /* Try and create an isl_multi_union_pw_aff that is equivalent
8960 * to the given isl_union_map.
8961 * The isl_union_map is required to be single-valued in each space.
8962 * Moreover, it cannot be empty and all range spaces need to be the same.
8963 * Otherwise, an error is produced.
8965 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8966 __isl_take isl_union_map *umap)
8968 isl_union_pw_multi_aff *upma;
8970 upma = isl_union_pw_multi_aff_from_union_map(umap);
8971 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8974 /* This function performs the same operation as
8975 * isl_multi_union_pw_aff_from_union_map,
8976 * but is considered as a function on an isl_union_map when exported.
8978 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8979 __isl_take isl_union_map *umap)
8981 return isl_multi_union_pw_aff_from_union_map(umap);
8984 /* Return a multiple union piecewise affine expression
8985 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8986 * have been aligned.
8988 * If the resulting multi union piecewise affine expression has
8989 * an explicit domain, then assign it the input domain.
8990 * In other cases, the domain is stored in the individual elements.
8992 static __isl_give isl_multi_union_pw_aff *
8993 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8994 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8996 int i;
8997 isl_size n;
8998 isl_space *space;
8999 isl_multi_union_pw_aff *mupa;
9001 n = isl_multi_val_dim(mv, isl_dim_set);
9002 if (!domain || n < 0)
9003 goto error;
9005 space = isl_multi_val_get_space(mv);
9006 mupa = isl_multi_union_pw_aff_alloc(space);
9007 for (i = 0; i < n; ++i) {
9008 isl_val *v;
9009 isl_union_pw_aff *upa;
9011 v = isl_multi_val_get_val(mv, i);
9012 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
9014 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9016 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9017 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
9018 isl_union_set_copy(domain));
9020 isl_union_set_free(domain);
9021 isl_multi_val_free(mv);
9022 return mupa;
9023 error:
9024 isl_union_set_free(domain);
9025 isl_multi_val_free(mv);
9026 return NULL;
9029 /* Return a multiple union piecewise affine expression
9030 * that is equal to "mv" on "domain".
9032 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
9033 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
9035 isl_bool equal_params;
9037 if (!domain || !mv)
9038 goto error;
9039 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
9040 if (equal_params < 0)
9041 goto error;
9042 if (equal_params)
9043 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9044 domain, mv);
9045 domain = isl_union_set_align_params(domain,
9046 isl_multi_val_get_space(mv));
9047 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
9048 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
9049 error:
9050 isl_union_set_free(domain);
9051 isl_multi_val_free(mv);
9052 return NULL;
9055 /* Return a multiple union piecewise affine expression
9056 * that is equal to "ma" on "domain".
9058 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
9059 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
9061 isl_pw_multi_aff *pma;
9063 pma = isl_pw_multi_aff_from_multi_aff(ma);
9064 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
9067 /* Return a multiple union piecewise affine expression
9068 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9069 * have been aligned.
9071 * If the resulting multi union piecewise affine expression has
9072 * an explicit domain, then assign it the input domain.
9073 * In other cases, the domain is stored in the individual elements.
9075 static __isl_give isl_multi_union_pw_aff *
9076 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9077 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
9079 int i;
9080 isl_size n;
9081 isl_space *space;
9082 isl_multi_union_pw_aff *mupa;
9084 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
9085 if (!domain || n < 0)
9086 goto error;
9087 space = isl_pw_multi_aff_get_space(pma);
9088 mupa = isl_multi_union_pw_aff_alloc(space);
9089 for (i = 0; i < n; ++i) {
9090 isl_pw_aff *pa;
9091 isl_union_pw_aff *upa;
9093 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9094 upa = isl_union_pw_aff_pw_aff_on_domain(
9095 isl_union_set_copy(domain), pa);
9096 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9098 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9099 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
9100 isl_union_set_copy(domain));
9102 isl_union_set_free(domain);
9103 isl_pw_multi_aff_free(pma);
9104 return mupa;
9105 error:
9106 isl_union_set_free(domain);
9107 isl_pw_multi_aff_free(pma);
9108 return NULL;
9111 /* Return a multiple union piecewise affine expression
9112 * that is equal to "pma" on "domain".
9114 __isl_give isl_multi_union_pw_aff *
9115 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
9116 __isl_take isl_pw_multi_aff *pma)
9118 isl_bool equal_params;
9119 isl_space *space;
9121 space = isl_pw_multi_aff_peek_space(pma);
9122 equal_params = isl_union_set_space_has_equal_params(domain, space);
9123 if (equal_params < 0)
9124 goto error;
9125 if (equal_params)
9126 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9127 domain, pma);
9128 domain = isl_union_set_align_params(domain,
9129 isl_pw_multi_aff_get_space(pma));
9130 pma = isl_pw_multi_aff_align_params(pma,
9131 isl_union_set_get_space(domain));
9132 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
9133 pma);
9134 error:
9135 isl_union_set_free(domain);
9136 isl_pw_multi_aff_free(pma);
9137 return NULL;
9140 /* Return a union set containing those elements in the domains
9141 * of the elements of "mupa" where they are all zero.
9143 * If there are no elements, then simply return the entire domain.
9145 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
9146 __isl_take isl_multi_union_pw_aff *mupa)
9148 int i;
9149 isl_size n;
9150 isl_union_pw_aff *upa;
9151 isl_union_set *zero;
9153 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9154 if (n < 0)
9155 mupa = isl_multi_union_pw_aff_free(mupa);
9156 if (!mupa)
9157 return NULL;
9159 if (n == 0)
9160 return isl_multi_union_pw_aff_domain(mupa);
9162 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9163 zero = isl_union_pw_aff_zero_union_set(upa);
9165 for (i = 1; i < n; ++i) {
9166 isl_union_set *zero_i;
9168 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9169 zero_i = isl_union_pw_aff_zero_union_set(upa);
9171 zero = isl_union_set_intersect(zero, zero_i);
9174 isl_multi_union_pw_aff_free(mupa);
9175 return zero;
9178 /* Construct a union map mapping the shared domain
9179 * of the union piecewise affine expressions to the range of "mupa"
9180 * in the special case of a 0D multi union piecewise affine expression.
9182 * Construct a map between the explicit domain of "mupa" and
9183 * the range space.
9184 * Note that this assumes that the domain consists of explicit elements.
9186 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9187 __isl_take isl_multi_union_pw_aff *mupa)
9189 isl_bool is_params;
9190 isl_space *space;
9191 isl_union_set *dom, *ran;
9193 space = isl_multi_union_pw_aff_get_space(mupa);
9194 dom = isl_multi_union_pw_aff_domain(mupa);
9195 ran = isl_union_set_from_set(isl_set_universe(space));
9197 is_params = isl_union_set_is_params(dom);
9198 if (is_params < 0)
9199 dom = isl_union_set_free(dom);
9200 else if (is_params)
9201 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9202 "cannot create union map from expression without "
9203 "explicit domain elements",
9204 dom = isl_union_set_free(dom));
9206 return isl_union_map_from_domain_and_range(dom, ran);
9209 /* Construct a union map mapping the shared domain
9210 * of the union piecewise affine expressions to the range of "mupa"
9211 * with each dimension in the range equated to the
9212 * corresponding union piecewise affine expression.
9214 * If the input is zero-dimensional, then construct a mapping
9215 * from its explicit domain.
9217 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9218 __isl_take isl_multi_union_pw_aff *mupa)
9220 int i;
9221 isl_size n;
9222 isl_space *space;
9223 isl_union_map *umap;
9224 isl_union_pw_aff *upa;
9226 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9227 if (n < 0)
9228 mupa = isl_multi_union_pw_aff_free(mupa);
9229 if (!mupa)
9230 return NULL;
9232 if (n == 0)
9233 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9235 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9236 umap = isl_union_map_from_union_pw_aff(upa);
9238 for (i = 1; i < n; ++i) {
9239 isl_union_map *umap_i;
9241 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9242 umap_i = isl_union_map_from_union_pw_aff(upa);
9243 umap = isl_union_map_flat_range_product(umap, umap_i);
9246 space = isl_multi_union_pw_aff_get_space(mupa);
9247 umap = isl_union_map_reset_range_space(umap, space);
9249 isl_multi_union_pw_aff_free(mupa);
9250 return umap;
9253 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9254 * "range" is the space from which to set the range space.
9255 * "res" collects the results.
9257 struct isl_union_pw_multi_aff_reset_range_space_data {
9258 isl_space *range;
9259 isl_union_pw_multi_aff *res;
9262 /* Replace the range space of "pma" by the range space of data->range and
9263 * add the result to data->res.
9265 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9267 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9268 isl_space *space;
9270 space = isl_pw_multi_aff_get_space(pma);
9271 space = isl_space_domain(space);
9272 space = isl_space_extend_domain_with_range(space,
9273 isl_space_copy(data->range));
9274 pma = isl_pw_multi_aff_reset_space(pma, space);
9275 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9277 return data->res ? isl_stat_ok : isl_stat_error;
9280 /* Replace the range space of all the piecewise affine expressions in "upma" by
9281 * the range space of "space".
9283 * This assumes that all these expressions have the same output dimension.
9285 * Since the spaces of the expressions change, so do their hash values.
9286 * We therefore need to create a new isl_union_pw_multi_aff.
9287 * Note that the hash value is currently computed based on the entire
9288 * space even though there can only be a single expression with a given
9289 * domain space.
9291 static __isl_give isl_union_pw_multi_aff *
9292 isl_union_pw_multi_aff_reset_range_space(
9293 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9295 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9296 isl_space *space_upma;
9298 space_upma = isl_union_pw_multi_aff_get_space(upma);
9299 data.res = isl_union_pw_multi_aff_empty(space_upma);
9300 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9301 &reset_range_space, &data) < 0)
9302 data.res = isl_union_pw_multi_aff_free(data.res);
9304 isl_space_free(space);
9305 isl_union_pw_multi_aff_free(upma);
9306 return data.res;
9309 /* Construct and return a union piecewise multi affine expression
9310 * that is equal to the given multi union piecewise affine expression,
9311 * in the special case of a 0D multi union piecewise affine expression.
9313 * Construct a union piecewise multi affine expression
9314 * on top of the explicit domain of the input.
9316 __isl_give isl_union_pw_multi_aff *
9317 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9318 __isl_take isl_multi_union_pw_aff *mupa)
9320 isl_space *space;
9321 isl_multi_val *mv;
9322 isl_union_set *domain;
9324 space = isl_multi_union_pw_aff_get_space(mupa);
9325 mv = isl_multi_val_zero(space);
9326 domain = isl_multi_union_pw_aff_domain(mupa);
9327 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9330 /* Construct and return a union piecewise multi affine expression
9331 * that is equal to the given multi union piecewise affine expression.
9333 * If the input is zero-dimensional, then
9334 * construct a union piecewise multi affine expression
9335 * on top of the explicit domain of the input.
9337 __isl_give isl_union_pw_multi_aff *
9338 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9339 __isl_take isl_multi_union_pw_aff *mupa)
9341 int i;
9342 isl_size n;
9343 isl_space *space;
9344 isl_union_pw_multi_aff *upma;
9345 isl_union_pw_aff *upa;
9347 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9348 if (n < 0)
9349 mupa = isl_multi_union_pw_aff_free(mupa);
9350 if (!mupa)
9351 return NULL;
9353 if (n == 0)
9354 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9356 space = isl_multi_union_pw_aff_get_space(mupa);
9357 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9358 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9360 for (i = 1; i < n; ++i) {
9361 isl_union_pw_multi_aff *upma_i;
9363 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9364 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9365 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9368 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9370 isl_multi_union_pw_aff_free(mupa);
9371 return upma;
9374 /* Intersect the range of "mupa" with "range",
9375 * in the special case where "mupa" is 0D.
9377 * Intersect the domain of "mupa" with the constraints on the parameters
9378 * of "range".
9380 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9381 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9383 range = isl_set_params(range);
9384 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9385 return mupa;
9388 /* Intersect the range of "mupa" with "range".
9389 * That is, keep only those domain elements that have a function value
9390 * in "range".
9392 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9393 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9395 isl_union_pw_multi_aff *upma;
9396 isl_union_set *domain;
9397 isl_space *space;
9398 isl_size n;
9399 int match;
9401 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9402 if (n < 0 || !range)
9403 goto error;
9405 space = isl_set_get_space(range);
9406 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9407 space, isl_dim_set);
9408 isl_space_free(space);
9409 if (match < 0)
9410 goto error;
9411 if (!match)
9412 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9413 "space don't match", goto error);
9414 if (n == 0)
9415 return mupa_intersect_range_0D(mupa, range);
9417 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9418 isl_multi_union_pw_aff_copy(mupa));
9419 domain = isl_union_set_from_set(range);
9420 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9421 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9423 return mupa;
9424 error:
9425 isl_multi_union_pw_aff_free(mupa);
9426 isl_set_free(range);
9427 return NULL;
9430 /* Return the shared domain of the elements of "mupa",
9431 * in the special case where "mupa" is zero-dimensional.
9433 * Return the explicit domain of "mupa".
9434 * Note that this domain may be a parameter set, either
9435 * because "mupa" is meant to live in a set space or
9436 * because no explicit domain has been set.
9438 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9439 __isl_take isl_multi_union_pw_aff *mupa)
9441 isl_union_set *dom;
9443 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9444 isl_multi_union_pw_aff_free(mupa);
9446 return dom;
9449 /* Return the shared domain of the elements of "mupa".
9451 * If "mupa" is zero-dimensional, then return its explicit domain.
9453 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9454 __isl_take isl_multi_union_pw_aff *mupa)
9456 int i;
9457 isl_size n;
9458 isl_union_pw_aff *upa;
9459 isl_union_set *dom;
9461 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9462 if (n < 0)
9463 mupa = isl_multi_union_pw_aff_free(mupa);
9464 if (!mupa)
9465 return NULL;
9467 if (n == 0)
9468 return isl_multi_union_pw_aff_domain_0D(mupa);
9470 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9471 dom = isl_union_pw_aff_domain(upa);
9472 for (i = 1; i < n; ++i) {
9473 isl_union_set *dom_i;
9475 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9476 dom_i = isl_union_pw_aff_domain(upa);
9477 dom = isl_union_set_intersect(dom, dom_i);
9480 isl_multi_union_pw_aff_free(mupa);
9481 return dom;
9484 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9485 * In particular, the spaces have been aligned.
9486 * The result is defined over the shared domain of the elements of "mupa"
9488 * We first extract the parametric constant part of "aff" and
9489 * define that over the shared domain.
9490 * Then we iterate over all input dimensions of "aff" and add the corresponding
9491 * multiples of the elements of "mupa".
9492 * Finally, we consider the integer divisions, calling the function
9493 * recursively to obtain an isl_union_pw_aff corresponding to the
9494 * integer division argument.
9496 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9497 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9499 int i;
9500 isl_size n_in, n_div;
9501 isl_union_pw_aff *upa;
9502 isl_union_set *uset;
9503 isl_val *v;
9504 isl_aff *cst;
9506 n_in = isl_aff_dim(aff, isl_dim_in);
9507 n_div = isl_aff_dim(aff, isl_dim_div);
9508 if (n_in < 0 || n_div < 0)
9509 goto error;
9511 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9512 cst = isl_aff_copy(aff);
9513 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9514 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9515 cst = isl_aff_project_domain_on_params(cst);
9516 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9518 for (i = 0; i < n_in; ++i) {
9519 isl_union_pw_aff *upa_i;
9521 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9522 continue;
9523 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9524 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9525 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9526 upa = isl_union_pw_aff_add(upa, upa_i);
9529 for (i = 0; i < n_div; ++i) {
9530 isl_aff *div;
9531 isl_union_pw_aff *upa_i;
9533 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9534 continue;
9535 div = isl_aff_get_div(aff, i);
9536 upa_i = multi_union_pw_aff_apply_aff(
9537 isl_multi_union_pw_aff_copy(mupa), div);
9538 upa_i = isl_union_pw_aff_floor(upa_i);
9539 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9540 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9541 upa = isl_union_pw_aff_add(upa, upa_i);
9544 isl_multi_union_pw_aff_free(mupa);
9545 isl_aff_free(aff);
9547 return upa;
9548 error:
9549 isl_multi_union_pw_aff_free(mupa);
9550 isl_aff_free(aff);
9551 return NULL;
9554 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9555 * with the domain of "aff".
9556 * Furthermore, the dimension of this space needs to be greater than zero.
9557 * The result is defined over the shared domain of the elements of "mupa"
9559 * We perform these checks and then hand over control to
9560 * multi_union_pw_aff_apply_aff.
9562 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9563 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9565 isl_size dim;
9566 isl_space *space1, *space2;
9567 isl_bool equal;
9569 mupa = isl_multi_union_pw_aff_align_params(mupa,
9570 isl_aff_get_space(aff));
9571 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9572 if (!mupa || !aff)
9573 goto error;
9575 space1 = isl_multi_union_pw_aff_get_space(mupa);
9576 space2 = isl_aff_get_domain_space(aff);
9577 equal = isl_space_is_equal(space1, space2);
9578 isl_space_free(space1);
9579 isl_space_free(space2);
9580 if (equal < 0)
9581 goto error;
9582 if (!equal)
9583 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9584 "spaces don't match", goto error);
9585 dim = isl_aff_dim(aff, isl_dim_in);
9586 if (dim < 0)
9587 goto error;
9588 if (dim == 0)
9589 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9590 "cannot determine domains", goto error);
9592 return multi_union_pw_aff_apply_aff(mupa, aff);
9593 error:
9594 isl_multi_union_pw_aff_free(mupa);
9595 isl_aff_free(aff);
9596 return NULL;
9599 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9600 * The space of "mupa" is known to be compatible with the domain of "ma".
9602 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9603 * on the domain of "mupa".
9605 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9606 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9608 isl_union_set *dom;
9610 dom = isl_multi_union_pw_aff_domain(mupa);
9611 ma = isl_multi_aff_project_domain_on_params(ma);
9613 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9616 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9617 * with the domain of "ma".
9618 * The result is defined over the shared domain of the elements of "mupa"
9620 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9621 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9623 isl_space *space1, *space2;
9624 isl_multi_union_pw_aff *res;
9625 isl_bool equal;
9626 int i;
9627 isl_size n_in, n_out;
9629 mupa = isl_multi_union_pw_aff_align_params(mupa,
9630 isl_multi_aff_get_space(ma));
9631 ma = isl_multi_aff_align_params(ma,
9632 isl_multi_union_pw_aff_get_space(mupa));
9633 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9634 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9635 if (!mupa || n_in < 0 || n_out < 0)
9636 goto error;
9638 space1 = isl_multi_union_pw_aff_get_space(mupa);
9639 space2 = isl_multi_aff_get_domain_space(ma);
9640 equal = isl_space_is_equal(space1, space2);
9641 isl_space_free(space1);
9642 isl_space_free(space2);
9643 if (equal < 0)
9644 goto error;
9645 if (!equal)
9646 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9647 "spaces don't match", goto error);
9648 if (n_in == 0)
9649 return mupa_apply_multi_aff_0D(mupa, ma);
9651 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9652 res = isl_multi_union_pw_aff_alloc(space1);
9654 for (i = 0; i < n_out; ++i) {
9655 isl_aff *aff;
9656 isl_union_pw_aff *upa;
9658 aff = isl_multi_aff_get_aff(ma, i);
9659 upa = multi_union_pw_aff_apply_aff(
9660 isl_multi_union_pw_aff_copy(mupa), aff);
9661 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9664 isl_multi_aff_free(ma);
9665 isl_multi_union_pw_aff_free(mupa);
9666 return res;
9667 error:
9668 isl_multi_union_pw_aff_free(mupa);
9669 isl_multi_aff_free(ma);
9670 return NULL;
9673 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9674 * The space of "mupa" is known to be compatible with the domain of "pa".
9676 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9677 * on the domain of "mupa".
9679 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9680 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9682 isl_union_set *dom;
9684 dom = isl_multi_union_pw_aff_domain(mupa);
9685 pa = isl_pw_aff_project_domain_on_params(pa);
9687 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9690 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9691 * with the domain of "pa".
9692 * Furthermore, the dimension of this space needs to be greater than zero.
9693 * The result is defined over the shared domain of the elements of "mupa"
9695 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9696 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9698 int i;
9699 isl_bool equal;
9700 isl_size n_in;
9701 isl_space *space, *space2;
9702 isl_union_pw_aff *upa;
9704 mupa = isl_multi_union_pw_aff_align_params(mupa,
9705 isl_pw_aff_get_space(pa));
9706 pa = isl_pw_aff_align_params(pa,
9707 isl_multi_union_pw_aff_get_space(mupa));
9708 if (!mupa || !pa)
9709 goto error;
9711 space = isl_multi_union_pw_aff_get_space(mupa);
9712 space2 = isl_pw_aff_get_domain_space(pa);
9713 equal = isl_space_is_equal(space, space2);
9714 isl_space_free(space);
9715 isl_space_free(space2);
9716 if (equal < 0)
9717 goto error;
9718 if (!equal)
9719 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9720 "spaces don't match", goto error);
9721 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9722 if (n_in < 0)
9723 goto error;
9724 if (n_in == 0)
9725 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9727 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9728 upa = isl_union_pw_aff_empty(space);
9730 for (i = 0; i < pa->n; ++i) {
9731 isl_aff *aff;
9732 isl_set *domain;
9733 isl_multi_union_pw_aff *mupa_i;
9734 isl_union_pw_aff *upa_i;
9736 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9737 domain = isl_set_copy(pa->p[i].set);
9738 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9739 aff = isl_aff_copy(pa->p[i].aff);
9740 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9741 upa = isl_union_pw_aff_union_add(upa, upa_i);
9744 isl_multi_union_pw_aff_free(mupa);
9745 isl_pw_aff_free(pa);
9746 return upa;
9747 error:
9748 isl_multi_union_pw_aff_free(mupa);
9749 isl_pw_aff_free(pa);
9750 return NULL;
9753 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9754 * The space of "mupa" is known to be compatible with the domain of "pma".
9756 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9757 * on the domain of "mupa".
9759 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9760 __isl_take isl_multi_union_pw_aff *mupa,
9761 __isl_take isl_pw_multi_aff *pma)
9763 isl_union_set *dom;
9765 dom = isl_multi_union_pw_aff_domain(mupa);
9766 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9768 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9771 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9772 * with the domain of "pma".
9773 * The result is defined over the shared domain of the elements of "mupa"
9775 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9776 __isl_take isl_multi_union_pw_aff *mupa,
9777 __isl_take isl_pw_multi_aff *pma)
9779 isl_space *space1, *space2;
9780 isl_multi_union_pw_aff *res;
9781 isl_bool equal;
9782 int i;
9783 isl_size n_in, n_out;
9785 mupa = isl_multi_union_pw_aff_align_params(mupa,
9786 isl_pw_multi_aff_get_space(pma));
9787 pma = isl_pw_multi_aff_align_params(pma,
9788 isl_multi_union_pw_aff_get_space(mupa));
9789 if (!mupa || !pma)
9790 goto error;
9792 space1 = isl_multi_union_pw_aff_get_space(mupa);
9793 space2 = isl_pw_multi_aff_get_domain_space(pma);
9794 equal = isl_space_is_equal(space1, space2);
9795 isl_space_free(space1);
9796 isl_space_free(space2);
9797 if (equal < 0)
9798 goto error;
9799 if (!equal)
9800 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9801 "spaces don't match", goto error);
9802 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9803 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9804 if (n_in < 0 || n_out < 0)
9805 goto error;
9806 if (n_in == 0)
9807 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9809 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9810 res = isl_multi_union_pw_aff_alloc(space1);
9812 for (i = 0; i < n_out; ++i) {
9813 isl_pw_aff *pa;
9814 isl_union_pw_aff *upa;
9816 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9817 upa = isl_multi_union_pw_aff_apply_pw_aff(
9818 isl_multi_union_pw_aff_copy(mupa), pa);
9819 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9822 isl_pw_multi_aff_free(pma);
9823 isl_multi_union_pw_aff_free(mupa);
9824 return res;
9825 error:
9826 isl_multi_union_pw_aff_free(mupa);
9827 isl_pw_multi_aff_free(pma);
9828 return NULL;
9831 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9832 * If the explicit domain only keeps track of constraints on the parameters,
9833 * then only update those constraints.
9835 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9836 __isl_take isl_multi_union_pw_aff *mupa,
9837 __isl_keep isl_union_pw_multi_aff *upma)
9839 isl_bool is_params;
9841 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9842 return isl_multi_union_pw_aff_free(mupa);
9844 mupa = isl_multi_union_pw_aff_cow(mupa);
9845 if (!mupa)
9846 return NULL;
9848 is_params = isl_union_set_is_params(mupa->u.dom);
9849 if (is_params < 0)
9850 return isl_multi_union_pw_aff_free(mupa);
9852 upma = isl_union_pw_multi_aff_copy(upma);
9853 if (is_params)
9854 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9855 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9856 else
9857 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9858 mupa->u.dom, upma);
9859 if (!mupa->u.dom)
9860 return isl_multi_union_pw_aff_free(mupa);
9861 return mupa;
9864 /* Compute the pullback of "mupa" by the function represented by "upma".
9865 * In other words, plug in "upma" in "mupa". The result contains
9866 * expressions defined over the domain space of "upma".
9868 * Run over all elements of "mupa" and plug in "upma" in each of them.
9870 * If "mupa" has an explicit domain, then it is this domain
9871 * that needs to undergo a pullback instead, i.e., a preimage.
9873 __isl_give isl_multi_union_pw_aff *
9874 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9875 __isl_take isl_multi_union_pw_aff *mupa,
9876 __isl_take isl_union_pw_multi_aff *upma)
9878 int i;
9879 isl_size n;
9881 mupa = isl_multi_union_pw_aff_align_params(mupa,
9882 isl_union_pw_multi_aff_get_space(upma));
9883 upma = isl_union_pw_multi_aff_align_params(upma,
9884 isl_multi_union_pw_aff_get_space(mupa));
9885 mupa = isl_multi_union_pw_aff_cow(mupa);
9886 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9887 if (n < 0 || !upma)
9888 goto error;
9890 for (i = 0; i < n; ++i) {
9891 isl_union_pw_aff *upa;
9893 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9894 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9895 isl_union_pw_multi_aff_copy(upma));
9896 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9899 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9900 mupa = preimage_explicit_domain(mupa, upma);
9902 isl_union_pw_multi_aff_free(upma);
9903 return mupa;
9904 error:
9905 isl_multi_union_pw_aff_free(mupa);
9906 isl_union_pw_multi_aff_free(upma);
9907 return NULL;
9910 /* Extract the sequence of elements in "mupa" with domain space "space"
9911 * (ignoring parameters).
9913 * For the elements of "mupa" that are not defined on the specified space,
9914 * the corresponding element in the result is empty.
9916 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9917 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9919 int i;
9920 isl_size n;
9921 isl_space *space_mpa;
9922 isl_multi_pw_aff *mpa;
9924 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9925 if (n < 0 || !space)
9926 goto error;
9928 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9929 space = isl_space_replace_params(space, space_mpa);
9930 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9931 space_mpa);
9932 mpa = isl_multi_pw_aff_alloc(space_mpa);
9934 space = isl_space_from_domain(space);
9935 space = isl_space_add_dims(space, isl_dim_out, 1);
9936 for (i = 0; i < n; ++i) {
9937 isl_union_pw_aff *upa;
9938 isl_pw_aff *pa;
9940 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9941 pa = isl_union_pw_aff_extract_pw_aff(upa,
9942 isl_space_copy(space));
9943 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9944 isl_union_pw_aff_free(upa);
9947 isl_space_free(space);
9948 return mpa;
9949 error:
9950 isl_space_free(space);
9951 return NULL;
9954 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9955 * should modify the base expressions in the input.
9957 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9958 * are taken into account.
9959 * "fn" is applied to each entry in the input.
9961 struct isl_union_pw_multi_aff_un_op_control {
9962 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9963 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9966 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9967 * a second argument) for use as an isl_union_pw_multi_aff_transform
9968 * filter function (which does take a second argument).
9969 * Simply call control->filter without the second argument.
9971 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9972 __isl_take isl_pw_multi_aff *pma, void *user)
9974 struct isl_union_pw_multi_aff_un_op_control *control = user;
9976 return control->filter(pma);
9979 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9980 * a second argument) for use as an isl_union_pw_multi_aff_transform
9981 * base function (which does take a second argument).
9982 * Simply call control->fn without the second argument.
9984 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9985 __isl_take isl_pw_multi_aff *pma, void *user)
9987 struct isl_union_pw_multi_aff_un_op_control *control = user;
9989 return control->fn(pma);
9992 /* Construct an isl_union_pw_multi_aff that is obtained by
9993 * modifying "upma" according to "control".
9995 * isl_union_pw_multi_aff_transform performs essentially
9996 * the same operation, but takes a filter and a callback function
9997 * of a different form (with an extra argument).
9998 * Call isl_union_pw_multi_aff_transform with wrappers
9999 * that remove this extra argument.
10001 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
10002 __isl_take isl_union_pw_multi_aff *upma,
10003 struct isl_union_pw_multi_aff_un_op_control *control)
10005 struct isl_union_pw_multi_aff_transform_control t_control = {
10006 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
10007 .filter_user = control,
10008 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
10009 .fn_user = control,
10012 return isl_union_pw_multi_aff_transform(upma, &t_control);
10015 /* For each function in "upma" of the form A -> [B -> C],
10016 * extract the function A -> B and collect the results.
10018 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
10019 __isl_take isl_union_pw_multi_aff *upma)
10021 struct isl_union_pw_multi_aff_un_op_control control = {
10022 .filter = &isl_pw_multi_aff_range_is_wrapping,
10023 .fn = &isl_pw_multi_aff_range_factor_domain,
10025 return isl_union_pw_multi_aff_un_op(upma, &control);
10028 /* For each function in "upma" of the form A -> [B -> C],
10029 * extract the function A -> C and collect the results.
10031 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
10032 __isl_take isl_union_pw_multi_aff *upma)
10034 struct isl_union_pw_multi_aff_un_op_control control = {
10035 .filter = &isl_pw_multi_aff_range_is_wrapping,
10036 .fn = &isl_pw_multi_aff_range_factor_range,
10038 return isl_union_pw_multi_aff_un_op(upma, &control);
10041 /* Evaluate the affine function "aff" in the void point "pnt".
10042 * In particular, return the value NaN.
10044 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
10045 __isl_take isl_point *pnt)
10047 isl_ctx *ctx;
10049 ctx = isl_point_get_ctx(pnt);
10050 isl_aff_free(aff);
10051 isl_point_free(pnt);
10052 return isl_val_nan(ctx);
10055 /* Evaluate the affine expression "aff"
10056 * in the coordinates (with denominator) "pnt".
10058 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
10059 __isl_keep isl_vec *pnt)
10061 isl_int n, d;
10062 isl_ctx *ctx;
10063 isl_val *v;
10065 if (!aff || !pnt)
10066 return NULL;
10068 ctx = isl_vec_get_ctx(aff);
10069 isl_int_init(n);
10070 isl_int_init(d);
10071 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
10072 isl_int_mul(d, aff->el[0], pnt->el[0]);
10073 v = isl_val_rat_from_isl_int(ctx, n, d);
10074 v = isl_val_normalize(v);
10075 isl_int_clear(n);
10076 isl_int_clear(d);
10078 return v;
10081 /* Check that the domain space of "aff" is equal to "space".
10083 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
10084 __isl_keep isl_space *space)
10086 isl_bool ok;
10088 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
10089 if (ok < 0)
10090 return isl_stat_error;
10091 if (!ok)
10092 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
10093 "incompatible spaces", return isl_stat_error);
10094 return isl_stat_ok;
10097 /* Evaluate the affine function "aff" in "pnt".
10099 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
10100 __isl_take isl_point *pnt)
10102 isl_bool is_void;
10103 isl_val *v;
10104 isl_local_space *ls;
10106 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
10107 goto error;
10108 is_void = isl_point_is_void(pnt);
10109 if (is_void < 0)
10110 goto error;
10111 if (is_void)
10112 return eval_void(aff, pnt);
10114 ls = isl_aff_get_domain_local_space(aff);
10115 pnt = isl_local_space_lift_point(ls, pnt);
10117 v = eval(aff->v, isl_point_peek_vec(pnt));
10119 isl_aff_free(aff);
10120 isl_point_free(pnt);
10122 return v;
10123 error:
10124 isl_aff_free(aff);
10125 isl_point_free(pnt);
10126 return NULL;