isl_val_is_pos: use isl_bool_ok
[isl.git] / isl_aff.c
bloba71df704971eeee545f283fd7452a97a2913c768
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
25 #include <isl/id.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
53 #undef BASE
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
59 __isl_take isl_vec *v)
61 isl_aff *aff;
63 if (!ls || !v)
64 goto error;
66 aff = isl_calloc_type(v->ctx, struct isl_aff);
67 if (!aff)
68 goto error;
70 aff->ref = 1;
71 aff->ls = ls;
72 aff->v = v;
74 return aff;
75 error:
76 isl_local_space_free(ls);
77 isl_vec_free(v);
78 return NULL;
81 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
83 isl_ctx *ctx;
84 isl_vec *v;
85 unsigned total;
87 if (!ls)
88 return NULL;
90 ctx = isl_local_space_get_ctx(ls);
91 if (!isl_local_space_divs_known(ls))
92 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
93 goto error);
94 if (!isl_local_space_is_set(ls))
95 isl_die(ctx, isl_error_invalid,
96 "domain of affine expression should be a set",
97 goto error);
99 total = isl_local_space_dim(ls, isl_dim_all);
100 v = isl_vec_alloc(ctx, 1 + 1 + total);
101 return isl_aff_alloc_vec(ls, v);
102 error:
103 isl_local_space_free(ls);
104 return NULL;
107 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
109 isl_aff *aff;
111 aff = isl_aff_alloc(ls);
112 if (!aff)
113 return NULL;
115 isl_int_set_si(aff->v->el[0], 1);
116 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
118 return aff;
121 /* Return a piecewise affine expression defined on the specified domain
122 * that is equal to zero.
124 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
126 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
129 /* Return an affine expression defined on the specified domain
130 * that represents NaN.
132 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
134 isl_aff *aff;
136 aff = isl_aff_alloc(ls);
137 if (!aff)
138 return NULL;
140 isl_seq_clr(aff->v->el, aff->v->size);
142 return aff;
145 /* Return a piecewise affine expression defined on the specified domain
146 * that represents NaN.
148 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
150 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
153 /* Return an affine expression that is equal to "val" on
154 * domain local space "ls".
156 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
157 __isl_take isl_val *val)
159 isl_aff *aff;
161 if (!ls || !val)
162 goto error;
163 if (!isl_val_is_rat(val))
164 isl_die(isl_val_get_ctx(val), isl_error_invalid,
165 "expecting rational value", goto error);
167 aff = isl_aff_alloc(isl_local_space_copy(ls));
168 if (!aff)
169 goto error;
171 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
172 isl_int_set(aff->v->el[1], val->n);
173 isl_int_set(aff->v->el[0], val->d);
175 isl_local_space_free(ls);
176 isl_val_free(val);
177 return aff;
178 error:
179 isl_local_space_free(ls);
180 isl_val_free(val);
181 return NULL;
184 /* Return an affine expression that is equal to the specified dimension
185 * in "ls".
187 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
188 enum isl_dim_type type, unsigned pos)
190 isl_space *space;
191 isl_aff *aff;
193 if (!ls)
194 return NULL;
196 space = isl_local_space_get_space(ls);
197 if (!space)
198 goto error;
199 if (isl_space_is_map(space))
200 isl_die(isl_space_get_ctx(space), isl_error_invalid,
201 "expecting (parameter) set space", goto error);
202 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
203 goto error;
205 isl_space_free(space);
206 aff = isl_aff_alloc(ls);
207 if (!aff)
208 return NULL;
210 pos += isl_local_space_offset(aff->ls, type);
212 isl_int_set_si(aff->v->el[0], 1);
213 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
214 isl_int_set_si(aff->v->el[1 + pos], 1);
216 return aff;
217 error:
218 isl_local_space_free(ls);
219 isl_space_free(space);
220 return NULL;
223 /* Return a piecewise affine expression that is equal to
224 * the specified dimension in "ls".
226 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
227 enum isl_dim_type type, unsigned pos)
229 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
232 /* Return an affine expression that is equal to the parameter
233 * in the domain space "space" with identifier "id".
235 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
236 __isl_take isl_space *space, __isl_take isl_id *id)
238 int pos;
239 isl_local_space *ls;
241 if (!space || !id)
242 goto error;
243 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
244 if (pos < 0)
245 isl_die(isl_space_get_ctx(space), isl_error_invalid,
246 "parameter not found in space", goto error);
247 isl_id_free(id);
248 ls = isl_local_space_from_space(space);
249 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
250 error:
251 isl_space_free(space);
252 isl_id_free(id);
253 return NULL;
256 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
258 if (!aff)
259 return NULL;
261 aff->ref++;
262 return aff;
265 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
267 if (!aff)
268 return NULL;
270 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
271 isl_vec_copy(aff->v));
274 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
276 if (!aff)
277 return NULL;
279 if (aff->ref == 1)
280 return aff;
281 aff->ref--;
282 return isl_aff_dup(aff);
285 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
287 if (!aff)
288 return NULL;
290 if (--aff->ref > 0)
291 return NULL;
293 isl_local_space_free(aff->ls);
294 isl_vec_free(aff->v);
296 free(aff);
298 return NULL;
301 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
303 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
306 /* Return a hash value that digests "aff".
308 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
310 uint32_t hash, ls_hash, v_hash;
312 if (!aff)
313 return 0;
315 hash = isl_hash_init();
316 ls_hash = isl_local_space_get_hash(aff->ls);
317 isl_hash_hash(hash, ls_hash);
318 v_hash = isl_vec_get_hash(aff->v);
319 isl_hash_hash(hash, v_hash);
321 return hash;
324 /* Externally, an isl_aff has a map space, but internally, the
325 * ls field corresponds to the domain of that space.
327 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
329 if (!aff)
330 return 0;
331 if (type == isl_dim_out)
332 return 1;
333 if (type == isl_dim_in)
334 type = isl_dim_set;
335 return isl_local_space_dim(aff->ls, type);
338 /* Return the position of the dimension of the given type and name
339 * in "aff".
340 * Return -1 if no such dimension can be found.
342 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
343 const char *name)
345 if (!aff)
346 return -1;
347 if (type == isl_dim_out)
348 return -1;
349 if (type == isl_dim_in)
350 type = isl_dim_set;
351 return isl_local_space_find_dim_by_name(aff->ls, type, name);
354 /* Return the domain space of "aff".
356 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
358 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
361 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
363 return isl_space_copy(isl_aff_peek_domain_space(aff));
366 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
368 isl_space *space;
369 if (!aff)
370 return NULL;
371 space = isl_local_space_get_space(aff->ls);
372 space = isl_space_from_domain(space);
373 space = isl_space_add_dims(space, isl_dim_out, 1);
374 return space;
377 __isl_give isl_local_space *isl_aff_get_domain_local_space(
378 __isl_keep isl_aff *aff)
380 return aff ? isl_local_space_copy(aff->ls) : NULL;
383 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
385 isl_local_space *ls;
386 if (!aff)
387 return NULL;
388 ls = isl_local_space_copy(aff->ls);
389 ls = isl_local_space_from_domain(ls);
390 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
391 return ls;
394 /* Return the local space of the domain of "aff".
395 * This may be either a copy or the local space itself
396 * if there is only one reference to "aff".
397 * This allows the local space to be modified inplace
398 * if both the expression and its local space have only a single reference.
399 * The caller is not allowed to modify "aff" between this call and
400 * a subsequent call to isl_aff_restore_domain_local_space.
401 * The only exception is that isl_aff_free can be called instead.
403 __isl_give isl_local_space *isl_aff_take_domain_local_space(
404 __isl_keep isl_aff *aff)
406 isl_local_space *ls;
408 if (!aff)
409 return NULL;
410 if (aff->ref != 1)
411 return isl_aff_get_domain_local_space(aff);
412 ls = aff->ls;
413 aff->ls = NULL;
414 return ls;
417 /* Set the local space of the domain of "aff" to "ls",
418 * where the local space of "aff" may be missing
419 * due to a preceding call to isl_aff_take_domain_local_space.
420 * However, in this case, "aff" only has a single reference and
421 * then the call to isl_aff_cow has no effect.
423 __isl_give isl_aff *isl_aff_restore_domain_local_space(
424 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
426 if (!aff || !ls)
427 goto error;
429 if (aff->ls == ls) {
430 isl_local_space_free(ls);
431 return aff;
434 aff = isl_aff_cow(aff);
435 if (!aff)
436 goto error;
437 isl_local_space_free(aff->ls);
438 aff->ls = ls;
440 return aff;
441 error:
442 isl_aff_free(aff);
443 isl_local_space_free(ls);
444 return NULL;
447 /* Externally, an isl_aff has a map space, but internally, the
448 * ls field corresponds to the domain of that space.
450 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
451 enum isl_dim_type type, unsigned pos)
453 if (!aff)
454 return NULL;
455 if (type == isl_dim_out)
456 return NULL;
457 if (type == isl_dim_in)
458 type = isl_dim_set;
459 return isl_local_space_get_dim_name(aff->ls, type, pos);
462 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
463 __isl_take isl_space *dim)
465 aff = isl_aff_cow(aff);
466 if (!aff || !dim)
467 goto error;
469 aff->ls = isl_local_space_reset_space(aff->ls, dim);
470 if (!aff->ls)
471 return isl_aff_free(aff);
473 return aff;
474 error:
475 isl_aff_free(aff);
476 isl_space_free(dim);
477 return NULL;
480 /* Reset the space of "aff". This function is called from isl_pw_templ.c
481 * and doesn't know if the space of an element object is represented
482 * directly or through its domain. It therefore passes along both.
484 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
485 __isl_take isl_space *space, __isl_take isl_space *domain)
487 isl_space_free(space);
488 return isl_aff_reset_domain_space(aff, domain);
491 /* Reorder the coefficients of the affine expression based
492 * on the given reordering.
493 * The reordering r is assumed to have been extended with the local
494 * variables.
496 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
497 __isl_take isl_reordering *r, int n_div)
499 isl_space *space;
500 isl_vec *res;
501 int i;
503 if (!vec || !r)
504 goto error;
506 space = isl_reordering_peek_space(r);
507 res = isl_vec_alloc(vec->ctx,
508 2 + isl_space_dim(space, isl_dim_all) + n_div);
509 if (!res)
510 goto error;
511 isl_seq_cpy(res->el, vec->el, 2);
512 isl_seq_clr(res->el + 2, res->size - 2);
513 for (i = 0; i < r->len; ++i)
514 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
516 isl_reordering_free(r);
517 isl_vec_free(vec);
518 return res;
519 error:
520 isl_vec_free(vec);
521 isl_reordering_free(r);
522 return NULL;
525 /* Reorder the dimensions of the domain of "aff" according
526 * to the given reordering.
528 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
529 __isl_take isl_reordering *r)
531 aff = isl_aff_cow(aff);
532 if (!aff)
533 goto error;
535 r = isl_reordering_extend(r, aff->ls->div->n_row);
536 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
537 aff->ls->div->n_row);
538 aff->ls = isl_local_space_realign(aff->ls, r);
540 if (!aff->v || !aff->ls)
541 return isl_aff_free(aff);
543 return aff;
544 error:
545 isl_aff_free(aff);
546 isl_reordering_free(r);
547 return NULL;
550 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
551 __isl_take isl_space *model)
553 isl_bool equal_params;
555 if (!aff || !model)
556 goto error;
558 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
559 if (equal_params < 0)
560 goto error;
561 if (!equal_params) {
562 isl_reordering *exp;
564 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
565 exp = isl_reordering_extend_space(exp,
566 isl_aff_get_domain_space(aff));
567 aff = isl_aff_realign_domain(aff, exp);
570 isl_space_free(model);
571 return aff;
572 error:
573 isl_space_free(model);
574 isl_aff_free(aff);
575 return NULL;
578 /* Is "aff" obviously equal to zero?
580 * If the denominator is zero, then "aff" is not equal to zero.
582 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
584 if (!aff)
585 return isl_bool_error;
587 if (isl_int_is_zero(aff->v->el[0]))
588 return isl_bool_false;
589 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
592 /* Does "aff" represent NaN?
594 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
596 if (!aff)
597 return isl_bool_error;
599 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
602 /* Are "aff1" and "aff2" obviously equal?
604 * NaN is not equal to anything, not even to another NaN.
606 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
607 __isl_keep isl_aff *aff2)
609 isl_bool equal;
611 if (!aff1 || !aff2)
612 return isl_bool_error;
614 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
615 return isl_bool_false;
617 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
618 if (equal < 0 || !equal)
619 return equal;
621 return isl_vec_is_equal(aff1->v, aff2->v);
624 /* Return the common denominator of "aff" in "v".
626 * We cannot return anything meaningful in case of a NaN.
628 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
630 if (!aff)
631 return isl_stat_error;
632 if (isl_aff_is_nan(aff))
633 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
634 "cannot get denominator of NaN", return isl_stat_error);
635 isl_int_set(*v, aff->v->el[0]);
636 return isl_stat_ok;
639 /* Return the common denominator of "aff".
641 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
643 isl_ctx *ctx;
645 if (!aff)
646 return NULL;
648 ctx = isl_aff_get_ctx(aff);
649 if (isl_aff_is_nan(aff))
650 return isl_val_nan(ctx);
651 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
654 /* Return the constant term of "aff".
656 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
658 isl_ctx *ctx;
659 isl_val *v;
661 if (!aff)
662 return NULL;
664 ctx = isl_aff_get_ctx(aff);
665 if (isl_aff_is_nan(aff))
666 return isl_val_nan(ctx);
667 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
668 return isl_val_normalize(v);
671 /* Return the coefficient of the variable of type "type" at position "pos"
672 * of "aff".
674 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
675 enum isl_dim_type type, int pos)
677 isl_ctx *ctx;
678 isl_val *v;
680 if (!aff)
681 return NULL;
683 ctx = isl_aff_get_ctx(aff);
684 if (type == isl_dim_out)
685 isl_die(ctx, isl_error_invalid,
686 "output/set dimension does not have a coefficient",
687 return NULL);
688 if (type == isl_dim_in)
689 type = isl_dim_set;
691 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
692 return NULL;
694 if (isl_aff_is_nan(aff))
695 return isl_val_nan(ctx);
696 pos += isl_local_space_offset(aff->ls, type);
697 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
698 return isl_val_normalize(v);
701 /* Return the sign of the coefficient of the variable of type "type"
702 * at position "pos" of "aff".
704 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
705 int pos)
707 isl_ctx *ctx;
709 if (!aff)
710 return 0;
712 ctx = isl_aff_get_ctx(aff);
713 if (type == isl_dim_out)
714 isl_die(ctx, isl_error_invalid,
715 "output/set dimension does not have a coefficient",
716 return 0);
717 if (type == isl_dim_in)
718 type = isl_dim_set;
720 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
721 return 0;
723 pos += isl_local_space_offset(aff->ls, type);
724 return isl_int_sgn(aff->v->el[1 + pos]);
727 /* Replace the numerator of the constant term of "aff" by "v".
729 * A NaN is unaffected by this operation.
731 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
733 if (!aff)
734 return NULL;
735 if (isl_aff_is_nan(aff))
736 return aff;
737 aff = isl_aff_cow(aff);
738 if (!aff)
739 return NULL;
741 aff->v = isl_vec_cow(aff->v);
742 if (!aff->v)
743 return isl_aff_free(aff);
745 isl_int_set(aff->v->el[1], v);
747 return aff;
750 /* Replace the constant term of "aff" by "v".
752 * A NaN is unaffected by this operation.
754 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
755 __isl_take isl_val *v)
757 if (!aff || !v)
758 goto error;
760 if (isl_aff_is_nan(aff)) {
761 isl_val_free(v);
762 return aff;
765 if (!isl_val_is_rat(v))
766 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
767 "expecting rational value", goto error);
769 if (isl_int_eq(aff->v->el[1], v->n) &&
770 isl_int_eq(aff->v->el[0], v->d)) {
771 isl_val_free(v);
772 return aff;
775 aff = isl_aff_cow(aff);
776 if (!aff)
777 goto error;
778 aff->v = isl_vec_cow(aff->v);
779 if (!aff->v)
780 goto error;
782 if (isl_int_eq(aff->v->el[0], v->d)) {
783 isl_int_set(aff->v->el[1], v->n);
784 } else if (isl_int_is_one(v->d)) {
785 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
786 } else {
787 isl_seq_scale(aff->v->el + 1,
788 aff->v->el + 1, v->d, aff->v->size - 1);
789 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
790 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
791 aff->v = isl_vec_normalize(aff->v);
792 if (!aff->v)
793 goto error;
796 isl_val_free(v);
797 return aff;
798 error:
799 isl_aff_free(aff);
800 isl_val_free(v);
801 return NULL;
804 /* Add "v" to the constant term of "aff".
806 * A NaN is unaffected by this operation.
808 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
810 if (isl_int_is_zero(v))
811 return aff;
813 if (!aff)
814 return NULL;
815 if (isl_aff_is_nan(aff))
816 return aff;
817 aff = isl_aff_cow(aff);
818 if (!aff)
819 return NULL;
821 aff->v = isl_vec_cow(aff->v);
822 if (!aff->v)
823 return isl_aff_free(aff);
825 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
827 return aff;
830 /* Add "v" to the constant term of "aff".
832 * A NaN is unaffected by this operation.
834 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
835 __isl_take isl_val *v)
837 if (!aff || !v)
838 goto error;
840 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
841 isl_val_free(v);
842 return aff;
845 if (!isl_val_is_rat(v))
846 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
847 "expecting rational value", goto error);
849 aff = isl_aff_cow(aff);
850 if (!aff)
851 goto error;
853 aff->v = isl_vec_cow(aff->v);
854 if (!aff->v)
855 goto error;
857 if (isl_int_is_one(v->d)) {
858 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
859 } else if (isl_int_eq(aff->v->el[0], v->d)) {
860 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
861 aff->v = isl_vec_normalize(aff->v);
862 if (!aff->v)
863 goto error;
864 } else {
865 isl_seq_scale(aff->v->el + 1,
866 aff->v->el + 1, v->d, aff->v->size - 1);
867 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
868 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
869 aff->v = isl_vec_normalize(aff->v);
870 if (!aff->v)
871 goto error;
874 isl_val_free(v);
875 return aff;
876 error:
877 isl_aff_free(aff);
878 isl_val_free(v);
879 return NULL;
882 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
884 isl_int t;
886 isl_int_init(t);
887 isl_int_set_si(t, v);
888 aff = isl_aff_add_constant(aff, t);
889 isl_int_clear(t);
891 return aff;
894 /* Add "v" to the numerator of the constant term of "aff".
896 * A NaN is unaffected by this operation.
898 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
900 if (isl_int_is_zero(v))
901 return aff;
903 if (!aff)
904 return NULL;
905 if (isl_aff_is_nan(aff))
906 return aff;
907 aff = isl_aff_cow(aff);
908 if (!aff)
909 return NULL;
911 aff->v = isl_vec_cow(aff->v);
912 if (!aff->v)
913 return isl_aff_free(aff);
915 isl_int_add(aff->v->el[1], aff->v->el[1], v);
917 return aff;
920 /* Add "v" to the numerator of the constant term of "aff".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
926 isl_int t;
928 if (v == 0)
929 return aff;
931 isl_int_init(t);
932 isl_int_set_si(t, v);
933 aff = isl_aff_add_constant_num(aff, t);
934 isl_int_clear(t);
936 return aff;
939 /* Replace the numerator of the constant term of "aff" by "v".
941 * A NaN is unaffected by this operation.
943 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
945 if (!aff)
946 return NULL;
947 if (isl_aff_is_nan(aff))
948 return aff;
949 aff = isl_aff_cow(aff);
950 if (!aff)
951 return NULL;
953 aff->v = isl_vec_cow(aff->v);
954 if (!aff->v)
955 return isl_aff_free(aff);
957 isl_int_set_si(aff->v->el[1], v);
959 return aff;
962 /* Replace the numerator of the coefficient of the variable of type "type"
963 * at position "pos" of "aff" by "v".
965 * A NaN is unaffected by this operation.
967 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
968 enum isl_dim_type type, int pos, isl_int v)
970 if (!aff)
971 return NULL;
973 if (type == isl_dim_out)
974 isl_die(aff->v->ctx, isl_error_invalid,
975 "output/set dimension does not have a coefficient",
976 return isl_aff_free(aff));
977 if (type == isl_dim_in)
978 type = isl_dim_set;
980 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
981 return isl_aff_free(aff);
983 if (isl_aff_is_nan(aff))
984 return aff;
985 aff = isl_aff_cow(aff);
986 if (!aff)
987 return NULL;
989 aff->v = isl_vec_cow(aff->v);
990 if (!aff->v)
991 return isl_aff_free(aff);
993 pos += isl_local_space_offset(aff->ls, type);
994 isl_int_set(aff->v->el[1 + pos], v);
996 return aff;
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1005 enum isl_dim_type type, int pos, int v)
1007 if (!aff)
1008 return NULL;
1010 if (type == isl_dim_out)
1011 isl_die(aff->v->ctx, isl_error_invalid,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff));
1014 if (type == isl_dim_in)
1015 type = isl_dim_set;
1017 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1018 return isl_aff_free(aff);
1020 if (isl_aff_is_nan(aff))
1021 return aff;
1022 pos += isl_local_space_offset(aff->ls, type);
1023 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1024 return aff;
1026 aff = isl_aff_cow(aff);
1027 if (!aff)
1028 return NULL;
1030 aff->v = isl_vec_cow(aff->v);
1031 if (!aff->v)
1032 return isl_aff_free(aff);
1034 isl_int_set_si(aff->v->el[1 + pos], v);
1036 return aff;
1039 /* Replace the coefficient of the variable of type "type" at position "pos"
1040 * of "aff" by "v".
1042 * A NaN is unaffected by this operation.
1044 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1045 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1047 if (!aff || !v)
1048 goto error;
1050 if (type == isl_dim_out)
1051 isl_die(aff->v->ctx, isl_error_invalid,
1052 "output/set dimension does not have a coefficient",
1053 goto error);
1054 if (type == isl_dim_in)
1055 type = isl_dim_set;
1057 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1058 return isl_aff_free(aff);
1060 if (isl_aff_is_nan(aff)) {
1061 isl_val_free(v);
1062 return aff;
1064 if (!isl_val_is_rat(v))
1065 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1066 "expecting rational value", goto error);
1068 pos += isl_local_space_offset(aff->ls, type);
1069 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1070 isl_int_eq(aff->v->el[0], v->d)) {
1071 isl_val_free(v);
1072 return aff;
1075 aff = isl_aff_cow(aff);
1076 if (!aff)
1077 goto error;
1078 aff->v = isl_vec_cow(aff->v);
1079 if (!aff->v)
1080 goto error;
1082 if (isl_int_eq(aff->v->el[0], v->d)) {
1083 isl_int_set(aff->v->el[1 + pos], v->n);
1084 } else if (isl_int_is_one(v->d)) {
1085 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1086 } else {
1087 isl_seq_scale(aff->v->el + 1,
1088 aff->v->el + 1, v->d, aff->v->size - 1);
1089 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1090 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1091 aff->v = isl_vec_normalize(aff->v);
1092 if (!aff->v)
1093 goto error;
1096 isl_val_free(v);
1097 return aff;
1098 error:
1099 isl_aff_free(aff);
1100 isl_val_free(v);
1101 return NULL;
1104 /* Add "v" to the coefficient of the variable of type "type"
1105 * at position "pos" of "aff".
1107 * A NaN is unaffected by this operation.
1109 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1110 enum isl_dim_type type, int pos, isl_int v)
1112 if (!aff)
1113 return NULL;
1115 if (type == isl_dim_out)
1116 isl_die(aff->v->ctx, isl_error_invalid,
1117 "output/set dimension does not have a coefficient",
1118 return isl_aff_free(aff));
1119 if (type == isl_dim_in)
1120 type = isl_dim_set;
1122 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1123 return isl_aff_free(aff);
1125 if (isl_aff_is_nan(aff))
1126 return aff;
1127 aff = isl_aff_cow(aff);
1128 if (!aff)
1129 return NULL;
1131 aff->v = isl_vec_cow(aff->v);
1132 if (!aff->v)
1133 return isl_aff_free(aff);
1135 pos += isl_local_space_offset(aff->ls, type);
1136 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1138 return aff;
1141 /* Add "v" to the coefficient of the variable of type "type"
1142 * at position "pos" of "aff".
1144 * A NaN is unaffected by this operation.
1146 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1147 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1149 if (!aff || !v)
1150 goto error;
1152 if (isl_val_is_zero(v)) {
1153 isl_val_free(v);
1154 return aff;
1157 if (type == isl_dim_out)
1158 isl_die(aff->v->ctx, isl_error_invalid,
1159 "output/set dimension does not have a coefficient",
1160 goto error);
1161 if (type == isl_dim_in)
1162 type = isl_dim_set;
1164 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1165 goto error;
1167 if (isl_aff_is_nan(aff)) {
1168 isl_val_free(v);
1169 return aff;
1171 if (!isl_val_is_rat(v))
1172 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1173 "expecting rational value", goto error);
1175 aff = isl_aff_cow(aff);
1176 if (!aff)
1177 goto error;
1179 aff->v = isl_vec_cow(aff->v);
1180 if (!aff->v)
1181 goto error;
1183 pos += isl_local_space_offset(aff->ls, type);
1184 if (isl_int_is_one(v->d)) {
1185 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1186 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1187 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1188 aff->v = isl_vec_normalize(aff->v);
1189 if (!aff->v)
1190 goto error;
1191 } else {
1192 isl_seq_scale(aff->v->el + 1,
1193 aff->v->el + 1, v->d, aff->v->size - 1);
1194 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1195 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1196 aff->v = isl_vec_normalize(aff->v);
1197 if (!aff->v)
1198 goto error;
1201 isl_val_free(v);
1202 return aff;
1203 error:
1204 isl_aff_free(aff);
1205 isl_val_free(v);
1206 return NULL;
1209 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1210 enum isl_dim_type type, int pos, int v)
1212 isl_int t;
1214 isl_int_init(t);
1215 isl_int_set_si(t, v);
1216 aff = isl_aff_add_coefficient(aff, type, pos, t);
1217 isl_int_clear(t);
1219 return aff;
1222 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1224 if (!aff)
1225 return NULL;
1227 return isl_local_space_get_div(aff->ls, pos);
1230 /* Return the negation of "aff".
1232 * As a special case, -NaN = NaN.
1234 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1236 if (!aff)
1237 return NULL;
1238 if (isl_aff_is_nan(aff))
1239 return aff;
1240 aff = isl_aff_cow(aff);
1241 if (!aff)
1242 return NULL;
1243 aff->v = isl_vec_cow(aff->v);
1244 if (!aff->v)
1245 return isl_aff_free(aff);
1247 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1249 return aff;
1252 /* Remove divs from the local space that do not appear in the affine
1253 * expression.
1254 * We currently only remove divs at the end.
1255 * Some intermediate divs may also not appear directly in the affine
1256 * expression, but we would also need to check that no other divs are
1257 * defined in terms of them.
1259 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1261 int pos;
1262 int off;
1263 int n;
1265 if (!aff)
1266 return NULL;
1268 n = isl_local_space_dim(aff->ls, isl_dim_div);
1269 off = isl_local_space_offset(aff->ls, isl_dim_div);
1271 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1272 if (pos == n)
1273 return aff;
1275 aff = isl_aff_cow(aff);
1276 if (!aff)
1277 return NULL;
1279 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1280 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1281 if (!aff->ls || !aff->v)
1282 return isl_aff_free(aff);
1284 return aff;
1287 /* Look for any divs in the aff->ls with a denominator equal to one
1288 * and plug them into the affine expression and any subsequent divs
1289 * that may reference the div.
1291 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1293 int i, n;
1294 int len;
1295 isl_int v;
1296 isl_vec *vec;
1297 isl_local_space *ls;
1298 unsigned pos;
1300 if (!aff)
1301 return NULL;
1303 n = isl_local_space_dim(aff->ls, isl_dim_div);
1304 len = aff->v->size;
1305 for (i = 0; i < n; ++i) {
1306 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1307 continue;
1308 ls = isl_local_space_copy(aff->ls);
1309 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1310 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1311 vec = isl_vec_copy(aff->v);
1312 vec = isl_vec_cow(vec);
1313 if (!ls || !vec)
1314 goto error;
1316 isl_int_init(v);
1318 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1319 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1320 len, len, v);
1322 isl_int_clear(v);
1324 isl_vec_free(aff->v);
1325 aff->v = vec;
1326 isl_local_space_free(aff->ls);
1327 aff->ls = ls;
1330 return aff;
1331 error:
1332 isl_vec_free(vec);
1333 isl_local_space_free(ls);
1334 return isl_aff_free(aff);
1337 /* Look for any divs j that appear with a unit coefficient inside
1338 * the definitions of other divs i and plug them into the definitions
1339 * of the divs i.
1341 * In particular, an expression of the form
1343 * floor((f(..) + floor(g(..)/n))/m)
1345 * is simplified to
1347 * floor((n * f(..) + g(..))/(n * m))
1349 * This simplification is correct because we can move the expression
1350 * f(..) into the inner floor in the original expression to obtain
1352 * floor(floor((n * f(..) + g(..))/n)/m)
1354 * from which we can derive the simplified expression.
1356 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1358 int i, j, n;
1359 int off;
1361 if (!aff)
1362 return NULL;
1364 n = isl_local_space_dim(aff->ls, isl_dim_div);
1365 off = isl_local_space_offset(aff->ls, isl_dim_div);
1366 for (i = 1; i < n; ++i) {
1367 for (j = 0; j < i; ++j) {
1368 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1369 continue;
1370 aff->ls = isl_local_space_substitute_seq(aff->ls,
1371 isl_dim_div, j, aff->ls->div->row[j],
1372 aff->v->size, i, 1);
1373 if (!aff->ls)
1374 return isl_aff_free(aff);
1378 return aff;
1381 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1383 * Even though this function is only called on isl_affs with a single
1384 * reference, we are careful to only change aff->v and aff->ls together.
1386 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1388 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1389 isl_local_space *ls;
1390 isl_vec *v;
1392 ls = isl_local_space_copy(aff->ls);
1393 ls = isl_local_space_swap_div(ls, a, b);
1394 v = isl_vec_copy(aff->v);
1395 v = isl_vec_cow(v);
1396 if (!ls || !v)
1397 goto error;
1399 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1400 isl_vec_free(aff->v);
1401 aff->v = v;
1402 isl_local_space_free(aff->ls);
1403 aff->ls = ls;
1405 return aff;
1406 error:
1407 isl_vec_free(v);
1408 isl_local_space_free(ls);
1409 return isl_aff_free(aff);
1412 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1414 * We currently do not actually remove div "b", but simply add its
1415 * coefficient to that of "a" and then zero it out.
1417 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1419 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1421 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1422 return aff;
1424 aff->v = isl_vec_cow(aff->v);
1425 if (!aff->v)
1426 return isl_aff_free(aff);
1428 isl_int_add(aff->v->el[1 + off + a],
1429 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1430 isl_int_set_si(aff->v->el[1 + off + b], 0);
1432 return aff;
1435 /* Sort the divs in the local space of "aff" according to
1436 * the comparison function "cmp_row" in isl_local_space.c,
1437 * combining the coefficients of identical divs.
1439 * Reordering divs does not change the semantics of "aff",
1440 * so there is no need to call isl_aff_cow.
1441 * Moreover, this function is currently only called on isl_affs
1442 * with a single reference.
1444 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1446 int i, j, n;
1448 if (!aff)
1449 return NULL;
1451 n = isl_aff_dim(aff, isl_dim_div);
1452 for (i = 1; i < n; ++i) {
1453 for (j = i - 1; j >= 0; --j) {
1454 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1455 if (cmp < 0)
1456 break;
1457 if (cmp == 0)
1458 aff = merge_divs(aff, j, j + 1);
1459 else
1460 aff = swap_div(aff, j, j + 1);
1461 if (!aff)
1462 return NULL;
1466 return aff;
1469 /* Normalize the representation of "aff".
1471 * This function should only be called of "new" isl_affs, i.e.,
1472 * with only a single reference. We therefore do not need to
1473 * worry about affecting other instances.
1475 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1477 if (!aff)
1478 return NULL;
1479 aff->v = isl_vec_normalize(aff->v);
1480 if (!aff->v)
1481 return isl_aff_free(aff);
1482 aff = plug_in_integral_divs(aff);
1483 aff = plug_in_unit_divs(aff);
1484 aff = sort_divs(aff);
1485 aff = isl_aff_remove_unused_divs(aff);
1486 return aff;
1489 /* Given f, return floor(f).
1490 * If f is an integer expression, then just return f.
1491 * If f is a constant, then return the constant floor(f).
1492 * Otherwise, if f = g/m, write g = q m + r,
1493 * create a new div d = [r/m] and return the expression q + d.
1494 * The coefficients in r are taken to lie between -m/2 and m/2.
1496 * reduce_div_coefficients performs the same normalization.
1498 * As a special case, floor(NaN) = NaN.
1500 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1502 int i;
1503 int size;
1504 isl_ctx *ctx;
1505 isl_vec *div;
1507 if (!aff)
1508 return NULL;
1510 if (isl_aff_is_nan(aff))
1511 return aff;
1512 if (isl_int_is_one(aff->v->el[0]))
1513 return aff;
1515 aff = isl_aff_cow(aff);
1516 if (!aff)
1517 return NULL;
1519 aff->v = isl_vec_cow(aff->v);
1520 if (!aff->v)
1521 return isl_aff_free(aff);
1523 if (isl_aff_is_cst(aff)) {
1524 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1525 isl_int_set_si(aff->v->el[0], 1);
1526 return aff;
1529 div = isl_vec_copy(aff->v);
1530 div = isl_vec_cow(div);
1531 if (!div)
1532 return isl_aff_free(aff);
1534 ctx = isl_aff_get_ctx(aff);
1535 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1536 for (i = 1; i < aff->v->size; ++i) {
1537 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1538 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1539 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1540 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1541 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1545 aff->ls = isl_local_space_add_div(aff->ls, div);
1546 if (!aff->ls)
1547 return isl_aff_free(aff);
1549 size = aff->v->size;
1550 aff->v = isl_vec_extend(aff->v, size + 1);
1551 if (!aff->v)
1552 return isl_aff_free(aff);
1553 isl_int_set_si(aff->v->el[0], 1);
1554 isl_int_set_si(aff->v->el[size], 1);
1556 aff = isl_aff_normalize(aff);
1558 return aff;
1561 /* Compute
1563 * aff mod m = aff - m * floor(aff/m)
1565 * with m an integer value.
1567 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1568 __isl_take isl_val *m)
1570 isl_aff *res;
1572 if (!aff || !m)
1573 goto error;
1575 if (!isl_val_is_int(m))
1576 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1577 "expecting integer modulo", goto error);
1579 res = isl_aff_copy(aff);
1580 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1581 aff = isl_aff_floor(aff);
1582 aff = isl_aff_scale_val(aff, m);
1583 res = isl_aff_sub(res, aff);
1585 return res;
1586 error:
1587 isl_aff_free(aff);
1588 isl_val_free(m);
1589 return NULL;
1592 /* Compute
1594 * pwaff mod m = pwaff - m * floor(pwaff/m)
1596 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1598 isl_pw_aff *res;
1600 res = isl_pw_aff_copy(pwaff);
1601 pwaff = isl_pw_aff_scale_down(pwaff, m);
1602 pwaff = isl_pw_aff_floor(pwaff);
1603 pwaff = isl_pw_aff_scale(pwaff, m);
1604 res = isl_pw_aff_sub(res, pwaff);
1606 return res;
1609 /* Compute
1611 * pa mod m = pa - m * floor(pa/m)
1613 * with m an integer value.
1615 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1616 __isl_take isl_val *m)
1618 if (!pa || !m)
1619 goto error;
1620 if (!isl_val_is_int(m))
1621 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1622 "expecting integer modulo", goto error);
1623 pa = isl_pw_aff_mod(pa, m->n);
1624 isl_val_free(m);
1625 return pa;
1626 error:
1627 isl_pw_aff_free(pa);
1628 isl_val_free(m);
1629 return NULL;
1632 /* Given f, return ceil(f).
1633 * If f is an integer expression, then just return f.
1634 * Otherwise, let f be the expression
1636 * e/m
1638 * then return
1640 * floor((e + m - 1)/m)
1642 * As a special case, ceil(NaN) = NaN.
1644 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1646 if (!aff)
1647 return NULL;
1649 if (isl_aff_is_nan(aff))
1650 return aff;
1651 if (isl_int_is_one(aff->v->el[0]))
1652 return aff;
1654 aff = isl_aff_cow(aff);
1655 if (!aff)
1656 return NULL;
1657 aff->v = isl_vec_cow(aff->v);
1658 if (!aff->v)
1659 return isl_aff_free(aff);
1661 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1662 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1663 aff = isl_aff_floor(aff);
1665 return aff;
1668 /* Apply the expansion computed by isl_merge_divs.
1669 * The expansion itself is given by "exp" while the resulting
1670 * list of divs is given by "div".
1672 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1673 __isl_take isl_mat *div, int *exp)
1675 int old_n_div;
1676 int new_n_div;
1677 int offset;
1679 aff = isl_aff_cow(aff);
1680 if (!aff || !div)
1681 goto error;
1683 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1684 new_n_div = isl_mat_rows(div);
1685 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1687 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1688 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1689 if (!aff->v || !aff->ls)
1690 return isl_aff_free(aff);
1691 return aff;
1692 error:
1693 isl_aff_free(aff);
1694 isl_mat_free(div);
1695 return NULL;
1698 /* Add two affine expressions that live in the same local space.
1700 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1701 __isl_take isl_aff *aff2)
1703 isl_int gcd, f;
1705 aff1 = isl_aff_cow(aff1);
1706 if (!aff1 || !aff2)
1707 goto error;
1709 aff1->v = isl_vec_cow(aff1->v);
1710 if (!aff1->v)
1711 goto error;
1713 isl_int_init(gcd);
1714 isl_int_init(f);
1715 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1716 isl_int_divexact(f, aff2->v->el[0], gcd);
1717 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1718 isl_int_divexact(f, aff1->v->el[0], gcd);
1719 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1720 isl_int_divexact(f, aff2->v->el[0], gcd);
1721 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1722 isl_int_clear(f);
1723 isl_int_clear(gcd);
1725 isl_aff_free(aff2);
1726 return aff1;
1727 error:
1728 isl_aff_free(aff1);
1729 isl_aff_free(aff2);
1730 return NULL;
1733 /* Return the sum of "aff1" and "aff2".
1735 * If either of the two is NaN, then the result is NaN.
1737 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1738 __isl_take isl_aff *aff2)
1740 isl_ctx *ctx;
1741 int *exp1 = NULL;
1742 int *exp2 = NULL;
1743 isl_mat *div;
1744 int n_div1, n_div2;
1746 if (!aff1 || !aff2)
1747 goto error;
1749 ctx = isl_aff_get_ctx(aff1);
1750 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1751 isl_die(ctx, isl_error_invalid,
1752 "spaces don't match", goto error);
1754 if (isl_aff_is_nan(aff1)) {
1755 isl_aff_free(aff2);
1756 return aff1;
1758 if (isl_aff_is_nan(aff2)) {
1759 isl_aff_free(aff1);
1760 return aff2;
1763 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1764 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1765 if (n_div1 == 0 && n_div2 == 0)
1766 return add_expanded(aff1, aff2);
1768 exp1 = isl_alloc_array(ctx, int, n_div1);
1769 exp2 = isl_alloc_array(ctx, int, n_div2);
1770 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1771 goto error;
1773 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1774 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1775 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1776 free(exp1);
1777 free(exp2);
1779 return add_expanded(aff1, aff2);
1780 error:
1781 free(exp1);
1782 free(exp2);
1783 isl_aff_free(aff1);
1784 isl_aff_free(aff2);
1785 return NULL;
1788 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1789 __isl_take isl_aff *aff2)
1791 return isl_aff_add(aff1, isl_aff_neg(aff2));
1794 /* Return the result of scaling "aff" by a factor of "f".
1796 * As a special case, f * NaN = NaN.
1798 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1800 isl_int gcd;
1802 if (!aff)
1803 return NULL;
1804 if (isl_aff_is_nan(aff))
1805 return aff;
1807 if (isl_int_is_one(f))
1808 return aff;
1810 aff = isl_aff_cow(aff);
1811 if (!aff)
1812 return NULL;
1813 aff->v = isl_vec_cow(aff->v);
1814 if (!aff->v)
1815 return isl_aff_free(aff);
1817 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1818 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1819 return aff;
1822 isl_int_init(gcd);
1823 isl_int_gcd(gcd, aff->v->el[0], f);
1824 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1825 isl_int_divexact(gcd, f, gcd);
1826 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1827 isl_int_clear(gcd);
1829 return aff;
1832 /* Multiple "aff" by "v".
1834 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1835 __isl_take isl_val *v)
1837 if (!aff || !v)
1838 goto error;
1840 if (isl_val_is_one(v)) {
1841 isl_val_free(v);
1842 return aff;
1845 if (!isl_val_is_rat(v))
1846 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1847 "expecting rational factor", goto error);
1849 aff = isl_aff_scale(aff, v->n);
1850 aff = isl_aff_scale_down(aff, v->d);
1852 isl_val_free(v);
1853 return aff;
1854 error:
1855 isl_aff_free(aff);
1856 isl_val_free(v);
1857 return NULL;
1860 /* Return the result of scaling "aff" down by a factor of "f".
1862 * As a special case, NaN/f = NaN.
1864 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1866 isl_int gcd;
1868 if (!aff)
1869 return NULL;
1870 if (isl_aff_is_nan(aff))
1871 return aff;
1873 if (isl_int_is_one(f))
1874 return aff;
1876 aff = isl_aff_cow(aff);
1877 if (!aff)
1878 return NULL;
1880 if (isl_int_is_zero(f))
1881 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1882 "cannot scale down by zero", return isl_aff_free(aff));
1884 aff->v = isl_vec_cow(aff->v);
1885 if (!aff->v)
1886 return isl_aff_free(aff);
1888 isl_int_init(gcd);
1889 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1890 isl_int_gcd(gcd, gcd, f);
1891 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1892 isl_int_divexact(gcd, f, gcd);
1893 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1894 isl_int_clear(gcd);
1896 return aff;
1899 /* Divide "aff" by "v".
1901 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1902 __isl_take isl_val *v)
1904 if (!aff || !v)
1905 goto error;
1907 if (isl_val_is_one(v)) {
1908 isl_val_free(v);
1909 return aff;
1912 if (!isl_val_is_rat(v))
1913 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1914 "expecting rational factor", goto error);
1915 if (!isl_val_is_pos(v))
1916 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1917 "factor needs to be positive", goto error);
1919 aff = isl_aff_scale(aff, v->d);
1920 aff = isl_aff_scale_down(aff, v->n);
1922 isl_val_free(v);
1923 return aff;
1924 error:
1925 isl_aff_free(aff);
1926 isl_val_free(v);
1927 return NULL;
1930 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1932 isl_int v;
1934 if (f == 1)
1935 return aff;
1937 isl_int_init(v);
1938 isl_int_set_ui(v, f);
1939 aff = isl_aff_scale_down(aff, v);
1940 isl_int_clear(v);
1942 return aff;
1945 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1946 enum isl_dim_type type, unsigned pos, const char *s)
1948 aff = isl_aff_cow(aff);
1949 if (!aff)
1950 return NULL;
1951 if (type == isl_dim_out)
1952 isl_die(aff->v->ctx, isl_error_invalid,
1953 "cannot set name of output/set dimension",
1954 return isl_aff_free(aff));
1955 if (type == isl_dim_in)
1956 type = isl_dim_set;
1957 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1958 if (!aff->ls)
1959 return isl_aff_free(aff);
1961 return aff;
1964 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1965 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1967 aff = isl_aff_cow(aff);
1968 if (!aff)
1969 goto error;
1970 if (type == isl_dim_out)
1971 isl_die(aff->v->ctx, isl_error_invalid,
1972 "cannot set name of output/set dimension",
1973 goto error);
1974 if (type == isl_dim_in)
1975 type = isl_dim_set;
1976 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1977 if (!aff->ls)
1978 return isl_aff_free(aff);
1980 return aff;
1981 error:
1982 isl_id_free(id);
1983 isl_aff_free(aff);
1984 return NULL;
1987 /* Replace the identifier of the input tuple of "aff" by "id".
1988 * type is currently required to be equal to isl_dim_in
1990 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1991 enum isl_dim_type type, __isl_take isl_id *id)
1993 aff = isl_aff_cow(aff);
1994 if (!aff)
1995 goto error;
1996 if (type != isl_dim_in)
1997 isl_die(aff->v->ctx, isl_error_invalid,
1998 "cannot only set id of input tuple", goto error);
1999 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2000 if (!aff->ls)
2001 return isl_aff_free(aff);
2003 return aff;
2004 error:
2005 isl_id_free(id);
2006 isl_aff_free(aff);
2007 return NULL;
2010 /* Exploit the equalities in "eq" to simplify the affine expression
2011 * and the expressions of the integer divisions in the local space.
2012 * The integer divisions in this local space are assumed to appear
2013 * as regular dimensions in "eq".
2015 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2016 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2018 int i, j;
2019 unsigned o_div;
2020 unsigned n_div;
2022 if (!eq)
2023 goto error;
2024 if (eq->n_eq == 0) {
2025 isl_basic_set_free(eq);
2026 return aff;
2029 aff = isl_aff_cow(aff);
2030 if (!aff)
2031 goto error;
2033 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2034 isl_basic_set_copy(eq));
2035 aff->v = isl_vec_cow(aff->v);
2036 if (!aff->ls || !aff->v)
2037 goto error;
2039 o_div = isl_basic_set_offset(eq, isl_dim_div);
2040 n_div = eq->n_div;
2041 for (i = 0; i < eq->n_eq; ++i) {
2042 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2043 if (j < 0 || j == 0 || j >= o_div)
2044 continue;
2046 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2047 &aff->v->el[0]);
2050 isl_basic_set_free(eq);
2051 aff = isl_aff_normalize(aff);
2052 return aff;
2053 error:
2054 isl_basic_set_free(eq);
2055 isl_aff_free(aff);
2056 return NULL;
2059 /* Exploit the equalities in "eq" to simplify the affine expression
2060 * and the expressions of the integer divisions in the local space.
2062 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2063 __isl_take isl_basic_set *eq)
2065 int n_div;
2067 if (!aff || !eq)
2068 goto error;
2069 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2070 if (n_div > 0)
2071 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2072 return isl_aff_substitute_equalities_lifted(aff, eq);
2073 error:
2074 isl_basic_set_free(eq);
2075 isl_aff_free(aff);
2076 return NULL;
2079 /* Look for equalities among the variables shared by context and aff
2080 * and the integer divisions of aff, if any.
2081 * The equalities are then used to eliminate coefficients and/or integer
2082 * divisions from aff.
2084 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2085 __isl_take isl_set *context)
2087 isl_local_space *ls;
2088 isl_basic_set *hull;
2090 ls = isl_aff_get_domain_local_space(aff);
2091 context = isl_local_space_lift_set(ls, context);
2093 hull = isl_set_affine_hull(context);
2094 return isl_aff_substitute_equalities_lifted(aff, hull);
2097 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2098 __isl_take isl_set *context)
2100 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2101 dom_context = isl_set_intersect_params(dom_context, context);
2102 return isl_aff_gist(aff, dom_context);
2105 /* Return a basic set containing those elements in the space
2106 * of aff where it is positive. "rational" should not be set.
2108 * If "aff" is NaN, then it is not positive.
2110 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2111 int rational)
2113 isl_constraint *ineq;
2114 isl_basic_set *bset;
2115 isl_val *c;
2117 if (!aff)
2118 return NULL;
2119 if (isl_aff_is_nan(aff)) {
2120 isl_space *space = isl_aff_get_domain_space(aff);
2121 isl_aff_free(aff);
2122 return isl_basic_set_empty(space);
2124 if (rational)
2125 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2126 "rational sets not supported", goto error);
2128 ineq = isl_inequality_from_aff(aff);
2129 c = isl_constraint_get_constant_val(ineq);
2130 c = isl_val_sub_ui(c, 1);
2131 ineq = isl_constraint_set_constant_val(ineq, c);
2133 bset = isl_basic_set_from_constraint(ineq);
2134 bset = isl_basic_set_simplify(bset);
2135 return bset;
2136 error:
2137 isl_aff_free(aff);
2138 return NULL;
2141 /* Return a basic set containing those elements in the space
2142 * of aff where it is non-negative.
2143 * If "rational" is set, then return a rational basic set.
2145 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2147 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2148 __isl_take isl_aff *aff, int rational)
2150 isl_constraint *ineq;
2151 isl_basic_set *bset;
2153 if (!aff)
2154 return NULL;
2155 if (isl_aff_is_nan(aff)) {
2156 isl_space *space = isl_aff_get_domain_space(aff);
2157 isl_aff_free(aff);
2158 return isl_basic_set_empty(space);
2161 ineq = isl_inequality_from_aff(aff);
2163 bset = isl_basic_set_from_constraint(ineq);
2164 if (rational)
2165 bset = isl_basic_set_set_rational(bset);
2166 bset = isl_basic_set_simplify(bset);
2167 return bset;
2170 /* Return a basic set containing those elements in the space
2171 * of aff where it is non-negative.
2173 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2175 return aff_nonneg_basic_set(aff, 0);
2178 /* Return a basic set containing those elements in the domain space
2179 * of "aff" where it is positive.
2181 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2183 aff = isl_aff_add_constant_num_si(aff, -1);
2184 return isl_aff_nonneg_basic_set(aff);
2187 /* Return a basic set containing those elements in the domain space
2188 * of aff where it is negative.
2190 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2192 aff = isl_aff_neg(aff);
2193 return isl_aff_pos_basic_set(aff);
2196 /* Return a basic set containing those elements in the space
2197 * of aff where it is zero.
2198 * If "rational" is set, then return a rational basic set.
2200 * If "aff" is NaN, then it is not zero.
2202 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2203 int rational)
2205 isl_constraint *ineq;
2206 isl_basic_set *bset;
2208 if (!aff)
2209 return NULL;
2210 if (isl_aff_is_nan(aff)) {
2211 isl_space *space = isl_aff_get_domain_space(aff);
2212 isl_aff_free(aff);
2213 return isl_basic_set_empty(space);
2216 ineq = isl_equality_from_aff(aff);
2218 bset = isl_basic_set_from_constraint(ineq);
2219 if (rational)
2220 bset = isl_basic_set_set_rational(bset);
2221 bset = isl_basic_set_simplify(bset);
2222 return bset;
2225 /* Return a basic set containing those elements in the space
2226 * of aff where it is zero.
2228 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2230 return aff_zero_basic_set(aff, 0);
2233 /* Return a basic set containing those elements in the shared space
2234 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2236 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2237 __isl_take isl_aff *aff2)
2239 aff1 = isl_aff_sub(aff1, aff2);
2241 return isl_aff_nonneg_basic_set(aff1);
2244 /* Return a basic set containing those elements in the shared domain space
2245 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2247 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2248 __isl_take isl_aff *aff2)
2250 aff1 = isl_aff_sub(aff1, aff2);
2252 return isl_aff_pos_basic_set(aff1);
2255 /* Return a set containing those elements in the shared space
2256 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2258 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2259 __isl_take isl_aff *aff2)
2261 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2264 /* Return a set containing those elements in the shared domain space
2265 * of aff1 and aff2 where aff1 is greater than aff2.
2267 * If either of the two inputs is NaN, then the result is empty,
2268 * as comparisons with NaN always return false.
2270 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2271 __isl_take isl_aff *aff2)
2273 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2276 /* Return a basic set containing those elements in the shared space
2277 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2279 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2280 __isl_take isl_aff *aff2)
2282 return isl_aff_ge_basic_set(aff2, aff1);
2285 /* Return a basic set containing those elements in the shared domain space
2286 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2288 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2289 __isl_take isl_aff *aff2)
2291 return isl_aff_gt_basic_set(aff2, aff1);
2294 /* Return a set containing those elements in the shared space
2295 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2297 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2298 __isl_take isl_aff *aff2)
2300 return isl_aff_ge_set(aff2, aff1);
2303 /* Return a set containing those elements in the shared domain space
2304 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2306 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2307 __isl_take isl_aff *aff2)
2309 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2312 /* Return a basic set containing those elements in the shared space
2313 * of aff1 and aff2 where aff1 and aff2 are equal.
2315 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2316 __isl_take isl_aff *aff2)
2318 aff1 = isl_aff_sub(aff1, aff2);
2320 return isl_aff_zero_basic_set(aff1);
2323 /* Return a set containing those elements in the shared space
2324 * of aff1 and aff2 where aff1 and aff2 are equal.
2326 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2327 __isl_take isl_aff *aff2)
2329 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2332 /* Return a set containing those elements in the shared domain space
2333 * of aff1 and aff2 where aff1 and aff2 are not equal.
2335 * If either of the two inputs is NaN, then the result is empty,
2336 * as comparisons with NaN always return false.
2338 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2339 __isl_take isl_aff *aff2)
2341 isl_set *set_lt, *set_gt;
2343 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2344 isl_aff_copy(aff2));
2345 set_gt = isl_aff_gt_set(aff1, aff2);
2346 return isl_set_union_disjoint(set_lt, set_gt);
2349 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2350 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2352 aff1 = isl_aff_add(aff1, aff2);
2353 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2354 return aff1;
2357 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2359 if (!aff)
2360 return -1;
2362 return 0;
2365 #undef TYPE
2366 #define TYPE isl_aff
2367 static
2368 #include "check_type_range_templ.c"
2370 /* Check whether the given affine expression has non-zero coefficient
2371 * for any dimension in the given range or if any of these dimensions
2372 * appear with non-zero coefficients in any of the integer divisions
2373 * involved in the affine expression.
2375 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2376 enum isl_dim_type type, unsigned first, unsigned n)
2378 int i;
2379 int *active = NULL;
2380 isl_bool involves = isl_bool_false;
2382 if (!aff)
2383 return isl_bool_error;
2384 if (n == 0)
2385 return isl_bool_false;
2386 if (isl_aff_check_range(aff, type, first, n) < 0)
2387 return isl_bool_error;
2389 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2390 if (!active)
2391 goto error;
2393 first += isl_local_space_offset(aff->ls, type) - 1;
2394 for (i = 0; i < n; ++i)
2395 if (active[first + i]) {
2396 involves = isl_bool_true;
2397 break;
2400 free(active);
2402 return involves;
2403 error:
2404 free(active);
2405 return isl_bool_error;
2408 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2409 enum isl_dim_type type, unsigned first, unsigned n)
2411 isl_ctx *ctx;
2413 if (!aff)
2414 return NULL;
2415 if (type == isl_dim_out)
2416 isl_die(aff->v->ctx, isl_error_invalid,
2417 "cannot drop output/set dimension",
2418 return isl_aff_free(aff));
2419 if (type == isl_dim_in)
2420 type = isl_dim_set;
2421 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2422 return aff;
2424 ctx = isl_aff_get_ctx(aff);
2425 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2426 return isl_aff_free(aff);
2428 aff = isl_aff_cow(aff);
2429 if (!aff)
2430 return NULL;
2432 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2433 if (!aff->ls)
2434 return isl_aff_free(aff);
2436 first += 1 + isl_local_space_offset(aff->ls, type);
2437 aff->v = isl_vec_drop_els(aff->v, first, n);
2438 if (!aff->v)
2439 return isl_aff_free(aff);
2441 return aff;
2444 /* Drop the "n" domain dimensions starting at "first" from "aff",
2445 * after checking that they do not appear in the affine expression.
2447 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2448 unsigned n)
2450 isl_bool involves;
2452 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2453 if (involves < 0)
2454 return isl_aff_free(aff);
2455 if (involves)
2456 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2457 "affine expression involves some of the domain dimensions",
2458 return isl_aff_free(aff));
2459 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2462 /* Project the domain of the affine expression onto its parameter space.
2463 * The affine expression may not involve any of the domain dimensions.
2465 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2467 isl_space *space;
2468 unsigned n;
2470 n = isl_aff_dim(aff, isl_dim_in);
2471 aff = drop_domain(aff, 0, n);
2472 space = isl_aff_get_domain_space(aff);
2473 space = isl_space_params(space);
2474 aff = isl_aff_reset_domain_space(aff, space);
2475 return aff;
2478 /* Check that the domain of "aff" is a product.
2480 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2482 isl_bool is_product;
2484 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2485 if (is_product < 0)
2486 return isl_stat_error;
2487 if (!is_product)
2488 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2489 "domain is not a product", return isl_stat_error);
2490 return isl_stat_ok;
2493 /* Given an affine function with a domain of the form [A -> B] that
2494 * does not depend on B, return the same function on domain A.
2496 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2498 isl_space *space;
2499 int n, n_in;
2501 if (check_domain_product(aff) < 0)
2502 return isl_aff_free(aff);
2503 space = isl_aff_get_domain_space(aff);
2504 n = isl_space_dim(space, isl_dim_set);
2505 space = isl_space_factor_domain(space);
2506 n_in = isl_space_dim(space, isl_dim_set);
2507 aff = drop_domain(aff, n_in, n - n_in);
2508 aff = isl_aff_reset_domain_space(aff, space);
2509 return aff;
2512 /* Convert an affine expression defined over a parameter domain
2513 * into one that is defined over a zero-dimensional set.
2515 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2517 isl_local_space *ls;
2519 ls = isl_aff_take_domain_local_space(aff);
2520 ls = isl_local_space_set_from_params(ls);
2521 aff = isl_aff_restore_domain_local_space(aff, ls);
2523 return aff;
2526 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2527 enum isl_dim_type type, unsigned first, unsigned n)
2529 isl_ctx *ctx;
2531 if (!aff)
2532 return NULL;
2533 if (type == isl_dim_out)
2534 isl_die(aff->v->ctx, isl_error_invalid,
2535 "cannot insert output/set dimensions",
2536 return isl_aff_free(aff));
2537 if (type == isl_dim_in)
2538 type = isl_dim_set;
2539 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2540 return aff;
2542 ctx = isl_aff_get_ctx(aff);
2543 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2544 return isl_aff_free(aff);
2546 aff = isl_aff_cow(aff);
2547 if (!aff)
2548 return NULL;
2550 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2551 if (!aff->ls)
2552 return isl_aff_free(aff);
2554 first += 1 + isl_local_space_offset(aff->ls, type);
2555 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2556 if (!aff->v)
2557 return isl_aff_free(aff);
2559 return aff;
2562 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2563 enum isl_dim_type type, unsigned n)
2565 unsigned pos;
2567 pos = isl_aff_dim(aff, type);
2569 return isl_aff_insert_dims(aff, type, pos, n);
2572 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2573 enum isl_dim_type type, unsigned n)
2575 unsigned pos;
2577 pos = isl_pw_aff_dim(pwaff, type);
2579 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2582 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2583 * to dimensions of "dst_type" at "dst_pos".
2585 * We only support moving input dimensions to parameters and vice versa.
2587 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2588 enum isl_dim_type dst_type, unsigned dst_pos,
2589 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2591 unsigned g_dst_pos;
2592 unsigned g_src_pos;
2594 if (!aff)
2595 return NULL;
2596 if (n == 0 &&
2597 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2598 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2599 return aff;
2601 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2602 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2603 "cannot move output/set dimension",
2604 return isl_aff_free(aff));
2605 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2606 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2607 "cannot move divs", return isl_aff_free(aff));
2608 if (dst_type == isl_dim_in)
2609 dst_type = isl_dim_set;
2610 if (src_type == isl_dim_in)
2611 src_type = isl_dim_set;
2613 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2614 return isl_aff_free(aff);
2615 if (dst_type == src_type)
2616 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2617 "moving dims within the same type not supported",
2618 return isl_aff_free(aff));
2620 aff = isl_aff_cow(aff);
2621 if (!aff)
2622 return NULL;
2624 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2625 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2626 if (dst_type > src_type)
2627 g_dst_pos -= n;
2629 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2630 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2631 src_type, src_pos, n);
2632 if (!aff->v || !aff->ls)
2633 return isl_aff_free(aff);
2635 aff = sort_divs(aff);
2637 return aff;
2640 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2642 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2643 return isl_pw_aff_alloc(dom, aff);
2646 #define isl_aff_involves_nan isl_aff_is_nan
2648 #undef PW
2649 #define PW isl_pw_aff
2650 #undef EL
2651 #define EL isl_aff
2652 #undef EL_IS_ZERO
2653 #define EL_IS_ZERO is_empty
2654 #undef ZERO
2655 #define ZERO empty
2656 #undef IS_ZERO
2657 #define IS_ZERO is_empty
2658 #undef FIELD
2659 #define FIELD aff
2660 #undef DEFAULT_IS_ZERO
2661 #define DEFAULT_IS_ZERO 0
2663 #define NO_OPT
2664 #define NO_LIFT
2665 #define NO_MORPH
2667 #include <isl_pw_templ.c>
2668 #include <isl_pw_eval.c>
2669 #include <isl_pw_hash.c>
2670 #include <isl_pw_union_opt.c>
2672 #undef BASE
2673 #define BASE pw_aff
2675 #include <isl_union_single.c>
2676 #include <isl_union_neg.c>
2678 static __isl_give isl_set *align_params_pw_pw_set_and(
2679 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2680 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2681 __isl_take isl_pw_aff *pwaff2))
2683 isl_bool equal_params;
2685 if (!pwaff1 || !pwaff2)
2686 goto error;
2687 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2688 if (equal_params < 0)
2689 goto error;
2690 if (equal_params)
2691 return fn(pwaff1, pwaff2);
2692 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2693 isl_pw_aff_check_named_params(pwaff2) < 0)
2694 goto error;
2695 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2696 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2697 return fn(pwaff1, pwaff2);
2698 error:
2699 isl_pw_aff_free(pwaff1);
2700 isl_pw_aff_free(pwaff2);
2701 return NULL;
2704 /* Align the parameters of the to isl_pw_aff arguments and
2705 * then apply a function "fn" on them that returns an isl_map.
2707 static __isl_give isl_map *align_params_pw_pw_map_and(
2708 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2709 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2710 __isl_take isl_pw_aff *pa2))
2712 isl_bool equal_params;
2714 if (!pa1 || !pa2)
2715 goto error;
2716 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2717 if (equal_params < 0)
2718 goto error;
2719 if (equal_params)
2720 return fn(pa1, pa2);
2721 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2722 isl_pw_aff_check_named_params(pa2) < 0)
2723 goto error;
2724 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2725 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2726 return fn(pa1, pa2);
2727 error:
2728 isl_pw_aff_free(pa1);
2729 isl_pw_aff_free(pa2);
2730 return NULL;
2733 /* Compute a piecewise quasi-affine expression with a domain that
2734 * is the union of those of pwaff1 and pwaff2 and such that on each
2735 * cell, the quasi-affine expression is the maximum of those of pwaff1
2736 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2737 * cell, then the associated expression is the defined one.
2739 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2740 __isl_take isl_pw_aff *pwaff2)
2742 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2745 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2746 __isl_take isl_pw_aff *pwaff2)
2748 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2749 &pw_aff_union_max);
2752 /* Compute a piecewise quasi-affine expression with a domain that
2753 * is the union of those of pwaff1 and pwaff2 and such that on each
2754 * cell, the quasi-affine expression is the minimum of those of pwaff1
2755 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2756 * cell, then the associated expression is the defined one.
2758 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2759 __isl_take isl_pw_aff *pwaff2)
2761 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2764 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2765 __isl_take isl_pw_aff *pwaff2)
2767 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2768 &pw_aff_union_min);
2771 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2772 __isl_take isl_pw_aff *pwaff2, int max)
2774 if (max)
2775 return isl_pw_aff_union_max(pwaff1, pwaff2);
2776 else
2777 return isl_pw_aff_union_min(pwaff1, pwaff2);
2780 /* Return a set containing those elements in the domain
2781 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2782 * does not satisfy "fn" (if complement is 1).
2784 * The pieces with a NaN never belong to the result since
2785 * NaN does not satisfy any property.
2787 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2788 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2789 int complement)
2791 int i;
2792 isl_set *set;
2794 if (!pwaff)
2795 return NULL;
2797 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2799 for (i = 0; i < pwaff->n; ++i) {
2800 isl_basic_set *bset;
2801 isl_set *set_i, *locus;
2802 isl_bool rational;
2804 if (isl_aff_is_nan(pwaff->p[i].aff))
2805 continue;
2807 rational = isl_set_has_rational(pwaff->p[i].set);
2808 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2809 locus = isl_set_from_basic_set(bset);
2810 set_i = isl_set_copy(pwaff->p[i].set);
2811 if (complement)
2812 set_i = isl_set_subtract(set_i, locus);
2813 else
2814 set_i = isl_set_intersect(set_i, locus);
2815 set = isl_set_union_disjoint(set, set_i);
2818 isl_pw_aff_free(pwaff);
2820 return set;
2823 /* Return a set containing those elements in the domain
2824 * of "pa" where it is positive.
2826 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2828 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2831 /* Return a set containing those elements in the domain
2832 * of pwaff where it is non-negative.
2834 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2836 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2839 /* Return a set containing those elements in the domain
2840 * of pwaff where it is zero.
2842 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2844 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2847 /* Return a set containing those elements in the domain
2848 * of pwaff where it is not zero.
2850 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2852 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2855 /* Return a set containing those elements in the shared domain
2856 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2858 * We compute the difference on the shared domain and then construct
2859 * the set of values where this difference is non-negative.
2860 * If strict is set, we first subtract 1 from the difference.
2861 * If equal is set, we only return the elements where pwaff1 and pwaff2
2862 * are equal.
2864 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2865 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2867 isl_set *set1, *set2;
2869 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2870 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2871 set1 = isl_set_intersect(set1, set2);
2872 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2873 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2874 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2876 if (strict) {
2877 isl_space *space = isl_set_get_space(set1);
2878 isl_aff *aff;
2879 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2880 aff = isl_aff_add_constant_si(aff, -1);
2881 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2882 } else
2883 isl_set_free(set1);
2885 if (equal)
2886 return isl_pw_aff_zero_set(pwaff1);
2887 return isl_pw_aff_nonneg_set(pwaff1);
2890 /* Return a set containing those elements in the shared domain
2891 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2893 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2894 __isl_take isl_pw_aff *pwaff2)
2896 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2899 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2900 __isl_take isl_pw_aff *pwaff2)
2902 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2905 /* Return a set containing those elements in the shared domain
2906 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2908 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2909 __isl_take isl_pw_aff *pwaff2)
2911 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2914 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2915 __isl_take isl_pw_aff *pwaff2)
2917 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2920 /* Return a set containing those elements in the shared domain
2921 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2923 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2924 __isl_take isl_pw_aff *pwaff2)
2926 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2929 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2930 __isl_take isl_pw_aff *pwaff2)
2932 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2935 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2936 __isl_take isl_pw_aff *pwaff2)
2938 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2941 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2942 __isl_take isl_pw_aff *pwaff2)
2944 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2947 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2948 * where the function values are ordered in the same way as "order",
2949 * which returns a set in the shared domain of its two arguments.
2950 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2952 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2953 * We first pull back the two functions such that they are defined on
2954 * the domain [A -> B]. Then we apply "order", resulting in a set
2955 * in the space [A -> B]. Finally, we unwrap this set to obtain
2956 * a map in the space A -> B.
2958 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2959 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2960 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2961 __isl_take isl_pw_aff *pa2))
2963 isl_space *space1, *space2;
2964 isl_multi_aff *ma;
2965 isl_set *set;
2967 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2968 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2969 space1 = isl_space_map_from_domain_and_range(space1, space2);
2970 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2971 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2972 ma = isl_multi_aff_range_map(space1);
2973 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2974 set = order(pa1, pa2);
2976 return isl_set_unwrap(set);
2979 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2980 * where the function values are equal.
2981 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2983 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2984 __isl_take isl_pw_aff *pa2)
2986 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2989 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2990 * where the function values are equal.
2992 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
2993 __isl_take isl_pw_aff *pa2)
2995 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
2998 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2999 * where the function value of "pa1" is less than the function value of "pa2".
3000 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3002 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3003 __isl_take isl_pw_aff *pa2)
3005 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3008 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3009 * where the function value of "pa1" is less than the function value of "pa2".
3011 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3012 __isl_take isl_pw_aff *pa2)
3014 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3017 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3018 * where the function value of "pa1" is greater than the function value
3019 * of "pa2".
3020 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3022 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3023 __isl_take isl_pw_aff *pa2)
3025 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3028 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3029 * where the function value of "pa1" is greater than the function value
3030 * of "pa2".
3032 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3033 __isl_take isl_pw_aff *pa2)
3035 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3038 /* Return a set containing those elements in the shared domain
3039 * of the elements of list1 and list2 where each element in list1
3040 * has the relation specified by "fn" with each element in list2.
3042 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3043 __isl_take isl_pw_aff_list *list2,
3044 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3045 __isl_take isl_pw_aff *pwaff2))
3047 int i, j;
3048 isl_ctx *ctx;
3049 isl_set *set;
3051 if (!list1 || !list2)
3052 goto error;
3054 ctx = isl_pw_aff_list_get_ctx(list1);
3055 if (list1->n < 1 || list2->n < 1)
3056 isl_die(ctx, isl_error_invalid,
3057 "list should contain at least one element", goto error);
3059 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3060 for (i = 0; i < list1->n; ++i)
3061 for (j = 0; j < list2->n; ++j) {
3062 isl_set *set_ij;
3064 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3065 isl_pw_aff_copy(list2->p[j]));
3066 set = isl_set_intersect(set, set_ij);
3069 isl_pw_aff_list_free(list1);
3070 isl_pw_aff_list_free(list2);
3071 return set;
3072 error:
3073 isl_pw_aff_list_free(list1);
3074 isl_pw_aff_list_free(list2);
3075 return NULL;
3078 /* Return a set containing those elements in the shared domain
3079 * of the elements of list1 and list2 where each element in list1
3080 * is equal to each element in list2.
3082 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3083 __isl_take isl_pw_aff_list *list2)
3085 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3088 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3089 __isl_take isl_pw_aff_list *list2)
3091 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3094 /* Return a set containing those elements in the shared domain
3095 * of the elements of list1 and list2 where each element in list1
3096 * is less than or equal to each element in list2.
3098 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3099 __isl_take isl_pw_aff_list *list2)
3101 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3104 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3105 __isl_take isl_pw_aff_list *list2)
3107 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3110 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3111 __isl_take isl_pw_aff_list *list2)
3113 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3116 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3117 __isl_take isl_pw_aff_list *list2)
3119 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3123 /* Return a set containing those elements in the shared domain
3124 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3126 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3127 __isl_take isl_pw_aff *pwaff2)
3129 isl_set *set_lt, *set_gt;
3131 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3132 isl_pw_aff_copy(pwaff2));
3133 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3134 return isl_set_union_disjoint(set_lt, set_gt);
3137 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3138 __isl_take isl_pw_aff *pwaff2)
3140 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3143 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3144 isl_int v)
3146 int i;
3148 if (isl_int_is_one(v))
3149 return pwaff;
3150 if (!isl_int_is_pos(v))
3151 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3152 "factor needs to be positive",
3153 return isl_pw_aff_free(pwaff));
3154 pwaff = isl_pw_aff_cow(pwaff);
3155 if (!pwaff)
3156 return NULL;
3157 if (pwaff->n == 0)
3158 return pwaff;
3160 for (i = 0; i < pwaff->n; ++i) {
3161 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3162 if (!pwaff->p[i].aff)
3163 return isl_pw_aff_free(pwaff);
3166 return pwaff;
3169 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3171 int i;
3173 pwaff = isl_pw_aff_cow(pwaff);
3174 if (!pwaff)
3175 return NULL;
3176 if (pwaff->n == 0)
3177 return pwaff;
3179 for (i = 0; i < pwaff->n; ++i) {
3180 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3181 if (!pwaff->p[i].aff)
3182 return isl_pw_aff_free(pwaff);
3185 return pwaff;
3188 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3190 int i;
3192 pwaff = isl_pw_aff_cow(pwaff);
3193 if (!pwaff)
3194 return NULL;
3195 if (pwaff->n == 0)
3196 return pwaff;
3198 for (i = 0; i < pwaff->n; ++i) {
3199 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3200 if (!pwaff->p[i].aff)
3201 return isl_pw_aff_free(pwaff);
3204 return pwaff;
3207 /* Assuming that "cond1" and "cond2" are disjoint,
3208 * return an affine expression that is equal to pwaff1 on cond1
3209 * and to pwaff2 on cond2.
3211 static __isl_give isl_pw_aff *isl_pw_aff_select(
3212 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3213 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3215 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3216 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3218 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3221 /* Return an affine expression that is equal to pwaff_true for elements
3222 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3223 * is zero.
3224 * That is, return cond ? pwaff_true : pwaff_false;
3226 * If "cond" involves and NaN, then we conservatively return a NaN
3227 * on its entire domain. In principle, we could consider the pieces
3228 * where it is NaN separately from those where it is not.
3230 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3231 * then only use the domain of "cond" to restrict the domain.
3233 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3234 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3236 isl_set *cond_true, *cond_false;
3237 isl_bool equal;
3239 if (!cond)
3240 goto error;
3241 if (isl_pw_aff_involves_nan(cond)) {
3242 isl_space *space = isl_pw_aff_get_domain_space(cond);
3243 isl_local_space *ls = isl_local_space_from_space(space);
3244 isl_pw_aff_free(cond);
3245 isl_pw_aff_free(pwaff_true);
3246 isl_pw_aff_free(pwaff_false);
3247 return isl_pw_aff_nan_on_domain(ls);
3250 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3251 isl_pw_aff_get_space(pwaff_false));
3252 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3253 isl_pw_aff_get_space(pwaff_true));
3254 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3255 if (equal < 0)
3256 goto error;
3257 if (equal) {
3258 isl_set *dom;
3260 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3261 isl_pw_aff_free(pwaff_false);
3262 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3265 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3266 cond_false = isl_pw_aff_zero_set(cond);
3267 return isl_pw_aff_select(cond_true, pwaff_true,
3268 cond_false, pwaff_false);
3269 error:
3270 isl_pw_aff_free(cond);
3271 isl_pw_aff_free(pwaff_true);
3272 isl_pw_aff_free(pwaff_false);
3273 return NULL;
3276 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3278 if (!aff)
3279 return isl_bool_error;
3281 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3284 /* Check whether pwaff is a piecewise constant.
3286 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3288 int i;
3290 if (!pwaff)
3291 return isl_bool_error;
3293 for (i = 0; i < pwaff->n; ++i) {
3294 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3295 if (is_cst < 0 || !is_cst)
3296 return is_cst;
3299 return isl_bool_true;
3302 /* Are all elements of "mpa" piecewise constants?
3304 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3306 int i;
3308 if (!mpa)
3309 return isl_bool_error;
3311 for (i = 0; i < mpa->n; ++i) {
3312 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3313 if (is_cst < 0 || !is_cst)
3314 return is_cst;
3317 return isl_bool_true;
3320 /* Return the product of "aff1" and "aff2".
3322 * If either of the two is NaN, then the result is NaN.
3324 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3326 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3327 __isl_take isl_aff *aff2)
3329 if (!aff1 || !aff2)
3330 goto error;
3332 if (isl_aff_is_nan(aff1)) {
3333 isl_aff_free(aff2);
3334 return aff1;
3336 if (isl_aff_is_nan(aff2)) {
3337 isl_aff_free(aff1);
3338 return aff2;
3341 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3342 return isl_aff_mul(aff2, aff1);
3344 if (!isl_aff_is_cst(aff2))
3345 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3346 "at least one affine expression should be constant",
3347 goto error);
3349 aff1 = isl_aff_cow(aff1);
3350 if (!aff1 || !aff2)
3351 goto error;
3353 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3354 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3356 isl_aff_free(aff2);
3357 return aff1;
3358 error:
3359 isl_aff_free(aff1);
3360 isl_aff_free(aff2);
3361 return NULL;
3364 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3366 * If either of the two is NaN, then the result is NaN.
3368 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3369 __isl_take isl_aff *aff2)
3371 int is_cst;
3372 int neg;
3374 if (!aff1 || !aff2)
3375 goto error;
3377 if (isl_aff_is_nan(aff1)) {
3378 isl_aff_free(aff2);
3379 return aff1;
3381 if (isl_aff_is_nan(aff2)) {
3382 isl_aff_free(aff1);
3383 return aff2;
3386 is_cst = isl_aff_is_cst(aff2);
3387 if (is_cst < 0)
3388 goto error;
3389 if (!is_cst)
3390 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3391 "second argument should be a constant", goto error);
3393 if (!aff2)
3394 goto error;
3396 neg = isl_int_is_neg(aff2->v->el[1]);
3397 if (neg) {
3398 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3399 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3402 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3403 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3405 if (neg) {
3406 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3407 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3410 isl_aff_free(aff2);
3411 return aff1;
3412 error:
3413 isl_aff_free(aff1);
3414 isl_aff_free(aff2);
3415 return NULL;
3418 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3419 __isl_take isl_pw_aff *pwaff2)
3421 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3424 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3425 __isl_take isl_pw_aff *pwaff2)
3427 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3430 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3431 __isl_take isl_pw_aff *pwaff2)
3433 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3436 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3437 __isl_take isl_pw_aff *pwaff2)
3439 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3442 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3443 __isl_take isl_pw_aff *pwaff2)
3445 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3448 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3449 __isl_take isl_pw_aff *pa2)
3451 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3454 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3456 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3457 __isl_take isl_pw_aff *pa2)
3459 int is_cst;
3461 is_cst = isl_pw_aff_is_cst(pa2);
3462 if (is_cst < 0)
3463 goto error;
3464 if (!is_cst)
3465 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3466 "second argument should be a piecewise constant",
3467 goto error);
3468 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3469 error:
3470 isl_pw_aff_free(pa1);
3471 isl_pw_aff_free(pa2);
3472 return NULL;
3475 /* Compute the quotient of the integer division of "pa1" by "pa2"
3476 * with rounding towards zero.
3477 * "pa2" is assumed to be a piecewise constant.
3479 * In particular, return
3481 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3484 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3485 __isl_take isl_pw_aff *pa2)
3487 int is_cst;
3488 isl_set *cond;
3489 isl_pw_aff *f, *c;
3491 is_cst = isl_pw_aff_is_cst(pa2);
3492 if (is_cst < 0)
3493 goto error;
3494 if (!is_cst)
3495 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3496 "second argument should be a piecewise constant",
3497 goto error);
3499 pa1 = isl_pw_aff_div(pa1, pa2);
3501 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3502 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3503 c = isl_pw_aff_ceil(pa1);
3504 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3505 error:
3506 isl_pw_aff_free(pa1);
3507 isl_pw_aff_free(pa2);
3508 return NULL;
3511 /* Compute the remainder of the integer division of "pa1" by "pa2"
3512 * with rounding towards zero.
3513 * "pa2" is assumed to be a piecewise constant.
3515 * In particular, return
3517 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3520 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3521 __isl_take isl_pw_aff *pa2)
3523 int is_cst;
3524 isl_pw_aff *res;
3526 is_cst = isl_pw_aff_is_cst(pa2);
3527 if (is_cst < 0)
3528 goto error;
3529 if (!is_cst)
3530 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3531 "second argument should be a piecewise constant",
3532 goto error);
3533 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3534 res = isl_pw_aff_mul(pa2, res);
3535 res = isl_pw_aff_sub(pa1, res);
3536 return res;
3537 error:
3538 isl_pw_aff_free(pa1);
3539 isl_pw_aff_free(pa2);
3540 return NULL;
3543 /* Does either of "pa1" or "pa2" involve any NaN2?
3545 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3546 __isl_keep isl_pw_aff *pa2)
3548 isl_bool has_nan;
3550 has_nan = isl_pw_aff_involves_nan(pa1);
3551 if (has_nan < 0 || has_nan)
3552 return has_nan;
3553 return isl_pw_aff_involves_nan(pa2);
3556 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3557 * by a NaN on their shared domain.
3559 * In principle, the result could be refined to only being NaN
3560 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3562 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3563 __isl_take isl_pw_aff *pa2)
3565 isl_local_space *ls;
3566 isl_set *dom;
3567 isl_pw_aff *pa;
3569 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3570 ls = isl_local_space_from_space(isl_set_get_space(dom));
3571 pa = isl_pw_aff_nan_on_domain(ls);
3572 pa = isl_pw_aff_intersect_domain(pa, dom);
3574 return pa;
3577 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3578 __isl_take isl_pw_aff *pwaff2)
3580 isl_set *le;
3581 isl_set *dom;
3583 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3584 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3585 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3586 isl_pw_aff_copy(pwaff2));
3587 dom = isl_set_subtract(dom, isl_set_copy(le));
3588 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3591 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3592 __isl_take isl_pw_aff *pwaff2)
3594 isl_set *ge;
3595 isl_set *dom;
3597 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3598 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3599 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3600 isl_pw_aff_copy(pwaff2));
3601 dom = isl_set_subtract(dom, isl_set_copy(ge));
3602 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3605 /* Return an expression for the minimum (if "max" is not set) or
3606 * the maximum (if "max" is set) of "pa1" and "pa2".
3607 * If either expression involves any NaN, then return a NaN
3608 * on the shared domain as result.
3610 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3611 __isl_take isl_pw_aff *pa2, int max)
3613 isl_bool has_nan;
3615 has_nan = either_involves_nan(pa1, pa2);
3616 if (has_nan < 0)
3617 pa1 = isl_pw_aff_free(pa1);
3618 else if (has_nan)
3619 return replace_by_nan(pa1, pa2);
3621 if (max)
3622 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3623 else
3624 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3627 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3629 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3630 __isl_take isl_pw_aff *pwaff2)
3632 return pw_aff_min_max(pwaff1, pwaff2, 0);
3635 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3637 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3638 __isl_take isl_pw_aff *pwaff2)
3640 return pw_aff_min_max(pwaff1, pwaff2, 1);
3643 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3644 __isl_take isl_pw_aff_list *list,
3645 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3646 __isl_take isl_pw_aff *pwaff2))
3648 int i;
3649 isl_ctx *ctx;
3650 isl_pw_aff *res;
3652 if (!list)
3653 return NULL;
3655 ctx = isl_pw_aff_list_get_ctx(list);
3656 if (list->n < 1)
3657 isl_die(ctx, isl_error_invalid,
3658 "list should contain at least one element", goto error);
3660 res = isl_pw_aff_copy(list->p[0]);
3661 for (i = 1; i < list->n; ++i)
3662 res = fn(res, isl_pw_aff_copy(list->p[i]));
3664 isl_pw_aff_list_free(list);
3665 return res;
3666 error:
3667 isl_pw_aff_list_free(list);
3668 return NULL;
3671 /* Return an isl_pw_aff that maps each element in the intersection of the
3672 * domains of the elements of list to the minimal corresponding affine
3673 * expression.
3675 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3677 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3680 /* Return an isl_pw_aff that maps each element in the intersection of the
3681 * domains of the elements of list to the maximal corresponding affine
3682 * expression.
3684 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3686 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3689 /* Mark the domains of "pwaff" as rational.
3691 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3693 int i;
3695 pwaff = isl_pw_aff_cow(pwaff);
3696 if (!pwaff)
3697 return NULL;
3698 if (pwaff->n == 0)
3699 return pwaff;
3701 for (i = 0; i < pwaff->n; ++i) {
3702 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3703 if (!pwaff->p[i].set)
3704 return isl_pw_aff_free(pwaff);
3707 return pwaff;
3710 /* Mark the domains of the elements of "list" as rational.
3712 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3713 __isl_take isl_pw_aff_list *list)
3715 int i, n;
3717 if (!list)
3718 return NULL;
3719 if (list->n == 0)
3720 return list;
3722 n = list->n;
3723 for (i = 0; i < n; ++i) {
3724 isl_pw_aff *pa;
3726 pa = isl_pw_aff_list_get_pw_aff(list, i);
3727 pa = isl_pw_aff_set_rational(pa);
3728 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3731 return list;
3734 /* Do the parameters of "aff" match those of "space"?
3736 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3737 __isl_keep isl_space *space)
3739 isl_space *aff_space;
3740 isl_bool match;
3742 if (!aff || !space)
3743 return isl_bool_error;
3745 aff_space = isl_aff_get_domain_space(aff);
3747 match = isl_space_has_equal_params(space, aff_space);
3749 isl_space_free(aff_space);
3750 return match;
3753 /* Check that the domain space of "aff" matches "space".
3755 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3756 __isl_keep isl_space *space)
3758 isl_space *aff_space;
3759 isl_bool match;
3761 if (!aff || !space)
3762 return isl_stat_error;
3764 aff_space = isl_aff_get_domain_space(aff);
3766 match = isl_space_has_equal_params(space, aff_space);
3767 if (match < 0)
3768 goto error;
3769 if (!match)
3770 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3771 "parameters don't match", goto error);
3772 match = isl_space_tuple_is_equal(space, isl_dim_in,
3773 aff_space, isl_dim_set);
3774 if (match < 0)
3775 goto error;
3776 if (!match)
3777 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3778 "domains don't match", goto error);
3779 isl_space_free(aff_space);
3780 return isl_stat_ok;
3781 error:
3782 isl_space_free(aff_space);
3783 return isl_stat_error;
3786 #undef BASE
3787 #define BASE aff
3788 #undef DOMBASE
3789 #define DOMBASE set
3791 #include <isl_multi_no_explicit_domain.c>
3792 #include <isl_multi_templ.c>
3793 #include <isl_multi_apply_set.c>
3794 #include <isl_multi_cmp.c>
3795 #include <isl_multi_dims.c>
3796 #include <isl_multi_floor.c>
3797 #include <isl_multi_from_base_templ.c>
3798 #include <isl_multi_gist.c>
3799 #include <isl_multi_identity_templ.c>
3800 #include <isl_multi_move_dims_templ.c>
3801 #include <isl_multi_product_templ.c>
3802 #include <isl_multi_splice_templ.c>
3803 #include <isl_multi_zero_templ.c>
3805 /* Construct an isl_multi_aff living in "space" that corresponds
3806 * to the affine transformation matrix "mat".
3808 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3809 __isl_take isl_space *space, __isl_take isl_mat *mat)
3811 isl_ctx *ctx;
3812 isl_local_space *ls = NULL;
3813 isl_multi_aff *ma = NULL;
3814 int n_row, n_col, n_out, total;
3815 int i;
3817 if (!space || !mat)
3818 goto error;
3820 ctx = isl_mat_get_ctx(mat);
3822 n_row = isl_mat_rows(mat);
3823 n_col = isl_mat_cols(mat);
3824 if (n_row < 1)
3825 isl_die(ctx, isl_error_invalid,
3826 "insufficient number of rows", goto error);
3827 if (n_col < 1)
3828 isl_die(ctx, isl_error_invalid,
3829 "insufficient number of columns", goto error);
3830 n_out = isl_space_dim(space, isl_dim_out);
3831 total = isl_space_dim(space, isl_dim_all);
3832 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3833 isl_die(ctx, isl_error_invalid,
3834 "dimension mismatch", goto error);
3836 ma = isl_multi_aff_zero(isl_space_copy(space));
3837 ls = isl_local_space_from_space(isl_space_domain(space));
3839 for (i = 0; i < n_row - 1; ++i) {
3840 isl_vec *v;
3841 isl_aff *aff;
3843 v = isl_vec_alloc(ctx, 1 + n_col);
3844 if (!v)
3845 goto error;
3846 isl_int_set(v->el[0], mat->row[0][0]);
3847 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3848 v = isl_vec_normalize(v);
3849 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3850 ma = isl_multi_aff_set_aff(ma, i, aff);
3853 isl_local_space_free(ls);
3854 isl_mat_free(mat);
3855 return ma;
3856 error:
3857 isl_local_space_free(ls);
3858 isl_mat_free(mat);
3859 isl_multi_aff_free(ma);
3860 return NULL;
3863 /* Remove any internal structure of the domain of "ma".
3864 * If there is any such internal structure in the input,
3865 * then the name of the corresponding space is also removed.
3867 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3868 __isl_take isl_multi_aff *ma)
3870 isl_space *space;
3872 if (!ma)
3873 return NULL;
3875 if (!ma->space->nested[0])
3876 return ma;
3878 space = isl_multi_aff_get_space(ma);
3879 space = isl_space_flatten_domain(space);
3880 ma = isl_multi_aff_reset_space(ma, space);
3882 return ma;
3885 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3886 * of the space to its domain.
3888 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3890 int i, n_in;
3891 isl_local_space *ls;
3892 isl_multi_aff *ma;
3894 if (!space)
3895 return NULL;
3896 if (!isl_space_is_map(space))
3897 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3898 "not a map space", goto error);
3900 n_in = isl_space_dim(space, isl_dim_in);
3901 space = isl_space_domain_map(space);
3903 ma = isl_multi_aff_alloc(isl_space_copy(space));
3904 if (n_in == 0) {
3905 isl_space_free(space);
3906 return ma;
3909 space = isl_space_domain(space);
3910 ls = isl_local_space_from_space(space);
3911 for (i = 0; i < n_in; ++i) {
3912 isl_aff *aff;
3914 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3915 isl_dim_set, i);
3916 ma = isl_multi_aff_set_aff(ma, i, aff);
3918 isl_local_space_free(ls);
3919 return ma;
3920 error:
3921 isl_space_free(space);
3922 return NULL;
3925 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3926 * of the space to its range.
3928 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3930 int i, n_in, n_out;
3931 isl_local_space *ls;
3932 isl_multi_aff *ma;
3934 if (!space)
3935 return NULL;
3936 if (!isl_space_is_map(space))
3937 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3938 "not a map space", goto error);
3940 n_in = isl_space_dim(space, isl_dim_in);
3941 n_out = isl_space_dim(space, isl_dim_out);
3942 space = isl_space_range_map(space);
3944 ma = isl_multi_aff_alloc(isl_space_copy(space));
3945 if (n_out == 0) {
3946 isl_space_free(space);
3947 return ma;
3950 space = isl_space_domain(space);
3951 ls = isl_local_space_from_space(space);
3952 for (i = 0; i < n_out; ++i) {
3953 isl_aff *aff;
3955 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3956 isl_dim_set, n_in + i);
3957 ma = isl_multi_aff_set_aff(ma, i, aff);
3959 isl_local_space_free(ls);
3960 return ma;
3961 error:
3962 isl_space_free(space);
3963 return NULL;
3966 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3967 * of the space to its range.
3969 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3970 __isl_take isl_space *space)
3972 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3975 /* Given the space of a set and a range of set dimensions,
3976 * construct an isl_multi_aff that projects out those dimensions.
3978 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3979 __isl_take isl_space *space, enum isl_dim_type type,
3980 unsigned first, unsigned n)
3982 int i, dim;
3983 isl_local_space *ls;
3984 isl_multi_aff *ma;
3986 if (!space)
3987 return NULL;
3988 if (!isl_space_is_set(space))
3989 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3990 "expecting set space", goto error);
3991 if (type != isl_dim_set)
3992 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3993 "only set dimensions can be projected out", goto error);
3994 if (isl_space_check_range(space, type, first, n) < 0)
3995 goto error;
3997 dim = isl_space_dim(space, isl_dim_set);
3999 space = isl_space_from_domain(space);
4000 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4002 if (dim == n)
4003 return isl_multi_aff_alloc(space);
4005 ma = isl_multi_aff_alloc(isl_space_copy(space));
4006 space = isl_space_domain(space);
4007 ls = isl_local_space_from_space(space);
4009 for (i = 0; i < first; ++i) {
4010 isl_aff *aff;
4012 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4013 isl_dim_set, i);
4014 ma = isl_multi_aff_set_aff(ma, i, aff);
4017 for (i = 0; i < dim - (first + n); ++i) {
4018 isl_aff *aff;
4020 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4021 isl_dim_set, first + n + i);
4022 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4025 isl_local_space_free(ls);
4026 return ma;
4027 error:
4028 isl_space_free(space);
4029 return NULL;
4032 /* Given the space of a set and a range of set dimensions,
4033 * construct an isl_pw_multi_aff that projects out those dimensions.
4035 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4036 __isl_take isl_space *space, enum isl_dim_type type,
4037 unsigned first, unsigned n)
4039 isl_multi_aff *ma;
4041 ma = isl_multi_aff_project_out_map(space, type, first, n);
4042 return isl_pw_multi_aff_from_multi_aff(ma);
4045 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4046 * domain.
4048 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4049 __isl_take isl_multi_aff *ma)
4051 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4052 return isl_pw_multi_aff_alloc(dom, ma);
4055 /* Create a piecewise multi-affine expression in the given space that maps each
4056 * input dimension to the corresponding output dimension.
4058 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4059 __isl_take isl_space *space)
4061 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4064 /* Exploit the equalities in "eq" to simplify the affine expressions.
4066 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4067 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4069 int i;
4071 maff = isl_multi_aff_cow(maff);
4072 if (!maff || !eq)
4073 goto error;
4075 for (i = 0; i < maff->n; ++i) {
4076 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4077 isl_basic_set_copy(eq));
4078 if (!maff->u.p[i])
4079 goto error;
4082 isl_basic_set_free(eq);
4083 return maff;
4084 error:
4085 isl_basic_set_free(eq);
4086 isl_multi_aff_free(maff);
4087 return NULL;
4090 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4091 isl_int f)
4093 int i;
4095 maff = isl_multi_aff_cow(maff);
4096 if (!maff)
4097 return NULL;
4099 for (i = 0; i < maff->n; ++i) {
4100 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4101 if (!maff->u.p[i])
4102 return isl_multi_aff_free(maff);
4105 return maff;
4108 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4109 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4111 maff1 = isl_multi_aff_add(maff1, maff2);
4112 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4113 return maff1;
4116 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4118 if (!maff)
4119 return -1;
4121 return 0;
4124 /* Return the set of domain elements where "ma1" is lexicographically
4125 * smaller than or equal to "ma2".
4127 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4128 __isl_take isl_multi_aff *ma2)
4130 return isl_multi_aff_lex_ge_set(ma2, ma1);
4133 /* Return the set of domain elements where "ma1" is lexicographically
4134 * smaller than "ma2".
4136 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4137 __isl_take isl_multi_aff *ma2)
4139 return isl_multi_aff_lex_gt_set(ma2, ma1);
4142 /* Return the set of domain elements where "ma1" and "ma2"
4143 * satisfy "order".
4145 static __isl_give isl_set *isl_multi_aff_order_set(
4146 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4147 __isl_give isl_map *order(__isl_take isl_space *set_space))
4149 isl_space *space;
4150 isl_map *map1, *map2;
4151 isl_map *map, *ge;
4153 map1 = isl_map_from_multi_aff_internal(ma1);
4154 map2 = isl_map_from_multi_aff_internal(ma2);
4155 map = isl_map_range_product(map1, map2);
4156 space = isl_space_range(isl_map_get_space(map));
4157 space = isl_space_domain(isl_space_unwrap(space));
4158 ge = order(space);
4159 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4161 return isl_map_domain(map);
4164 /* Return the set of domain elements where "ma1" is lexicographically
4165 * greater than or equal to "ma2".
4167 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4168 __isl_take isl_multi_aff *ma2)
4170 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4173 /* Return the set of domain elements where "ma1" is lexicographically
4174 * greater than "ma2".
4176 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4177 __isl_take isl_multi_aff *ma2)
4179 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4182 #undef PW
4183 #define PW isl_pw_multi_aff
4184 #undef EL
4185 #define EL isl_multi_aff
4186 #undef EL_IS_ZERO
4187 #define EL_IS_ZERO is_empty
4188 #undef ZERO
4189 #define ZERO empty
4190 #undef IS_ZERO
4191 #define IS_ZERO is_empty
4192 #undef FIELD
4193 #define FIELD maff
4194 #undef DEFAULT_IS_ZERO
4195 #define DEFAULT_IS_ZERO 0
4197 #define NO_SUB
4198 #define NO_OPT
4199 #define NO_INSERT_DIMS
4200 #define NO_LIFT
4201 #define NO_MORPH
4203 #include <isl_pw_templ.c>
4204 #include <isl_pw_union_opt.c>
4206 #undef NO_SUB
4208 #undef BASE
4209 #define BASE pw_multi_aff
4211 #include <isl_union_multi.c>
4212 #include <isl_union_neg.c>
4214 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4215 __isl_take isl_pw_multi_aff *pma1,
4216 __isl_take isl_pw_multi_aff *pma2)
4218 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4219 &isl_multi_aff_lex_ge_set);
4222 /* Given two piecewise multi affine expressions, return a piecewise
4223 * multi-affine expression defined on the union of the definition domains
4224 * of the inputs that is equal to the lexicographic maximum of the two
4225 * inputs on each cell. If only one of the two inputs is defined on
4226 * a given cell, then it is considered to be the maximum.
4228 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4229 __isl_take isl_pw_multi_aff *pma1,
4230 __isl_take isl_pw_multi_aff *pma2)
4232 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4233 &pw_multi_aff_union_lexmax);
4236 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4237 __isl_take isl_pw_multi_aff *pma1,
4238 __isl_take isl_pw_multi_aff *pma2)
4240 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4241 &isl_multi_aff_lex_le_set);
4244 /* Given two piecewise multi affine expressions, return a piecewise
4245 * multi-affine expression defined on the union of the definition domains
4246 * of the inputs that is equal to the lexicographic minimum of the two
4247 * inputs on each cell. If only one of the two inputs is defined on
4248 * a given cell, then it is considered to be the minimum.
4250 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4251 __isl_take isl_pw_multi_aff *pma1,
4252 __isl_take isl_pw_multi_aff *pma2)
4254 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4255 &pw_multi_aff_union_lexmin);
4258 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4259 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4261 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4262 &isl_multi_aff_add);
4265 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4266 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4268 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4269 &pw_multi_aff_add);
4272 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4273 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4275 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4276 &isl_multi_aff_sub);
4279 /* Subtract "pma2" from "pma1" and return the result.
4281 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4282 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4284 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4285 &pw_multi_aff_sub);
4288 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4289 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4291 return isl_pw_multi_aff_union_add_(pma1, pma2);
4294 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4295 * with the actual sum on the shared domain and
4296 * the defined expression on the symmetric difference of the domains.
4298 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4299 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4301 return isl_union_pw_aff_union_add_(upa1, upa2);
4304 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4305 * with the actual sum on the shared domain and
4306 * the defined expression on the symmetric difference of the domains.
4308 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4309 __isl_take isl_union_pw_multi_aff *upma1,
4310 __isl_take isl_union_pw_multi_aff *upma2)
4312 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4315 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4316 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4318 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4319 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4321 int i, j, n;
4322 isl_space *space;
4323 isl_pw_multi_aff *res;
4325 if (!pma1 || !pma2)
4326 goto error;
4328 n = pma1->n * pma2->n;
4329 space = isl_space_product(isl_space_copy(pma1->dim),
4330 isl_space_copy(pma2->dim));
4331 res = isl_pw_multi_aff_alloc_size(space, n);
4333 for (i = 0; i < pma1->n; ++i) {
4334 for (j = 0; j < pma2->n; ++j) {
4335 isl_set *domain;
4336 isl_multi_aff *ma;
4338 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4339 isl_set_copy(pma2->p[j].set));
4340 ma = isl_multi_aff_product(
4341 isl_multi_aff_copy(pma1->p[i].maff),
4342 isl_multi_aff_copy(pma2->p[j].maff));
4343 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4347 isl_pw_multi_aff_free(pma1);
4348 isl_pw_multi_aff_free(pma2);
4349 return res;
4350 error:
4351 isl_pw_multi_aff_free(pma1);
4352 isl_pw_multi_aff_free(pma2);
4353 return NULL;
4356 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4357 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4359 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4360 &pw_multi_aff_product);
4363 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4364 * denominator "denom".
4365 * "denom" is allowed to be negative, in which case the actual denominator
4366 * is -denom and the expressions are added instead.
4368 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4369 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4371 int i, first;
4372 int sign;
4373 isl_int d;
4375 first = isl_seq_first_non_zero(c, n);
4376 if (first == -1)
4377 return aff;
4379 sign = isl_int_sgn(denom);
4380 isl_int_init(d);
4381 isl_int_abs(d, denom);
4382 for (i = first; i < n; ++i) {
4383 isl_aff *aff_i;
4385 if (isl_int_is_zero(c[i]))
4386 continue;
4387 aff_i = isl_multi_aff_get_aff(ma, i);
4388 aff_i = isl_aff_scale(aff_i, c[i]);
4389 aff_i = isl_aff_scale_down(aff_i, d);
4390 if (sign >= 0)
4391 aff = isl_aff_sub(aff, aff_i);
4392 else
4393 aff = isl_aff_add(aff, aff_i);
4395 isl_int_clear(d);
4397 return aff;
4400 /* Extract an affine expression that expresses the output dimension "pos"
4401 * of "bmap" in terms of the parameters and input dimensions from
4402 * equality "eq".
4403 * Note that this expression may involve integer divisions defined
4404 * in terms of parameters and input dimensions.
4405 * The equality may also involve references to earlier (but not later)
4406 * output dimensions. These are replaced by the corresponding elements
4407 * in "ma".
4409 * If the equality is of the form
4411 * f(i) + h(j) + a x + g(i) = 0,
4413 * with f(i) a linear combinations of the parameters and input dimensions,
4414 * g(i) a linear combination of integer divisions defined in terms of the same
4415 * and h(j) a linear combinations of earlier output dimensions,
4416 * then the affine expression is
4418 * (-f(i) - g(i))/a - h(j)/a
4420 * If the equality is of the form
4422 * f(i) + h(j) - a x + g(i) = 0,
4424 * then the affine expression is
4426 * (f(i) + g(i))/a - h(j)/(-a)
4429 * If "div" refers to an integer division (i.e., it is smaller than
4430 * the number of integer divisions), then the equality constraint
4431 * does involve an integer division (the one at position "div") that
4432 * is defined in terms of output dimensions. However, this integer
4433 * division can be eliminated by exploiting a pair of constraints
4434 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4435 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4436 * -l + x >= 0.
4437 * In particular, let
4439 * x = e(i) + m floor(...)
4441 * with e(i) the expression derived above and floor(...) the integer
4442 * division involving output dimensions.
4443 * From
4445 * l <= x <= l + n,
4447 * we have
4449 * 0 <= x - l <= n
4451 * This means
4453 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4454 * = (e(i) - l) mod m
4456 * Therefore,
4458 * x - l = (e(i) - l) mod m
4460 * or
4462 * x = ((e(i) - l) mod m) + l
4464 * The variable "shift" below contains the expression -l, which may
4465 * also involve a linear combination of earlier output dimensions.
4467 static __isl_give isl_aff *extract_aff_from_equality(
4468 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4469 __isl_keep isl_multi_aff *ma)
4471 unsigned o_out;
4472 unsigned n_div, n_out;
4473 isl_ctx *ctx;
4474 isl_local_space *ls;
4475 isl_aff *aff, *shift;
4476 isl_val *mod;
4478 ctx = isl_basic_map_get_ctx(bmap);
4479 ls = isl_basic_map_get_local_space(bmap);
4480 ls = isl_local_space_domain(ls);
4481 aff = isl_aff_alloc(isl_local_space_copy(ls));
4482 if (!aff)
4483 goto error;
4484 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4485 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4486 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4487 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4488 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4489 isl_seq_cpy(aff->v->el + 1 + o_out,
4490 bmap->eq[eq] + o_out + n_out, n_div);
4491 } else {
4492 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4493 isl_seq_neg(aff->v->el + 1 + o_out,
4494 bmap->eq[eq] + o_out + n_out, n_div);
4496 if (div < n_div)
4497 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4498 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4499 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4500 bmap->eq[eq][o_out + pos]);
4501 if (div < n_div) {
4502 shift = isl_aff_alloc(isl_local_space_copy(ls));
4503 if (!shift)
4504 goto error;
4505 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4506 isl_seq_cpy(shift->v->el + 1 + o_out,
4507 bmap->ineq[ineq] + o_out + n_out, n_div);
4508 isl_int_set_si(shift->v->el[0], 1);
4509 shift = subtract_initial(shift, ma, pos,
4510 bmap->ineq[ineq] + o_out, ctx->negone);
4511 aff = isl_aff_add(aff, isl_aff_copy(shift));
4512 mod = isl_val_int_from_isl_int(ctx,
4513 bmap->eq[eq][o_out + n_out + div]);
4514 mod = isl_val_abs(mod);
4515 aff = isl_aff_mod_val(aff, mod);
4516 aff = isl_aff_sub(aff, shift);
4519 isl_local_space_free(ls);
4520 return aff;
4521 error:
4522 isl_local_space_free(ls);
4523 isl_aff_free(aff);
4524 return NULL;
4527 /* Given a basic map with output dimensions defined
4528 * in terms of the parameters input dimensions and earlier
4529 * output dimensions using an equality (and possibly a pair on inequalities),
4530 * extract an isl_aff that expresses output dimension "pos" in terms
4531 * of the parameters and input dimensions.
4532 * Note that this expression may involve integer divisions defined
4533 * in terms of parameters and input dimensions.
4534 * "ma" contains the expressions corresponding to earlier output dimensions.
4536 * This function shares some similarities with
4537 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4539 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4540 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4542 int eq, div, ineq;
4543 isl_aff *aff;
4545 if (!bmap)
4546 return NULL;
4547 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4548 if (eq >= bmap->n_eq)
4549 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4550 "unable to find suitable equality", return NULL);
4551 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4553 aff = isl_aff_remove_unused_divs(aff);
4554 return aff;
4557 /* Given a basic map where each output dimension is defined
4558 * in terms of the parameters and input dimensions using an equality,
4559 * extract an isl_multi_aff that expresses the output dimensions in terms
4560 * of the parameters and input dimensions.
4562 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4563 __isl_take isl_basic_map *bmap)
4565 int i;
4566 unsigned n_out;
4567 isl_multi_aff *ma;
4569 if (!bmap)
4570 return NULL;
4572 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4573 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4575 for (i = 0; i < n_out; ++i) {
4576 isl_aff *aff;
4578 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4579 ma = isl_multi_aff_set_aff(ma, i, aff);
4582 isl_basic_map_free(bmap);
4584 return ma;
4587 /* Given a basic set where each set dimension is defined
4588 * in terms of the parameters using an equality,
4589 * extract an isl_multi_aff that expresses the set dimensions in terms
4590 * of the parameters.
4592 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4593 __isl_take isl_basic_set *bset)
4595 return extract_isl_multi_aff_from_basic_map(bset);
4598 /* Create an isl_pw_multi_aff that is equivalent to
4599 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4600 * The given basic map is such that each output dimension is defined
4601 * in terms of the parameters and input dimensions using an equality.
4603 * Since some applications expect the result of isl_pw_multi_aff_from_map
4604 * to only contain integer affine expressions, we compute the floor
4605 * of the expression before returning.
4607 * Remove all constraints involving local variables without
4608 * an explicit representation (resulting in the removal of those
4609 * local variables) prior to the actual extraction to ensure
4610 * that the local spaces in which the resulting affine expressions
4611 * are created do not contain any unknown local variables.
4612 * Removing such constraints is safe because constraints involving
4613 * unknown local variables are not used to determine whether
4614 * a basic map is obviously single-valued.
4616 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4617 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4619 isl_multi_aff *ma;
4621 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4622 ma = extract_isl_multi_aff_from_basic_map(bmap);
4623 ma = isl_multi_aff_floor(ma);
4624 return isl_pw_multi_aff_alloc(domain, ma);
4627 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4628 * This obviously only works if the input "map" is single-valued.
4629 * If so, we compute the lexicographic minimum of the image in the form
4630 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4631 * to its lexicographic minimum.
4632 * If the input is not single-valued, we produce an error.
4634 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4635 __isl_take isl_map *map)
4637 int i;
4638 int sv;
4639 isl_pw_multi_aff *pma;
4641 sv = isl_map_is_single_valued(map);
4642 if (sv < 0)
4643 goto error;
4644 if (!sv)
4645 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4646 "map is not single-valued", goto error);
4647 map = isl_map_make_disjoint(map);
4648 if (!map)
4649 return NULL;
4651 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4653 for (i = 0; i < map->n; ++i) {
4654 isl_pw_multi_aff *pma_i;
4655 isl_basic_map *bmap;
4656 bmap = isl_basic_map_copy(map->p[i]);
4657 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4658 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4661 isl_map_free(map);
4662 return pma;
4663 error:
4664 isl_map_free(map);
4665 return NULL;
4668 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4669 * taking into account that the output dimension at position "d"
4670 * can be represented as
4672 * x = floor((e(...) + c1) / m)
4674 * given that constraint "i" is of the form
4676 * e(...) + c1 - m x >= 0
4679 * Let "map" be of the form
4681 * A -> B
4683 * We construct a mapping
4685 * A -> [A -> x = floor(...)]
4687 * apply that to the map, obtaining
4689 * [A -> x = floor(...)] -> B
4691 * and equate dimension "d" to x.
4692 * We then compute a isl_pw_multi_aff representation of the resulting map
4693 * and plug in the mapping above.
4695 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4696 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4698 isl_ctx *ctx;
4699 isl_space *space;
4700 isl_local_space *ls;
4701 isl_multi_aff *ma;
4702 isl_aff *aff;
4703 isl_vec *v;
4704 isl_map *insert;
4705 int offset;
4706 int n;
4707 int n_in;
4708 isl_pw_multi_aff *pma;
4709 isl_bool is_set;
4711 is_set = isl_map_is_set(map);
4712 if (is_set < 0)
4713 goto error;
4715 offset = isl_basic_map_offset(hull, isl_dim_out);
4716 ctx = isl_map_get_ctx(map);
4717 space = isl_space_domain(isl_map_get_space(map));
4718 n_in = isl_space_dim(space, isl_dim_set);
4719 n = isl_space_dim(space, isl_dim_all);
4721 v = isl_vec_alloc(ctx, 1 + 1 + n);
4722 if (v) {
4723 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4724 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4726 isl_basic_map_free(hull);
4728 ls = isl_local_space_from_space(isl_space_copy(space));
4729 aff = isl_aff_alloc_vec(ls, v);
4730 aff = isl_aff_floor(aff);
4731 if (is_set) {
4732 isl_space_free(space);
4733 ma = isl_multi_aff_from_aff(aff);
4734 } else {
4735 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4736 ma = isl_multi_aff_range_product(ma,
4737 isl_multi_aff_from_aff(aff));
4740 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4741 map = isl_map_apply_domain(map, insert);
4742 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4743 pma = isl_pw_multi_aff_from_map(map);
4744 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4746 return pma;
4747 error:
4748 isl_map_free(map);
4749 isl_basic_map_free(hull);
4750 return NULL;
4753 /* Is constraint "c" of the form
4755 * e(...) + c1 - m x >= 0
4757 * or
4759 * -e(...) + c2 + m x >= 0
4761 * where m > 1 and e only depends on parameters and input dimemnsions?
4763 * "offset" is the offset of the output dimensions
4764 * "pos" is the position of output dimension x.
4766 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4768 if (isl_int_is_zero(c[offset + d]))
4769 return 0;
4770 if (isl_int_is_one(c[offset + d]))
4771 return 0;
4772 if (isl_int_is_negone(c[offset + d]))
4773 return 0;
4774 if (isl_seq_first_non_zero(c + offset, d) != -1)
4775 return 0;
4776 if (isl_seq_first_non_zero(c + offset + d + 1,
4777 total - (offset + d + 1)) != -1)
4778 return 0;
4779 return 1;
4782 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4784 * As a special case, we first check if there is any pair of constraints,
4785 * shared by all the basic maps in "map" that force a given dimension
4786 * to be equal to the floor of some affine combination of the input dimensions.
4788 * In particular, if we can find two constraints
4790 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4792 * and
4794 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4796 * where m > 1 and e only depends on parameters and input dimemnsions,
4797 * and such that
4799 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4801 * then we know that we can take
4803 * x = floor((e(...) + c1) / m)
4805 * without having to perform any computation.
4807 * Note that we know that
4809 * c1 + c2 >= 1
4811 * If c1 + c2 were 0, then we would have detected an equality during
4812 * simplification. If c1 + c2 were negative, then we would have detected
4813 * a contradiction.
4815 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4816 __isl_take isl_map *map)
4818 int d, dim;
4819 int i, j, n;
4820 int offset, total;
4821 isl_int sum;
4822 isl_basic_map *hull;
4824 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4825 if (!hull)
4826 goto error;
4828 isl_int_init(sum);
4829 dim = isl_map_dim(map, isl_dim_out);
4830 offset = isl_basic_map_offset(hull, isl_dim_out);
4831 total = 1 + isl_basic_map_total_dim(hull);
4832 n = hull->n_ineq;
4833 for (d = 0; d < dim; ++d) {
4834 for (i = 0; i < n; ++i) {
4835 if (!is_potential_div_constraint(hull->ineq[i],
4836 offset, d, total))
4837 continue;
4838 for (j = i + 1; j < n; ++j) {
4839 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4840 hull->ineq[j] + 1, total - 1))
4841 continue;
4842 isl_int_add(sum, hull->ineq[i][0],
4843 hull->ineq[j][0]);
4844 if (isl_int_abs_lt(sum,
4845 hull->ineq[i][offset + d]))
4846 break;
4849 if (j >= n)
4850 continue;
4851 isl_int_clear(sum);
4852 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4853 j = i;
4854 return pw_multi_aff_from_map_div(map, hull, d, j);
4857 isl_int_clear(sum);
4858 isl_basic_map_free(hull);
4859 return pw_multi_aff_from_map_base(map);
4860 error:
4861 isl_map_free(map);
4862 isl_basic_map_free(hull);
4863 return NULL;
4866 /* Given an affine expression
4868 * [A -> B] -> f(A,B)
4870 * construct an isl_multi_aff
4872 * [A -> B] -> B'
4874 * such that dimension "d" in B' is set to "aff" and the remaining
4875 * dimensions are set equal to the corresponding dimensions in B.
4876 * "n_in" is the dimension of the space A.
4877 * "n_out" is the dimension of the space B.
4879 * If "is_set" is set, then the affine expression is of the form
4881 * [B] -> f(B)
4883 * and we construct an isl_multi_aff
4885 * B -> B'
4887 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4888 unsigned n_in, unsigned n_out, int is_set)
4890 int i;
4891 isl_multi_aff *ma;
4892 isl_space *space, *space2;
4893 isl_local_space *ls;
4895 space = isl_aff_get_domain_space(aff);
4896 ls = isl_local_space_from_space(isl_space_copy(space));
4897 space2 = isl_space_copy(space);
4898 if (!is_set)
4899 space2 = isl_space_range(isl_space_unwrap(space2));
4900 space = isl_space_map_from_domain_and_range(space, space2);
4901 ma = isl_multi_aff_alloc(space);
4902 ma = isl_multi_aff_set_aff(ma, d, aff);
4904 for (i = 0; i < n_out; ++i) {
4905 if (i == d)
4906 continue;
4907 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4908 isl_dim_set, n_in + i);
4909 ma = isl_multi_aff_set_aff(ma, i, aff);
4912 isl_local_space_free(ls);
4914 return ma;
4917 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4918 * taking into account that the dimension at position "d" can be written as
4920 * x = m a + f(..) (1)
4922 * where m is equal to "gcd".
4923 * "i" is the index of the equality in "hull" that defines f(..).
4924 * In particular, the equality is of the form
4926 * f(..) - x + m g(existentials) = 0
4928 * or
4930 * -f(..) + x + m g(existentials) = 0
4932 * We basically plug (1) into "map", resulting in a map with "a"
4933 * in the range instead of "x". The corresponding isl_pw_multi_aff
4934 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4936 * Specifically, given the input map
4938 * A -> B
4940 * We first wrap it into a set
4942 * [A -> B]
4944 * and define (1) on top of the corresponding space, resulting in "aff".
4945 * We use this to create an isl_multi_aff that maps the output position "d"
4946 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4947 * We plug this into the wrapped map, unwrap the result and compute the
4948 * corresponding isl_pw_multi_aff.
4949 * The result is an expression
4951 * A -> T(A)
4953 * We adjust that to
4955 * A -> [A -> T(A)]
4957 * so that we can plug that into "aff", after extending the latter to
4958 * a mapping
4960 * [A -> B] -> B'
4963 * If "map" is actually a set, then there is no "A" space, meaning
4964 * that we do not need to perform any wrapping, and that the result
4965 * of the recursive call is of the form
4967 * [T]
4969 * which is plugged into a mapping of the form
4971 * B -> B'
4973 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4974 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4975 isl_int gcd)
4977 isl_set *set;
4978 isl_space *space;
4979 isl_local_space *ls;
4980 isl_aff *aff;
4981 isl_multi_aff *ma;
4982 isl_pw_multi_aff *pma, *id;
4983 unsigned n_in;
4984 unsigned o_out;
4985 unsigned n_out;
4986 isl_bool is_set;
4988 is_set = isl_map_is_set(map);
4989 if (is_set < 0)
4990 goto error;
4992 n_in = isl_basic_map_dim(hull, isl_dim_in);
4993 n_out = isl_basic_map_dim(hull, isl_dim_out);
4994 o_out = isl_basic_map_offset(hull, isl_dim_out);
4996 if (is_set)
4997 set = map;
4998 else
4999 set = isl_map_wrap(map);
5000 space = isl_space_map_from_set(isl_set_get_space(set));
5001 ma = isl_multi_aff_identity(space);
5002 ls = isl_local_space_from_space(isl_set_get_space(set));
5003 aff = isl_aff_alloc(ls);
5004 if (aff) {
5005 isl_int_set_si(aff->v->el[0], 1);
5006 if (isl_int_is_one(hull->eq[i][o_out + d]))
5007 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5008 aff->v->size - 1);
5009 else
5010 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5011 aff->v->size - 1);
5012 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5014 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5015 set = isl_set_preimage_multi_aff(set, ma);
5017 ma = range_map(aff, d, n_in, n_out, is_set);
5019 if (is_set)
5020 map = set;
5021 else
5022 map = isl_set_unwrap(set);
5023 pma = isl_pw_multi_aff_from_map(map);
5025 if (!is_set) {
5026 space = isl_pw_multi_aff_get_domain_space(pma);
5027 space = isl_space_map_from_set(space);
5028 id = isl_pw_multi_aff_identity(space);
5029 pma = isl_pw_multi_aff_range_product(id, pma);
5031 id = isl_pw_multi_aff_from_multi_aff(ma);
5032 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5034 isl_basic_map_free(hull);
5035 return pma;
5036 error:
5037 isl_map_free(map);
5038 isl_basic_map_free(hull);
5039 return NULL;
5042 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5043 * "hull" contains the equalities valid for "map".
5045 * Check if any of the output dimensions is "strided".
5046 * That is, we check if it can be written as
5048 * x = m a + f(..)
5050 * with m greater than 1, a some combination of existentially quantified
5051 * variables and f an expression in the parameters and input dimensions.
5052 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5054 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5055 * special case.
5057 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5058 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5060 int i, j;
5061 unsigned n_out;
5062 unsigned o_out;
5063 unsigned n_div;
5064 unsigned o_div;
5065 isl_int gcd;
5067 n_div = isl_basic_map_dim(hull, isl_dim_div);
5068 o_div = isl_basic_map_offset(hull, isl_dim_div);
5070 if (n_div == 0) {
5071 isl_basic_map_free(hull);
5072 return pw_multi_aff_from_map_check_div(map);
5075 isl_int_init(gcd);
5077 n_out = isl_basic_map_dim(hull, isl_dim_out);
5078 o_out = isl_basic_map_offset(hull, isl_dim_out);
5080 for (i = 0; i < n_out; ++i) {
5081 for (j = 0; j < hull->n_eq; ++j) {
5082 isl_int *eq = hull->eq[j];
5083 isl_pw_multi_aff *res;
5085 if (!isl_int_is_one(eq[o_out + i]) &&
5086 !isl_int_is_negone(eq[o_out + i]))
5087 continue;
5088 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5089 continue;
5090 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5091 n_out - (i + 1)) != -1)
5092 continue;
5093 isl_seq_gcd(eq + o_div, n_div, &gcd);
5094 if (isl_int_is_zero(gcd))
5095 continue;
5096 if (isl_int_is_one(gcd))
5097 continue;
5099 res = pw_multi_aff_from_map_stride(map, hull,
5100 i, j, gcd);
5101 isl_int_clear(gcd);
5102 return res;
5106 isl_int_clear(gcd);
5107 isl_basic_map_free(hull);
5108 return pw_multi_aff_from_map_check_div(map);
5111 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5113 * As a special case, we first check if all output dimensions are uniquely
5114 * defined in terms of the parameters and input dimensions over the entire
5115 * domain. If so, we extract the desired isl_pw_multi_aff directly
5116 * from the affine hull of "map" and its domain.
5118 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5119 * special cases.
5121 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5123 isl_bool sv;
5124 isl_basic_map *hull;
5126 if (!map)
5127 return NULL;
5129 if (isl_map_n_basic_map(map) == 1) {
5130 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5131 hull = isl_basic_map_plain_affine_hull(hull);
5132 sv = isl_basic_map_plain_is_single_valued(hull);
5133 if (sv >= 0 && sv)
5134 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5135 hull);
5136 isl_basic_map_free(hull);
5138 map = isl_map_detect_equalities(map);
5139 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5140 sv = isl_basic_map_plain_is_single_valued(hull);
5141 if (sv >= 0 && sv)
5142 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5143 if (sv >= 0)
5144 return pw_multi_aff_from_map_check_strides(map, hull);
5145 isl_basic_map_free(hull);
5146 isl_map_free(map);
5147 return NULL;
5150 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5152 return isl_pw_multi_aff_from_map(set);
5155 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5156 * add it to *user.
5158 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5160 isl_union_pw_multi_aff **upma = user;
5161 isl_pw_multi_aff *pma;
5163 pma = isl_pw_multi_aff_from_map(map);
5164 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5166 return *upma ? isl_stat_ok : isl_stat_error;
5169 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5170 * domain.
5172 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5173 __isl_take isl_aff *aff)
5175 isl_multi_aff *ma;
5176 isl_pw_multi_aff *pma;
5178 ma = isl_multi_aff_from_aff(aff);
5179 pma = isl_pw_multi_aff_from_multi_aff(ma);
5180 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5183 /* Try and create an isl_union_pw_multi_aff that is equivalent
5184 * to the given isl_union_map.
5185 * The isl_union_map is required to be single-valued in each space.
5186 * Otherwise, an error is produced.
5188 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5189 __isl_take isl_union_map *umap)
5191 isl_space *space;
5192 isl_union_pw_multi_aff *upma;
5194 space = isl_union_map_get_space(umap);
5195 upma = isl_union_pw_multi_aff_empty(space);
5196 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5197 upma = isl_union_pw_multi_aff_free(upma);
5198 isl_union_map_free(umap);
5200 return upma;
5203 /* Try and create an isl_union_pw_multi_aff that is equivalent
5204 * to the given isl_union_set.
5205 * The isl_union_set is required to be a singleton in each space.
5206 * Otherwise, an error is produced.
5208 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5209 __isl_take isl_union_set *uset)
5211 return isl_union_pw_multi_aff_from_union_map(uset);
5214 /* Return the piecewise affine expression "set ? 1 : 0".
5216 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5218 isl_pw_aff *pa;
5219 isl_space *space = isl_set_get_space(set);
5220 isl_local_space *ls = isl_local_space_from_space(space);
5221 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5222 isl_aff *one = isl_aff_zero_on_domain(ls);
5224 one = isl_aff_add_constant_si(one, 1);
5225 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5226 set = isl_set_complement(set);
5227 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5229 return pa;
5232 /* Plug in "subs" for dimension "type", "pos" of "aff".
5234 * Let i be the dimension to replace and let "subs" be of the form
5236 * f/d
5238 * and "aff" of the form
5240 * (a i + g)/m
5242 * The result is
5244 * (a f + d g')/(m d)
5246 * where g' is the result of plugging in "subs" in each of the integer
5247 * divisions in g.
5249 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5250 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5252 isl_ctx *ctx;
5253 isl_int v;
5255 aff = isl_aff_cow(aff);
5256 if (!aff || !subs)
5257 return isl_aff_free(aff);
5259 ctx = isl_aff_get_ctx(aff);
5260 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5261 isl_die(ctx, isl_error_invalid,
5262 "spaces don't match", return isl_aff_free(aff));
5263 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5264 isl_die(ctx, isl_error_unsupported,
5265 "cannot handle divs yet", return isl_aff_free(aff));
5267 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5268 if (!aff->ls)
5269 return isl_aff_free(aff);
5271 aff->v = isl_vec_cow(aff->v);
5272 if (!aff->v)
5273 return isl_aff_free(aff);
5275 pos += isl_local_space_offset(aff->ls, type);
5277 isl_int_init(v);
5278 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5279 aff->v->size, subs->v->size, v);
5280 isl_int_clear(v);
5282 return aff;
5285 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5286 * expressions in "maff".
5288 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5289 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5290 __isl_keep isl_aff *subs)
5292 int i;
5294 maff = isl_multi_aff_cow(maff);
5295 if (!maff || !subs)
5296 return isl_multi_aff_free(maff);
5298 if (type == isl_dim_in)
5299 type = isl_dim_set;
5301 for (i = 0; i < maff->n; ++i) {
5302 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5303 type, pos, subs);
5304 if (!maff->u.p[i])
5305 return isl_multi_aff_free(maff);
5308 return maff;
5311 /* Plug in "subs" for dimension "type", "pos" of "pma".
5313 * pma is of the form
5315 * A_i(v) -> M_i(v)
5317 * while subs is of the form
5319 * v' = B_j(v) -> S_j
5321 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5322 * has a contribution in the result, in particular
5324 * C_ij(S_j) -> M_i(S_j)
5326 * Note that plugging in S_j in C_ij may also result in an empty set
5327 * and this contribution should simply be discarded.
5329 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5330 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5331 __isl_keep isl_pw_aff *subs)
5333 int i, j, n;
5334 isl_pw_multi_aff *res;
5336 if (!pma || !subs)
5337 return isl_pw_multi_aff_free(pma);
5339 n = pma->n * subs->n;
5340 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5342 for (i = 0; i < pma->n; ++i) {
5343 for (j = 0; j < subs->n; ++j) {
5344 isl_set *common;
5345 isl_multi_aff *res_ij;
5346 int empty;
5348 common = isl_set_intersect(
5349 isl_set_copy(pma->p[i].set),
5350 isl_set_copy(subs->p[j].set));
5351 common = isl_set_substitute(common,
5352 type, pos, subs->p[j].aff);
5353 empty = isl_set_plain_is_empty(common);
5354 if (empty < 0 || empty) {
5355 isl_set_free(common);
5356 if (empty < 0)
5357 goto error;
5358 continue;
5361 res_ij = isl_multi_aff_substitute(
5362 isl_multi_aff_copy(pma->p[i].maff),
5363 type, pos, subs->p[j].aff);
5365 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5369 isl_pw_multi_aff_free(pma);
5370 return res;
5371 error:
5372 isl_pw_multi_aff_free(pma);
5373 isl_pw_multi_aff_free(res);
5374 return NULL;
5377 /* Compute the preimage of a range of dimensions in the affine expression "src"
5378 * under "ma" and put the result in "dst". The number of dimensions in "src"
5379 * that precede the range is given by "n_before". The number of dimensions
5380 * in the range is given by the number of output dimensions of "ma".
5381 * The number of dimensions that follow the range is given by "n_after".
5382 * If "has_denom" is set (to one),
5383 * then "src" and "dst" have an extra initial denominator.
5384 * "n_div_ma" is the number of existentials in "ma"
5385 * "n_div_bset" is the number of existentials in "src"
5386 * The resulting "dst" (which is assumed to have been allocated by
5387 * the caller) contains coefficients for both sets of existentials,
5388 * first those in "ma" and then those in "src".
5389 * f, c1, c2 and g are temporary objects that have been initialized
5390 * by the caller.
5392 * Let src represent the expression
5394 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5396 * and let ma represent the expressions
5398 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5400 * We start out with the following expression for dst:
5402 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5404 * with the multiplication factor f initially equal to 1
5405 * and f \sum_i b_i v_i kept separately.
5406 * For each x_i that we substitute, we multiply the numerator
5407 * (and denominator) of dst by c_1 = m_i and add the numerator
5408 * of the x_i expression multiplied by c_2 = f b_i,
5409 * after removing the common factors of c_1 and c_2.
5410 * The multiplication factor f also needs to be multiplied by c_1
5411 * for the next x_j, j > i.
5413 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5414 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5415 int n_div_ma, int n_div_bmap,
5416 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5418 int i;
5419 int n_param, n_in, n_out;
5420 int o_dst, o_src;
5422 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5423 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5424 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5426 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5427 o_dst = o_src = has_denom + 1 + n_param + n_before;
5428 isl_seq_clr(dst + o_dst, n_in);
5429 o_dst += n_in;
5430 o_src += n_out;
5431 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5432 o_dst += n_after;
5433 o_src += n_after;
5434 isl_seq_clr(dst + o_dst, n_div_ma);
5435 o_dst += n_div_ma;
5436 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5438 isl_int_set_si(f, 1);
5440 for (i = 0; i < n_out; ++i) {
5441 int offset = has_denom + 1 + n_param + n_before + i;
5443 if (isl_int_is_zero(src[offset]))
5444 continue;
5445 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5446 isl_int_mul(c2, f, src[offset]);
5447 isl_int_gcd(g, c1, c2);
5448 isl_int_divexact(c1, c1, g);
5449 isl_int_divexact(c2, c2, g);
5451 isl_int_mul(f, f, c1);
5452 o_dst = has_denom;
5453 o_src = 1;
5454 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5455 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5456 o_dst += 1 + n_param;
5457 o_src += 1 + n_param;
5458 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5459 o_dst += n_before;
5460 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5461 c2, ma->u.p[i]->v->el + o_src, n_in);
5462 o_dst += n_in;
5463 o_src += n_in;
5464 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5465 o_dst += n_after;
5466 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5467 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5468 o_dst += n_div_ma;
5469 o_src += n_div_ma;
5470 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5471 if (has_denom)
5472 isl_int_mul(dst[0], dst[0], c1);
5475 return isl_stat_ok;
5478 /* Compute the pullback of "aff" by the function represented by "ma".
5479 * In other words, plug in "ma" in "aff". The result is an affine expression
5480 * defined over the domain space of "ma".
5482 * If "aff" is represented by
5484 * (a(p) + b x + c(divs))/d
5486 * and ma is represented by
5488 * x = D(p) + F(y) + G(divs')
5490 * then the result is
5492 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5494 * The divs in the local space of the input are similarly adjusted
5495 * through a call to isl_local_space_preimage_multi_aff.
5497 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5498 __isl_take isl_multi_aff *ma)
5500 isl_aff *res = NULL;
5501 isl_local_space *ls;
5502 int n_div_aff, n_div_ma;
5503 isl_int f, c1, c2, g;
5505 ma = isl_multi_aff_align_divs(ma);
5506 if (!aff || !ma)
5507 goto error;
5509 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5510 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5512 ls = isl_aff_get_domain_local_space(aff);
5513 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5514 res = isl_aff_alloc(ls);
5515 if (!res)
5516 goto error;
5518 isl_int_init(f);
5519 isl_int_init(c1);
5520 isl_int_init(c2);
5521 isl_int_init(g);
5523 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5524 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5525 res = isl_aff_free(res);
5527 isl_int_clear(f);
5528 isl_int_clear(c1);
5529 isl_int_clear(c2);
5530 isl_int_clear(g);
5532 isl_aff_free(aff);
5533 isl_multi_aff_free(ma);
5534 res = isl_aff_normalize(res);
5535 return res;
5536 error:
5537 isl_aff_free(aff);
5538 isl_multi_aff_free(ma);
5539 isl_aff_free(res);
5540 return NULL;
5543 /* Compute the pullback of "aff1" by the function represented by "aff2".
5544 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5545 * defined over the domain space of "aff1".
5547 * The domain of "aff1" should match the range of "aff2", which means
5548 * that it should be single-dimensional.
5550 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5551 __isl_take isl_aff *aff2)
5553 isl_multi_aff *ma;
5555 ma = isl_multi_aff_from_aff(aff2);
5556 return isl_aff_pullback_multi_aff(aff1, ma);
5559 /* Compute the pullback of "ma1" by the function represented by "ma2".
5560 * In other words, plug in "ma2" in "ma1".
5562 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5564 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5565 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5567 int i;
5568 isl_space *space = NULL;
5570 ma2 = isl_multi_aff_align_divs(ma2);
5571 ma1 = isl_multi_aff_cow(ma1);
5572 if (!ma1 || !ma2)
5573 goto error;
5575 space = isl_space_join(isl_multi_aff_get_space(ma2),
5576 isl_multi_aff_get_space(ma1));
5578 for (i = 0; i < ma1->n; ++i) {
5579 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5580 isl_multi_aff_copy(ma2));
5581 if (!ma1->u.p[i])
5582 goto error;
5585 ma1 = isl_multi_aff_reset_space(ma1, space);
5586 isl_multi_aff_free(ma2);
5587 return ma1;
5588 error:
5589 isl_space_free(space);
5590 isl_multi_aff_free(ma2);
5591 isl_multi_aff_free(ma1);
5592 return NULL;
5595 /* Compute the pullback of "ma1" by the function represented by "ma2".
5596 * In other words, plug in "ma2" in "ma1".
5598 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5599 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5601 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5602 &isl_multi_aff_pullback_multi_aff_aligned);
5605 /* Extend the local space of "dst" to include the divs
5606 * in the local space of "src".
5608 * If "src" does not have any divs or if the local spaces of "dst" and
5609 * "src" are the same, then no extension is required.
5611 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5612 __isl_keep isl_aff *src)
5614 isl_ctx *ctx;
5615 int src_n_div, dst_n_div;
5616 int *exp1 = NULL;
5617 int *exp2 = NULL;
5618 isl_bool equal;
5619 isl_mat *div;
5621 if (!src || !dst)
5622 return isl_aff_free(dst);
5624 ctx = isl_aff_get_ctx(src);
5625 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5626 if (equal < 0)
5627 return isl_aff_free(dst);
5628 if (!equal)
5629 isl_die(ctx, isl_error_invalid,
5630 "spaces don't match", goto error);
5632 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5633 if (src_n_div == 0)
5634 return dst;
5635 equal = isl_local_space_is_equal(src->ls, dst->ls);
5636 if (equal < 0)
5637 return isl_aff_free(dst);
5638 if (equal)
5639 return dst;
5641 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5642 exp1 = isl_alloc_array(ctx, int, src_n_div);
5643 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5644 if (!exp1 || (dst_n_div && !exp2))
5645 goto error;
5647 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5648 dst = isl_aff_expand_divs(dst, div, exp2);
5649 free(exp1);
5650 free(exp2);
5652 return dst;
5653 error:
5654 free(exp1);
5655 free(exp2);
5656 return isl_aff_free(dst);
5659 /* Adjust the local spaces of the affine expressions in "maff"
5660 * such that they all have the save divs.
5662 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5663 __isl_take isl_multi_aff *maff)
5665 int i;
5667 if (!maff)
5668 return NULL;
5669 if (maff->n == 0)
5670 return maff;
5671 maff = isl_multi_aff_cow(maff);
5672 if (!maff)
5673 return NULL;
5675 for (i = 1; i < maff->n; ++i)
5676 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5677 for (i = 1; i < maff->n; ++i) {
5678 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5679 if (!maff->u.p[i])
5680 return isl_multi_aff_free(maff);
5683 return maff;
5686 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5688 aff = isl_aff_cow(aff);
5689 if (!aff)
5690 return NULL;
5692 aff->ls = isl_local_space_lift(aff->ls);
5693 if (!aff->ls)
5694 return isl_aff_free(aff);
5696 return aff;
5699 /* Lift "maff" to a space with extra dimensions such that the result
5700 * has no more existentially quantified variables.
5701 * If "ls" is not NULL, then *ls is assigned the local space that lies
5702 * at the basis of the lifting applied to "maff".
5704 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5705 __isl_give isl_local_space **ls)
5707 int i;
5708 isl_space *space;
5709 unsigned n_div;
5711 if (ls)
5712 *ls = NULL;
5714 if (!maff)
5715 return NULL;
5717 if (maff->n == 0) {
5718 if (ls) {
5719 isl_space *space = isl_multi_aff_get_domain_space(maff);
5720 *ls = isl_local_space_from_space(space);
5721 if (!*ls)
5722 return isl_multi_aff_free(maff);
5724 return maff;
5727 maff = isl_multi_aff_cow(maff);
5728 maff = isl_multi_aff_align_divs(maff);
5729 if (!maff)
5730 return NULL;
5732 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5733 space = isl_multi_aff_get_space(maff);
5734 space = isl_space_lift(isl_space_domain(space), n_div);
5735 space = isl_space_extend_domain_with_range(space,
5736 isl_multi_aff_get_space(maff));
5737 if (!space)
5738 return isl_multi_aff_free(maff);
5739 isl_space_free(maff->space);
5740 maff->space = space;
5742 if (ls) {
5743 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5744 if (!*ls)
5745 return isl_multi_aff_free(maff);
5748 for (i = 0; i < maff->n; ++i) {
5749 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5750 if (!maff->u.p[i])
5751 goto error;
5754 return maff;
5755 error:
5756 if (ls)
5757 isl_local_space_free(*ls);
5758 return isl_multi_aff_free(maff);
5761 #undef TYPE
5762 #define TYPE isl_pw_multi_aff
5763 static
5764 #include "check_type_range_templ.c"
5766 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5768 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5769 __isl_keep isl_pw_multi_aff *pma, int pos)
5771 int i;
5772 int n_out;
5773 isl_space *space;
5774 isl_pw_aff *pa;
5776 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
5777 return NULL;
5779 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5781 space = isl_pw_multi_aff_get_space(pma);
5782 space = isl_space_drop_dims(space, isl_dim_out,
5783 pos + 1, n_out - pos - 1);
5784 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5786 pa = isl_pw_aff_alloc_size(space, pma->n);
5787 for (i = 0; i < pma->n; ++i) {
5788 isl_aff *aff;
5789 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5790 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5793 return pa;
5796 /* Return an isl_pw_multi_aff with the given "set" as domain and
5797 * an unnamed zero-dimensional range.
5799 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5800 __isl_take isl_set *set)
5802 isl_multi_aff *ma;
5803 isl_space *space;
5805 space = isl_set_get_space(set);
5806 space = isl_space_from_domain(space);
5807 ma = isl_multi_aff_zero(space);
5808 return isl_pw_multi_aff_alloc(set, ma);
5811 /* Add an isl_pw_multi_aff with the given "set" as domain and
5812 * an unnamed zero-dimensional range to *user.
5814 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5815 void *user)
5817 isl_union_pw_multi_aff **upma = user;
5818 isl_pw_multi_aff *pma;
5820 pma = isl_pw_multi_aff_from_domain(set);
5821 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5823 return isl_stat_ok;
5826 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5827 * an unnamed zero-dimensional range.
5829 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5830 __isl_take isl_union_set *uset)
5832 isl_space *space;
5833 isl_union_pw_multi_aff *upma;
5835 if (!uset)
5836 return NULL;
5838 space = isl_union_set_get_space(uset);
5839 upma = isl_union_pw_multi_aff_empty(space);
5841 if (isl_union_set_foreach_set(uset,
5842 &add_pw_multi_aff_from_domain, &upma) < 0)
5843 goto error;
5845 isl_union_set_free(uset);
5846 return upma;
5847 error:
5848 isl_union_set_free(uset);
5849 isl_union_pw_multi_aff_free(upma);
5850 return NULL;
5853 /* Local data for bin_entry and the callback "fn".
5855 struct isl_union_pw_multi_aff_bin_data {
5856 isl_union_pw_multi_aff *upma2;
5857 isl_union_pw_multi_aff *res;
5858 isl_pw_multi_aff *pma;
5859 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5862 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5863 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5865 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5867 struct isl_union_pw_multi_aff_bin_data *data = user;
5868 isl_stat r;
5870 data->pma = pma;
5871 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5872 data->fn, data);
5873 isl_pw_multi_aff_free(pma);
5875 return r;
5878 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5879 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5880 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5881 * as *entry. The callback should adjust data->res if desired.
5883 static __isl_give isl_union_pw_multi_aff *bin_op(
5884 __isl_take isl_union_pw_multi_aff *upma1,
5885 __isl_take isl_union_pw_multi_aff *upma2,
5886 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5888 isl_space *space;
5889 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5891 space = isl_union_pw_multi_aff_get_space(upma2);
5892 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5893 space = isl_union_pw_multi_aff_get_space(upma1);
5894 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5896 if (!upma1 || !upma2)
5897 goto error;
5899 data.upma2 = upma2;
5900 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5901 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5902 &bin_entry, &data) < 0)
5903 goto error;
5905 isl_union_pw_multi_aff_free(upma1);
5906 isl_union_pw_multi_aff_free(upma2);
5907 return data.res;
5908 error:
5909 isl_union_pw_multi_aff_free(upma1);
5910 isl_union_pw_multi_aff_free(upma2);
5911 isl_union_pw_multi_aff_free(data.res);
5912 return NULL;
5915 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5916 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5918 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5919 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5921 isl_space *space;
5923 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5924 isl_pw_multi_aff_get_space(pma2));
5925 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5926 &isl_multi_aff_range_product);
5929 /* Given two isl_pw_multi_affs A -> B and C -> D,
5930 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5932 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5933 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5935 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5936 &pw_multi_aff_range_product);
5939 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5940 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5942 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5943 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5945 isl_space *space;
5947 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5948 isl_pw_multi_aff_get_space(pma2));
5949 space = isl_space_flatten_range(space);
5950 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5951 &isl_multi_aff_flat_range_product);
5954 /* Given two isl_pw_multi_affs A -> B and C -> D,
5955 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5957 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5958 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5960 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5961 &pw_multi_aff_flat_range_product);
5964 /* If data->pma and "pma2" have the same domain space, then compute
5965 * their flat range product and the result to data->res.
5967 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5968 void *user)
5970 struct isl_union_pw_multi_aff_bin_data *data = user;
5972 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5973 pma2->dim, isl_dim_in)) {
5974 isl_pw_multi_aff_free(pma2);
5975 return isl_stat_ok;
5978 pma2 = isl_pw_multi_aff_flat_range_product(
5979 isl_pw_multi_aff_copy(data->pma), pma2);
5981 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5983 return isl_stat_ok;
5986 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5987 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5989 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5990 __isl_take isl_union_pw_multi_aff *upma1,
5991 __isl_take isl_union_pw_multi_aff *upma2)
5993 return bin_op(upma1, upma2, &flat_range_product_entry);
5996 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5997 * The parameters are assumed to have been aligned.
5999 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6000 * except that it works on two different isl_pw_* types.
6002 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6003 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6004 __isl_take isl_pw_aff *pa)
6006 int i, j, n;
6007 isl_pw_multi_aff *res = NULL;
6009 if (!pma || !pa)
6010 goto error;
6012 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6013 pa->dim, isl_dim_in))
6014 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6015 "domains don't match", goto error);
6016 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6017 goto error;
6019 n = pma->n * pa->n;
6020 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6022 for (i = 0; i < pma->n; ++i) {
6023 for (j = 0; j < pa->n; ++j) {
6024 isl_set *common;
6025 isl_multi_aff *res_ij;
6026 int empty;
6028 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6029 isl_set_copy(pa->p[j].set));
6030 empty = isl_set_plain_is_empty(common);
6031 if (empty < 0 || empty) {
6032 isl_set_free(common);
6033 if (empty < 0)
6034 goto error;
6035 continue;
6038 res_ij = isl_multi_aff_set_aff(
6039 isl_multi_aff_copy(pma->p[i].maff), pos,
6040 isl_aff_copy(pa->p[j].aff));
6041 res_ij = isl_multi_aff_gist(res_ij,
6042 isl_set_copy(common));
6044 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6048 isl_pw_multi_aff_free(pma);
6049 isl_pw_aff_free(pa);
6050 return res;
6051 error:
6052 isl_pw_multi_aff_free(pma);
6053 isl_pw_aff_free(pa);
6054 return isl_pw_multi_aff_free(res);
6057 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6059 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6060 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6061 __isl_take isl_pw_aff *pa)
6063 isl_bool equal_params;
6065 if (!pma || !pa)
6066 goto error;
6067 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6068 if (equal_params < 0)
6069 goto error;
6070 if (equal_params)
6071 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6072 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6073 isl_pw_aff_check_named_params(pa) < 0)
6074 goto error;
6075 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6076 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6077 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6078 error:
6079 isl_pw_multi_aff_free(pma);
6080 isl_pw_aff_free(pa);
6081 return NULL;
6084 /* Do the parameters of "pa" match those of "space"?
6086 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6087 __isl_keep isl_space *space)
6089 isl_space *pa_space;
6090 isl_bool match;
6092 if (!pa || !space)
6093 return isl_bool_error;
6095 pa_space = isl_pw_aff_get_space(pa);
6097 match = isl_space_has_equal_params(space, pa_space);
6099 isl_space_free(pa_space);
6100 return match;
6103 /* Check that the domain space of "pa" matches "space".
6105 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6106 __isl_keep isl_space *space)
6108 isl_space *pa_space;
6109 isl_bool match;
6111 if (!pa || !space)
6112 return isl_stat_error;
6114 pa_space = isl_pw_aff_get_space(pa);
6116 match = isl_space_has_equal_params(space, pa_space);
6117 if (match < 0)
6118 goto error;
6119 if (!match)
6120 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6121 "parameters don't match", goto error);
6122 match = isl_space_tuple_is_equal(space, isl_dim_in,
6123 pa_space, isl_dim_in);
6124 if (match < 0)
6125 goto error;
6126 if (!match)
6127 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6128 "domains don't match", goto error);
6129 isl_space_free(pa_space);
6130 return isl_stat_ok;
6131 error:
6132 isl_space_free(pa_space);
6133 return isl_stat_error;
6136 #undef BASE
6137 #define BASE pw_aff
6138 #undef DOMBASE
6139 #define DOMBASE set
6141 #include <isl_multi_explicit_domain.c>
6142 #include <isl_multi_pw_aff_explicit_domain.c>
6143 #include <isl_multi_templ.c>
6144 #include <isl_multi_apply_set.c>
6145 #include <isl_multi_coalesce.c>
6146 #include <isl_multi_domain_templ.c>
6147 #include <isl_multi_dims.c>
6148 #include <isl_multi_from_base_templ.c>
6149 #include <isl_multi_gist.c>
6150 #include <isl_multi_hash.c>
6151 #include <isl_multi_identity_templ.c>
6152 #include <isl_multi_align_set.c>
6153 #include <isl_multi_intersect.c>
6154 #include <isl_multi_move_dims_templ.c>
6155 #include <isl_multi_product_templ.c>
6156 #include <isl_multi_splice_templ.c>
6157 #include <isl_multi_zero_templ.c>
6159 /* Does "mpa" have a non-trivial explicit domain?
6161 * The explicit domain, if present, is trivial if it represents
6162 * an (obviously) universe set.
6164 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6165 __isl_keep isl_multi_pw_aff *mpa)
6167 if (!mpa)
6168 return isl_bool_error;
6169 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6170 return isl_bool_false;
6171 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6174 /* Scale the elements of "pma" by the corresponding elements of "mv".
6176 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6177 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6179 int i;
6180 isl_bool equal_params;
6182 pma = isl_pw_multi_aff_cow(pma);
6183 if (!pma || !mv)
6184 goto error;
6185 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6186 mv->space, isl_dim_set))
6187 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6188 "spaces don't match", goto error);
6189 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6190 if (equal_params < 0)
6191 goto error;
6192 if (!equal_params) {
6193 pma = isl_pw_multi_aff_align_params(pma,
6194 isl_multi_val_get_space(mv));
6195 mv = isl_multi_val_align_params(mv,
6196 isl_pw_multi_aff_get_space(pma));
6197 if (!pma || !mv)
6198 goto error;
6201 for (i = 0; i < pma->n; ++i) {
6202 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6203 isl_multi_val_copy(mv));
6204 if (!pma->p[i].maff)
6205 goto error;
6208 isl_multi_val_free(mv);
6209 return pma;
6210 error:
6211 isl_multi_val_free(mv);
6212 isl_pw_multi_aff_free(pma);
6213 return NULL;
6216 /* This function is called for each entry of an isl_union_pw_multi_aff.
6217 * If the space of the entry matches that of data->mv,
6218 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6219 * Otherwise, return an empty isl_pw_multi_aff.
6221 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6222 __isl_take isl_pw_multi_aff *pma, void *user)
6224 isl_multi_val *mv = user;
6226 if (!pma)
6227 return NULL;
6228 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6229 mv->space, isl_dim_set)) {
6230 isl_space *space = isl_pw_multi_aff_get_space(pma);
6231 isl_pw_multi_aff_free(pma);
6232 return isl_pw_multi_aff_empty(space);
6235 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6238 /* Scale the elements of "upma" by the corresponding elements of "mv",
6239 * for those entries that match the space of "mv".
6241 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6242 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6244 upma = isl_union_pw_multi_aff_align_params(upma,
6245 isl_multi_val_get_space(mv));
6246 mv = isl_multi_val_align_params(mv,
6247 isl_union_pw_multi_aff_get_space(upma));
6248 if (!upma || !mv)
6249 goto error;
6251 return isl_union_pw_multi_aff_transform(upma,
6252 &union_pw_multi_aff_scale_multi_val_entry, mv);
6254 isl_multi_val_free(mv);
6255 return upma;
6256 error:
6257 isl_multi_val_free(mv);
6258 isl_union_pw_multi_aff_free(upma);
6259 return NULL;
6262 /* Construct and return a piecewise multi affine expression
6263 * in the given space with value zero in each of the output dimensions and
6264 * a universe domain.
6266 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6268 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6271 /* Construct and return a piecewise multi affine expression
6272 * that is equal to the given piecewise affine expression.
6274 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6275 __isl_take isl_pw_aff *pa)
6277 int i;
6278 isl_space *space;
6279 isl_pw_multi_aff *pma;
6281 if (!pa)
6282 return NULL;
6284 space = isl_pw_aff_get_space(pa);
6285 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6287 for (i = 0; i < pa->n; ++i) {
6288 isl_set *set;
6289 isl_multi_aff *ma;
6291 set = isl_set_copy(pa->p[i].set);
6292 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6293 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6296 isl_pw_aff_free(pa);
6297 return pma;
6300 /* Construct and return a piecewise multi affine expression
6301 * that is equal to the given multi piecewise affine expression
6302 * on the shared domain of the piecewise affine expressions,
6303 * in the special case of a 0D multi piecewise affine expression.
6305 * Create a piecewise multi affine expression with the explicit domain of
6306 * the 0D multi piecewise affine expression as domain.
6308 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6309 __isl_take isl_multi_pw_aff *mpa)
6311 isl_space *space;
6312 isl_set *dom;
6313 isl_multi_aff *ma;
6315 space = isl_multi_pw_aff_get_space(mpa);
6316 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6317 isl_multi_pw_aff_free(mpa);
6319 ma = isl_multi_aff_zero(space);
6320 return isl_pw_multi_aff_alloc(dom, ma);
6323 /* Construct and return a piecewise multi affine expression
6324 * that is equal to the given multi piecewise affine expression
6325 * on the shared domain of the piecewise affine expressions.
6327 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6328 __isl_take isl_multi_pw_aff *mpa)
6330 int i;
6331 isl_space *space;
6332 isl_pw_aff *pa;
6333 isl_pw_multi_aff *pma;
6335 if (!mpa)
6336 return NULL;
6338 if (mpa->n == 0)
6339 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6341 space = isl_multi_pw_aff_get_space(mpa);
6342 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6343 pma = isl_pw_multi_aff_from_pw_aff(pa);
6345 for (i = 1; i < mpa->n; ++i) {
6346 isl_pw_multi_aff *pma_i;
6348 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6349 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6350 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6353 pma = isl_pw_multi_aff_reset_space(pma, space);
6355 isl_multi_pw_aff_free(mpa);
6356 return pma;
6359 /* Construct and return a multi piecewise affine expression
6360 * that is equal to the given multi affine expression.
6362 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6363 __isl_take isl_multi_aff *ma)
6365 int i, n;
6366 isl_multi_pw_aff *mpa;
6368 if (!ma)
6369 return NULL;
6371 n = isl_multi_aff_dim(ma, isl_dim_out);
6372 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6374 for (i = 0; i < n; ++i) {
6375 isl_pw_aff *pa;
6377 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6378 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6381 isl_multi_aff_free(ma);
6382 return mpa;
6385 /* Construct and return a multi piecewise affine expression
6386 * that is equal to the given piecewise multi affine expression.
6388 * If the resulting multi piecewise affine expression has
6389 * an explicit domain, then assign it the domain of the input.
6390 * In other cases, the domain is stored in the individual elements.
6392 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6393 __isl_take isl_pw_multi_aff *pma)
6395 int i, n;
6396 isl_space *space;
6397 isl_multi_pw_aff *mpa;
6399 if (!pma)
6400 return NULL;
6402 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6403 space = isl_pw_multi_aff_get_space(pma);
6404 mpa = isl_multi_pw_aff_alloc(space);
6406 for (i = 0; i < n; ++i) {
6407 isl_pw_aff *pa;
6409 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6410 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6412 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6413 isl_set *dom;
6415 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6416 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6419 isl_pw_multi_aff_free(pma);
6420 return mpa;
6423 /* Do "pa1" and "pa2" represent the same function?
6425 * We first check if they are obviously equal.
6426 * If not, we convert them to maps and check if those are equal.
6428 * If "pa1" or "pa2" contain any NaNs, then they are considered
6429 * not to be the same. A NaN is not equal to anything, not even
6430 * to another NaN.
6432 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6433 __isl_keep isl_pw_aff *pa2)
6435 isl_bool equal;
6436 isl_bool has_nan;
6437 isl_map *map1, *map2;
6439 if (!pa1 || !pa2)
6440 return isl_bool_error;
6442 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6443 if (equal < 0 || equal)
6444 return equal;
6445 has_nan = either_involves_nan(pa1, pa2);
6446 if (has_nan < 0)
6447 return isl_bool_error;
6448 if (has_nan)
6449 return isl_bool_false;
6451 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6452 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6453 equal = isl_map_is_equal(map1, map2);
6454 isl_map_free(map1);
6455 isl_map_free(map2);
6457 return equal;
6460 /* Do "mpa1" and "mpa2" represent the same function?
6462 * Note that we cannot convert the entire isl_multi_pw_aff
6463 * to a map because the domains of the piecewise affine expressions
6464 * may not be the same.
6466 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6467 __isl_keep isl_multi_pw_aff *mpa2)
6469 int i;
6470 isl_bool equal, equal_params;
6472 if (!mpa1 || !mpa2)
6473 return isl_bool_error;
6475 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6476 if (equal_params < 0)
6477 return isl_bool_error;
6478 if (!equal_params) {
6479 if (!isl_space_has_named_params(mpa1->space))
6480 return isl_bool_false;
6481 if (!isl_space_has_named_params(mpa2->space))
6482 return isl_bool_false;
6483 mpa1 = isl_multi_pw_aff_copy(mpa1);
6484 mpa2 = isl_multi_pw_aff_copy(mpa2);
6485 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6486 isl_multi_pw_aff_get_space(mpa2));
6487 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6488 isl_multi_pw_aff_get_space(mpa1));
6489 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6490 isl_multi_pw_aff_free(mpa1);
6491 isl_multi_pw_aff_free(mpa2);
6492 return equal;
6495 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6496 if (equal < 0 || !equal)
6497 return equal;
6499 for (i = 0; i < mpa1->n; ++i) {
6500 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6501 if (equal < 0 || !equal)
6502 return equal;
6505 return isl_bool_true;
6508 /* Do "pma1" and "pma2" represent the same function?
6510 * First check if they are obviously equal.
6511 * If not, then convert them to maps and check if those are equal.
6513 * If "pa1" or "pa2" contain any NaNs, then they are considered
6514 * not to be the same. A NaN is not equal to anything, not even
6515 * to another NaN.
6517 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6518 __isl_keep isl_pw_multi_aff *pma2)
6520 isl_bool equal;
6521 isl_bool has_nan;
6522 isl_map *map1, *map2;
6524 if (!pma1 || !pma2)
6525 return isl_bool_error;
6527 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6528 if (equal < 0 || equal)
6529 return equal;
6530 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6531 if (has_nan >= 0 && !has_nan)
6532 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6533 if (has_nan < 0 || has_nan)
6534 return isl_bool_not(has_nan);
6536 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6537 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6538 equal = isl_map_is_equal(map1, map2);
6539 isl_map_free(map1);
6540 isl_map_free(map2);
6542 return equal;
6545 /* Compute the pullback of "mpa" by the function represented by "ma".
6546 * In other words, plug in "ma" in "mpa".
6548 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6550 * If "mpa" has an explicit domain, then it is this domain
6551 * that needs to undergo a pullback, i.e., a preimage.
6553 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6554 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6556 int i;
6557 isl_space *space = NULL;
6559 mpa = isl_multi_pw_aff_cow(mpa);
6560 if (!mpa || !ma)
6561 goto error;
6563 space = isl_space_join(isl_multi_aff_get_space(ma),
6564 isl_multi_pw_aff_get_space(mpa));
6565 if (!space)
6566 goto error;
6568 for (i = 0; i < mpa->n; ++i) {
6569 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6570 isl_multi_aff_copy(ma));
6571 if (!mpa->u.p[i])
6572 goto error;
6574 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6575 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6576 isl_multi_aff_copy(ma));
6577 if (!mpa->u.dom)
6578 goto error;
6581 isl_multi_aff_free(ma);
6582 isl_space_free(mpa->space);
6583 mpa->space = space;
6584 return mpa;
6585 error:
6586 isl_space_free(space);
6587 isl_multi_pw_aff_free(mpa);
6588 isl_multi_aff_free(ma);
6589 return NULL;
6592 /* Compute the pullback of "mpa" by the function represented by "ma".
6593 * In other words, plug in "ma" in "mpa".
6595 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6596 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6598 isl_bool equal_params;
6600 if (!mpa || !ma)
6601 goto error;
6602 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6603 if (equal_params < 0)
6604 goto error;
6605 if (equal_params)
6606 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6607 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6608 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6609 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6610 error:
6611 isl_multi_pw_aff_free(mpa);
6612 isl_multi_aff_free(ma);
6613 return NULL;
6616 /* Compute the pullback of "mpa" by the function represented by "pma".
6617 * In other words, plug in "pma" in "mpa".
6619 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6621 * If "mpa" has an explicit domain, then it is this domain
6622 * that needs to undergo a pullback, i.e., a preimage.
6624 static __isl_give isl_multi_pw_aff *
6625 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6626 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6628 int i;
6629 isl_space *space = NULL;
6631 mpa = isl_multi_pw_aff_cow(mpa);
6632 if (!mpa || !pma)
6633 goto error;
6635 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6636 isl_multi_pw_aff_get_space(mpa));
6638 for (i = 0; i < mpa->n; ++i) {
6639 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6640 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6641 if (!mpa->u.p[i])
6642 goto error;
6644 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6645 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6646 isl_pw_multi_aff_copy(pma));
6647 if (!mpa->u.dom)
6648 goto error;
6651 isl_pw_multi_aff_free(pma);
6652 isl_space_free(mpa->space);
6653 mpa->space = space;
6654 return mpa;
6655 error:
6656 isl_space_free(space);
6657 isl_multi_pw_aff_free(mpa);
6658 isl_pw_multi_aff_free(pma);
6659 return NULL;
6662 /* Compute the pullback of "mpa" by the function represented by "pma".
6663 * In other words, plug in "pma" in "mpa".
6665 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6666 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6668 isl_bool equal_params;
6670 if (!mpa || !pma)
6671 goto error;
6672 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6673 if (equal_params < 0)
6674 goto error;
6675 if (equal_params)
6676 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6677 mpa = isl_multi_pw_aff_align_params(mpa,
6678 isl_pw_multi_aff_get_space(pma));
6679 pma = isl_pw_multi_aff_align_params(pma,
6680 isl_multi_pw_aff_get_space(mpa));
6681 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6682 error:
6683 isl_multi_pw_aff_free(mpa);
6684 isl_pw_multi_aff_free(pma);
6685 return NULL;
6688 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6689 * with the domain of "aff". The domain of the result is the same
6690 * as that of "mpa".
6691 * "mpa" and "aff" are assumed to have been aligned.
6693 * We first extract the parametric constant from "aff", defined
6694 * over the correct domain.
6695 * Then we add the appropriate combinations of the members of "mpa".
6696 * Finally, we add the integer divisions through recursive calls.
6698 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6699 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6701 int i, n_in, n_div;
6702 isl_space *space;
6703 isl_val *v;
6704 isl_pw_aff *pa;
6705 isl_aff *tmp;
6707 n_in = isl_aff_dim(aff, isl_dim_in);
6708 n_div = isl_aff_dim(aff, isl_dim_div);
6710 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6711 tmp = isl_aff_copy(aff);
6712 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6713 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6714 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6715 isl_space_dim(space, isl_dim_set));
6716 tmp = isl_aff_reset_domain_space(tmp, space);
6717 pa = isl_pw_aff_from_aff(tmp);
6719 for (i = 0; i < n_in; ++i) {
6720 isl_pw_aff *pa_i;
6722 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6723 continue;
6724 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6725 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6726 pa_i = isl_pw_aff_scale_val(pa_i, v);
6727 pa = isl_pw_aff_add(pa, pa_i);
6730 for (i = 0; i < n_div; ++i) {
6731 isl_aff *div;
6732 isl_pw_aff *pa_i;
6734 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6735 continue;
6736 div = isl_aff_get_div(aff, i);
6737 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6738 isl_multi_pw_aff_copy(mpa), div);
6739 pa_i = isl_pw_aff_floor(pa_i);
6740 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6741 pa_i = isl_pw_aff_scale_val(pa_i, v);
6742 pa = isl_pw_aff_add(pa, pa_i);
6745 isl_multi_pw_aff_free(mpa);
6746 isl_aff_free(aff);
6748 return pa;
6751 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6752 * with the domain of "aff". The domain of the result is the same
6753 * as that of "mpa".
6755 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6756 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6758 isl_bool equal_params;
6760 if (!aff || !mpa)
6761 goto error;
6762 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6763 if (equal_params < 0)
6764 goto error;
6765 if (equal_params)
6766 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6768 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6769 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6771 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6772 error:
6773 isl_aff_free(aff);
6774 isl_multi_pw_aff_free(mpa);
6775 return NULL;
6778 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6779 * with the domain of "pa". The domain of the result is the same
6780 * as that of "mpa".
6781 * "mpa" and "pa" are assumed to have been aligned.
6783 * We consider each piece in turn. Note that the domains of the
6784 * pieces are assumed to be disjoint and they remain disjoint
6785 * after taking the preimage (over the same function).
6787 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6788 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6790 isl_space *space;
6791 isl_pw_aff *res;
6792 int i;
6794 if (!mpa || !pa)
6795 goto error;
6797 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6798 isl_pw_aff_get_space(pa));
6799 res = isl_pw_aff_empty(space);
6801 for (i = 0; i < pa->n; ++i) {
6802 isl_pw_aff *pa_i;
6803 isl_set *domain;
6805 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6806 isl_multi_pw_aff_copy(mpa),
6807 isl_aff_copy(pa->p[i].aff));
6808 domain = isl_set_copy(pa->p[i].set);
6809 domain = isl_set_preimage_multi_pw_aff(domain,
6810 isl_multi_pw_aff_copy(mpa));
6811 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6812 res = isl_pw_aff_add_disjoint(res, pa_i);
6815 isl_pw_aff_free(pa);
6816 isl_multi_pw_aff_free(mpa);
6817 return res;
6818 error:
6819 isl_pw_aff_free(pa);
6820 isl_multi_pw_aff_free(mpa);
6821 return NULL;
6824 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6825 * with the domain of "pa". The domain of the result is the same
6826 * as that of "mpa".
6828 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6829 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6831 isl_bool equal_params;
6833 if (!pa || !mpa)
6834 goto error;
6835 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6836 if (equal_params < 0)
6837 goto error;
6838 if (equal_params)
6839 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6841 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6842 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6844 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6845 error:
6846 isl_pw_aff_free(pa);
6847 isl_multi_pw_aff_free(mpa);
6848 return NULL;
6851 /* Compute the pullback of "pa" by the function represented by "mpa".
6852 * In other words, plug in "mpa" in "pa".
6853 * "pa" and "mpa" are assumed to have been aligned.
6855 * The pullback is computed by applying "pa" to "mpa".
6857 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6858 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6860 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6863 /* Compute the pullback of "pa" by the function represented by "mpa".
6864 * In other words, plug in "mpa" in "pa".
6866 * The pullback is computed by applying "pa" to "mpa".
6868 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6869 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6871 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6874 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6875 * In other words, plug in "mpa2" in "mpa1".
6877 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6879 * We pullback each member of "mpa1" in turn.
6881 * If "mpa1" has an explicit domain, then it is this domain
6882 * that needs to undergo a pullback instead, i.e., a preimage.
6884 static __isl_give isl_multi_pw_aff *
6885 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6886 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6888 int i;
6889 isl_space *space = NULL;
6891 mpa1 = isl_multi_pw_aff_cow(mpa1);
6892 if (!mpa1 || !mpa2)
6893 goto error;
6895 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6896 isl_multi_pw_aff_get_space(mpa1));
6898 for (i = 0; i < mpa1->n; ++i) {
6899 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6900 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
6901 if (!mpa1->u.p[i])
6902 goto error;
6905 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
6906 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
6907 isl_multi_pw_aff_copy(mpa2));
6908 if (!mpa1->u.dom)
6909 goto error;
6911 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6913 isl_multi_pw_aff_free(mpa2);
6914 return mpa1;
6915 error:
6916 isl_space_free(space);
6917 isl_multi_pw_aff_free(mpa1);
6918 isl_multi_pw_aff_free(mpa2);
6919 return NULL;
6922 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6923 * In other words, plug in "mpa2" in "mpa1".
6925 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6926 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6928 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6929 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6932 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6933 * of "mpa1" and "mpa2" live in the same space, construct map space
6934 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6935 * with this map space as extract argument.
6937 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6938 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6939 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6940 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6942 int match;
6943 isl_space *space1, *space2;
6944 isl_map *res;
6946 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6947 isl_multi_pw_aff_get_space(mpa2));
6948 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6949 isl_multi_pw_aff_get_space(mpa1));
6950 if (!mpa1 || !mpa2)
6951 goto error;
6952 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6953 mpa2->space, isl_dim_out);
6954 if (match < 0)
6955 goto error;
6956 if (!match)
6957 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6958 "range spaces don't match", goto error);
6959 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6960 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6961 space1 = isl_space_map_from_domain_and_range(space1, space2);
6963 res = order(mpa1, mpa2, space1);
6964 isl_multi_pw_aff_free(mpa1);
6965 isl_multi_pw_aff_free(mpa2);
6966 return res;
6967 error:
6968 isl_multi_pw_aff_free(mpa1);
6969 isl_multi_pw_aff_free(mpa2);
6970 return NULL;
6973 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6974 * where the function values are equal. "space" is the space of the result.
6975 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6977 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6978 * in the sequences are equal.
6980 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6981 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6982 __isl_take isl_space *space)
6984 int i, n;
6985 isl_map *res;
6987 res = isl_map_universe(space);
6989 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6990 for (i = 0; i < n; ++i) {
6991 isl_pw_aff *pa1, *pa2;
6992 isl_map *map;
6994 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6995 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6996 map = isl_pw_aff_eq_map(pa1, pa2);
6997 res = isl_map_intersect(res, map);
7000 return res;
7003 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7004 * where the function values are equal.
7006 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7007 __isl_take isl_multi_pw_aff *mpa2)
7009 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7010 &isl_multi_pw_aff_eq_map_on_space);
7013 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7014 * where the function values of "mpa1" is lexicographically satisfies "base"
7015 * compared to that of "mpa2". "space" is the space of the result.
7016 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7018 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7019 * if its i-th element satisfies "base" when compared to
7020 * the i-th element of "mpa2" while all previous elements are
7021 * pairwise equal.
7023 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7024 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7025 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7026 __isl_take isl_pw_aff *pa2),
7027 __isl_take isl_space *space)
7029 int i, n;
7030 isl_map *res, *rest;
7032 res = isl_map_empty(isl_space_copy(space));
7033 rest = isl_map_universe(space);
7035 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7036 for (i = 0; i < n; ++i) {
7037 isl_pw_aff *pa1, *pa2;
7038 isl_map *map;
7040 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7041 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7042 map = base(pa1, pa2);
7043 map = isl_map_intersect(map, isl_map_copy(rest));
7044 res = isl_map_union(res, map);
7046 if (i == n - 1)
7047 continue;
7049 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7050 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7051 map = isl_pw_aff_eq_map(pa1, pa2);
7052 rest = isl_map_intersect(rest, map);
7055 isl_map_free(rest);
7056 return res;
7059 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7060 * where the function value of "mpa1" is lexicographically less than that
7061 * of "mpa2". "space" is the space of the result.
7062 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7064 * "mpa1" is less than "mpa2" if its i-th element is smaller
7065 * than the i-th element of "mpa2" while all previous elements are
7066 * pairwise equal.
7068 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7069 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7070 __isl_take isl_space *space)
7072 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7073 &isl_pw_aff_lt_map, space);
7076 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7077 * where the function value of "mpa1" is lexicographically less than that
7078 * of "mpa2".
7080 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7081 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7083 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7084 &isl_multi_pw_aff_lex_lt_map_on_space);
7087 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7088 * where the function value of "mpa1" is lexicographically greater than that
7089 * of "mpa2". "space" is the space of the result.
7090 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7092 * "mpa1" is greater than "mpa2" if its i-th element is greater
7093 * than the i-th element of "mpa2" while all previous elements are
7094 * pairwise equal.
7096 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7097 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7098 __isl_take isl_space *space)
7100 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7101 &isl_pw_aff_gt_map, space);
7104 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7105 * where the function value of "mpa1" is lexicographically greater than that
7106 * of "mpa2".
7108 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7109 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7111 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7112 &isl_multi_pw_aff_lex_gt_map_on_space);
7115 /* Compare two isl_affs.
7117 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7118 * than "aff2" and 0 if they are equal.
7120 * The order is fairly arbitrary. We do consider expressions that only involve
7121 * earlier dimensions as "smaller".
7123 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7125 int cmp;
7126 int last1, last2;
7128 if (aff1 == aff2)
7129 return 0;
7131 if (!aff1)
7132 return -1;
7133 if (!aff2)
7134 return 1;
7136 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7137 if (cmp != 0)
7138 return cmp;
7140 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7141 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7142 if (last1 != last2)
7143 return last1 - last2;
7145 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7148 /* Compare two isl_pw_affs.
7150 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7151 * than "pa2" and 0 if they are equal.
7153 * The order is fairly arbitrary. We do consider expressions that only involve
7154 * earlier dimensions as "smaller".
7156 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7157 __isl_keep isl_pw_aff *pa2)
7159 int i;
7160 int cmp;
7162 if (pa1 == pa2)
7163 return 0;
7165 if (!pa1)
7166 return -1;
7167 if (!pa2)
7168 return 1;
7170 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7171 if (cmp != 0)
7172 return cmp;
7174 if (pa1->n != pa2->n)
7175 return pa1->n - pa2->n;
7177 for (i = 0; i < pa1->n; ++i) {
7178 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7179 if (cmp != 0)
7180 return cmp;
7181 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7182 if (cmp != 0)
7183 return cmp;
7186 return 0;
7189 /* Return a piecewise affine expression that is equal to "v" on "domain".
7191 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7192 __isl_take isl_val *v)
7194 isl_space *space;
7195 isl_local_space *ls;
7196 isl_aff *aff;
7198 space = isl_set_get_space(domain);
7199 ls = isl_local_space_from_space(space);
7200 aff = isl_aff_val_on_domain(ls, v);
7202 return isl_pw_aff_alloc(domain, aff);
7205 /* Return a multi affine expression that is equal to "mv" on domain
7206 * space "space".
7208 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7209 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7211 int i, n;
7212 isl_space *space2;
7213 isl_local_space *ls;
7214 isl_multi_aff *ma;
7216 if (!space || !mv)
7217 goto error;
7219 n = isl_multi_val_dim(mv, isl_dim_set);
7220 space2 = isl_multi_val_get_space(mv);
7221 space2 = isl_space_align_params(space2, isl_space_copy(space));
7222 space = isl_space_align_params(space, isl_space_copy(space2));
7223 space = isl_space_map_from_domain_and_range(space, space2);
7224 ma = isl_multi_aff_alloc(isl_space_copy(space));
7225 ls = isl_local_space_from_space(isl_space_domain(space));
7226 for (i = 0; i < n; ++i) {
7227 isl_val *v;
7228 isl_aff *aff;
7230 v = isl_multi_val_get_val(mv, i);
7231 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7232 ma = isl_multi_aff_set_aff(ma, i, aff);
7234 isl_local_space_free(ls);
7236 isl_multi_val_free(mv);
7237 return ma;
7238 error:
7239 isl_space_free(space);
7240 isl_multi_val_free(mv);
7241 return NULL;
7244 /* Return a piecewise multi-affine expression
7245 * that is equal to "mv" on "domain".
7247 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7248 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7250 isl_space *space;
7251 isl_multi_aff *ma;
7253 space = isl_set_get_space(domain);
7254 ma = isl_multi_aff_multi_val_on_space(space, mv);
7256 return isl_pw_multi_aff_alloc(domain, ma);
7259 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7260 * mv is the value that should be attained on each domain set
7261 * res collects the results
7263 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7264 isl_multi_val *mv;
7265 isl_union_pw_multi_aff *res;
7268 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7269 * and add it to data->res.
7271 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7272 void *user)
7274 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7275 isl_pw_multi_aff *pma;
7276 isl_multi_val *mv;
7278 mv = isl_multi_val_copy(data->mv);
7279 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7280 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7282 return data->res ? isl_stat_ok : isl_stat_error;
7285 /* Return a union piecewise multi-affine expression
7286 * that is equal to "mv" on "domain".
7288 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7289 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7291 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7292 isl_space *space;
7294 space = isl_union_set_get_space(domain);
7295 data.res = isl_union_pw_multi_aff_empty(space);
7296 data.mv = mv;
7297 if (isl_union_set_foreach_set(domain,
7298 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7299 data.res = isl_union_pw_multi_aff_free(data.res);
7300 isl_union_set_free(domain);
7301 isl_multi_val_free(mv);
7302 return data.res;
7305 /* Compute the pullback of data->pma by the function represented by "pma2",
7306 * provided the spaces match, and add the results to data->res.
7308 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7310 struct isl_union_pw_multi_aff_bin_data *data = user;
7312 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7313 pma2->dim, isl_dim_out)) {
7314 isl_pw_multi_aff_free(pma2);
7315 return isl_stat_ok;
7318 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7319 isl_pw_multi_aff_copy(data->pma), pma2);
7321 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7322 if (!data->res)
7323 return isl_stat_error;
7325 return isl_stat_ok;
7328 /* Compute the pullback of "upma1" by the function represented by "upma2".
7330 __isl_give isl_union_pw_multi_aff *
7331 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7332 __isl_take isl_union_pw_multi_aff *upma1,
7333 __isl_take isl_union_pw_multi_aff *upma2)
7335 return bin_op(upma1, upma2, &pullback_entry);
7338 /* Check that the domain space of "upa" matches "space".
7340 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7341 * can in principle never fail since the space "space" is that
7342 * of the isl_multi_union_pw_aff and is a set space such that
7343 * there is no domain space to match.
7345 * We check the parameters and double-check that "space" is
7346 * indeed that of a set.
7348 static isl_stat isl_union_pw_aff_check_match_domain_space(
7349 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7351 isl_space *upa_space;
7352 isl_bool match;
7354 if (!upa || !space)
7355 return isl_stat_error;
7357 match = isl_space_is_set(space);
7358 if (match < 0)
7359 return isl_stat_error;
7360 if (!match)
7361 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7362 "expecting set space", return isl_stat_error);
7364 upa_space = isl_union_pw_aff_get_space(upa);
7365 match = isl_space_has_equal_params(space, upa_space);
7366 if (match < 0)
7367 goto error;
7368 if (!match)
7369 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7370 "parameters don't match", goto error);
7372 isl_space_free(upa_space);
7373 return isl_stat_ok;
7374 error:
7375 isl_space_free(upa_space);
7376 return isl_stat_error;
7379 /* Do the parameters of "upa" match those of "space"?
7381 static isl_bool isl_union_pw_aff_matching_params(
7382 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7384 isl_space *upa_space;
7385 isl_bool match;
7387 if (!upa || !space)
7388 return isl_bool_error;
7390 upa_space = isl_union_pw_aff_get_space(upa);
7392 match = isl_space_has_equal_params(space, upa_space);
7394 isl_space_free(upa_space);
7395 return match;
7398 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7399 * space represents the new parameters.
7400 * res collects the results.
7402 struct isl_union_pw_aff_reset_params_data {
7403 isl_space *space;
7404 isl_union_pw_aff *res;
7407 /* Replace the parameters of "pa" by data->space and
7408 * add the result to data->res.
7410 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7412 struct isl_union_pw_aff_reset_params_data *data = user;
7413 isl_space *space;
7415 space = isl_pw_aff_get_space(pa);
7416 space = isl_space_replace_params(space, data->space);
7417 pa = isl_pw_aff_reset_space(pa, space);
7418 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7420 return data->res ? isl_stat_ok : isl_stat_error;
7423 /* Replace the domain space of "upa" by "space".
7424 * Since a union expression does not have a (single) domain space,
7425 * "space" is necessarily a parameter space.
7427 * Since the order and the names of the parameters determine
7428 * the hash value, we need to create a new hash table.
7430 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7431 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7433 struct isl_union_pw_aff_reset_params_data data = { space };
7434 isl_bool match;
7436 match = isl_union_pw_aff_matching_params(upa, space);
7437 if (match < 0)
7438 upa = isl_union_pw_aff_free(upa);
7439 else if (match) {
7440 isl_space_free(space);
7441 return upa;
7444 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7445 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7446 data.res = isl_union_pw_aff_free(data.res);
7448 isl_union_pw_aff_free(upa);
7449 isl_space_free(space);
7450 return data.res;
7453 /* Return the floor of "pa".
7455 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7457 return isl_pw_aff_floor(pa);
7460 /* Given f, return floor(f).
7462 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7463 __isl_take isl_union_pw_aff *upa)
7465 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7468 /* Compute
7470 * upa mod m = upa - m * floor(upa/m)
7472 * with m an integer value.
7474 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7475 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7477 isl_union_pw_aff *res;
7479 if (!upa || !m)
7480 goto error;
7482 if (!isl_val_is_int(m))
7483 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7484 "expecting integer modulo", goto error);
7485 if (!isl_val_is_pos(m))
7486 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7487 "expecting positive modulo", goto error);
7489 res = isl_union_pw_aff_copy(upa);
7490 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7491 upa = isl_union_pw_aff_floor(upa);
7492 upa = isl_union_pw_aff_scale_val(upa, m);
7493 res = isl_union_pw_aff_sub(res, upa);
7495 return res;
7496 error:
7497 isl_val_free(m);
7498 isl_union_pw_aff_free(upa);
7499 return NULL;
7502 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7503 * pos is the output position that needs to be extracted.
7504 * res collects the results.
7506 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7507 int pos;
7508 isl_union_pw_aff *res;
7511 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7512 * (assuming it has such a dimension) and add it to data->res.
7514 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7516 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7517 int n_out;
7518 isl_pw_aff *pa;
7520 if (!pma)
7521 return isl_stat_error;
7523 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7524 if (data->pos >= n_out) {
7525 isl_pw_multi_aff_free(pma);
7526 return isl_stat_ok;
7529 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7530 isl_pw_multi_aff_free(pma);
7532 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7534 return data->res ? isl_stat_ok : isl_stat_error;
7537 /* Extract an isl_union_pw_aff corresponding to
7538 * output dimension "pos" of "upma".
7540 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7541 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7543 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7544 isl_space *space;
7546 if (!upma)
7547 return NULL;
7549 if (pos < 0)
7550 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7551 "cannot extract at negative position", return NULL);
7553 space = isl_union_pw_multi_aff_get_space(upma);
7554 data.res = isl_union_pw_aff_empty(space);
7555 data.pos = pos;
7556 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7557 &get_union_pw_aff, &data) < 0)
7558 data.res = isl_union_pw_aff_free(data.res);
7560 return data.res;
7563 /* Return a union piecewise affine expression
7564 * that is equal to "aff" on "domain".
7566 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7567 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7569 isl_pw_aff *pa;
7571 pa = isl_pw_aff_from_aff(aff);
7572 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7575 /* Return a union piecewise affine expression
7576 * that is equal to the parameter identified by "id" on "domain".
7578 * Make sure the parameter appears in the space passed to
7579 * isl_aff_param_on_domain_space_id.
7581 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7582 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7584 isl_space *space;
7585 isl_aff *aff;
7587 space = isl_union_set_get_space(domain);
7588 space = isl_space_add_param_id(space, isl_id_copy(id));
7589 aff = isl_aff_param_on_domain_space_id(space, id);
7590 return isl_union_pw_aff_aff_on_domain(domain, aff);
7593 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7594 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7595 * needs to attain.
7596 * "res" collects the results.
7598 struct isl_union_pw_aff_pw_aff_on_domain_data {
7599 isl_pw_aff *pa;
7600 isl_union_pw_aff *res;
7603 /* Construct a piecewise affine expression that is equal to data->pa
7604 * on "domain" and add the result to data->res.
7606 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7608 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7609 isl_pw_aff *pa;
7610 int dim;
7612 pa = isl_pw_aff_copy(data->pa);
7613 dim = isl_set_dim(domain, isl_dim_set);
7614 pa = isl_pw_aff_from_range(pa);
7615 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7616 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7617 pa = isl_pw_aff_intersect_domain(pa, domain);
7618 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7620 return data->res ? isl_stat_ok : isl_stat_error;
7623 /* Return a union piecewise affine expression
7624 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7625 * have been aligned.
7627 * Construct an isl_pw_aff on each of the sets in "domain" and
7628 * collect the results.
7630 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7631 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7633 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7634 isl_space *space;
7636 space = isl_union_set_get_space(domain);
7637 data.res = isl_union_pw_aff_empty(space);
7638 data.pa = pa;
7639 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7640 data.res = isl_union_pw_aff_free(data.res);
7641 isl_union_set_free(domain);
7642 isl_pw_aff_free(pa);
7643 return data.res;
7646 /* Return a union piecewise affine expression
7647 * that is equal to "pa" on "domain".
7649 * Check that "pa" is a parametric expression,
7650 * align the parameters if needed and call
7651 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7653 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7654 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7656 isl_bool is_set;
7657 isl_bool equal_params;
7658 isl_space *domain_space, *pa_space;
7660 pa_space = isl_pw_aff_peek_space(pa);
7661 is_set = isl_space_is_set(pa_space);
7662 if (is_set < 0)
7663 goto error;
7664 if (!is_set)
7665 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7666 "expecting parametric expression", goto error);
7668 domain_space = isl_union_set_get_space(domain);
7669 pa_space = isl_pw_aff_get_space(pa);
7670 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7671 if (equal_params >= 0 && !equal_params) {
7672 isl_space *space;
7674 space = isl_space_align_params(domain_space, pa_space);
7675 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7676 domain = isl_union_set_align_params(domain, space);
7677 } else {
7678 isl_space_free(domain_space);
7679 isl_space_free(pa_space);
7682 if (equal_params < 0)
7683 goto error;
7684 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7685 error:
7686 isl_union_set_free(domain);
7687 isl_pw_aff_free(pa);
7688 return NULL;
7691 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7692 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7693 * "res" collects the results.
7695 struct isl_union_pw_aff_val_on_domain_data {
7696 isl_val *v;
7697 isl_union_pw_aff *res;
7700 /* Construct a piecewise affine expression that is equal to data->v
7701 * on "domain" and add the result to data->res.
7703 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7705 struct isl_union_pw_aff_val_on_domain_data *data = user;
7706 isl_pw_aff *pa;
7707 isl_val *v;
7709 v = isl_val_copy(data->v);
7710 pa = isl_pw_aff_val_on_domain(domain, v);
7711 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7713 return data->res ? isl_stat_ok : isl_stat_error;
7716 /* Return a union piecewise affine expression
7717 * that is equal to "v" on "domain".
7719 * Construct an isl_pw_aff on each of the sets in "domain" and
7720 * collect the results.
7722 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7723 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7725 struct isl_union_pw_aff_val_on_domain_data data;
7726 isl_space *space;
7728 space = isl_union_set_get_space(domain);
7729 data.res = isl_union_pw_aff_empty(space);
7730 data.v = v;
7731 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7732 data.res = isl_union_pw_aff_free(data.res);
7733 isl_union_set_free(domain);
7734 isl_val_free(v);
7735 return data.res;
7738 /* Construct a piecewise multi affine expression
7739 * that is equal to "pa" and add it to upma.
7741 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7742 void *user)
7744 isl_union_pw_multi_aff **upma = user;
7745 isl_pw_multi_aff *pma;
7747 pma = isl_pw_multi_aff_from_pw_aff(pa);
7748 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7750 return *upma ? isl_stat_ok : isl_stat_error;
7753 /* Construct and return a union piecewise multi affine expression
7754 * that is equal to the given union piecewise affine expression.
7756 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7757 __isl_take isl_union_pw_aff *upa)
7759 isl_space *space;
7760 isl_union_pw_multi_aff *upma;
7762 if (!upa)
7763 return NULL;
7765 space = isl_union_pw_aff_get_space(upa);
7766 upma = isl_union_pw_multi_aff_empty(space);
7768 if (isl_union_pw_aff_foreach_pw_aff(upa,
7769 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7770 upma = isl_union_pw_multi_aff_free(upma);
7772 isl_union_pw_aff_free(upa);
7773 return upma;
7776 /* Compute the set of elements in the domain of "pa" where it is zero and
7777 * add this set to "uset".
7779 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7781 isl_union_set **uset = (isl_union_set **)user;
7783 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7785 return *uset ? isl_stat_ok : isl_stat_error;
7788 /* Return a union set containing those elements in the domain
7789 * of "upa" where it is zero.
7791 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7792 __isl_take isl_union_pw_aff *upa)
7794 isl_union_set *zero;
7796 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7797 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7798 zero = isl_union_set_free(zero);
7800 isl_union_pw_aff_free(upa);
7801 return zero;
7804 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7805 * upma is the function that is plugged in.
7806 * pa is the current part of the function in which upma is plugged in.
7807 * res collects the results.
7809 struct isl_union_pw_aff_pullback_upma_data {
7810 isl_union_pw_multi_aff *upma;
7811 isl_pw_aff *pa;
7812 isl_union_pw_aff *res;
7815 /* Check if "pma" can be plugged into data->pa.
7816 * If so, perform the pullback and add the result to data->res.
7818 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7820 struct isl_union_pw_aff_pullback_upma_data *data = user;
7821 isl_pw_aff *pa;
7823 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7824 pma->dim, isl_dim_out)) {
7825 isl_pw_multi_aff_free(pma);
7826 return isl_stat_ok;
7829 pa = isl_pw_aff_copy(data->pa);
7830 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7832 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7834 return data->res ? isl_stat_ok : isl_stat_error;
7837 /* Check if any of the elements of data->upma can be plugged into pa,
7838 * add if so add the result to data->res.
7840 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7842 struct isl_union_pw_aff_pullback_upma_data *data = user;
7843 isl_stat r;
7845 data->pa = pa;
7846 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7847 &pa_pb_pma, data);
7848 isl_pw_aff_free(pa);
7850 return r;
7853 /* Compute the pullback of "upa" by the function represented by "upma".
7854 * In other words, plug in "upma" in "upa". The result contains
7855 * expressions defined over the domain space of "upma".
7857 * Run over all pairs of elements in "upa" and "upma", perform
7858 * the pullback when appropriate and collect the results.
7859 * If the hash value were based on the domain space rather than
7860 * the function space, then we could run through all elements
7861 * of "upma" and directly pick out the corresponding element of "upa".
7863 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7864 __isl_take isl_union_pw_aff *upa,
7865 __isl_take isl_union_pw_multi_aff *upma)
7867 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7868 isl_space *space;
7870 space = isl_union_pw_multi_aff_get_space(upma);
7871 upa = isl_union_pw_aff_align_params(upa, space);
7872 space = isl_union_pw_aff_get_space(upa);
7873 upma = isl_union_pw_multi_aff_align_params(upma, space);
7875 if (!upa || !upma)
7876 goto error;
7878 data.upma = upma;
7879 data.res = isl_union_pw_aff_alloc_same_size(upa);
7880 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7881 data.res = isl_union_pw_aff_free(data.res);
7883 isl_union_pw_aff_free(upa);
7884 isl_union_pw_multi_aff_free(upma);
7885 return data.res;
7886 error:
7887 isl_union_pw_aff_free(upa);
7888 isl_union_pw_multi_aff_free(upma);
7889 return NULL;
7892 #undef BASE
7893 #define BASE union_pw_aff
7894 #undef DOMBASE
7895 #define DOMBASE union_set
7897 #include <isl_multi_explicit_domain.c>
7898 #include <isl_multi_union_pw_aff_explicit_domain.c>
7899 #include <isl_multi_templ.c>
7900 #include <isl_multi_apply_set.c>
7901 #include <isl_multi_apply_union_set.c>
7902 #include <isl_multi_coalesce.c>
7903 #include <isl_multi_floor.c>
7904 #include <isl_multi_from_base_templ.c>
7905 #include <isl_multi_gist.c>
7906 #include <isl_multi_align_set.c>
7907 #include <isl_multi_align_union_set.c>
7908 #include <isl_multi_intersect.c>
7910 /* Does "mupa" have a non-trivial explicit domain?
7912 * The explicit domain, if present, is trivial if it represents
7913 * an (obviously) universe parameter set.
7915 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
7916 __isl_keep isl_multi_union_pw_aff *mupa)
7918 isl_bool is_params, trivial;
7919 isl_set *set;
7921 if (!mupa)
7922 return isl_bool_error;
7923 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
7924 return isl_bool_false;
7925 is_params = isl_union_set_is_params(mupa->u.dom);
7926 if (is_params < 0 || !is_params)
7927 return isl_bool_not(is_params);
7928 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
7929 trivial = isl_set_plain_is_universe(set);
7930 isl_set_free(set);
7931 return isl_bool_not(trivial);
7934 /* Construct a multiple union piecewise affine expression
7935 * in the given space with value zero in each of the output dimensions.
7937 * Since there is no canonical zero value for
7938 * a union piecewise affine expression, we can only construct
7939 * a zero-dimensional "zero" value.
7941 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7942 __isl_take isl_space *space)
7944 isl_bool params;
7946 if (!space)
7947 return NULL;
7949 params = isl_space_is_params(space);
7950 if (params < 0)
7951 goto error;
7952 if (params)
7953 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7954 "expecting proper set space", goto error);
7955 if (!isl_space_is_set(space))
7956 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7957 "expecting set space", goto error);
7958 if (isl_space_dim(space , isl_dim_out) != 0)
7959 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7960 "expecting 0D space", goto error);
7962 return isl_multi_union_pw_aff_alloc(space);
7963 error:
7964 isl_space_free(space);
7965 return NULL;
7968 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7969 * with the actual sum on the shared domain and
7970 * the defined expression on the symmetric difference of the domains.
7972 * We simply iterate over the elements in both arguments and
7973 * call isl_union_pw_aff_union_add on each of them, if there is
7974 * at least one element.
7976 * Otherwise, the two expressions have an explicit domain and
7977 * the union of these explicit domains is computed.
7978 * This assumes that the explicit domains are either both in terms
7979 * of specific domains elements or both in terms of parameters.
7980 * However, if one of the expressions does not have any constraints
7981 * on its explicit domain, then this is allowed as well and the result
7982 * is the expression with no constraints on its explicit domain.
7984 static __isl_give isl_multi_union_pw_aff *
7985 isl_multi_union_pw_aff_union_add_aligned(
7986 __isl_take isl_multi_union_pw_aff *mupa1,
7987 __isl_take isl_multi_union_pw_aff *mupa2)
7989 isl_bool has_domain, is_params1, is_params2;
7991 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
7992 goto error;
7993 if (mupa1->n > 0)
7994 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7995 &isl_union_pw_aff_union_add);
7996 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
7997 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
7998 goto error;
8000 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8001 if (has_domain < 0)
8002 goto error;
8003 if (!has_domain) {
8004 isl_multi_union_pw_aff_free(mupa2);
8005 return mupa1;
8007 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8008 if (has_domain < 0)
8009 goto error;
8010 if (!has_domain) {
8011 isl_multi_union_pw_aff_free(mupa1);
8012 return mupa2;
8015 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8016 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8017 if (is_params1 < 0 || is_params2 < 0)
8018 goto error;
8019 if (is_params1 != is_params2)
8020 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8021 isl_error_invalid,
8022 "cannot compute union of concrete domain and "
8023 "parameter constraints", goto error);
8024 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8025 if (!mupa1)
8026 goto error;
8027 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8028 isl_union_set_copy(mupa2->u.dom));
8029 if (!mupa1->u.dom)
8030 goto error;
8031 isl_multi_union_pw_aff_free(mupa2);
8032 return mupa1;
8033 error:
8034 isl_multi_union_pw_aff_free(mupa1);
8035 isl_multi_union_pw_aff_free(mupa2);
8036 return NULL;
8039 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8040 * with the actual sum on the shared domain and
8041 * the defined expression on the symmetric difference of the domains.
8043 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8044 __isl_take isl_multi_union_pw_aff *mupa1,
8045 __isl_take isl_multi_union_pw_aff *mupa2)
8047 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8048 &isl_multi_union_pw_aff_union_add_aligned);
8051 /* Construct and return a multi union piecewise affine expression
8052 * that is equal to the given multi affine expression.
8054 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8055 __isl_take isl_multi_aff *ma)
8057 isl_multi_pw_aff *mpa;
8059 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8060 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8063 /* Construct and return a multi union piecewise affine expression
8064 * that is equal to the given multi piecewise affine expression.
8066 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8067 __isl_take isl_multi_pw_aff *mpa)
8069 int i, n;
8070 isl_space *space;
8071 isl_multi_union_pw_aff *mupa;
8073 if (!mpa)
8074 return NULL;
8076 space = isl_multi_pw_aff_get_space(mpa);
8077 space = isl_space_range(space);
8078 mupa = isl_multi_union_pw_aff_alloc(space);
8080 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8081 for (i = 0; i < n; ++i) {
8082 isl_pw_aff *pa;
8083 isl_union_pw_aff *upa;
8085 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8086 upa = isl_union_pw_aff_from_pw_aff(pa);
8087 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8090 isl_multi_pw_aff_free(mpa);
8092 return mupa;
8095 /* Extract the range space of "pma" and assign it to *space.
8096 * If *space has already been set (through a previous call to this function),
8097 * then check that the range space is the same.
8099 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8101 isl_space **space = user;
8102 isl_space *pma_space;
8103 isl_bool equal;
8105 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8106 isl_pw_multi_aff_free(pma);
8108 if (!pma_space)
8109 return isl_stat_error;
8110 if (!*space) {
8111 *space = pma_space;
8112 return isl_stat_ok;
8115 equal = isl_space_is_equal(pma_space, *space);
8116 isl_space_free(pma_space);
8118 if (equal < 0)
8119 return isl_stat_error;
8120 if (!equal)
8121 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8122 "range spaces not the same", return isl_stat_error);
8123 return isl_stat_ok;
8126 /* Construct and return a multi union piecewise affine expression
8127 * that is equal to the given union piecewise multi affine expression.
8129 * In order to be able to perform the conversion, the input
8130 * needs to be non-empty and may only involve a single range space.
8132 * If the resulting multi union piecewise affine expression has
8133 * an explicit domain, then assign it the domain of the input.
8134 * In other cases, the domain is stored in the individual elements.
8136 __isl_give isl_multi_union_pw_aff *
8137 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8138 __isl_take isl_union_pw_multi_aff *upma)
8140 isl_space *space = NULL;
8141 isl_multi_union_pw_aff *mupa;
8142 int i, n;
8144 if (!upma)
8145 return NULL;
8146 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8147 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8148 "cannot extract range space from empty input",
8149 goto error);
8150 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8151 &space) < 0)
8152 goto error;
8154 if (!space)
8155 goto error;
8157 n = isl_space_dim(space, isl_dim_set);
8158 mupa = isl_multi_union_pw_aff_alloc(space);
8160 for (i = 0; i < n; ++i) {
8161 isl_union_pw_aff *upa;
8163 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8164 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8166 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8167 isl_union_set *dom;
8168 isl_union_pw_multi_aff *copy;
8170 copy = isl_union_pw_multi_aff_copy(upma);
8171 dom = isl_union_pw_multi_aff_domain(copy);
8172 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8175 isl_union_pw_multi_aff_free(upma);
8176 return mupa;
8177 error:
8178 isl_space_free(space);
8179 isl_union_pw_multi_aff_free(upma);
8180 return NULL;
8183 /* Try and create an isl_multi_union_pw_aff that is equivalent
8184 * to the given isl_union_map.
8185 * The isl_union_map is required to be single-valued in each space.
8186 * Moreover, it cannot be empty and all range spaces need to be the same.
8187 * Otherwise, an error is produced.
8189 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8190 __isl_take isl_union_map *umap)
8192 isl_union_pw_multi_aff *upma;
8194 upma = isl_union_pw_multi_aff_from_union_map(umap);
8195 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8198 /* Return a multiple union piecewise affine expression
8199 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8200 * have been aligned.
8202 * If the resulting multi union piecewise affine expression has
8203 * an explicit domain, then assign it the input domain.
8204 * In other cases, the domain is stored in the individual elements.
8206 static __isl_give isl_multi_union_pw_aff *
8207 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8208 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8210 int i, n;
8211 isl_space *space;
8212 isl_multi_union_pw_aff *mupa;
8214 if (!domain || !mv)
8215 goto error;
8217 n = isl_multi_val_dim(mv, isl_dim_set);
8218 space = isl_multi_val_get_space(mv);
8219 mupa = isl_multi_union_pw_aff_alloc(space);
8220 for (i = 0; i < n; ++i) {
8221 isl_val *v;
8222 isl_union_pw_aff *upa;
8224 v = isl_multi_val_get_val(mv, i);
8225 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8227 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8229 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8230 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8231 isl_union_set_copy(domain));
8233 isl_union_set_free(domain);
8234 isl_multi_val_free(mv);
8235 return mupa;
8236 error:
8237 isl_union_set_free(domain);
8238 isl_multi_val_free(mv);
8239 return NULL;
8242 /* Return a multiple union piecewise affine expression
8243 * that is equal to "mv" on "domain".
8245 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8246 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8248 isl_bool equal_params;
8250 if (!domain || !mv)
8251 goto error;
8252 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8253 if (equal_params < 0)
8254 goto error;
8255 if (equal_params)
8256 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8257 domain, mv);
8258 domain = isl_union_set_align_params(domain,
8259 isl_multi_val_get_space(mv));
8260 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8261 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8262 error:
8263 isl_union_set_free(domain);
8264 isl_multi_val_free(mv);
8265 return NULL;
8268 /* Return a multiple union piecewise affine expression
8269 * that is equal to "ma" on "domain".
8271 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8272 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8274 isl_pw_multi_aff *pma;
8276 pma = isl_pw_multi_aff_from_multi_aff(ma);
8277 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8280 /* Return a multiple union piecewise affine expression
8281 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8282 * have been aligned.
8284 * If the resulting multi union piecewise affine expression has
8285 * an explicit domain, then assign it the input domain.
8286 * In other cases, the domain is stored in the individual elements.
8288 static __isl_give isl_multi_union_pw_aff *
8289 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8290 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8292 int i, n;
8293 isl_space *space;
8294 isl_multi_union_pw_aff *mupa;
8296 if (!domain || !pma)
8297 goto error;
8299 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8300 space = isl_pw_multi_aff_get_space(pma);
8301 mupa = isl_multi_union_pw_aff_alloc(space);
8302 for (i = 0; i < n; ++i) {
8303 isl_pw_aff *pa;
8304 isl_union_pw_aff *upa;
8306 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8307 upa = isl_union_pw_aff_pw_aff_on_domain(
8308 isl_union_set_copy(domain), pa);
8309 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8311 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8312 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8313 isl_union_set_copy(domain));
8315 isl_union_set_free(domain);
8316 isl_pw_multi_aff_free(pma);
8317 return mupa;
8318 error:
8319 isl_union_set_free(domain);
8320 isl_pw_multi_aff_free(pma);
8321 return NULL;
8324 /* Return a multiple union piecewise affine expression
8325 * that is equal to "pma" on "domain".
8327 __isl_give isl_multi_union_pw_aff *
8328 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8329 __isl_take isl_pw_multi_aff *pma)
8331 isl_bool equal_params;
8332 isl_space *space;
8334 space = isl_pw_multi_aff_peek_space(pma);
8335 equal_params = isl_union_set_space_has_equal_params(domain, space);
8336 if (equal_params < 0)
8337 goto error;
8338 if (equal_params)
8339 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8340 domain, pma);
8341 domain = isl_union_set_align_params(domain,
8342 isl_pw_multi_aff_get_space(pma));
8343 pma = isl_pw_multi_aff_align_params(pma,
8344 isl_union_set_get_space(domain));
8345 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8346 pma);
8347 error:
8348 isl_union_set_free(domain);
8349 isl_pw_multi_aff_free(pma);
8350 return NULL;
8353 /* Return a union set containing those elements in the domains
8354 * of the elements of "mupa" where they are all zero.
8356 * If there are no elements, then simply return the entire domain.
8358 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8359 __isl_take isl_multi_union_pw_aff *mupa)
8361 int i, n;
8362 isl_union_pw_aff *upa;
8363 isl_union_set *zero;
8365 if (!mupa)
8366 return NULL;
8368 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8369 if (n == 0)
8370 return isl_multi_union_pw_aff_domain(mupa);
8372 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8373 zero = isl_union_pw_aff_zero_union_set(upa);
8375 for (i = 1; i < n; ++i) {
8376 isl_union_set *zero_i;
8378 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8379 zero_i = isl_union_pw_aff_zero_union_set(upa);
8381 zero = isl_union_set_intersect(zero, zero_i);
8384 isl_multi_union_pw_aff_free(mupa);
8385 return zero;
8388 /* Construct a union map mapping the shared domain
8389 * of the union piecewise affine expressions to the range of "mupa"
8390 * in the special case of a 0D multi union piecewise affine expression.
8392 * Construct a map between the explicit domain of "mupa" and
8393 * the range space.
8394 * Note that this assumes that the domain consists of explicit elements.
8396 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8397 __isl_take isl_multi_union_pw_aff *mupa)
8399 isl_bool is_params;
8400 isl_space *space;
8401 isl_union_set *dom, *ran;
8403 space = isl_multi_union_pw_aff_get_space(mupa);
8404 dom = isl_multi_union_pw_aff_domain(mupa);
8405 ran = isl_union_set_from_set(isl_set_universe(space));
8407 is_params = isl_union_set_is_params(dom);
8408 if (is_params < 0)
8409 dom = isl_union_set_free(dom);
8410 else if (is_params)
8411 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8412 "cannot create union map from expression without "
8413 "explicit domain elements",
8414 dom = isl_union_set_free(dom));
8416 return isl_union_map_from_domain_and_range(dom, ran);
8419 /* Construct a union map mapping the shared domain
8420 * of the union piecewise affine expressions to the range of "mupa"
8421 * with each dimension in the range equated to the
8422 * corresponding union piecewise affine expression.
8424 * If the input is zero-dimensional, then construct a mapping
8425 * from its explicit domain.
8427 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8428 __isl_take isl_multi_union_pw_aff *mupa)
8430 int i, n;
8431 isl_space *space;
8432 isl_union_map *umap;
8433 isl_union_pw_aff *upa;
8435 if (!mupa)
8436 return NULL;
8438 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8439 if (n == 0)
8440 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8442 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8443 umap = isl_union_map_from_union_pw_aff(upa);
8445 for (i = 1; i < n; ++i) {
8446 isl_union_map *umap_i;
8448 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8449 umap_i = isl_union_map_from_union_pw_aff(upa);
8450 umap = isl_union_map_flat_range_product(umap, umap_i);
8453 space = isl_multi_union_pw_aff_get_space(mupa);
8454 umap = isl_union_map_reset_range_space(umap, space);
8456 isl_multi_union_pw_aff_free(mupa);
8457 return umap;
8460 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8461 * "range" is the space from which to set the range space.
8462 * "res" collects the results.
8464 struct isl_union_pw_multi_aff_reset_range_space_data {
8465 isl_space *range;
8466 isl_union_pw_multi_aff *res;
8469 /* Replace the range space of "pma" by the range space of data->range and
8470 * add the result to data->res.
8472 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8474 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8475 isl_space *space;
8477 space = isl_pw_multi_aff_get_space(pma);
8478 space = isl_space_domain(space);
8479 space = isl_space_extend_domain_with_range(space,
8480 isl_space_copy(data->range));
8481 pma = isl_pw_multi_aff_reset_space(pma, space);
8482 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8484 return data->res ? isl_stat_ok : isl_stat_error;
8487 /* Replace the range space of all the piecewise affine expressions in "upma" by
8488 * the range space of "space".
8490 * This assumes that all these expressions have the same output dimension.
8492 * Since the spaces of the expressions change, so do their hash values.
8493 * We therefore need to create a new isl_union_pw_multi_aff.
8494 * Note that the hash value is currently computed based on the entire
8495 * space even though there can only be a single expression with a given
8496 * domain space.
8498 static __isl_give isl_union_pw_multi_aff *
8499 isl_union_pw_multi_aff_reset_range_space(
8500 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8502 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8503 isl_space *space_upma;
8505 space_upma = isl_union_pw_multi_aff_get_space(upma);
8506 data.res = isl_union_pw_multi_aff_empty(space_upma);
8507 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8508 &reset_range_space, &data) < 0)
8509 data.res = isl_union_pw_multi_aff_free(data.res);
8511 isl_space_free(space);
8512 isl_union_pw_multi_aff_free(upma);
8513 return data.res;
8516 /* Construct and return a union piecewise multi affine expression
8517 * that is equal to the given multi union piecewise affine expression,
8518 * in the special case of a 0D multi union piecewise affine expression.
8520 * Construct a union piecewise multi affine expression
8521 * on top of the explicit domain of the input.
8523 __isl_give isl_union_pw_multi_aff *
8524 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8525 __isl_take isl_multi_union_pw_aff *mupa)
8527 isl_space *space;
8528 isl_multi_val *mv;
8529 isl_union_set *domain;
8531 space = isl_multi_union_pw_aff_get_space(mupa);
8532 mv = isl_multi_val_zero(space);
8533 domain = isl_multi_union_pw_aff_domain(mupa);
8534 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8537 /* Construct and return a union piecewise multi affine expression
8538 * that is equal to the given multi union piecewise affine expression.
8540 * If the input is zero-dimensional, then
8541 * construct a union piecewise multi affine expression
8542 * on top of the explicit domain of the input.
8544 __isl_give isl_union_pw_multi_aff *
8545 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8546 __isl_take isl_multi_union_pw_aff *mupa)
8548 int i, n;
8549 isl_space *space;
8550 isl_union_pw_multi_aff *upma;
8551 isl_union_pw_aff *upa;
8553 if (!mupa)
8554 return NULL;
8556 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8557 if (n == 0)
8558 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8560 space = isl_multi_union_pw_aff_get_space(mupa);
8561 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8562 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8564 for (i = 1; i < n; ++i) {
8565 isl_union_pw_multi_aff *upma_i;
8567 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8568 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8569 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8572 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8574 isl_multi_union_pw_aff_free(mupa);
8575 return upma;
8578 /* Intersect the range of "mupa" with "range",
8579 * in the special case where "mupa" is 0D.
8581 * Intersect the domain of "mupa" with the constraints on the parameters
8582 * of "range".
8584 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8585 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8587 range = isl_set_params(range);
8588 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8589 return mupa;
8592 /* Intersect the range of "mupa" with "range".
8593 * That is, keep only those domain elements that have a function value
8594 * in "range".
8596 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8597 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8599 isl_union_pw_multi_aff *upma;
8600 isl_union_set *domain;
8601 isl_space *space;
8602 int n;
8603 int match;
8605 if (!mupa || !range)
8606 goto error;
8608 space = isl_set_get_space(range);
8609 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8610 space, isl_dim_set);
8611 isl_space_free(space);
8612 if (match < 0)
8613 goto error;
8614 if (!match)
8615 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8616 "space don't match", goto error);
8617 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8618 if (n == 0)
8619 return mupa_intersect_range_0D(mupa, range);
8621 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8622 isl_multi_union_pw_aff_copy(mupa));
8623 domain = isl_union_set_from_set(range);
8624 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8625 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8627 return mupa;
8628 error:
8629 isl_multi_union_pw_aff_free(mupa);
8630 isl_set_free(range);
8631 return NULL;
8634 /* Return the shared domain of the elements of "mupa",
8635 * in the special case where "mupa" is zero-dimensional.
8637 * Return the explicit domain of "mupa".
8638 * Note that this domain may be a parameter set, either
8639 * because "mupa" is meant to live in a set space or
8640 * because no explicit domain has been set.
8642 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8643 __isl_take isl_multi_union_pw_aff *mupa)
8645 isl_union_set *dom;
8647 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8648 isl_multi_union_pw_aff_free(mupa);
8650 return dom;
8653 /* Return the shared domain of the elements of "mupa".
8655 * If "mupa" is zero-dimensional, then return its explicit domain.
8657 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8658 __isl_take isl_multi_union_pw_aff *mupa)
8660 int i, n;
8661 isl_union_pw_aff *upa;
8662 isl_union_set *dom;
8664 if (!mupa)
8665 return NULL;
8667 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8668 if (n == 0)
8669 return isl_multi_union_pw_aff_domain_0D(mupa);
8671 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8672 dom = isl_union_pw_aff_domain(upa);
8673 for (i = 1; i < n; ++i) {
8674 isl_union_set *dom_i;
8676 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8677 dom_i = isl_union_pw_aff_domain(upa);
8678 dom = isl_union_set_intersect(dom, dom_i);
8681 isl_multi_union_pw_aff_free(mupa);
8682 return dom;
8685 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8686 * In particular, the spaces have been aligned.
8687 * The result is defined over the shared domain of the elements of "mupa"
8689 * We first extract the parametric constant part of "aff" and
8690 * define that over the shared domain.
8691 * Then we iterate over all input dimensions of "aff" and add the corresponding
8692 * multiples of the elements of "mupa".
8693 * Finally, we consider the integer divisions, calling the function
8694 * recursively to obtain an isl_union_pw_aff corresponding to the
8695 * integer division argument.
8697 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8698 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8700 int i, n_in, n_div;
8701 isl_union_pw_aff *upa;
8702 isl_union_set *uset;
8703 isl_val *v;
8704 isl_aff *cst;
8706 n_in = isl_aff_dim(aff, isl_dim_in);
8707 n_div = isl_aff_dim(aff, isl_dim_div);
8709 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8710 cst = isl_aff_copy(aff);
8711 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8712 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8713 cst = isl_aff_project_domain_on_params(cst);
8714 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8716 for (i = 0; i < n_in; ++i) {
8717 isl_union_pw_aff *upa_i;
8719 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8720 continue;
8721 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8722 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8723 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8724 upa = isl_union_pw_aff_add(upa, upa_i);
8727 for (i = 0; i < n_div; ++i) {
8728 isl_aff *div;
8729 isl_union_pw_aff *upa_i;
8731 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8732 continue;
8733 div = isl_aff_get_div(aff, i);
8734 upa_i = multi_union_pw_aff_apply_aff(
8735 isl_multi_union_pw_aff_copy(mupa), div);
8736 upa_i = isl_union_pw_aff_floor(upa_i);
8737 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8738 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8739 upa = isl_union_pw_aff_add(upa, upa_i);
8742 isl_multi_union_pw_aff_free(mupa);
8743 isl_aff_free(aff);
8745 return upa;
8748 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8749 * with the domain of "aff".
8750 * Furthermore, the dimension of this space needs to be greater than zero.
8751 * The result is defined over the shared domain of the elements of "mupa"
8753 * We perform these checks and then hand over control to
8754 * multi_union_pw_aff_apply_aff.
8756 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8757 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8759 isl_space *space1, *space2;
8760 isl_bool equal;
8762 mupa = isl_multi_union_pw_aff_align_params(mupa,
8763 isl_aff_get_space(aff));
8764 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8765 if (!mupa || !aff)
8766 goto error;
8768 space1 = isl_multi_union_pw_aff_get_space(mupa);
8769 space2 = isl_aff_get_domain_space(aff);
8770 equal = isl_space_is_equal(space1, space2);
8771 isl_space_free(space1);
8772 isl_space_free(space2);
8773 if (equal < 0)
8774 goto error;
8775 if (!equal)
8776 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8777 "spaces don't match", goto error);
8778 if (isl_aff_dim(aff, isl_dim_in) == 0)
8779 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8780 "cannot determine domains", goto error);
8782 return multi_union_pw_aff_apply_aff(mupa, aff);
8783 error:
8784 isl_multi_union_pw_aff_free(mupa);
8785 isl_aff_free(aff);
8786 return NULL;
8789 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8790 * The space of "mupa" is known to be compatible with the domain of "ma".
8792 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8793 * on the domain of "mupa".
8795 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
8796 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8798 isl_union_set *dom;
8800 dom = isl_multi_union_pw_aff_domain(mupa);
8801 ma = isl_multi_aff_project_domain_on_params(ma);
8803 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
8806 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8807 * with the domain of "ma".
8808 * The result is defined over the shared domain of the elements of "mupa"
8810 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8811 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8813 isl_space *space1, *space2;
8814 isl_multi_union_pw_aff *res;
8815 isl_bool equal;
8816 int i, n_out;
8818 mupa = isl_multi_union_pw_aff_align_params(mupa,
8819 isl_multi_aff_get_space(ma));
8820 ma = isl_multi_aff_align_params(ma,
8821 isl_multi_union_pw_aff_get_space(mupa));
8822 if (!mupa || !ma)
8823 goto error;
8825 space1 = isl_multi_union_pw_aff_get_space(mupa);
8826 space2 = isl_multi_aff_get_domain_space(ma);
8827 equal = isl_space_is_equal(space1, space2);
8828 isl_space_free(space1);
8829 isl_space_free(space2);
8830 if (equal < 0)
8831 goto error;
8832 if (!equal)
8833 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8834 "spaces don't match", goto error);
8835 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8836 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
8837 return mupa_apply_multi_aff_0D(mupa, ma);
8839 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8840 res = isl_multi_union_pw_aff_alloc(space1);
8842 for (i = 0; i < n_out; ++i) {
8843 isl_aff *aff;
8844 isl_union_pw_aff *upa;
8846 aff = isl_multi_aff_get_aff(ma, i);
8847 upa = multi_union_pw_aff_apply_aff(
8848 isl_multi_union_pw_aff_copy(mupa), aff);
8849 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8852 isl_multi_aff_free(ma);
8853 isl_multi_union_pw_aff_free(mupa);
8854 return res;
8855 error:
8856 isl_multi_union_pw_aff_free(mupa);
8857 isl_multi_aff_free(ma);
8858 return NULL;
8861 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8862 * The space of "mupa" is known to be compatible with the domain of "pa".
8864 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8865 * on the domain of "mupa".
8867 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
8868 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8870 isl_union_set *dom;
8872 dom = isl_multi_union_pw_aff_domain(mupa);
8873 pa = isl_pw_aff_project_domain_on_params(pa);
8875 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
8878 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8879 * with the domain of "pa".
8880 * Furthermore, the dimension of this space needs to be greater than zero.
8881 * The result is defined over the shared domain of the elements of "mupa"
8883 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8884 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8886 int i;
8887 isl_bool equal;
8888 isl_space *space, *space2;
8889 isl_union_pw_aff *upa;
8891 mupa = isl_multi_union_pw_aff_align_params(mupa,
8892 isl_pw_aff_get_space(pa));
8893 pa = isl_pw_aff_align_params(pa,
8894 isl_multi_union_pw_aff_get_space(mupa));
8895 if (!mupa || !pa)
8896 goto error;
8898 space = isl_multi_union_pw_aff_get_space(mupa);
8899 space2 = isl_pw_aff_get_domain_space(pa);
8900 equal = isl_space_is_equal(space, space2);
8901 isl_space_free(space);
8902 isl_space_free(space2);
8903 if (equal < 0)
8904 goto error;
8905 if (!equal)
8906 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8907 "spaces don't match", goto error);
8908 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8909 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
8911 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8912 upa = isl_union_pw_aff_empty(space);
8914 for (i = 0; i < pa->n; ++i) {
8915 isl_aff *aff;
8916 isl_set *domain;
8917 isl_multi_union_pw_aff *mupa_i;
8918 isl_union_pw_aff *upa_i;
8920 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8921 domain = isl_set_copy(pa->p[i].set);
8922 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8923 aff = isl_aff_copy(pa->p[i].aff);
8924 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8925 upa = isl_union_pw_aff_union_add(upa, upa_i);
8928 isl_multi_union_pw_aff_free(mupa);
8929 isl_pw_aff_free(pa);
8930 return upa;
8931 error:
8932 isl_multi_union_pw_aff_free(mupa);
8933 isl_pw_aff_free(pa);
8934 return NULL;
8937 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
8938 * The space of "mupa" is known to be compatible with the domain of "pma".
8940 * Construct an isl_multi_union_pw_aff that is equal to "pma"
8941 * on the domain of "mupa".
8943 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
8944 __isl_take isl_multi_union_pw_aff *mupa,
8945 __isl_take isl_pw_multi_aff *pma)
8947 isl_union_set *dom;
8949 dom = isl_multi_union_pw_aff_domain(mupa);
8950 pma = isl_pw_multi_aff_project_domain_on_params(pma);
8952 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
8955 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8956 * with the domain of "pma".
8957 * The result is defined over the shared domain of the elements of "mupa"
8959 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8960 __isl_take isl_multi_union_pw_aff *mupa,
8961 __isl_take isl_pw_multi_aff *pma)
8963 isl_space *space1, *space2;
8964 isl_multi_union_pw_aff *res;
8965 isl_bool equal;
8966 int i, n_out;
8968 mupa = isl_multi_union_pw_aff_align_params(mupa,
8969 isl_pw_multi_aff_get_space(pma));
8970 pma = isl_pw_multi_aff_align_params(pma,
8971 isl_multi_union_pw_aff_get_space(mupa));
8972 if (!mupa || !pma)
8973 goto error;
8975 space1 = isl_multi_union_pw_aff_get_space(mupa);
8976 space2 = isl_pw_multi_aff_get_domain_space(pma);
8977 equal = isl_space_is_equal(space1, space2);
8978 isl_space_free(space1);
8979 isl_space_free(space2);
8980 if (equal < 0)
8981 goto error;
8982 if (!equal)
8983 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8984 "spaces don't match", goto error);
8985 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8986 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
8987 return mupa_apply_pw_multi_aff_0D(mupa, pma);
8989 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8990 res = isl_multi_union_pw_aff_alloc(space1);
8992 for (i = 0; i < n_out; ++i) {
8993 isl_pw_aff *pa;
8994 isl_union_pw_aff *upa;
8996 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8997 upa = isl_multi_union_pw_aff_apply_pw_aff(
8998 isl_multi_union_pw_aff_copy(mupa), pa);
8999 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9002 isl_pw_multi_aff_free(pma);
9003 isl_multi_union_pw_aff_free(mupa);
9004 return res;
9005 error:
9006 isl_multi_union_pw_aff_free(mupa);
9007 isl_pw_multi_aff_free(pma);
9008 return NULL;
9011 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9012 * If the explicit domain only keeps track of constraints on the parameters,
9013 * then only update those constraints.
9015 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9016 __isl_take isl_multi_union_pw_aff *mupa,
9017 __isl_keep isl_union_pw_multi_aff *upma)
9019 isl_bool is_params;
9021 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9022 return isl_multi_union_pw_aff_free(mupa);
9024 mupa = isl_multi_union_pw_aff_cow(mupa);
9025 if (!mupa)
9026 return NULL;
9028 is_params = isl_union_set_is_params(mupa->u.dom);
9029 if (is_params < 0)
9030 return isl_multi_union_pw_aff_free(mupa);
9032 upma = isl_union_pw_multi_aff_copy(upma);
9033 if (is_params)
9034 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9035 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9036 else
9037 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9038 mupa->u.dom, upma);
9039 if (!mupa->u.dom)
9040 return isl_multi_union_pw_aff_free(mupa);
9041 return mupa;
9044 /* Compute the pullback of "mupa" by the function represented by "upma".
9045 * In other words, plug in "upma" in "mupa". The result contains
9046 * expressions defined over the domain space of "upma".
9048 * Run over all elements of "mupa" and plug in "upma" in each of them.
9050 * If "mupa" has an explicit domain, then it is this domain
9051 * that needs to undergo a pullback instead, i.e., a preimage.
9053 __isl_give isl_multi_union_pw_aff *
9054 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9055 __isl_take isl_multi_union_pw_aff *mupa,
9056 __isl_take isl_union_pw_multi_aff *upma)
9058 int i, n;
9060 mupa = isl_multi_union_pw_aff_align_params(mupa,
9061 isl_union_pw_multi_aff_get_space(upma));
9062 upma = isl_union_pw_multi_aff_align_params(upma,
9063 isl_multi_union_pw_aff_get_space(mupa));
9064 mupa = isl_multi_union_pw_aff_cow(mupa);
9065 if (!mupa || !upma)
9066 goto error;
9068 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9069 for (i = 0; i < n; ++i) {
9070 isl_union_pw_aff *upa;
9072 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9073 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9074 isl_union_pw_multi_aff_copy(upma));
9075 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9078 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9079 mupa = preimage_explicit_domain(mupa, upma);
9081 isl_union_pw_multi_aff_free(upma);
9082 return mupa;
9083 error:
9084 isl_multi_union_pw_aff_free(mupa);
9085 isl_union_pw_multi_aff_free(upma);
9086 return NULL;
9089 /* Extract the sequence of elements in "mupa" with domain space "space"
9090 * (ignoring parameters).
9092 * For the elements of "mupa" that are not defined on the specified space,
9093 * the corresponding element in the result is empty.
9095 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9096 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9098 int i, n;
9099 isl_space *space_mpa;
9100 isl_multi_pw_aff *mpa;
9102 if (!mupa || !space)
9103 goto error;
9105 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9106 space = isl_space_replace_params(space, space_mpa);
9107 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9108 space_mpa);
9109 mpa = isl_multi_pw_aff_alloc(space_mpa);
9111 space = isl_space_from_domain(space);
9112 space = isl_space_add_dims(space, isl_dim_out, 1);
9113 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9114 for (i = 0; i < n; ++i) {
9115 isl_union_pw_aff *upa;
9116 isl_pw_aff *pa;
9118 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9119 pa = isl_union_pw_aff_extract_pw_aff(upa,
9120 isl_space_copy(space));
9121 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9122 isl_union_pw_aff_free(upa);
9125 isl_space_free(space);
9126 return mpa;
9127 error:
9128 isl_space_free(space);
9129 return NULL;
9132 /* Evaluate the affine function "aff" in the void point "pnt".
9133 * In particular, return the value NaN.
9135 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9136 __isl_take isl_point *pnt)
9138 isl_ctx *ctx;
9140 ctx = isl_point_get_ctx(pnt);
9141 isl_aff_free(aff);
9142 isl_point_free(pnt);
9143 return isl_val_nan(ctx);
9146 /* Evaluate the affine expression "aff"
9147 * in the coordinates (with denominator) "pnt".
9149 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9150 __isl_keep isl_vec *pnt)
9152 isl_int n, d;
9153 isl_ctx *ctx;
9154 isl_val *v;
9156 if (!aff || !pnt)
9157 return NULL;
9159 ctx = isl_vec_get_ctx(aff);
9160 isl_int_init(n);
9161 isl_int_init(d);
9162 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9163 isl_int_mul(d, aff->el[0], pnt->el[0]);
9164 v = isl_val_rat_from_isl_int(ctx, n, d);
9165 v = isl_val_normalize(v);
9166 isl_int_clear(n);
9167 isl_int_clear(d);
9169 return v;
9172 /* Check that the domain space of "aff" is equal to "space".
9174 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9175 __isl_keep isl_space *space)
9177 isl_bool ok;
9179 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9180 if (ok < 0)
9181 return isl_stat_error;
9182 if (!ok)
9183 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9184 "incompatible spaces", return isl_stat_error);
9185 return isl_stat_ok;
9188 /* Evaluate the affine function "aff" in "pnt".
9190 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9191 __isl_take isl_point *pnt)
9193 isl_bool is_void;
9194 isl_val *v;
9195 isl_local_space *ls;
9197 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9198 goto error;
9199 is_void = isl_point_is_void(pnt);
9200 if (is_void < 0)
9201 goto error;
9202 if (is_void)
9203 return eval_void(aff, pnt);
9205 ls = isl_aff_get_domain_local_space(aff);
9206 pnt = isl_local_space_lift_point(ls, pnt);
9208 v = eval(aff->v, isl_point_peek_vec(pnt));
9210 isl_aff_free(aff);
9211 isl_point_free(pnt);
9213 return v;
9214 error:
9215 isl_aff_free(aff);
9216 isl_point_free(pnt);
9217 return NULL;