drop isl_basic_set_drop_redundant_divs
[isl.git] / isl_aff.c
blob5906ae08aab5aaa6acd0f9de71085fe941cd7940
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 #define ISL_DIM_H
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.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/deprecated/aff_int.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 union_pw_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
54 __isl_take isl_vec *v)
56 isl_aff *aff;
58 if (!ls || !v)
59 goto error;
61 aff = isl_calloc_type(v->ctx, struct isl_aff);
62 if (!aff)
63 goto error;
65 aff->ref = 1;
66 aff->ls = ls;
67 aff->v = v;
69 return aff;
70 error:
71 isl_local_space_free(ls);
72 isl_vec_free(v);
73 return NULL;
76 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
78 isl_ctx *ctx;
79 isl_vec *v;
80 unsigned total;
82 if (!ls)
83 return NULL;
85 ctx = isl_local_space_get_ctx(ls);
86 if (!isl_local_space_divs_known(ls))
87 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
88 goto error);
89 if (!isl_local_space_is_set(ls))
90 isl_die(ctx, isl_error_invalid,
91 "domain of affine expression should be a set",
92 goto error);
94 total = isl_local_space_dim(ls, isl_dim_all);
95 v = isl_vec_alloc(ctx, 1 + 1 + total);
96 return isl_aff_alloc_vec(ls, v);
97 error:
98 isl_local_space_free(ls);
99 return NULL;
102 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
104 isl_aff *aff;
106 aff = isl_aff_alloc(ls);
107 if (!aff)
108 return NULL;
110 isl_int_set_si(aff->v->el[0], 1);
111 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
113 return aff;
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
129 isl_aff *aff;
131 aff = isl_aff_alloc(ls);
132 if (!aff)
133 return NULL;
135 isl_seq_clr(aff->v->el, aff->v->size);
137 return aff;
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
152 __isl_take isl_val *val)
154 isl_aff *aff;
156 if (!ls || !val)
157 goto error;
158 if (!isl_val_is_rat(val))
159 isl_die(isl_val_get_ctx(val), isl_error_invalid,
160 "expecting rational value", goto error);
162 aff = isl_aff_alloc(isl_local_space_copy(ls));
163 if (!aff)
164 goto error;
166 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
167 isl_int_set(aff->v->el[1], val->n);
168 isl_int_set(aff->v->el[0], val->d);
170 isl_local_space_free(ls);
171 isl_val_free(val);
172 return aff;
173 error:
174 isl_local_space_free(ls);
175 isl_val_free(val);
176 return NULL;
179 /* Return an affine expression that is equal to the specified dimension
180 * in "ls".
182 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
183 enum isl_dim_type type, unsigned pos)
185 isl_space *space;
186 isl_aff *aff;
188 if (!ls)
189 return NULL;
191 space = isl_local_space_get_space(ls);
192 if (!space)
193 goto error;
194 if (isl_space_is_map(space))
195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
196 "expecting (parameter) set space", goto error);
197 if (pos >= isl_local_space_dim(ls, type))
198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
199 "position out of bounds", goto error);
201 isl_space_free(space);
202 aff = isl_aff_alloc(ls);
203 if (!aff)
204 return NULL;
206 pos += isl_local_space_offset(aff->ls, type);
208 isl_int_set_si(aff->v->el[0], 1);
209 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
210 isl_int_set_si(aff->v->el[1 + pos], 1);
212 return aff;
213 error:
214 isl_local_space_free(ls);
215 isl_space_free(space);
216 return NULL;
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
223 enum isl_dim_type type, unsigned pos)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
228 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
230 if (!aff)
231 return NULL;
233 aff->ref++;
234 return aff;
237 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
239 if (!aff)
240 return NULL;
242 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
243 isl_vec_copy(aff->v));
246 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
248 if (!aff)
249 return NULL;
251 if (aff->ref == 1)
252 return aff;
253 aff->ref--;
254 return isl_aff_dup(aff);
257 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
259 if (!aff)
260 return NULL;
262 if (--aff->ref > 0)
263 return NULL;
265 isl_local_space_free(aff->ls);
266 isl_vec_free(aff->v);
268 free(aff);
270 return NULL;
273 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
275 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
278 /* Return a hash value that digests "aff".
280 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
282 uint32_t hash, ls_hash, v_hash;
284 if (!aff)
285 return 0;
287 hash = isl_hash_init();
288 ls_hash = isl_local_space_get_hash(aff->ls);
289 isl_hash_hash(hash, ls_hash);
290 v_hash = isl_vec_get_hash(aff->v);
291 isl_hash_hash(hash, v_hash);
293 return hash;
296 /* Externally, an isl_aff has a map space, but internally, the
297 * ls field corresponds to the domain of that space.
299 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
301 if (!aff)
302 return 0;
303 if (type == isl_dim_out)
304 return 1;
305 if (type == isl_dim_in)
306 type = isl_dim_set;
307 return isl_local_space_dim(aff->ls, type);
310 /* Return the position of the dimension of the given type and name
311 * in "aff".
312 * Return -1 if no such dimension can be found.
314 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
315 const char *name)
317 if (!aff)
318 return -1;
319 if (type == isl_dim_out)
320 return -1;
321 if (type == isl_dim_in)
322 type = isl_dim_set;
323 return isl_local_space_find_dim_by_name(aff->ls, type, name);
326 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
328 return aff ? isl_local_space_get_space(aff->ls) : NULL;
331 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
333 isl_space *space;
334 if (!aff)
335 return NULL;
336 space = isl_local_space_get_space(aff->ls);
337 space = isl_space_from_domain(space);
338 space = isl_space_add_dims(space, isl_dim_out, 1);
339 return space;
342 __isl_give isl_local_space *isl_aff_get_domain_local_space(
343 __isl_keep isl_aff *aff)
345 return aff ? isl_local_space_copy(aff->ls) : NULL;
348 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
350 isl_local_space *ls;
351 if (!aff)
352 return NULL;
353 ls = isl_local_space_copy(aff->ls);
354 ls = isl_local_space_from_domain(ls);
355 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
356 return ls;
359 /* Externally, an isl_aff has a map space, but internally, the
360 * ls field corresponds to the domain of that space.
362 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
363 enum isl_dim_type type, unsigned pos)
365 if (!aff)
366 return NULL;
367 if (type == isl_dim_out)
368 return NULL;
369 if (type == isl_dim_in)
370 type = isl_dim_set;
371 return isl_local_space_get_dim_name(aff->ls, type, pos);
374 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
375 __isl_take isl_space *dim)
377 aff = isl_aff_cow(aff);
378 if (!aff || !dim)
379 goto error;
381 aff->ls = isl_local_space_reset_space(aff->ls, dim);
382 if (!aff->ls)
383 return isl_aff_free(aff);
385 return aff;
386 error:
387 isl_aff_free(aff);
388 isl_space_free(dim);
389 return NULL;
392 /* Reset the space of "aff". This function is called from isl_pw_templ.c
393 * and doesn't know if the space of an element object is represented
394 * directly or through its domain. It therefore passes along both.
396 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
397 __isl_take isl_space *space, __isl_take isl_space *domain)
399 isl_space_free(space);
400 return isl_aff_reset_domain_space(aff, domain);
403 /* Reorder the coefficients of the affine expression based
404 * on the given reordering.
405 * The reordering r is assumed to have been extended with the local
406 * variables.
408 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
409 __isl_take isl_reordering *r, int n_div)
411 isl_vec *res;
412 int i;
414 if (!vec || !r)
415 goto error;
417 res = isl_vec_alloc(vec->ctx,
418 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
419 if (!res)
420 goto error;
421 isl_seq_cpy(res->el, vec->el, 2);
422 isl_seq_clr(res->el + 2, res->size - 2);
423 for (i = 0; i < r->len; ++i)
424 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
426 isl_reordering_free(r);
427 isl_vec_free(vec);
428 return res;
429 error:
430 isl_vec_free(vec);
431 isl_reordering_free(r);
432 return NULL;
435 /* Reorder the dimensions of the domain of "aff" according
436 * to the given reordering.
438 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
439 __isl_take isl_reordering *r)
441 aff = isl_aff_cow(aff);
442 if (!aff)
443 goto error;
445 r = isl_reordering_extend(r, aff->ls->div->n_row);
446 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
447 aff->ls->div->n_row);
448 aff->ls = isl_local_space_realign(aff->ls, r);
450 if (!aff->v || !aff->ls)
451 return isl_aff_free(aff);
453 return aff;
454 error:
455 isl_aff_free(aff);
456 isl_reordering_free(r);
457 return NULL;
460 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
461 __isl_take isl_space *model)
463 if (!aff || !model)
464 goto error;
466 if (!isl_space_match(aff->ls->dim, isl_dim_param,
467 model, isl_dim_param)) {
468 isl_reordering *exp;
470 model = isl_space_drop_dims(model, isl_dim_in,
471 0, isl_space_dim(model, isl_dim_in));
472 model = isl_space_drop_dims(model, isl_dim_out,
473 0, isl_space_dim(model, isl_dim_out));
474 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
475 exp = isl_reordering_extend_space(exp,
476 isl_aff_get_domain_space(aff));
477 aff = isl_aff_realign_domain(aff, exp);
480 isl_space_free(model);
481 return aff;
482 error:
483 isl_space_free(model);
484 isl_aff_free(aff);
485 return NULL;
488 /* Is "aff" obviously equal to zero?
490 * If the denominator is zero, then "aff" is not equal to zero.
492 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
494 if (!aff)
495 return isl_bool_error;
497 if (isl_int_is_zero(aff->v->el[0]))
498 return isl_bool_false;
499 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
502 /* Does "aff" represent NaN?
504 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
506 if (!aff)
507 return isl_bool_error;
509 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
512 /* Are "aff1" and "aff2" obviously equal?
514 * NaN is not equal to anything, not even to another NaN.
516 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
517 __isl_keep isl_aff *aff2)
519 isl_bool equal;
521 if (!aff1 || !aff2)
522 return isl_bool_error;
524 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
525 return isl_bool_false;
527 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
528 if (equal < 0 || !equal)
529 return equal;
531 return isl_vec_is_equal(aff1->v, aff2->v);
534 /* Return the common denominator of "aff" in "v".
536 * We cannot return anything meaningful in case of a NaN.
538 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
540 if (!aff)
541 return isl_stat_error;
542 if (isl_aff_is_nan(aff))
543 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
544 "cannot get denominator of NaN", return isl_stat_error);
545 isl_int_set(*v, aff->v->el[0]);
546 return isl_stat_ok;
549 /* Return the common denominator of "aff".
551 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
553 isl_ctx *ctx;
555 if (!aff)
556 return NULL;
558 ctx = isl_aff_get_ctx(aff);
559 if (isl_aff_is_nan(aff))
560 return isl_val_nan(ctx);
561 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
564 /* Return the constant term of "aff" in "v".
566 * We cannot return anything meaningful in case of a NaN.
568 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
570 if (!aff)
571 return -1;
572 if (isl_aff_is_nan(aff))
573 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
574 "cannot get constant term of NaN", return -1);
575 isl_int_set(*v, aff->v->el[1]);
576 return 0;
579 /* Return the constant term of "aff".
581 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
583 isl_ctx *ctx;
584 isl_val *v;
586 if (!aff)
587 return NULL;
589 ctx = isl_aff_get_ctx(aff);
590 if (isl_aff_is_nan(aff))
591 return isl_val_nan(ctx);
592 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
593 return isl_val_normalize(v);
596 /* Return the coefficient of the variable of type "type" at position "pos"
597 * of "aff" in "v".
599 * We cannot return anything meaningful in case of a NaN.
601 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
602 enum isl_dim_type type, int pos, isl_int *v)
604 if (!aff)
605 return -1;
607 if (type == isl_dim_out)
608 isl_die(aff->v->ctx, isl_error_invalid,
609 "output/set dimension does not have a coefficient",
610 return -1);
611 if (type == isl_dim_in)
612 type = isl_dim_set;
614 if (pos >= isl_local_space_dim(aff->ls, type))
615 isl_die(aff->v->ctx, isl_error_invalid,
616 "position out of bounds", return -1);
618 if (isl_aff_is_nan(aff))
619 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
620 "cannot get coefficient of NaN", return -1);
621 pos += isl_local_space_offset(aff->ls, type);
622 isl_int_set(*v, aff->v->el[1 + pos]);
624 return 0;
627 /* Return the coefficient of the variable of type "type" at position "pos"
628 * of "aff".
630 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
631 enum isl_dim_type type, int pos)
633 isl_ctx *ctx;
634 isl_val *v;
636 if (!aff)
637 return NULL;
639 ctx = isl_aff_get_ctx(aff);
640 if (type == isl_dim_out)
641 isl_die(ctx, isl_error_invalid,
642 "output/set dimension does not have a coefficient",
643 return NULL);
644 if (type == isl_dim_in)
645 type = isl_dim_set;
647 if (pos >= isl_local_space_dim(aff->ls, type))
648 isl_die(ctx, isl_error_invalid,
649 "position out of bounds", return NULL);
651 if (isl_aff_is_nan(aff))
652 return isl_val_nan(ctx);
653 pos += isl_local_space_offset(aff->ls, type);
654 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
655 return isl_val_normalize(v);
658 /* Return the sign of the coefficient of the variable of type "type"
659 * at position "pos" of "aff".
661 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
662 int pos)
664 isl_ctx *ctx;
666 if (!aff)
667 return 0;
669 ctx = isl_aff_get_ctx(aff);
670 if (type == isl_dim_out)
671 isl_die(ctx, isl_error_invalid,
672 "output/set dimension does not have a coefficient",
673 return 0);
674 if (type == isl_dim_in)
675 type = isl_dim_set;
677 if (pos >= isl_local_space_dim(aff->ls, type))
678 isl_die(ctx, isl_error_invalid,
679 "position out of bounds", return 0);
681 pos += isl_local_space_offset(aff->ls, type);
682 return isl_int_sgn(aff->v->el[1 + pos]);
685 /* Replace the denominator of "aff" by "v".
687 * A NaN is unaffected by this operation.
689 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
691 if (!aff)
692 return NULL;
693 if (isl_aff_is_nan(aff))
694 return aff;
695 aff = isl_aff_cow(aff);
696 if (!aff)
697 return NULL;
699 aff->v = isl_vec_cow(aff->v);
700 if (!aff->v)
701 return isl_aff_free(aff);
703 isl_int_set(aff->v->el[0], v);
705 return aff;
708 /* Replace the numerator of the constant term of "aff" by "v".
710 * A NaN is unaffected by this operation.
712 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
714 if (!aff)
715 return NULL;
716 if (isl_aff_is_nan(aff))
717 return aff;
718 aff = isl_aff_cow(aff);
719 if (!aff)
720 return NULL;
722 aff->v = isl_vec_cow(aff->v);
723 if (!aff->v)
724 return isl_aff_free(aff);
726 isl_int_set(aff->v->el[1], v);
728 return aff;
731 /* Replace the constant term of "aff" by "v".
733 * A NaN is unaffected by this operation.
735 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
736 __isl_take isl_val *v)
738 if (!aff || !v)
739 goto error;
741 if (isl_aff_is_nan(aff)) {
742 isl_val_free(v);
743 return aff;
746 if (!isl_val_is_rat(v))
747 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
748 "expecting rational value", goto error);
750 if (isl_int_eq(aff->v->el[1], v->n) &&
751 isl_int_eq(aff->v->el[0], v->d)) {
752 isl_val_free(v);
753 return aff;
756 aff = isl_aff_cow(aff);
757 if (!aff)
758 goto error;
759 aff->v = isl_vec_cow(aff->v);
760 if (!aff->v)
761 goto error;
763 if (isl_int_eq(aff->v->el[0], v->d)) {
764 isl_int_set(aff->v->el[1], v->n);
765 } else if (isl_int_is_one(v->d)) {
766 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
767 } else {
768 isl_seq_scale(aff->v->el + 1,
769 aff->v->el + 1, v->d, aff->v->size - 1);
770 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
771 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
772 aff->v = isl_vec_normalize(aff->v);
773 if (!aff->v)
774 goto error;
777 isl_val_free(v);
778 return aff;
779 error:
780 isl_aff_free(aff);
781 isl_val_free(v);
782 return NULL;
785 /* Add "v" to the constant term of "aff".
787 * A NaN is unaffected by this operation.
789 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
791 if (isl_int_is_zero(v))
792 return aff;
794 if (!aff)
795 return NULL;
796 if (isl_aff_is_nan(aff))
797 return aff;
798 aff = isl_aff_cow(aff);
799 if (!aff)
800 return NULL;
802 aff->v = isl_vec_cow(aff->v);
803 if (!aff->v)
804 return isl_aff_free(aff);
806 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
808 return aff;
811 /* Add "v" to the constant term of "aff".
813 * A NaN is unaffected by this operation.
815 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
816 __isl_take isl_val *v)
818 if (!aff || !v)
819 goto error;
821 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
822 isl_val_free(v);
823 return aff;
826 if (!isl_val_is_rat(v))
827 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
828 "expecting rational value", goto error);
830 aff = isl_aff_cow(aff);
831 if (!aff)
832 goto error;
834 aff->v = isl_vec_cow(aff->v);
835 if (!aff->v)
836 goto error;
838 if (isl_int_is_one(v->d)) {
839 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
840 } else if (isl_int_eq(aff->v->el[0], v->d)) {
841 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
842 aff->v = isl_vec_normalize(aff->v);
843 if (!aff->v)
844 goto error;
845 } else {
846 isl_seq_scale(aff->v->el + 1,
847 aff->v->el + 1, v->d, aff->v->size - 1);
848 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
849 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
850 aff->v = isl_vec_normalize(aff->v);
851 if (!aff->v)
852 goto error;
855 isl_val_free(v);
856 return aff;
857 error:
858 isl_aff_free(aff);
859 isl_val_free(v);
860 return NULL;
863 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
865 isl_int t;
867 isl_int_init(t);
868 isl_int_set_si(t, v);
869 aff = isl_aff_add_constant(aff, t);
870 isl_int_clear(t);
872 return aff;
875 /* Add "v" to the numerator of the constant term of "aff".
877 * A NaN is unaffected by this operation.
879 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
881 if (isl_int_is_zero(v))
882 return aff;
884 if (!aff)
885 return NULL;
886 if (isl_aff_is_nan(aff))
887 return aff;
888 aff = isl_aff_cow(aff);
889 if (!aff)
890 return NULL;
892 aff->v = isl_vec_cow(aff->v);
893 if (!aff->v)
894 return isl_aff_free(aff);
896 isl_int_add(aff->v->el[1], aff->v->el[1], v);
898 return aff;
901 /* Add "v" to the numerator of the constant term of "aff".
903 * A NaN is unaffected by this operation.
905 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
907 isl_int t;
909 if (v == 0)
910 return aff;
912 isl_int_init(t);
913 isl_int_set_si(t, v);
914 aff = isl_aff_add_constant_num(aff, t);
915 isl_int_clear(t);
917 return aff;
920 /* Replace the numerator of the constant term of "aff" by "v".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
926 if (!aff)
927 return NULL;
928 if (isl_aff_is_nan(aff))
929 return aff;
930 aff = isl_aff_cow(aff);
931 if (!aff)
932 return NULL;
934 aff->v = isl_vec_cow(aff->v);
935 if (!aff->v)
936 return isl_aff_free(aff);
938 isl_int_set_si(aff->v->el[1], v);
940 return aff;
943 /* Replace the numerator of the coefficient of the variable of type "type"
944 * at position "pos" of "aff" by "v".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
949 enum isl_dim_type type, int pos, isl_int v)
951 if (!aff)
952 return NULL;
954 if (type == isl_dim_out)
955 isl_die(aff->v->ctx, isl_error_invalid,
956 "output/set dimension does not have a coefficient",
957 return isl_aff_free(aff));
958 if (type == isl_dim_in)
959 type = isl_dim_set;
961 if (pos >= isl_local_space_dim(aff->ls, type))
962 isl_die(aff->v->ctx, isl_error_invalid,
963 "position out of bounds", return isl_aff_free(aff));
965 if (isl_aff_is_nan(aff))
966 return aff;
967 aff = isl_aff_cow(aff);
968 if (!aff)
969 return NULL;
971 aff->v = isl_vec_cow(aff->v);
972 if (!aff->v)
973 return isl_aff_free(aff);
975 pos += isl_local_space_offset(aff->ls, type);
976 isl_int_set(aff->v->el[1 + pos], v);
978 return aff;
981 /* Replace the numerator of the coefficient of the variable of type "type"
982 * at position "pos" of "aff" by "v".
984 * A NaN is unaffected by this operation.
986 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
987 enum isl_dim_type type, int pos, int v)
989 if (!aff)
990 return NULL;
992 if (type == isl_dim_out)
993 isl_die(aff->v->ctx, isl_error_invalid,
994 "output/set dimension does not have a coefficient",
995 return isl_aff_free(aff));
996 if (type == isl_dim_in)
997 type = isl_dim_set;
999 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1000 isl_die(aff->v->ctx, isl_error_invalid,
1001 "position out of bounds", return isl_aff_free(aff));
1003 if (isl_aff_is_nan(aff))
1004 return aff;
1005 pos += isl_local_space_offset(aff->ls, type);
1006 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1007 return aff;
1009 aff = isl_aff_cow(aff);
1010 if (!aff)
1011 return NULL;
1013 aff->v = isl_vec_cow(aff->v);
1014 if (!aff->v)
1015 return isl_aff_free(aff);
1017 isl_int_set_si(aff->v->el[1 + pos], v);
1019 return aff;
1022 /* Replace the coefficient of the variable of type "type" at position "pos"
1023 * of "aff" by "v".
1025 * A NaN is unaffected by this operation.
1027 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1028 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1030 if (!aff || !v)
1031 goto error;
1033 if (type == isl_dim_out)
1034 isl_die(aff->v->ctx, isl_error_invalid,
1035 "output/set dimension does not have a coefficient",
1036 goto error);
1037 if (type == isl_dim_in)
1038 type = isl_dim_set;
1040 if (pos >= isl_local_space_dim(aff->ls, type))
1041 isl_die(aff->v->ctx, isl_error_invalid,
1042 "position out of bounds", goto error);
1044 if (isl_aff_is_nan(aff)) {
1045 isl_val_free(v);
1046 return aff;
1048 if (!isl_val_is_rat(v))
1049 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1050 "expecting rational value", goto error);
1052 pos += isl_local_space_offset(aff->ls, type);
1053 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1054 isl_int_eq(aff->v->el[0], v->d)) {
1055 isl_val_free(v);
1056 return aff;
1059 aff = isl_aff_cow(aff);
1060 if (!aff)
1061 goto error;
1062 aff->v = isl_vec_cow(aff->v);
1063 if (!aff->v)
1064 goto error;
1066 if (isl_int_eq(aff->v->el[0], v->d)) {
1067 isl_int_set(aff->v->el[1 + pos], v->n);
1068 } else if (isl_int_is_one(v->d)) {
1069 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1070 } else {
1071 isl_seq_scale(aff->v->el + 1,
1072 aff->v->el + 1, v->d, aff->v->size - 1);
1073 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1074 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1075 aff->v = isl_vec_normalize(aff->v);
1076 if (!aff->v)
1077 goto error;
1080 isl_val_free(v);
1081 return aff;
1082 error:
1083 isl_aff_free(aff);
1084 isl_val_free(v);
1085 return NULL;
1088 /* Add "v" to the coefficient of the variable of type "type"
1089 * at position "pos" of "aff".
1091 * A NaN is unaffected by this operation.
1093 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1094 enum isl_dim_type type, int pos, isl_int v)
1096 if (!aff)
1097 return NULL;
1099 if (type == isl_dim_out)
1100 isl_die(aff->v->ctx, isl_error_invalid,
1101 "output/set dimension does not have a coefficient",
1102 return isl_aff_free(aff));
1103 if (type == isl_dim_in)
1104 type = isl_dim_set;
1106 if (pos >= isl_local_space_dim(aff->ls, type))
1107 isl_die(aff->v->ctx, isl_error_invalid,
1108 "position out of bounds", return isl_aff_free(aff));
1110 if (isl_aff_is_nan(aff))
1111 return aff;
1112 aff = isl_aff_cow(aff);
1113 if (!aff)
1114 return NULL;
1116 aff->v = isl_vec_cow(aff->v);
1117 if (!aff->v)
1118 return isl_aff_free(aff);
1120 pos += isl_local_space_offset(aff->ls, type);
1121 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1123 return aff;
1126 /* Add "v" to the coefficient of the variable of type "type"
1127 * at position "pos" of "aff".
1129 * A NaN is unaffected by this operation.
1131 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1132 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1134 if (!aff || !v)
1135 goto error;
1137 if (isl_val_is_zero(v)) {
1138 isl_val_free(v);
1139 return aff;
1142 if (type == isl_dim_out)
1143 isl_die(aff->v->ctx, isl_error_invalid,
1144 "output/set dimension does not have a coefficient",
1145 goto error);
1146 if (type == isl_dim_in)
1147 type = isl_dim_set;
1149 if (pos >= isl_local_space_dim(aff->ls, type))
1150 isl_die(aff->v->ctx, isl_error_invalid,
1151 "position out of bounds", goto error);
1153 if (isl_aff_is_nan(aff)) {
1154 isl_val_free(v);
1155 return aff;
1157 if (!isl_val_is_rat(v))
1158 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1159 "expecting rational value", goto error);
1161 aff = isl_aff_cow(aff);
1162 if (!aff)
1163 goto error;
1165 aff->v = isl_vec_cow(aff->v);
1166 if (!aff->v)
1167 goto error;
1169 pos += isl_local_space_offset(aff->ls, type);
1170 if (isl_int_is_one(v->d)) {
1171 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1172 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1173 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1174 aff->v = isl_vec_normalize(aff->v);
1175 if (!aff->v)
1176 goto error;
1177 } else {
1178 isl_seq_scale(aff->v->el + 1,
1179 aff->v->el + 1, v->d, aff->v->size - 1);
1180 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1181 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1182 aff->v = isl_vec_normalize(aff->v);
1183 if (!aff->v)
1184 goto error;
1187 isl_val_free(v);
1188 return aff;
1189 error:
1190 isl_aff_free(aff);
1191 isl_val_free(v);
1192 return NULL;
1195 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1196 enum isl_dim_type type, int pos, int v)
1198 isl_int t;
1200 isl_int_init(t);
1201 isl_int_set_si(t, v);
1202 aff = isl_aff_add_coefficient(aff, type, pos, t);
1203 isl_int_clear(t);
1205 return aff;
1208 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1210 if (!aff)
1211 return NULL;
1213 return isl_local_space_get_div(aff->ls, pos);
1216 /* Return the negation of "aff".
1218 * As a special case, -NaN = NaN.
1220 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1222 if (!aff)
1223 return NULL;
1224 if (isl_aff_is_nan(aff))
1225 return aff;
1226 aff = isl_aff_cow(aff);
1227 if (!aff)
1228 return NULL;
1229 aff->v = isl_vec_cow(aff->v);
1230 if (!aff->v)
1231 return isl_aff_free(aff);
1233 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1235 return aff;
1238 /* Remove divs from the local space that do not appear in the affine
1239 * expression.
1240 * We currently only remove divs at the end.
1241 * Some intermediate divs may also not appear directly in the affine
1242 * expression, but we would also need to check that no other divs are
1243 * defined in terms of them.
1245 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1247 int pos;
1248 int off;
1249 int n;
1251 if (!aff)
1252 return NULL;
1254 n = isl_local_space_dim(aff->ls, isl_dim_div);
1255 off = isl_local_space_offset(aff->ls, isl_dim_div);
1257 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1258 if (pos == n)
1259 return aff;
1261 aff = isl_aff_cow(aff);
1262 if (!aff)
1263 return NULL;
1265 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1266 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1267 if (!aff->ls || !aff->v)
1268 return isl_aff_free(aff);
1270 return aff;
1273 /* Given two affine expressions "p" of length p_len (including the
1274 * denominator and the constant term) and "subs" of length subs_len,
1275 * plug in "subs" for the variable at position "pos".
1276 * The variables of "subs" and "p" are assumed to match up to subs_len,
1277 * but "p" may have additional variables.
1278 * "v" is an initialized isl_int that can be used internally.
1280 * In particular, if "p" represents the expression
1282 * (a i + g)/m
1284 * with i the variable at position "pos" and "subs" represents the expression
1286 * f/d
1288 * then the result represents the expression
1290 * (a f + d g)/(m d)
1293 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1294 int p_len, int subs_len, isl_int v)
1296 isl_int_set(v, p[1 + pos]);
1297 isl_int_set_si(p[1 + pos], 0);
1298 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1299 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1300 isl_int_mul(p[0], p[0], subs[0]);
1303 /* Look for any divs in the aff->ls with a denominator equal to one
1304 * and plug them into the affine expression and any subsequent divs
1305 * that may reference the div.
1307 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1309 int i, n;
1310 int len;
1311 isl_int v;
1312 isl_vec *vec;
1313 isl_local_space *ls;
1314 unsigned pos;
1316 if (!aff)
1317 return NULL;
1319 n = isl_local_space_dim(aff->ls, isl_dim_div);
1320 len = aff->v->size;
1321 for (i = 0; i < n; ++i) {
1322 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1323 continue;
1324 ls = isl_local_space_copy(aff->ls);
1325 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1326 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1327 vec = isl_vec_copy(aff->v);
1328 vec = isl_vec_cow(vec);
1329 if (!ls || !vec)
1330 goto error;
1332 isl_int_init(v);
1334 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1335 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1336 len, len, v);
1338 isl_int_clear(v);
1340 isl_vec_free(aff->v);
1341 aff->v = vec;
1342 isl_local_space_free(aff->ls);
1343 aff->ls = ls;
1346 return aff;
1347 error:
1348 isl_vec_free(vec);
1349 isl_local_space_free(ls);
1350 return isl_aff_free(aff);
1353 /* Look for any divs j that appear with a unit coefficient inside
1354 * the definitions of other divs i and plug them into the definitions
1355 * of the divs i.
1357 * In particular, an expression of the form
1359 * floor((f(..) + floor(g(..)/n))/m)
1361 * is simplified to
1363 * floor((n * f(..) + g(..))/(n * m))
1365 * This simplification is correct because we can move the expression
1366 * f(..) into the inner floor in the original expression to obtain
1368 * floor(floor((n * f(..) + g(..))/n)/m)
1370 * from which we can derive the simplified expression.
1372 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1374 int i, j, n;
1375 int off;
1377 if (!aff)
1378 return NULL;
1380 n = isl_local_space_dim(aff->ls, isl_dim_div);
1381 off = isl_local_space_offset(aff->ls, isl_dim_div);
1382 for (i = 1; i < n; ++i) {
1383 for (j = 0; j < i; ++j) {
1384 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1385 continue;
1386 aff->ls = isl_local_space_substitute_seq(aff->ls,
1387 isl_dim_div, j, aff->ls->div->row[j],
1388 aff->v->size, i, 1);
1389 if (!aff->ls)
1390 return isl_aff_free(aff);
1394 return aff;
1397 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1399 * Even though this function is only called on isl_affs with a single
1400 * reference, we are careful to only change aff->v and aff->ls together.
1402 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1404 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1405 isl_local_space *ls;
1406 isl_vec *v;
1408 ls = isl_local_space_copy(aff->ls);
1409 ls = isl_local_space_swap_div(ls, a, b);
1410 v = isl_vec_copy(aff->v);
1411 v = isl_vec_cow(v);
1412 if (!ls || !v)
1413 goto error;
1415 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1416 isl_vec_free(aff->v);
1417 aff->v = v;
1418 isl_local_space_free(aff->ls);
1419 aff->ls = ls;
1421 return aff;
1422 error:
1423 isl_vec_free(v);
1424 isl_local_space_free(ls);
1425 return isl_aff_free(aff);
1428 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1430 * We currently do not actually remove div "b", but simply add its
1431 * coefficient to that of "a" and then zero it out.
1433 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1435 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1437 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1438 return aff;
1440 aff->v = isl_vec_cow(aff->v);
1441 if (!aff->v)
1442 return isl_aff_free(aff);
1444 isl_int_add(aff->v->el[1 + off + a],
1445 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1446 isl_int_set_si(aff->v->el[1 + off + b], 0);
1448 return aff;
1451 /* Sort the divs in the local space of "aff" according to
1452 * the comparison function "cmp_row" in isl_local_space.c,
1453 * combining the coefficients of identical divs.
1455 * Reordering divs does not change the semantics of "aff",
1456 * so there is no need to call isl_aff_cow.
1457 * Moreover, this function is currently only called on isl_affs
1458 * with a single reference.
1460 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1462 int i, j, n;
1464 if (!aff)
1465 return NULL;
1467 n = isl_aff_dim(aff, isl_dim_div);
1468 for (i = 1; i < n; ++i) {
1469 for (j = i - 1; j >= 0; --j) {
1470 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1471 if (cmp < 0)
1472 break;
1473 if (cmp == 0)
1474 aff = merge_divs(aff, j, j + 1);
1475 else
1476 aff = swap_div(aff, j, j + 1);
1477 if (!aff)
1478 return NULL;
1482 return aff;
1485 /* Normalize the representation of "aff".
1487 * This function should only be called of "new" isl_affs, i.e.,
1488 * with only a single reference. We therefore do not need to
1489 * worry about affecting other instances.
1491 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1493 if (!aff)
1494 return NULL;
1495 aff->v = isl_vec_normalize(aff->v);
1496 if (!aff->v)
1497 return isl_aff_free(aff);
1498 aff = plug_in_integral_divs(aff);
1499 aff = plug_in_unit_divs(aff);
1500 aff = sort_divs(aff);
1501 aff = isl_aff_remove_unused_divs(aff);
1502 return aff;
1505 /* Given f, return floor(f).
1506 * If f is an integer expression, then just return f.
1507 * If f is a constant, then return the constant floor(f).
1508 * Otherwise, if f = g/m, write g = q m + r,
1509 * create a new div d = [r/m] and return the expression q + d.
1510 * The coefficients in r are taken to lie between -m/2 and m/2.
1512 * reduce_div_coefficients performs the same normalization.
1514 * As a special case, floor(NaN) = NaN.
1516 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1518 int i;
1519 int size;
1520 isl_ctx *ctx;
1521 isl_vec *div;
1523 if (!aff)
1524 return NULL;
1526 if (isl_aff_is_nan(aff))
1527 return aff;
1528 if (isl_int_is_one(aff->v->el[0]))
1529 return aff;
1531 aff = isl_aff_cow(aff);
1532 if (!aff)
1533 return NULL;
1535 aff->v = isl_vec_cow(aff->v);
1536 if (!aff->v)
1537 return isl_aff_free(aff);
1539 if (isl_aff_is_cst(aff)) {
1540 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1541 isl_int_set_si(aff->v->el[0], 1);
1542 return aff;
1545 div = isl_vec_copy(aff->v);
1546 div = isl_vec_cow(div);
1547 if (!div)
1548 return isl_aff_free(aff);
1550 ctx = isl_aff_get_ctx(aff);
1551 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1552 for (i = 1; i < aff->v->size; ++i) {
1553 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1554 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1555 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1556 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1557 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1561 aff->ls = isl_local_space_add_div(aff->ls, div);
1562 if (!aff->ls)
1563 return isl_aff_free(aff);
1565 size = aff->v->size;
1566 aff->v = isl_vec_extend(aff->v, size + 1);
1567 if (!aff->v)
1568 return isl_aff_free(aff);
1569 isl_int_set_si(aff->v->el[0], 1);
1570 isl_int_set_si(aff->v->el[size], 1);
1572 aff = isl_aff_normalize(aff);
1574 return aff;
1577 /* Compute
1579 * aff mod m = aff - m * floor(aff/m)
1581 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1583 isl_aff *res;
1585 res = isl_aff_copy(aff);
1586 aff = isl_aff_scale_down(aff, m);
1587 aff = isl_aff_floor(aff);
1588 aff = isl_aff_scale(aff, m);
1589 res = isl_aff_sub(res, aff);
1591 return res;
1594 /* Compute
1596 * aff mod m = aff - m * floor(aff/m)
1598 * with m an integer value.
1600 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1601 __isl_take isl_val *m)
1603 isl_aff *res;
1605 if (!aff || !m)
1606 goto error;
1608 if (!isl_val_is_int(m))
1609 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1610 "expecting integer modulo", goto error);
1612 res = isl_aff_copy(aff);
1613 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1614 aff = isl_aff_floor(aff);
1615 aff = isl_aff_scale_val(aff, m);
1616 res = isl_aff_sub(res, aff);
1618 return res;
1619 error:
1620 isl_aff_free(aff);
1621 isl_val_free(m);
1622 return NULL;
1625 /* Compute
1627 * pwaff mod m = pwaff - m * floor(pwaff/m)
1629 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1631 isl_pw_aff *res;
1633 res = isl_pw_aff_copy(pwaff);
1634 pwaff = isl_pw_aff_scale_down(pwaff, m);
1635 pwaff = isl_pw_aff_floor(pwaff);
1636 pwaff = isl_pw_aff_scale(pwaff, m);
1637 res = isl_pw_aff_sub(res, pwaff);
1639 return res;
1642 /* Compute
1644 * pa mod m = pa - m * floor(pa/m)
1646 * with m an integer value.
1648 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1649 __isl_take isl_val *m)
1651 if (!pa || !m)
1652 goto error;
1653 if (!isl_val_is_int(m))
1654 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1655 "expecting integer modulo", goto error);
1656 pa = isl_pw_aff_mod(pa, m->n);
1657 isl_val_free(m);
1658 return pa;
1659 error:
1660 isl_pw_aff_free(pa);
1661 isl_val_free(m);
1662 return NULL;
1665 /* Given f, return ceil(f).
1666 * If f is an integer expression, then just return f.
1667 * Otherwise, let f be the expression
1669 * e/m
1671 * then return
1673 * floor((e + m - 1)/m)
1675 * As a special case, ceil(NaN) = NaN.
1677 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1679 if (!aff)
1680 return NULL;
1682 if (isl_aff_is_nan(aff))
1683 return aff;
1684 if (isl_int_is_one(aff->v->el[0]))
1685 return aff;
1687 aff = isl_aff_cow(aff);
1688 if (!aff)
1689 return NULL;
1690 aff->v = isl_vec_cow(aff->v);
1691 if (!aff->v)
1692 return isl_aff_free(aff);
1694 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1695 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1696 aff = isl_aff_floor(aff);
1698 return aff;
1701 /* Apply the expansion computed by isl_merge_divs.
1702 * The expansion itself is given by "exp" while the resulting
1703 * list of divs is given by "div".
1705 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1706 __isl_take isl_mat *div, int *exp)
1708 int old_n_div;
1709 int new_n_div;
1710 int offset;
1712 aff = isl_aff_cow(aff);
1713 if (!aff || !div)
1714 goto error;
1716 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1717 new_n_div = isl_mat_rows(div);
1718 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1720 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1721 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1722 if (!aff->v || !aff->ls)
1723 return isl_aff_free(aff);
1724 return aff;
1725 error:
1726 isl_aff_free(aff);
1727 isl_mat_free(div);
1728 return NULL;
1731 /* Add two affine expressions that live in the same local space.
1733 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1734 __isl_take isl_aff *aff2)
1736 isl_int gcd, f;
1738 aff1 = isl_aff_cow(aff1);
1739 if (!aff1 || !aff2)
1740 goto error;
1742 aff1->v = isl_vec_cow(aff1->v);
1743 if (!aff1->v)
1744 goto error;
1746 isl_int_init(gcd);
1747 isl_int_init(f);
1748 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1749 isl_int_divexact(f, aff2->v->el[0], gcd);
1750 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1751 isl_int_divexact(f, aff1->v->el[0], gcd);
1752 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1753 isl_int_divexact(f, aff2->v->el[0], gcd);
1754 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1755 isl_int_clear(f);
1756 isl_int_clear(gcd);
1758 isl_aff_free(aff2);
1759 return aff1;
1760 error:
1761 isl_aff_free(aff1);
1762 isl_aff_free(aff2);
1763 return NULL;
1766 /* Return the sum of "aff1" and "aff2".
1768 * If either of the two is NaN, then the result is NaN.
1770 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1771 __isl_take isl_aff *aff2)
1773 isl_ctx *ctx;
1774 int *exp1 = NULL;
1775 int *exp2 = NULL;
1776 isl_mat *div;
1777 int n_div1, n_div2;
1779 if (!aff1 || !aff2)
1780 goto error;
1782 ctx = isl_aff_get_ctx(aff1);
1783 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1784 isl_die(ctx, isl_error_invalid,
1785 "spaces don't match", goto error);
1787 if (isl_aff_is_nan(aff1)) {
1788 isl_aff_free(aff2);
1789 return aff1;
1791 if (isl_aff_is_nan(aff2)) {
1792 isl_aff_free(aff1);
1793 return aff2;
1796 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1797 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1798 if (n_div1 == 0 && n_div2 == 0)
1799 return add_expanded(aff1, aff2);
1801 exp1 = isl_alloc_array(ctx, int, n_div1);
1802 exp2 = isl_alloc_array(ctx, int, n_div2);
1803 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1804 goto error;
1806 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1807 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1808 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1809 free(exp1);
1810 free(exp2);
1812 return add_expanded(aff1, aff2);
1813 error:
1814 free(exp1);
1815 free(exp2);
1816 isl_aff_free(aff1);
1817 isl_aff_free(aff2);
1818 return NULL;
1821 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1822 __isl_take isl_aff *aff2)
1824 return isl_aff_add(aff1, isl_aff_neg(aff2));
1827 /* Return the result of scaling "aff" by a factor of "f".
1829 * As a special case, f * NaN = NaN.
1831 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1833 isl_int gcd;
1835 if (!aff)
1836 return NULL;
1837 if (isl_aff_is_nan(aff))
1838 return aff;
1840 if (isl_int_is_one(f))
1841 return aff;
1843 aff = isl_aff_cow(aff);
1844 if (!aff)
1845 return NULL;
1846 aff->v = isl_vec_cow(aff->v);
1847 if (!aff->v)
1848 return isl_aff_free(aff);
1850 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1851 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1852 return aff;
1855 isl_int_init(gcd);
1856 isl_int_gcd(gcd, aff->v->el[0], f);
1857 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1858 isl_int_divexact(gcd, f, gcd);
1859 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1860 isl_int_clear(gcd);
1862 return aff;
1865 /* Multiple "aff" by "v".
1867 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1868 __isl_take isl_val *v)
1870 if (!aff || !v)
1871 goto error;
1873 if (isl_val_is_one(v)) {
1874 isl_val_free(v);
1875 return aff;
1878 if (!isl_val_is_rat(v))
1879 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1880 "expecting rational factor", goto error);
1882 aff = isl_aff_scale(aff, v->n);
1883 aff = isl_aff_scale_down(aff, v->d);
1885 isl_val_free(v);
1886 return aff;
1887 error:
1888 isl_aff_free(aff);
1889 isl_val_free(v);
1890 return NULL;
1893 /* Return the result of scaling "aff" down by a factor of "f".
1895 * As a special case, NaN/f = NaN.
1897 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1899 isl_int gcd;
1901 if (!aff)
1902 return NULL;
1903 if (isl_aff_is_nan(aff))
1904 return aff;
1906 if (isl_int_is_one(f))
1907 return aff;
1909 aff = isl_aff_cow(aff);
1910 if (!aff)
1911 return NULL;
1913 if (isl_int_is_zero(f))
1914 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1915 "cannot scale down by zero", return isl_aff_free(aff));
1917 aff->v = isl_vec_cow(aff->v);
1918 if (!aff->v)
1919 return isl_aff_free(aff);
1921 isl_int_init(gcd);
1922 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1923 isl_int_gcd(gcd, gcd, f);
1924 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1925 isl_int_divexact(gcd, f, gcd);
1926 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1927 isl_int_clear(gcd);
1929 return aff;
1932 /* Divide "aff" by "v".
1934 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1935 __isl_take isl_val *v)
1937 if (!aff || !v)
1938 goto error;
1940 if (isl_val_is_one(v)) {
1941 isl_val_free(v);
1942 return aff;
1945 if (!isl_val_is_rat(v))
1946 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1947 "expecting rational factor", goto error);
1948 if (!isl_val_is_pos(v))
1949 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1950 "factor needs to be positive", goto error);
1952 aff = isl_aff_scale(aff, v->d);
1953 aff = isl_aff_scale_down(aff, v->n);
1955 isl_val_free(v);
1956 return aff;
1957 error:
1958 isl_aff_free(aff);
1959 isl_val_free(v);
1960 return NULL;
1963 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1965 isl_int v;
1967 if (f == 1)
1968 return aff;
1970 isl_int_init(v);
1971 isl_int_set_ui(v, f);
1972 aff = isl_aff_scale_down(aff, v);
1973 isl_int_clear(v);
1975 return aff;
1978 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1979 enum isl_dim_type type, unsigned pos, const char *s)
1981 aff = isl_aff_cow(aff);
1982 if (!aff)
1983 return NULL;
1984 if (type == isl_dim_out)
1985 isl_die(aff->v->ctx, isl_error_invalid,
1986 "cannot set name of output/set dimension",
1987 return isl_aff_free(aff));
1988 if (type == isl_dim_in)
1989 type = isl_dim_set;
1990 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1991 if (!aff->ls)
1992 return isl_aff_free(aff);
1994 return aff;
1997 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1998 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2000 aff = isl_aff_cow(aff);
2001 if (!aff)
2002 goto error;
2003 if (type == isl_dim_out)
2004 isl_die(aff->v->ctx, isl_error_invalid,
2005 "cannot set name of output/set dimension",
2006 goto error);
2007 if (type == isl_dim_in)
2008 type = isl_dim_set;
2009 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2010 if (!aff->ls)
2011 return isl_aff_free(aff);
2013 return aff;
2014 error:
2015 isl_id_free(id);
2016 isl_aff_free(aff);
2017 return NULL;
2020 /* Replace the identifier of the input tuple of "aff" by "id".
2021 * type is currently required to be equal to isl_dim_in
2023 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2024 enum isl_dim_type type, __isl_take isl_id *id)
2026 aff = isl_aff_cow(aff);
2027 if (!aff)
2028 goto error;
2029 if (type != isl_dim_out)
2030 isl_die(aff->v->ctx, isl_error_invalid,
2031 "cannot only set id of input tuple", goto error);
2032 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2033 if (!aff->ls)
2034 return isl_aff_free(aff);
2036 return aff;
2037 error:
2038 isl_id_free(id);
2039 isl_aff_free(aff);
2040 return NULL;
2043 /* Exploit the equalities in "eq" to simplify the affine expression
2044 * and the expressions of the integer divisions in the local space.
2045 * The integer divisions in this local space are assumed to appear
2046 * as regular dimensions in "eq".
2048 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2049 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2051 int i, j;
2052 unsigned total;
2053 unsigned n_div;
2055 if (!eq)
2056 goto error;
2057 if (eq->n_eq == 0) {
2058 isl_basic_set_free(eq);
2059 return aff;
2062 aff = isl_aff_cow(aff);
2063 if (!aff)
2064 goto error;
2066 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2067 isl_basic_set_copy(eq));
2068 aff->v = isl_vec_cow(aff->v);
2069 if (!aff->ls || !aff->v)
2070 goto error;
2072 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2073 n_div = eq->n_div;
2074 for (i = 0; i < eq->n_eq; ++i) {
2075 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2076 if (j < 0 || j == 0 || j >= total)
2077 continue;
2079 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2080 &aff->v->el[0]);
2083 isl_basic_set_free(eq);
2084 aff = isl_aff_normalize(aff);
2085 return aff;
2086 error:
2087 isl_basic_set_free(eq);
2088 isl_aff_free(aff);
2089 return NULL;
2092 /* Exploit the equalities in "eq" to simplify the affine expression
2093 * and the expressions of the integer divisions in the local space.
2095 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2096 __isl_take isl_basic_set *eq)
2098 int n_div;
2100 if (!aff || !eq)
2101 goto error;
2102 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2103 if (n_div > 0)
2104 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2105 return isl_aff_substitute_equalities_lifted(aff, eq);
2106 error:
2107 isl_basic_set_free(eq);
2108 isl_aff_free(aff);
2109 return NULL;
2112 /* Look for equalities among the variables shared by context and aff
2113 * and the integer divisions of aff, if any.
2114 * The equalities are then used to eliminate coefficients and/or integer
2115 * divisions from aff.
2117 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2118 __isl_take isl_set *context)
2120 isl_basic_set *hull;
2121 int n_div;
2123 if (!aff)
2124 goto error;
2125 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2126 if (n_div > 0) {
2127 isl_basic_set *bset;
2128 isl_local_space *ls;
2129 context = isl_set_add_dims(context, isl_dim_set, n_div);
2130 ls = isl_aff_get_domain_local_space(aff);
2131 bset = isl_basic_set_from_local_space(ls);
2132 bset = isl_basic_set_lift(bset);
2133 bset = isl_basic_set_flatten(bset);
2134 context = isl_set_intersect(context,
2135 isl_set_from_basic_set(bset));
2138 hull = isl_set_affine_hull(context);
2139 return isl_aff_substitute_equalities_lifted(aff, hull);
2140 error:
2141 isl_aff_free(aff);
2142 isl_set_free(context);
2143 return NULL;
2146 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2147 __isl_take isl_set *context)
2149 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2150 dom_context = isl_set_intersect_params(dom_context, context);
2151 return isl_aff_gist(aff, dom_context);
2154 /* Return a basic set containing those elements in the space
2155 * of aff where it is positive. "rational" should not be set.
2157 * If "aff" is NaN, then it is not positive.
2159 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2160 int rational)
2162 isl_constraint *ineq;
2163 isl_basic_set *bset;
2164 isl_val *c;
2166 if (!aff)
2167 return NULL;
2168 if (isl_aff_is_nan(aff)) {
2169 isl_space *space = isl_aff_get_domain_space(aff);
2170 isl_aff_free(aff);
2171 return isl_basic_set_empty(space);
2173 if (rational)
2174 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2175 "rational sets not supported", goto error);
2177 ineq = isl_inequality_from_aff(aff);
2178 c = isl_constraint_get_constant_val(ineq);
2179 c = isl_val_sub_ui(c, 1);
2180 ineq = isl_constraint_set_constant_val(ineq, c);
2182 bset = isl_basic_set_from_constraint(ineq);
2183 bset = isl_basic_set_simplify(bset);
2184 return bset;
2185 error:
2186 isl_aff_free(aff);
2187 return NULL;
2190 /* Return a basic set containing those elements in the space
2191 * of aff where it is non-negative.
2192 * If "rational" is set, then return a rational basic set.
2194 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2196 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2197 __isl_take isl_aff *aff, int rational)
2199 isl_constraint *ineq;
2200 isl_basic_set *bset;
2202 if (!aff)
2203 return NULL;
2204 if (isl_aff_is_nan(aff)) {
2205 isl_space *space = isl_aff_get_domain_space(aff);
2206 isl_aff_free(aff);
2207 return isl_basic_set_empty(space);
2210 ineq = isl_inequality_from_aff(aff);
2212 bset = isl_basic_set_from_constraint(ineq);
2213 if (rational)
2214 bset = isl_basic_set_set_rational(bset);
2215 bset = isl_basic_set_simplify(bset);
2216 return bset;
2219 /* Return a basic set containing those elements in the space
2220 * of aff where it is non-negative.
2222 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2224 return aff_nonneg_basic_set(aff, 0);
2227 /* Return a basic set containing those elements in the domain space
2228 * of aff where it is negative.
2230 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2232 aff = isl_aff_neg(aff);
2233 aff = isl_aff_add_constant_num_si(aff, -1);
2234 return isl_aff_nonneg_basic_set(aff);
2237 /* Return a basic set containing those elements in the space
2238 * of aff where it is zero.
2239 * If "rational" is set, then return a rational basic set.
2241 * If "aff" is NaN, then it is not zero.
2243 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2244 int rational)
2246 isl_constraint *ineq;
2247 isl_basic_set *bset;
2249 if (!aff)
2250 return NULL;
2251 if (isl_aff_is_nan(aff)) {
2252 isl_space *space = isl_aff_get_domain_space(aff);
2253 isl_aff_free(aff);
2254 return isl_basic_set_empty(space);
2257 ineq = isl_equality_from_aff(aff);
2259 bset = isl_basic_set_from_constraint(ineq);
2260 if (rational)
2261 bset = isl_basic_set_set_rational(bset);
2262 bset = isl_basic_set_simplify(bset);
2263 return bset;
2266 /* Return a basic set containing those elements in the space
2267 * of aff where it is zero.
2269 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2271 return aff_zero_basic_set(aff, 0);
2274 /* Return a basic set containing those elements in the shared space
2275 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2277 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2278 __isl_take isl_aff *aff2)
2280 aff1 = isl_aff_sub(aff1, aff2);
2282 return isl_aff_nonneg_basic_set(aff1);
2285 /* Return a set containing those elements in the shared space
2286 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2288 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2289 __isl_take isl_aff *aff2)
2291 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2294 /* Return a basic 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_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2298 __isl_take isl_aff *aff2)
2300 return isl_aff_ge_basic_set(aff2, aff1);
2303 /* Return a set containing those elements in the shared space
2304 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2306 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2307 __isl_take isl_aff *aff2)
2309 return isl_aff_ge_set(aff2, aff1);
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 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2333 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2335 aff1 = isl_aff_add(aff1, aff2);
2336 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2337 return aff1;
2340 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2342 if (!aff)
2343 return -1;
2345 return 0;
2348 /* Check whether the given affine expression has non-zero coefficient
2349 * for any dimension in the given range or if any of these dimensions
2350 * appear with non-zero coefficients in any of the integer divisions
2351 * involved in the affine expression.
2353 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2354 enum isl_dim_type type, unsigned first, unsigned n)
2356 int i;
2357 isl_ctx *ctx;
2358 int *active = NULL;
2359 isl_bool involves = isl_bool_false;
2361 if (!aff)
2362 return isl_bool_error;
2363 if (n == 0)
2364 return isl_bool_false;
2366 ctx = isl_aff_get_ctx(aff);
2367 if (first + n > isl_aff_dim(aff, type))
2368 isl_die(ctx, isl_error_invalid,
2369 "range out of bounds", return isl_bool_error);
2371 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2372 if (!active)
2373 goto error;
2375 first += isl_local_space_offset(aff->ls, type) - 1;
2376 for (i = 0; i < n; ++i)
2377 if (active[first + i]) {
2378 involves = isl_bool_true;
2379 break;
2382 free(active);
2384 return involves;
2385 error:
2386 free(active);
2387 return isl_bool_error;
2390 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2391 enum isl_dim_type type, unsigned first, unsigned n)
2393 isl_ctx *ctx;
2395 if (!aff)
2396 return NULL;
2397 if (type == isl_dim_out)
2398 isl_die(aff->v->ctx, isl_error_invalid,
2399 "cannot drop output/set dimension",
2400 return isl_aff_free(aff));
2401 if (type == isl_dim_in)
2402 type = isl_dim_set;
2403 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2404 return aff;
2406 ctx = isl_aff_get_ctx(aff);
2407 if (first + n > isl_local_space_dim(aff->ls, type))
2408 isl_die(ctx, isl_error_invalid, "range out of bounds",
2409 return isl_aff_free(aff));
2411 aff = isl_aff_cow(aff);
2412 if (!aff)
2413 return NULL;
2415 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2416 if (!aff->ls)
2417 return isl_aff_free(aff);
2419 first += 1 + isl_local_space_offset(aff->ls, type);
2420 aff->v = isl_vec_drop_els(aff->v, first, n);
2421 if (!aff->v)
2422 return isl_aff_free(aff);
2424 return aff;
2427 /* Project the domain of the affine expression onto its parameter space.
2428 * The affine expression may not involve any of the domain dimensions.
2430 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2432 isl_space *space;
2433 unsigned n;
2434 int involves;
2436 n = isl_aff_dim(aff, isl_dim_in);
2437 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2438 if (involves < 0)
2439 return isl_aff_free(aff);
2440 if (involves)
2441 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2442 "affine expression involves some of the domain dimensions",
2443 return isl_aff_free(aff));
2444 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2445 space = isl_aff_get_domain_space(aff);
2446 space = isl_space_params(space);
2447 aff = isl_aff_reset_domain_space(aff, space);
2448 return aff;
2451 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2452 enum isl_dim_type type, unsigned first, unsigned n)
2454 isl_ctx *ctx;
2456 if (!aff)
2457 return NULL;
2458 if (type == isl_dim_out)
2459 isl_die(aff->v->ctx, isl_error_invalid,
2460 "cannot insert output/set dimensions",
2461 return isl_aff_free(aff));
2462 if (type == isl_dim_in)
2463 type = isl_dim_set;
2464 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2465 return aff;
2467 ctx = isl_aff_get_ctx(aff);
2468 if (first > isl_local_space_dim(aff->ls, type))
2469 isl_die(ctx, isl_error_invalid, "position out of bounds",
2470 return isl_aff_free(aff));
2472 aff = isl_aff_cow(aff);
2473 if (!aff)
2474 return NULL;
2476 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2477 if (!aff->ls)
2478 return isl_aff_free(aff);
2480 first += 1 + isl_local_space_offset(aff->ls, type);
2481 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2482 if (!aff->v)
2483 return isl_aff_free(aff);
2485 return aff;
2488 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2489 enum isl_dim_type type, unsigned n)
2491 unsigned pos;
2493 pos = isl_aff_dim(aff, type);
2495 return isl_aff_insert_dims(aff, type, pos, n);
2498 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2499 enum isl_dim_type type, unsigned n)
2501 unsigned pos;
2503 pos = isl_pw_aff_dim(pwaff, type);
2505 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2508 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2509 * to dimensions of "dst_type" at "dst_pos".
2511 * We only support moving input dimensions to parameters and vice versa.
2513 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2514 enum isl_dim_type dst_type, unsigned dst_pos,
2515 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2517 unsigned g_dst_pos;
2518 unsigned g_src_pos;
2520 if (!aff)
2521 return NULL;
2522 if (n == 0 &&
2523 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2524 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2525 return aff;
2527 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2528 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2529 "cannot move output/set dimension",
2530 return isl_aff_free(aff));
2531 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2532 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2533 "cannot move divs", return isl_aff_free(aff));
2534 if (dst_type == isl_dim_in)
2535 dst_type = isl_dim_set;
2536 if (src_type == isl_dim_in)
2537 src_type = isl_dim_set;
2539 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2540 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2541 "range out of bounds", return isl_aff_free(aff));
2542 if (dst_type == src_type)
2543 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2544 "moving dims within the same type not supported",
2545 return isl_aff_free(aff));
2547 aff = isl_aff_cow(aff);
2548 if (!aff)
2549 return NULL;
2551 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2552 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2553 if (dst_type > src_type)
2554 g_dst_pos -= n;
2556 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2557 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2558 src_type, src_pos, n);
2559 if (!aff->v || !aff->ls)
2560 return isl_aff_free(aff);
2562 aff = sort_divs(aff);
2564 return aff;
2567 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2569 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2570 return isl_pw_aff_alloc(dom, aff);
2573 #define isl_aff_involves_nan isl_aff_is_nan
2575 #undef PW
2576 #define PW isl_pw_aff
2577 #undef EL
2578 #define EL isl_aff
2579 #undef EL_IS_ZERO
2580 #define EL_IS_ZERO is_empty
2581 #undef ZERO
2582 #define ZERO empty
2583 #undef IS_ZERO
2584 #define IS_ZERO is_empty
2585 #undef FIELD
2586 #define FIELD aff
2587 #undef DEFAULT_IS_ZERO
2588 #define DEFAULT_IS_ZERO 0
2590 #define NO_EVAL
2591 #define NO_OPT
2592 #define NO_LIFT
2593 #define NO_MORPH
2595 #include <isl_pw_templ.c>
2596 #include <isl_pw_hash.c>
2597 #include <isl_pw_union_opt.c>
2599 #undef UNION
2600 #define UNION isl_union_pw_aff
2601 #undef PART
2602 #define PART isl_pw_aff
2603 #undef PARTS
2604 #define PARTS pw_aff
2606 #include <isl_union_single.c>
2607 #include <isl_union_neg.c>
2609 static __isl_give isl_set *align_params_pw_pw_set_and(
2610 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2611 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2612 __isl_take isl_pw_aff *pwaff2))
2614 if (!pwaff1 || !pwaff2)
2615 goto error;
2616 if (isl_space_match(pwaff1->dim, isl_dim_param,
2617 pwaff2->dim, isl_dim_param))
2618 return fn(pwaff1, pwaff2);
2619 if (!isl_space_has_named_params(pwaff1->dim) ||
2620 !isl_space_has_named_params(pwaff2->dim))
2621 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2622 "unaligned unnamed parameters", goto error);
2623 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2624 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2625 return fn(pwaff1, pwaff2);
2626 error:
2627 isl_pw_aff_free(pwaff1);
2628 isl_pw_aff_free(pwaff2);
2629 return NULL;
2632 /* Align the parameters of the to isl_pw_aff arguments and
2633 * then apply a function "fn" on them that returns an isl_map.
2635 static __isl_give isl_map *align_params_pw_pw_map_and(
2636 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2637 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2638 __isl_take isl_pw_aff *pa2))
2640 if (!pa1 || !pa2)
2641 goto error;
2642 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2643 return fn(pa1, pa2);
2644 if (!isl_space_has_named_params(pa1->dim) ||
2645 !isl_space_has_named_params(pa2->dim))
2646 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2647 "unaligned unnamed parameters", goto error);
2648 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2649 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2650 return fn(pa1, pa2);
2651 error:
2652 isl_pw_aff_free(pa1);
2653 isl_pw_aff_free(pa2);
2654 return NULL;
2657 /* Compute a piecewise quasi-affine expression with a domain that
2658 * is the union of those of pwaff1 and pwaff2 and such that on each
2659 * cell, the quasi-affine expression is the maximum of those of pwaff1
2660 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2661 * cell, then the associated expression is the defined one.
2663 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2664 __isl_take isl_pw_aff *pwaff2)
2666 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2669 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2670 __isl_take isl_pw_aff *pwaff2)
2672 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2673 &pw_aff_union_max);
2676 /* Compute a piecewise quasi-affine expression with a domain that
2677 * is the union of those of pwaff1 and pwaff2 and such that on each
2678 * cell, the quasi-affine expression is the minimum of those of pwaff1
2679 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2680 * cell, then the associated expression is the defined one.
2682 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2683 __isl_take isl_pw_aff *pwaff2)
2685 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2688 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2689 __isl_take isl_pw_aff *pwaff2)
2691 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2692 &pw_aff_union_min);
2695 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2696 __isl_take isl_pw_aff *pwaff2, int max)
2698 if (max)
2699 return isl_pw_aff_union_max(pwaff1, pwaff2);
2700 else
2701 return isl_pw_aff_union_min(pwaff1, pwaff2);
2704 /* Construct a map with as domain the domain of pwaff and
2705 * one-dimensional range corresponding to the affine expressions.
2707 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2709 int i;
2710 isl_space *dim;
2711 isl_map *map;
2713 if (!pwaff)
2714 return NULL;
2716 dim = isl_pw_aff_get_space(pwaff);
2717 map = isl_map_empty(dim);
2719 for (i = 0; i < pwaff->n; ++i) {
2720 isl_basic_map *bmap;
2721 isl_map *map_i;
2723 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2724 map_i = isl_map_from_basic_map(bmap);
2725 map_i = isl_map_intersect_domain(map_i,
2726 isl_set_copy(pwaff->p[i].set));
2727 map = isl_map_union_disjoint(map, map_i);
2730 isl_pw_aff_free(pwaff);
2732 return map;
2735 /* Construct a map with as domain the domain of pwaff and
2736 * one-dimensional range corresponding to the affine expressions.
2738 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2740 if (!pwaff)
2741 return NULL;
2742 if (isl_space_is_set(pwaff->dim))
2743 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2744 "space of input is not a map", goto error);
2745 return map_from_pw_aff(pwaff);
2746 error:
2747 isl_pw_aff_free(pwaff);
2748 return NULL;
2751 /* Construct a one-dimensional set with as parameter domain
2752 * the domain of pwaff and the single set dimension
2753 * corresponding to the affine expressions.
2755 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2757 if (!pwaff)
2758 return NULL;
2759 if (!isl_space_is_set(pwaff->dim))
2760 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2761 "space of input is not a set", goto error);
2762 return map_from_pw_aff(pwaff);
2763 error:
2764 isl_pw_aff_free(pwaff);
2765 return NULL;
2768 /* Return a set containing those elements in the domain
2769 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2770 * does not satisfy "fn" (if complement is 1).
2772 * The pieces with a NaN never belong to the result since
2773 * NaN does not satisfy any property.
2775 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2776 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2777 int complement)
2779 int i;
2780 isl_set *set;
2782 if (!pwaff)
2783 return NULL;
2785 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2787 for (i = 0; i < pwaff->n; ++i) {
2788 isl_basic_set *bset;
2789 isl_set *set_i, *locus;
2790 isl_bool rational;
2792 if (isl_aff_is_nan(pwaff->p[i].aff))
2793 continue;
2795 rational = isl_set_has_rational(pwaff->p[i].set);
2796 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2797 locus = isl_set_from_basic_set(bset);
2798 set_i = isl_set_copy(pwaff->p[i].set);
2799 if (complement)
2800 set_i = isl_set_subtract(set_i, locus);
2801 else
2802 set_i = isl_set_intersect(set_i, locus);
2803 set = isl_set_union_disjoint(set, set_i);
2806 isl_pw_aff_free(pwaff);
2808 return set;
2811 /* Return a set containing those elements in the domain
2812 * of "pa" where it is positive.
2814 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2816 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2819 /* Return a set containing those elements in the domain
2820 * of pwaff where it is non-negative.
2822 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2824 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2827 /* Return a set containing those elements in the domain
2828 * of pwaff where it is zero.
2830 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2832 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2835 /* Return a set containing those elements in the domain
2836 * of pwaff where it is not zero.
2838 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2840 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2843 /* Return a set containing those elements in the shared domain
2844 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2846 * We compute the difference on the shared domain and then construct
2847 * the set of values where this difference is non-negative.
2848 * If strict is set, we first subtract 1 from the difference.
2849 * If equal is set, we only return the elements where pwaff1 and pwaff2
2850 * are equal.
2852 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2853 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2855 isl_set *set1, *set2;
2857 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2858 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2859 set1 = isl_set_intersect(set1, set2);
2860 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2861 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2862 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2864 if (strict) {
2865 isl_space *dim = isl_set_get_space(set1);
2866 isl_aff *aff;
2867 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2868 aff = isl_aff_add_constant_si(aff, -1);
2869 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2870 } else
2871 isl_set_free(set1);
2873 if (equal)
2874 return isl_pw_aff_zero_set(pwaff1);
2875 return isl_pw_aff_nonneg_set(pwaff1);
2878 /* Return a set containing those elements in the shared domain
2879 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2881 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2882 __isl_take isl_pw_aff *pwaff2)
2884 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2887 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2888 __isl_take isl_pw_aff *pwaff2)
2890 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2893 /* Return a set containing those elements in the shared domain
2894 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2896 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2897 __isl_take isl_pw_aff *pwaff2)
2899 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2902 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2903 __isl_take isl_pw_aff *pwaff2)
2905 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2908 /* Return a set containing those elements in the shared domain
2909 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2911 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2912 __isl_take isl_pw_aff *pwaff2)
2914 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2917 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2918 __isl_take isl_pw_aff *pwaff2)
2920 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2923 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2924 __isl_take isl_pw_aff *pwaff2)
2926 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2929 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2930 __isl_take isl_pw_aff *pwaff2)
2932 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2935 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2936 * where the function values are ordered in the same way as "order",
2937 * which returns a set in the shared domain of its two arguments.
2938 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2940 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2941 * We first pull back the two functions such that they are defined on
2942 * the domain [A -> B]. Then we apply "order", resulting in a set
2943 * in the space [A -> B]. Finally, we unwrap this set to obtain
2944 * a map in the space A -> B.
2946 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2947 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2948 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2949 __isl_take isl_pw_aff *pa2))
2951 isl_space *space1, *space2;
2952 isl_multi_aff *ma;
2953 isl_set *set;
2955 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2956 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2957 space1 = isl_space_map_from_domain_and_range(space1, space2);
2958 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2959 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2960 ma = isl_multi_aff_range_map(space1);
2961 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2962 set = order(pa1, pa2);
2964 return isl_set_unwrap(set);
2967 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2968 * where the function values are equal.
2969 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2971 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2972 __isl_take isl_pw_aff *pa2)
2974 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2977 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2978 * where the function values are equal.
2980 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
2981 __isl_take isl_pw_aff *pa2)
2983 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
2986 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2987 * where the function value of "pa1" is less than the function value of "pa2".
2988 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2990 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
2991 __isl_take isl_pw_aff *pa2)
2993 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
2996 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2997 * where the function value of "pa1" is less than the function value of "pa2".
2999 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3000 __isl_take isl_pw_aff *pa2)
3002 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3005 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3006 * where the function value of "pa1" is greater than the function value
3007 * of "pa2".
3008 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3010 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3011 __isl_take isl_pw_aff *pa2)
3013 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3016 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3017 * where the function value of "pa1" is greater than the function value
3018 * of "pa2".
3020 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3021 __isl_take isl_pw_aff *pa2)
3023 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3026 /* Return a set containing those elements in the shared domain
3027 * of the elements of list1 and list2 where each element in list1
3028 * has the relation specified by "fn" with each element in list2.
3030 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3031 __isl_take isl_pw_aff_list *list2,
3032 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3033 __isl_take isl_pw_aff *pwaff2))
3035 int i, j;
3036 isl_ctx *ctx;
3037 isl_set *set;
3039 if (!list1 || !list2)
3040 goto error;
3042 ctx = isl_pw_aff_list_get_ctx(list1);
3043 if (list1->n < 1 || list2->n < 1)
3044 isl_die(ctx, isl_error_invalid,
3045 "list should contain at least one element", goto error);
3047 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3048 for (i = 0; i < list1->n; ++i)
3049 for (j = 0; j < list2->n; ++j) {
3050 isl_set *set_ij;
3052 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3053 isl_pw_aff_copy(list2->p[j]));
3054 set = isl_set_intersect(set, set_ij);
3057 isl_pw_aff_list_free(list1);
3058 isl_pw_aff_list_free(list2);
3059 return set;
3060 error:
3061 isl_pw_aff_list_free(list1);
3062 isl_pw_aff_list_free(list2);
3063 return NULL;
3066 /* Return a set containing those elements in the shared domain
3067 * of the elements of list1 and list2 where each element in list1
3068 * is equal to each element in list2.
3070 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3071 __isl_take isl_pw_aff_list *list2)
3073 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3076 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3077 __isl_take isl_pw_aff_list *list2)
3079 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3082 /* Return a set containing those elements in the shared domain
3083 * of the elements of list1 and list2 where each element in list1
3084 * is less than or equal to each element in list2.
3086 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3087 __isl_take isl_pw_aff_list *list2)
3089 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3092 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3093 __isl_take isl_pw_aff_list *list2)
3095 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3098 __isl_give isl_set *isl_pw_aff_list_ge_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_ge_set);
3104 __isl_give isl_set *isl_pw_aff_list_gt_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_gt_set);
3111 /* Return a set containing those elements in the shared domain
3112 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3114 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3115 __isl_take isl_pw_aff *pwaff2)
3117 isl_set *set_lt, *set_gt;
3119 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3120 isl_pw_aff_copy(pwaff2));
3121 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3122 return isl_set_union_disjoint(set_lt, set_gt);
3125 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3126 __isl_take isl_pw_aff *pwaff2)
3128 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3131 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3132 isl_int v)
3134 int i;
3136 if (isl_int_is_one(v))
3137 return pwaff;
3138 if (!isl_int_is_pos(v))
3139 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3140 "factor needs to be positive",
3141 return isl_pw_aff_free(pwaff));
3142 pwaff = isl_pw_aff_cow(pwaff);
3143 if (!pwaff)
3144 return NULL;
3145 if (pwaff->n == 0)
3146 return pwaff;
3148 for (i = 0; i < pwaff->n; ++i) {
3149 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3150 if (!pwaff->p[i].aff)
3151 return isl_pw_aff_free(pwaff);
3154 return pwaff;
3157 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3159 int i;
3161 pwaff = isl_pw_aff_cow(pwaff);
3162 if (!pwaff)
3163 return NULL;
3164 if (pwaff->n == 0)
3165 return pwaff;
3167 for (i = 0; i < pwaff->n; ++i) {
3168 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3169 if (!pwaff->p[i].aff)
3170 return isl_pw_aff_free(pwaff);
3173 return pwaff;
3176 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3178 int i;
3180 pwaff = isl_pw_aff_cow(pwaff);
3181 if (!pwaff)
3182 return NULL;
3183 if (pwaff->n == 0)
3184 return pwaff;
3186 for (i = 0; i < pwaff->n; ++i) {
3187 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3188 if (!pwaff->p[i].aff)
3189 return isl_pw_aff_free(pwaff);
3192 return pwaff;
3195 /* Assuming that "cond1" and "cond2" are disjoint,
3196 * return an affine expression that is equal to pwaff1 on cond1
3197 * and to pwaff2 on cond2.
3199 static __isl_give isl_pw_aff *isl_pw_aff_select(
3200 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3201 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3203 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3204 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3206 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3209 /* Return an affine expression that is equal to pwaff_true for elements
3210 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3211 * is zero.
3212 * That is, return cond ? pwaff_true : pwaff_false;
3214 * If "cond" involves and NaN, then we conservatively return a NaN
3215 * on its entire domain. In principle, we could consider the pieces
3216 * where it is NaN separately from those where it is not.
3218 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3219 * then only use the domain of "cond" to restrict the domain.
3221 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3222 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3224 isl_set *cond_true, *cond_false;
3225 isl_bool equal;
3227 if (!cond)
3228 goto error;
3229 if (isl_pw_aff_involves_nan(cond)) {
3230 isl_space *space = isl_pw_aff_get_domain_space(cond);
3231 isl_local_space *ls = isl_local_space_from_space(space);
3232 isl_pw_aff_free(cond);
3233 isl_pw_aff_free(pwaff_true);
3234 isl_pw_aff_free(pwaff_false);
3235 return isl_pw_aff_nan_on_domain(ls);
3238 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3239 isl_pw_aff_get_space(pwaff_false));
3240 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3241 isl_pw_aff_get_space(pwaff_true));
3242 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3243 if (equal < 0)
3244 goto error;
3245 if (equal) {
3246 isl_set *dom;
3248 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3249 isl_pw_aff_free(pwaff_false);
3250 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3253 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3254 cond_false = isl_pw_aff_zero_set(cond);
3255 return isl_pw_aff_select(cond_true, pwaff_true,
3256 cond_false, pwaff_false);
3257 error:
3258 isl_pw_aff_free(cond);
3259 isl_pw_aff_free(pwaff_true);
3260 isl_pw_aff_free(pwaff_false);
3261 return NULL;
3264 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3266 if (!aff)
3267 return isl_bool_error;
3269 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3272 /* Check whether pwaff is a piecewise constant.
3274 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3276 int i;
3278 if (!pwaff)
3279 return isl_bool_error;
3281 for (i = 0; i < pwaff->n; ++i) {
3282 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3283 if (is_cst < 0 || !is_cst)
3284 return is_cst;
3287 return isl_bool_true;
3290 /* Are all elements of "mpa" piecewise constants?
3292 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3294 int i;
3296 if (!mpa)
3297 return isl_bool_error;
3299 for (i = 0; i < mpa->n; ++i) {
3300 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3301 if (is_cst < 0 || !is_cst)
3302 return is_cst;
3305 return isl_bool_true;
3308 /* Return the product of "aff1" and "aff2".
3310 * If either of the two is NaN, then the result is NaN.
3312 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3314 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3315 __isl_take isl_aff *aff2)
3317 if (!aff1 || !aff2)
3318 goto error;
3320 if (isl_aff_is_nan(aff1)) {
3321 isl_aff_free(aff2);
3322 return aff1;
3324 if (isl_aff_is_nan(aff2)) {
3325 isl_aff_free(aff1);
3326 return aff2;
3329 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3330 return isl_aff_mul(aff2, aff1);
3332 if (!isl_aff_is_cst(aff2))
3333 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3334 "at least one affine expression should be constant",
3335 goto error);
3337 aff1 = isl_aff_cow(aff1);
3338 if (!aff1 || !aff2)
3339 goto error;
3341 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3342 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3344 isl_aff_free(aff2);
3345 return aff1;
3346 error:
3347 isl_aff_free(aff1);
3348 isl_aff_free(aff2);
3349 return NULL;
3352 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3354 * If either of the two is NaN, then the result is NaN.
3356 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3357 __isl_take isl_aff *aff2)
3359 int is_cst;
3360 int neg;
3362 if (!aff1 || !aff2)
3363 goto error;
3365 if (isl_aff_is_nan(aff1)) {
3366 isl_aff_free(aff2);
3367 return aff1;
3369 if (isl_aff_is_nan(aff2)) {
3370 isl_aff_free(aff1);
3371 return aff2;
3374 is_cst = isl_aff_is_cst(aff2);
3375 if (is_cst < 0)
3376 goto error;
3377 if (!is_cst)
3378 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3379 "second argument should be a constant", goto error);
3381 if (!aff2)
3382 goto error;
3384 neg = isl_int_is_neg(aff2->v->el[1]);
3385 if (neg) {
3386 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3387 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3390 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3391 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3393 if (neg) {
3394 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3395 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3398 isl_aff_free(aff2);
3399 return aff1;
3400 error:
3401 isl_aff_free(aff1);
3402 isl_aff_free(aff2);
3403 return NULL;
3406 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3407 __isl_take isl_pw_aff *pwaff2)
3409 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3412 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3413 __isl_take isl_pw_aff *pwaff2)
3415 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3418 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3419 __isl_take isl_pw_aff *pwaff2)
3421 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3424 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3425 __isl_take isl_pw_aff *pwaff2)
3427 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3430 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3431 __isl_take isl_pw_aff *pwaff2)
3433 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3436 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3437 __isl_take isl_pw_aff *pa2)
3439 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3442 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3444 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3445 __isl_take isl_pw_aff *pa2)
3447 int is_cst;
3449 is_cst = isl_pw_aff_is_cst(pa2);
3450 if (is_cst < 0)
3451 goto error;
3452 if (!is_cst)
3453 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3454 "second argument should be a piecewise constant",
3455 goto error);
3456 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3457 error:
3458 isl_pw_aff_free(pa1);
3459 isl_pw_aff_free(pa2);
3460 return NULL;
3463 /* Compute the quotient of the integer division of "pa1" by "pa2"
3464 * with rounding towards zero.
3465 * "pa2" is assumed to be a piecewise constant.
3467 * In particular, return
3469 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3472 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3473 __isl_take isl_pw_aff *pa2)
3475 int is_cst;
3476 isl_set *cond;
3477 isl_pw_aff *f, *c;
3479 is_cst = isl_pw_aff_is_cst(pa2);
3480 if (is_cst < 0)
3481 goto error;
3482 if (!is_cst)
3483 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3484 "second argument should be a piecewise constant",
3485 goto error);
3487 pa1 = isl_pw_aff_div(pa1, pa2);
3489 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3490 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3491 c = isl_pw_aff_ceil(pa1);
3492 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3493 error:
3494 isl_pw_aff_free(pa1);
3495 isl_pw_aff_free(pa2);
3496 return NULL;
3499 /* Compute the remainder of the integer division of "pa1" by "pa2"
3500 * with rounding towards zero.
3501 * "pa2" is assumed to be a piecewise constant.
3503 * In particular, return
3505 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3508 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3509 __isl_take isl_pw_aff *pa2)
3511 int is_cst;
3512 isl_pw_aff *res;
3514 is_cst = isl_pw_aff_is_cst(pa2);
3515 if (is_cst < 0)
3516 goto error;
3517 if (!is_cst)
3518 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3519 "second argument should be a piecewise constant",
3520 goto error);
3521 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3522 res = isl_pw_aff_mul(pa2, res);
3523 res = isl_pw_aff_sub(pa1, res);
3524 return res;
3525 error:
3526 isl_pw_aff_free(pa1);
3527 isl_pw_aff_free(pa2);
3528 return NULL;
3531 /* Does either of "pa1" or "pa2" involve any NaN2?
3533 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3534 __isl_keep isl_pw_aff *pa2)
3536 isl_bool has_nan;
3538 has_nan = isl_pw_aff_involves_nan(pa1);
3539 if (has_nan < 0 || has_nan)
3540 return has_nan;
3541 return isl_pw_aff_involves_nan(pa2);
3544 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3545 * by a NaN on their shared domain.
3547 * In principle, the result could be refined to only being NaN
3548 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3550 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3551 __isl_take isl_pw_aff *pa2)
3553 isl_local_space *ls;
3554 isl_set *dom;
3555 isl_pw_aff *pa;
3557 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3558 ls = isl_local_space_from_space(isl_set_get_space(dom));
3559 pa = isl_pw_aff_nan_on_domain(ls);
3560 pa = isl_pw_aff_intersect_domain(pa, dom);
3562 return pa;
3565 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3566 __isl_take isl_pw_aff *pwaff2)
3568 isl_set *le;
3569 isl_set *dom;
3571 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3572 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3573 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3574 isl_pw_aff_copy(pwaff2));
3575 dom = isl_set_subtract(dom, isl_set_copy(le));
3576 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3579 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3580 __isl_take isl_pw_aff *pwaff2)
3582 isl_set *ge;
3583 isl_set *dom;
3585 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3586 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3587 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3588 isl_pw_aff_copy(pwaff2));
3589 dom = isl_set_subtract(dom, isl_set_copy(ge));
3590 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3593 /* Return an expression for the minimum (if "max" is not set) or
3594 * the maximum (if "max" is set) of "pa1" and "pa2".
3595 * If either expression involves any NaN, then return a NaN
3596 * on the shared domain as result.
3598 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3599 __isl_take isl_pw_aff *pa2, int max)
3601 isl_bool has_nan;
3603 has_nan = either_involves_nan(pa1, pa2);
3604 if (has_nan < 0)
3605 pa1 = isl_pw_aff_free(pa1);
3606 else if (has_nan)
3607 return replace_by_nan(pa1, pa2);
3609 if (max)
3610 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3611 else
3612 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3615 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3617 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3618 __isl_take isl_pw_aff *pwaff2)
3620 return pw_aff_min_max(pwaff1, pwaff2, 0);
3623 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3625 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3626 __isl_take isl_pw_aff *pwaff2)
3628 return pw_aff_min_max(pwaff1, pwaff2, 1);
3631 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3632 __isl_take isl_pw_aff_list *list,
3633 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3634 __isl_take isl_pw_aff *pwaff2))
3636 int i;
3637 isl_ctx *ctx;
3638 isl_pw_aff *res;
3640 if (!list)
3641 return NULL;
3643 ctx = isl_pw_aff_list_get_ctx(list);
3644 if (list->n < 1)
3645 isl_die(ctx, isl_error_invalid,
3646 "list should contain at least one element", goto error);
3648 res = isl_pw_aff_copy(list->p[0]);
3649 for (i = 1; i < list->n; ++i)
3650 res = fn(res, isl_pw_aff_copy(list->p[i]));
3652 isl_pw_aff_list_free(list);
3653 return res;
3654 error:
3655 isl_pw_aff_list_free(list);
3656 return NULL;
3659 /* Return an isl_pw_aff that maps each element in the intersection of the
3660 * domains of the elements of list to the minimal corresponding affine
3661 * expression.
3663 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3665 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3668 /* Return an isl_pw_aff that maps each element in the intersection of the
3669 * domains of the elements of list to the maximal corresponding affine
3670 * expression.
3672 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3674 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3677 /* Mark the domains of "pwaff" as rational.
3679 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3681 int i;
3683 pwaff = isl_pw_aff_cow(pwaff);
3684 if (!pwaff)
3685 return NULL;
3686 if (pwaff->n == 0)
3687 return pwaff;
3689 for (i = 0; i < pwaff->n; ++i) {
3690 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3691 if (!pwaff->p[i].set)
3692 return isl_pw_aff_free(pwaff);
3695 return pwaff;
3698 /* Mark the domains of the elements of "list" as rational.
3700 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3701 __isl_take isl_pw_aff_list *list)
3703 int i, n;
3705 if (!list)
3706 return NULL;
3707 if (list->n == 0)
3708 return list;
3710 n = list->n;
3711 for (i = 0; i < n; ++i) {
3712 isl_pw_aff *pa;
3714 pa = isl_pw_aff_list_get_pw_aff(list, i);
3715 pa = isl_pw_aff_set_rational(pa);
3716 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3719 return list;
3722 /* Do the parameters of "aff" match those of "space"?
3724 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3725 __isl_keep isl_space *space)
3727 isl_space *aff_space;
3728 isl_bool match;
3730 if (!aff || !space)
3731 return isl_bool_error;
3733 aff_space = isl_aff_get_domain_space(aff);
3735 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3737 isl_space_free(aff_space);
3738 return match;
3741 /* Check that the domain space of "aff" matches "space".
3743 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3744 __isl_keep isl_space *space)
3746 isl_space *aff_space;
3747 isl_bool match;
3749 if (!aff || !space)
3750 return isl_stat_error;
3752 aff_space = isl_aff_get_domain_space(aff);
3754 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3755 if (match < 0)
3756 goto error;
3757 if (!match)
3758 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3759 "parameters don't match", goto error);
3760 match = isl_space_tuple_is_equal(space, isl_dim_in,
3761 aff_space, isl_dim_set);
3762 if (match < 0)
3763 goto error;
3764 if (!match)
3765 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3766 "domains don't match", goto error);
3767 isl_space_free(aff_space);
3768 return isl_stat_ok;
3769 error:
3770 isl_space_free(aff_space);
3771 return isl_stat_error;
3774 #undef BASE
3775 #define BASE aff
3776 #undef DOMBASE
3777 #define DOMBASE set
3778 #define NO_DOMAIN
3780 #include <isl_multi_templ.c>
3781 #include <isl_multi_apply_set.c>
3782 #include <isl_multi_cmp.c>
3783 #include <isl_multi_floor.c>
3784 #include <isl_multi_gist.c>
3786 #undef NO_DOMAIN
3788 /* Remove any internal structure of the domain of "ma".
3789 * If there is any such internal structure in the input,
3790 * then the name of the corresponding space is also removed.
3792 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3793 __isl_take isl_multi_aff *ma)
3795 isl_space *space;
3797 if (!ma)
3798 return NULL;
3800 if (!ma->space->nested[0])
3801 return ma;
3803 space = isl_multi_aff_get_space(ma);
3804 space = isl_space_flatten_domain(space);
3805 ma = isl_multi_aff_reset_space(ma, space);
3807 return ma;
3810 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3811 * of the space to its domain.
3813 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3815 int i, n_in;
3816 isl_local_space *ls;
3817 isl_multi_aff *ma;
3819 if (!space)
3820 return NULL;
3821 if (!isl_space_is_map(space))
3822 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3823 "not a map space", goto error);
3825 n_in = isl_space_dim(space, isl_dim_in);
3826 space = isl_space_domain_map(space);
3828 ma = isl_multi_aff_alloc(isl_space_copy(space));
3829 if (n_in == 0) {
3830 isl_space_free(space);
3831 return ma;
3834 space = isl_space_domain(space);
3835 ls = isl_local_space_from_space(space);
3836 for (i = 0; i < n_in; ++i) {
3837 isl_aff *aff;
3839 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3840 isl_dim_set, i);
3841 ma = isl_multi_aff_set_aff(ma, i, aff);
3843 isl_local_space_free(ls);
3844 return ma;
3845 error:
3846 isl_space_free(space);
3847 return NULL;
3850 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3851 * of the space to its range.
3853 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3855 int i, n_in, n_out;
3856 isl_local_space *ls;
3857 isl_multi_aff *ma;
3859 if (!space)
3860 return NULL;
3861 if (!isl_space_is_map(space))
3862 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3863 "not a map space", goto error);
3865 n_in = isl_space_dim(space, isl_dim_in);
3866 n_out = isl_space_dim(space, isl_dim_out);
3867 space = isl_space_range_map(space);
3869 ma = isl_multi_aff_alloc(isl_space_copy(space));
3870 if (n_out == 0) {
3871 isl_space_free(space);
3872 return ma;
3875 space = isl_space_domain(space);
3876 ls = isl_local_space_from_space(space);
3877 for (i = 0; i < n_out; ++i) {
3878 isl_aff *aff;
3880 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3881 isl_dim_set, n_in + i);
3882 ma = isl_multi_aff_set_aff(ma, i, aff);
3884 isl_local_space_free(ls);
3885 return ma;
3886 error:
3887 isl_space_free(space);
3888 return NULL;
3891 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3892 * of the space to its range.
3894 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3895 __isl_take isl_space *space)
3897 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3900 /* Given the space of a set and a range of set dimensions,
3901 * construct an isl_multi_aff that projects out those dimensions.
3903 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3904 __isl_take isl_space *space, enum isl_dim_type type,
3905 unsigned first, unsigned n)
3907 int i, dim;
3908 isl_local_space *ls;
3909 isl_multi_aff *ma;
3911 if (!space)
3912 return NULL;
3913 if (!isl_space_is_set(space))
3914 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3915 "expecting set space", goto error);
3916 if (type != isl_dim_set)
3917 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3918 "only set dimensions can be projected out", goto error);
3920 dim = isl_space_dim(space, isl_dim_set);
3921 if (first + n > dim)
3922 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3923 "range out of bounds", goto error);
3925 space = isl_space_from_domain(space);
3926 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3928 if (dim == n)
3929 return isl_multi_aff_alloc(space);
3931 ma = isl_multi_aff_alloc(isl_space_copy(space));
3932 space = isl_space_domain(space);
3933 ls = isl_local_space_from_space(space);
3935 for (i = 0; i < first; ++i) {
3936 isl_aff *aff;
3938 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3939 isl_dim_set, i);
3940 ma = isl_multi_aff_set_aff(ma, i, aff);
3943 for (i = 0; i < dim - (first + n); ++i) {
3944 isl_aff *aff;
3946 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3947 isl_dim_set, first + n + i);
3948 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3951 isl_local_space_free(ls);
3952 return ma;
3953 error:
3954 isl_space_free(space);
3955 return NULL;
3958 /* Given the space of a set and a range of set dimensions,
3959 * construct an isl_pw_multi_aff that projects out those dimensions.
3961 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3962 __isl_take isl_space *space, enum isl_dim_type type,
3963 unsigned first, unsigned n)
3965 isl_multi_aff *ma;
3967 ma = isl_multi_aff_project_out_map(space, type, first, n);
3968 return isl_pw_multi_aff_from_multi_aff(ma);
3971 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3972 * domain.
3974 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3975 __isl_take isl_multi_aff *ma)
3977 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3978 return isl_pw_multi_aff_alloc(dom, ma);
3981 /* Create a piecewise multi-affine expression in the given space that maps each
3982 * input dimension to the corresponding output dimension.
3984 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3985 __isl_take isl_space *space)
3987 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3990 /* Exploit the equalities in "eq" to simplify the affine expressions.
3992 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3993 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3995 int i;
3997 maff = isl_multi_aff_cow(maff);
3998 if (!maff || !eq)
3999 goto error;
4001 for (i = 0; i < maff->n; ++i) {
4002 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
4003 isl_basic_set_copy(eq));
4004 if (!maff->p[i])
4005 goto error;
4008 isl_basic_set_free(eq);
4009 return maff;
4010 error:
4011 isl_basic_set_free(eq);
4012 isl_multi_aff_free(maff);
4013 return NULL;
4016 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4017 isl_int f)
4019 int i;
4021 maff = isl_multi_aff_cow(maff);
4022 if (!maff)
4023 return NULL;
4025 for (i = 0; i < maff->n; ++i) {
4026 maff->p[i] = isl_aff_scale(maff->p[i], f);
4027 if (!maff->p[i])
4028 return isl_multi_aff_free(maff);
4031 return maff;
4034 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4035 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4037 maff1 = isl_multi_aff_add(maff1, maff2);
4038 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4039 return maff1;
4042 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4044 if (!maff)
4045 return -1;
4047 return 0;
4050 /* Return the set of domain elements where "ma1" is lexicographically
4051 * smaller than or equal to "ma2".
4053 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4054 __isl_take isl_multi_aff *ma2)
4056 return isl_multi_aff_lex_ge_set(ma2, ma1);
4059 /* Return the set of domain elements where "ma1" is lexicographically
4060 * smaller than "ma2".
4062 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4063 __isl_take isl_multi_aff *ma2)
4065 return isl_multi_aff_lex_gt_set(ma2, ma1);
4068 /* Return the set of domain elements where "ma1" and "ma2"
4069 * satisfy "order".
4071 static __isl_give isl_set *isl_multi_aff_order_set(
4072 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4073 __isl_give isl_map *order(__isl_take isl_space *set_space))
4075 isl_space *space;
4076 isl_map *map1, *map2;
4077 isl_map *map, *ge;
4079 map1 = isl_map_from_multi_aff(ma1);
4080 map2 = isl_map_from_multi_aff(ma2);
4081 map = isl_map_range_product(map1, map2);
4082 space = isl_space_range(isl_map_get_space(map));
4083 space = isl_space_domain(isl_space_unwrap(space));
4084 ge = order(space);
4085 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4087 return isl_map_domain(map);
4090 /* Return the set of domain elements where "ma1" is lexicographically
4091 * greater than or equal to "ma2".
4093 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4094 __isl_take isl_multi_aff *ma2)
4096 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4099 /* Return the set of domain elements where "ma1" is lexicographically
4100 * greater than "ma2".
4102 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4103 __isl_take isl_multi_aff *ma2)
4105 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4108 #undef PW
4109 #define PW isl_pw_multi_aff
4110 #undef EL
4111 #define EL isl_multi_aff
4112 #undef EL_IS_ZERO
4113 #define EL_IS_ZERO is_empty
4114 #undef ZERO
4115 #define ZERO empty
4116 #undef IS_ZERO
4117 #define IS_ZERO is_empty
4118 #undef FIELD
4119 #define FIELD maff
4120 #undef DEFAULT_IS_ZERO
4121 #define DEFAULT_IS_ZERO 0
4123 #define NO_SUB
4124 #define NO_EVAL
4125 #define NO_OPT
4126 #define NO_INVOLVES_DIMS
4127 #define NO_INSERT_DIMS
4128 #define NO_LIFT
4129 #define NO_MORPH
4131 #include <isl_pw_templ.c>
4132 #include <isl_pw_union_opt.c>
4134 #undef NO_SUB
4136 #undef UNION
4137 #define UNION isl_union_pw_multi_aff
4138 #undef PART
4139 #define PART isl_pw_multi_aff
4140 #undef PARTS
4141 #define PARTS pw_multi_aff
4143 #include <isl_union_multi.c>
4144 #include <isl_union_neg.c>
4146 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4147 __isl_take isl_pw_multi_aff *pma1,
4148 __isl_take isl_pw_multi_aff *pma2)
4150 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4151 &isl_multi_aff_lex_ge_set);
4154 /* Given two piecewise multi affine expressions, return a piecewise
4155 * multi-affine expression defined on the union of the definition domains
4156 * of the inputs that is equal to the lexicographic maximum of the two
4157 * inputs on each cell. If only one of the two inputs is defined on
4158 * a given cell, then it is considered to be the maximum.
4160 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4161 __isl_take isl_pw_multi_aff *pma1,
4162 __isl_take isl_pw_multi_aff *pma2)
4164 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4165 &pw_multi_aff_union_lexmax);
4168 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4169 __isl_take isl_pw_multi_aff *pma1,
4170 __isl_take isl_pw_multi_aff *pma2)
4172 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4173 &isl_multi_aff_lex_le_set);
4176 /* Given two piecewise multi affine expressions, return a piecewise
4177 * multi-affine expression defined on the union of the definition domains
4178 * of the inputs that is equal to the lexicographic minimum of the two
4179 * inputs on each cell. If only one of the two inputs is defined on
4180 * a given cell, then it is considered to be the minimum.
4182 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4183 __isl_take isl_pw_multi_aff *pma1,
4184 __isl_take isl_pw_multi_aff *pma2)
4186 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4187 &pw_multi_aff_union_lexmin);
4190 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4191 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4193 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4194 &isl_multi_aff_add);
4197 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4198 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4200 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4201 &pw_multi_aff_add);
4204 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4205 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4207 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4208 &isl_multi_aff_sub);
4211 /* Subtract "pma2" from "pma1" and return the result.
4213 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4214 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4216 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4217 &pw_multi_aff_sub);
4220 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4221 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4223 return isl_pw_multi_aff_union_add_(pma1, pma2);
4226 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4227 * with the actual sum on the shared domain and
4228 * the defined expression on the symmetric difference of the domains.
4230 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4231 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4233 return isl_union_pw_aff_union_add_(upa1, upa2);
4236 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4237 * with the actual sum on the shared domain and
4238 * the defined expression on the symmetric difference of the domains.
4240 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4241 __isl_take isl_union_pw_multi_aff *upma1,
4242 __isl_take isl_union_pw_multi_aff *upma2)
4244 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4247 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4248 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4250 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4251 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4253 int i, j, n;
4254 isl_space *space;
4255 isl_pw_multi_aff *res;
4257 if (!pma1 || !pma2)
4258 goto error;
4260 n = pma1->n * pma2->n;
4261 space = isl_space_product(isl_space_copy(pma1->dim),
4262 isl_space_copy(pma2->dim));
4263 res = isl_pw_multi_aff_alloc_size(space, n);
4265 for (i = 0; i < pma1->n; ++i) {
4266 for (j = 0; j < pma2->n; ++j) {
4267 isl_set *domain;
4268 isl_multi_aff *ma;
4270 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4271 isl_set_copy(pma2->p[j].set));
4272 ma = isl_multi_aff_product(
4273 isl_multi_aff_copy(pma1->p[i].maff),
4274 isl_multi_aff_copy(pma2->p[j].maff));
4275 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4279 isl_pw_multi_aff_free(pma1);
4280 isl_pw_multi_aff_free(pma2);
4281 return res;
4282 error:
4283 isl_pw_multi_aff_free(pma1);
4284 isl_pw_multi_aff_free(pma2);
4285 return NULL;
4288 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4289 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4291 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4292 &pw_multi_aff_product);
4295 /* Construct a map mapping the domain of the piecewise multi-affine expression
4296 * to its range, with each dimension in the range equated to the
4297 * corresponding affine expression on its cell.
4299 * If the domain of "pma" is rational, then so is the constructed "map".
4301 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4303 int i;
4304 isl_map *map;
4306 if (!pma)
4307 return NULL;
4309 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4311 for (i = 0; i < pma->n; ++i) {
4312 isl_bool rational;
4313 isl_multi_aff *maff;
4314 isl_basic_map *bmap;
4315 isl_map *map_i;
4317 rational = isl_set_is_rational(pma->p[i].set);
4318 if (rational < 0)
4319 map = isl_map_free(map);
4320 maff = isl_multi_aff_copy(pma->p[i].maff);
4321 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4322 map_i = isl_map_from_basic_map(bmap);
4323 map_i = isl_map_intersect_domain(map_i,
4324 isl_set_copy(pma->p[i].set));
4325 map = isl_map_union_disjoint(map, map_i);
4328 isl_pw_multi_aff_free(pma);
4329 return map;
4332 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4334 if (!pma)
4335 return NULL;
4337 if (!isl_space_is_set(pma->dim))
4338 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4339 "isl_pw_multi_aff cannot be converted into an isl_set",
4340 goto error);
4342 return isl_map_from_pw_multi_aff(pma);
4343 error:
4344 isl_pw_multi_aff_free(pma);
4345 return NULL;
4348 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4349 * denominator "denom".
4350 * "denom" is allowed to be negative, in which case the actual denominator
4351 * is -denom and the expressions are added instead.
4353 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4354 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4356 int i, first;
4357 int sign;
4358 isl_int d;
4360 first = isl_seq_first_non_zero(c, n);
4361 if (first == -1)
4362 return aff;
4364 sign = isl_int_sgn(denom);
4365 isl_int_init(d);
4366 isl_int_abs(d, denom);
4367 for (i = first; i < n; ++i) {
4368 isl_aff *aff_i;
4370 if (isl_int_is_zero(c[i]))
4371 continue;
4372 aff_i = isl_multi_aff_get_aff(ma, i);
4373 aff_i = isl_aff_scale(aff_i, c[i]);
4374 aff_i = isl_aff_scale_down(aff_i, d);
4375 if (sign >= 0)
4376 aff = isl_aff_sub(aff, aff_i);
4377 else
4378 aff = isl_aff_add(aff, aff_i);
4380 isl_int_clear(d);
4382 return aff;
4385 /* Extract an affine expression that expresses the output dimension "pos"
4386 * of "bmap" in terms of the parameters and input dimensions from
4387 * equality "eq".
4388 * Note that this expression may involve integer divisions defined
4389 * in terms of parameters and input dimensions.
4390 * The equality may also involve references to earlier (but not later)
4391 * output dimensions. These are replaced by the corresponding elements
4392 * in "ma".
4394 * If the equality is of the form
4396 * f(i) + h(j) + a x + g(i) = 0,
4398 * with f(i) a linear combinations of the parameters and input dimensions,
4399 * g(i) a linear combination of integer divisions defined in terms of the same
4400 * and h(j) a linear combinations of earlier output dimensions,
4401 * then the affine expression is
4403 * (-f(i) - g(i))/a - h(j)/a
4405 * If the equality is of the form
4407 * f(i) + h(j) - a x + g(i) = 0,
4409 * then the affine expression is
4411 * (f(i) + g(i))/a - h(j)/(-a)
4414 * If "div" refers to an integer division (i.e., it is smaller than
4415 * the number of integer divisions), then the equality constraint
4416 * does involve an integer division (the one at position "div") that
4417 * is defined in terms of output dimensions. However, this integer
4418 * division can be eliminated by exploiting a pair of constraints
4419 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4420 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4421 * -l + x >= 0.
4422 * In particular, let
4424 * x = e(i) + m floor(...)
4426 * with e(i) the expression derived above and floor(...) the integer
4427 * division involving output dimensions.
4428 * From
4430 * l <= x <= l + n,
4432 * we have
4434 * 0 <= x - l <= n
4436 * This means
4438 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4439 * = (e(i) - l) mod m
4441 * Therefore,
4443 * x - l = (e(i) - l) mod m
4445 * or
4447 * x = ((e(i) - l) mod m) + l
4449 * The variable "shift" below contains the expression -l, which may
4450 * also involve a linear combination of earlier output dimensions.
4452 static __isl_give isl_aff *extract_aff_from_equality(
4453 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4454 __isl_keep isl_multi_aff *ma)
4456 unsigned o_out;
4457 unsigned n_div, n_out;
4458 isl_ctx *ctx;
4459 isl_local_space *ls;
4460 isl_aff *aff, *shift;
4461 isl_val *mod;
4463 ctx = isl_basic_map_get_ctx(bmap);
4464 ls = isl_basic_map_get_local_space(bmap);
4465 ls = isl_local_space_domain(ls);
4466 aff = isl_aff_alloc(isl_local_space_copy(ls));
4467 if (!aff)
4468 goto error;
4469 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4470 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4471 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4472 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4473 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4474 isl_seq_cpy(aff->v->el + 1 + o_out,
4475 bmap->eq[eq] + o_out + n_out, n_div);
4476 } else {
4477 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4478 isl_seq_neg(aff->v->el + 1 + o_out,
4479 bmap->eq[eq] + o_out + n_out, n_div);
4481 if (div < n_div)
4482 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4483 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4484 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4485 bmap->eq[eq][o_out + pos]);
4486 if (div < n_div) {
4487 shift = isl_aff_alloc(isl_local_space_copy(ls));
4488 if (!shift)
4489 goto error;
4490 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4491 isl_seq_cpy(shift->v->el + 1 + o_out,
4492 bmap->ineq[ineq] + o_out + n_out, n_div);
4493 isl_int_set_si(shift->v->el[0], 1);
4494 shift = subtract_initial(shift, ma, pos,
4495 bmap->ineq[ineq] + o_out, ctx->negone);
4496 aff = isl_aff_add(aff, isl_aff_copy(shift));
4497 mod = isl_val_int_from_isl_int(ctx,
4498 bmap->eq[eq][o_out + n_out + div]);
4499 mod = isl_val_abs(mod);
4500 aff = isl_aff_mod_val(aff, mod);
4501 aff = isl_aff_sub(aff, shift);
4504 isl_local_space_free(ls);
4505 return aff;
4506 error:
4507 isl_local_space_free(ls);
4508 isl_aff_free(aff);
4509 return NULL;
4512 /* Given a basic map with output dimensions defined
4513 * in terms of the parameters input dimensions and earlier
4514 * output dimensions using an equality (and possibly a pair on inequalities),
4515 * extract an isl_aff that expresses output dimension "pos" in terms
4516 * of the parameters and input dimensions.
4517 * Note that this expression may involve integer divisions defined
4518 * in terms of parameters and input dimensions.
4519 * "ma" contains the expressions corresponding to earlier output dimensions.
4521 * This function shares some similarities with
4522 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4524 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4525 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4527 int eq, div, ineq;
4528 isl_aff *aff;
4530 if (!bmap)
4531 return NULL;
4532 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4533 if (eq >= bmap->n_eq)
4534 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4535 "unable to find suitable equality", return NULL);
4536 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4538 aff = isl_aff_remove_unused_divs(aff);
4539 return aff;
4542 /* Given a basic map where each output dimension is defined
4543 * in terms of the parameters and input dimensions using an equality,
4544 * extract an isl_multi_aff that expresses the output dimensions in terms
4545 * of the parameters and input dimensions.
4547 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4548 __isl_take isl_basic_map *bmap)
4550 int i;
4551 unsigned n_out;
4552 isl_multi_aff *ma;
4554 if (!bmap)
4555 return NULL;
4557 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4558 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4560 for (i = 0; i < n_out; ++i) {
4561 isl_aff *aff;
4563 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4564 ma = isl_multi_aff_set_aff(ma, i, aff);
4567 isl_basic_map_free(bmap);
4569 return ma;
4572 /* Given a basic set where each set dimension is defined
4573 * in terms of the parameters using an equality,
4574 * extract an isl_multi_aff that expresses the set dimensions in terms
4575 * of the parameters.
4577 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4578 __isl_take isl_basic_set *bset)
4580 return extract_isl_multi_aff_from_basic_map(bset);
4583 /* Create an isl_pw_multi_aff that is equivalent to
4584 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4585 * The given basic map is such that each output dimension is defined
4586 * in terms of the parameters and input dimensions using an equality.
4588 * Since some applications expect the result of isl_pw_multi_aff_from_map
4589 * to only contain integer affine expressions, we compute the floor
4590 * of the expression before returning.
4592 * Remove all constraints involving local variables without
4593 * an explicit representation (resulting in the removal of those
4594 * local variables) prior to the actual extraction to ensure
4595 * that the local spaces in which the resulting affine expressions
4596 * are created do not contain any unknown local variables.
4597 * Removing such constraints is safe because constraints involving
4598 * unknown local variables are not used to determine whether
4599 * a basic map is obviously single-valued.
4601 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4602 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4604 isl_multi_aff *ma;
4606 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4607 ma = extract_isl_multi_aff_from_basic_map(bmap);
4608 ma = isl_multi_aff_floor(ma);
4609 return isl_pw_multi_aff_alloc(domain, ma);
4612 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4613 * This obviously only works if the input "map" is single-valued.
4614 * If so, we compute the lexicographic minimum of the image in the form
4615 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4616 * to its lexicographic minimum.
4617 * If the input is not single-valued, we produce an error.
4619 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4620 __isl_take isl_map *map)
4622 int i;
4623 int sv;
4624 isl_pw_multi_aff *pma;
4626 sv = isl_map_is_single_valued(map);
4627 if (sv < 0)
4628 goto error;
4629 if (!sv)
4630 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4631 "map is not single-valued", goto error);
4632 map = isl_map_make_disjoint(map);
4633 if (!map)
4634 return NULL;
4636 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4638 for (i = 0; i < map->n; ++i) {
4639 isl_pw_multi_aff *pma_i;
4640 isl_basic_map *bmap;
4641 bmap = isl_basic_map_copy(map->p[i]);
4642 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4643 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4646 isl_map_free(map);
4647 return pma;
4648 error:
4649 isl_map_free(map);
4650 return NULL;
4653 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4654 * taking into account that the output dimension at position "d"
4655 * can be represented as
4657 * x = floor((e(...) + c1) / m)
4659 * given that constraint "i" is of the form
4661 * e(...) + c1 - m x >= 0
4664 * Let "map" be of the form
4666 * A -> B
4668 * We construct a mapping
4670 * A -> [A -> x = floor(...)]
4672 * apply that to the map, obtaining
4674 * [A -> x = floor(...)] -> B
4676 * and equate dimension "d" to x.
4677 * We then compute a isl_pw_multi_aff representation of the resulting map
4678 * and plug in the mapping above.
4680 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4681 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4683 isl_ctx *ctx;
4684 isl_space *space;
4685 isl_local_space *ls;
4686 isl_multi_aff *ma;
4687 isl_aff *aff;
4688 isl_vec *v;
4689 isl_map *insert;
4690 int offset;
4691 int n;
4692 int n_in;
4693 isl_pw_multi_aff *pma;
4694 isl_bool is_set;
4696 is_set = isl_map_is_set(map);
4697 if (is_set < 0)
4698 goto error;
4700 offset = isl_basic_map_offset(hull, isl_dim_out);
4701 ctx = isl_map_get_ctx(map);
4702 space = isl_space_domain(isl_map_get_space(map));
4703 n_in = isl_space_dim(space, isl_dim_set);
4704 n = isl_space_dim(space, isl_dim_all);
4706 v = isl_vec_alloc(ctx, 1 + 1 + n);
4707 if (v) {
4708 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4709 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4711 isl_basic_map_free(hull);
4713 ls = isl_local_space_from_space(isl_space_copy(space));
4714 aff = isl_aff_alloc_vec(ls, v);
4715 aff = isl_aff_floor(aff);
4716 if (is_set) {
4717 isl_space_free(space);
4718 ma = isl_multi_aff_from_aff(aff);
4719 } else {
4720 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4721 ma = isl_multi_aff_range_product(ma,
4722 isl_multi_aff_from_aff(aff));
4725 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4726 map = isl_map_apply_domain(map, insert);
4727 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4728 pma = isl_pw_multi_aff_from_map(map);
4729 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4731 return pma;
4732 error:
4733 isl_map_free(map);
4734 isl_basic_map_free(hull);
4735 return NULL;
4738 /* Is constraint "c" of the form
4740 * e(...) + c1 - m x >= 0
4742 * or
4744 * -e(...) + c2 + m x >= 0
4746 * where m > 1 and e only depends on parameters and input dimemnsions?
4748 * "offset" is the offset of the output dimensions
4749 * "pos" is the position of output dimension x.
4751 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4753 if (isl_int_is_zero(c[offset + d]))
4754 return 0;
4755 if (isl_int_is_one(c[offset + d]))
4756 return 0;
4757 if (isl_int_is_negone(c[offset + d]))
4758 return 0;
4759 if (isl_seq_first_non_zero(c + offset, d) != -1)
4760 return 0;
4761 if (isl_seq_first_non_zero(c + offset + d + 1,
4762 total - (offset + d + 1)) != -1)
4763 return 0;
4764 return 1;
4767 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4769 * As a special case, we first check if there is any pair of constraints,
4770 * shared by all the basic maps in "map" that force a given dimension
4771 * to be equal to the floor of some affine combination of the input dimensions.
4773 * In particular, if we can find two constraints
4775 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4777 * and
4779 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4781 * where m > 1 and e only depends on parameters and input dimemnsions,
4782 * and such that
4784 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4786 * then we know that we can take
4788 * x = floor((e(...) + c1) / m)
4790 * without having to perform any computation.
4792 * Note that we know that
4794 * c1 + c2 >= 1
4796 * If c1 + c2 were 0, then we would have detected an equality during
4797 * simplification. If c1 + c2 were negative, then we would have detected
4798 * a contradiction.
4800 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4801 __isl_take isl_map *map)
4803 int d, dim;
4804 int i, j, n;
4805 int offset, total;
4806 isl_int sum;
4807 isl_basic_map *hull;
4809 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4810 if (!hull)
4811 goto error;
4813 isl_int_init(sum);
4814 dim = isl_map_dim(map, isl_dim_out);
4815 offset = isl_basic_map_offset(hull, isl_dim_out);
4816 total = 1 + isl_basic_map_total_dim(hull);
4817 n = hull->n_ineq;
4818 for (d = 0; d < dim; ++d) {
4819 for (i = 0; i < n; ++i) {
4820 if (!is_potential_div_constraint(hull->ineq[i],
4821 offset, d, total))
4822 continue;
4823 for (j = i + 1; j < n; ++j) {
4824 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4825 hull->ineq[j] + 1, total - 1))
4826 continue;
4827 isl_int_add(sum, hull->ineq[i][0],
4828 hull->ineq[j][0]);
4829 if (isl_int_abs_lt(sum,
4830 hull->ineq[i][offset + d]))
4831 break;
4834 if (j >= n)
4835 continue;
4836 isl_int_clear(sum);
4837 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4838 j = i;
4839 return pw_multi_aff_from_map_div(map, hull, d, j);
4842 isl_int_clear(sum);
4843 isl_basic_map_free(hull);
4844 return pw_multi_aff_from_map_base(map);
4845 error:
4846 isl_map_free(map);
4847 isl_basic_map_free(hull);
4848 return NULL;
4851 /* Given an affine expression
4853 * [A -> B] -> f(A,B)
4855 * construct an isl_multi_aff
4857 * [A -> B] -> B'
4859 * such that dimension "d" in B' is set to "aff" and the remaining
4860 * dimensions are set equal to the corresponding dimensions in B.
4861 * "n_in" is the dimension of the space A.
4862 * "n_out" is the dimension of the space B.
4864 * If "is_set" is set, then the affine expression is of the form
4866 * [B] -> f(B)
4868 * and we construct an isl_multi_aff
4870 * B -> B'
4872 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4873 unsigned n_in, unsigned n_out, int is_set)
4875 int i;
4876 isl_multi_aff *ma;
4877 isl_space *space, *space2;
4878 isl_local_space *ls;
4880 space = isl_aff_get_domain_space(aff);
4881 ls = isl_local_space_from_space(isl_space_copy(space));
4882 space2 = isl_space_copy(space);
4883 if (!is_set)
4884 space2 = isl_space_range(isl_space_unwrap(space2));
4885 space = isl_space_map_from_domain_and_range(space, space2);
4886 ma = isl_multi_aff_alloc(space);
4887 ma = isl_multi_aff_set_aff(ma, d, aff);
4889 for (i = 0; i < n_out; ++i) {
4890 if (i == d)
4891 continue;
4892 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4893 isl_dim_set, n_in + i);
4894 ma = isl_multi_aff_set_aff(ma, i, aff);
4897 isl_local_space_free(ls);
4899 return ma;
4902 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4903 * taking into account that the dimension at position "d" can be written as
4905 * x = m a + f(..) (1)
4907 * where m is equal to "gcd".
4908 * "i" is the index of the equality in "hull" that defines f(..).
4909 * In particular, the equality is of the form
4911 * f(..) - x + m g(existentials) = 0
4913 * or
4915 * -f(..) + x + m g(existentials) = 0
4917 * We basically plug (1) into "map", resulting in a map with "a"
4918 * in the range instead of "x". The corresponding isl_pw_multi_aff
4919 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4921 * Specifically, given the input map
4923 * A -> B
4925 * We first wrap it into a set
4927 * [A -> B]
4929 * and define (1) on top of the corresponding space, resulting in "aff".
4930 * We use this to create an isl_multi_aff that maps the output position "d"
4931 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4932 * We plug this into the wrapped map, unwrap the result and compute the
4933 * corresponding isl_pw_multi_aff.
4934 * The result is an expression
4936 * A -> T(A)
4938 * We adjust that to
4940 * A -> [A -> T(A)]
4942 * so that we can plug that into "aff", after extending the latter to
4943 * a mapping
4945 * [A -> B] -> B'
4948 * If "map" is actually a set, then there is no "A" space, meaning
4949 * that we do not need to perform any wrapping, and that the result
4950 * of the recursive call is of the form
4952 * [T]
4954 * which is plugged into a mapping of the form
4956 * B -> B'
4958 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4959 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4960 isl_int gcd)
4962 isl_set *set;
4963 isl_space *space;
4964 isl_local_space *ls;
4965 isl_aff *aff;
4966 isl_multi_aff *ma;
4967 isl_pw_multi_aff *pma, *id;
4968 unsigned n_in;
4969 unsigned o_out;
4970 unsigned n_out;
4971 isl_bool is_set;
4973 is_set = isl_map_is_set(map);
4974 if (is_set < 0)
4975 goto error;
4977 n_in = isl_basic_map_dim(hull, isl_dim_in);
4978 n_out = isl_basic_map_dim(hull, isl_dim_out);
4979 o_out = isl_basic_map_offset(hull, isl_dim_out);
4981 if (is_set)
4982 set = map;
4983 else
4984 set = isl_map_wrap(map);
4985 space = isl_space_map_from_set(isl_set_get_space(set));
4986 ma = isl_multi_aff_identity(space);
4987 ls = isl_local_space_from_space(isl_set_get_space(set));
4988 aff = isl_aff_alloc(ls);
4989 if (aff) {
4990 isl_int_set_si(aff->v->el[0], 1);
4991 if (isl_int_is_one(hull->eq[i][o_out + d]))
4992 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4993 aff->v->size - 1);
4994 else
4995 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4996 aff->v->size - 1);
4997 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4999 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5000 set = isl_set_preimage_multi_aff(set, ma);
5002 ma = range_map(aff, d, n_in, n_out, is_set);
5004 if (is_set)
5005 map = set;
5006 else
5007 map = isl_set_unwrap(set);
5008 pma = isl_pw_multi_aff_from_map(map);
5010 if (!is_set) {
5011 space = isl_pw_multi_aff_get_domain_space(pma);
5012 space = isl_space_map_from_set(space);
5013 id = isl_pw_multi_aff_identity(space);
5014 pma = isl_pw_multi_aff_range_product(id, pma);
5016 id = isl_pw_multi_aff_from_multi_aff(ma);
5017 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5019 isl_basic_map_free(hull);
5020 return pma;
5021 error:
5022 isl_map_free(map);
5023 isl_basic_map_free(hull);
5024 return NULL;
5027 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5028 * "hull" contains the equalities valid for "map".
5030 * Check if any of the output dimensions is "strided".
5031 * That is, we check if it can be written as
5033 * x = m a + f(..)
5035 * with m greater than 1, a some combination of existentially quantified
5036 * variables and f an expression in the parameters and input dimensions.
5037 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5039 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5040 * special case.
5042 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5043 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5045 int i, j;
5046 unsigned n_out;
5047 unsigned o_out;
5048 unsigned n_div;
5049 unsigned o_div;
5050 isl_int gcd;
5052 n_div = isl_basic_map_dim(hull, isl_dim_div);
5053 o_div = isl_basic_map_offset(hull, isl_dim_div);
5055 if (n_div == 0) {
5056 isl_basic_map_free(hull);
5057 return pw_multi_aff_from_map_check_div(map);
5060 isl_int_init(gcd);
5062 n_out = isl_basic_map_dim(hull, isl_dim_out);
5063 o_out = isl_basic_map_offset(hull, isl_dim_out);
5065 for (i = 0; i < n_out; ++i) {
5066 for (j = 0; j < hull->n_eq; ++j) {
5067 isl_int *eq = hull->eq[j];
5068 isl_pw_multi_aff *res;
5070 if (!isl_int_is_one(eq[o_out + i]) &&
5071 !isl_int_is_negone(eq[o_out + i]))
5072 continue;
5073 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5074 continue;
5075 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5076 n_out - (i + 1)) != -1)
5077 continue;
5078 isl_seq_gcd(eq + o_div, n_div, &gcd);
5079 if (isl_int_is_zero(gcd))
5080 continue;
5081 if (isl_int_is_one(gcd))
5082 continue;
5084 res = pw_multi_aff_from_map_stride(map, hull,
5085 i, j, gcd);
5086 isl_int_clear(gcd);
5087 return res;
5091 isl_int_clear(gcd);
5092 isl_basic_map_free(hull);
5093 return pw_multi_aff_from_map_check_div(map);
5096 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5098 * As a special case, we first check if all output dimensions are uniquely
5099 * defined in terms of the parameters and input dimensions over the entire
5100 * domain. If so, we extract the desired isl_pw_multi_aff directly
5101 * from the affine hull of "map" and its domain.
5103 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5104 * special cases.
5106 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5108 isl_bool sv;
5109 isl_basic_map *hull;
5111 if (!map)
5112 return NULL;
5114 if (isl_map_n_basic_map(map) == 1) {
5115 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5116 hull = isl_basic_map_plain_affine_hull(hull);
5117 sv = isl_basic_map_plain_is_single_valued(hull);
5118 if (sv >= 0 && sv)
5119 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5120 hull);
5121 isl_basic_map_free(hull);
5123 map = isl_map_detect_equalities(map);
5124 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5125 sv = isl_basic_map_plain_is_single_valued(hull);
5126 if (sv >= 0 && sv)
5127 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5128 if (sv >= 0)
5129 return pw_multi_aff_from_map_check_strides(map, hull);
5130 isl_basic_map_free(hull);
5131 isl_map_free(map);
5132 return NULL;
5135 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5137 return isl_pw_multi_aff_from_map(set);
5140 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5141 * add it to *user.
5143 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5145 isl_union_pw_multi_aff **upma = user;
5146 isl_pw_multi_aff *pma;
5148 pma = isl_pw_multi_aff_from_map(map);
5149 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5151 return *upma ? isl_stat_ok : isl_stat_error;
5154 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5155 * domain.
5157 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5158 __isl_take isl_aff *aff)
5160 isl_multi_aff *ma;
5161 isl_pw_multi_aff *pma;
5163 ma = isl_multi_aff_from_aff(aff);
5164 pma = isl_pw_multi_aff_from_multi_aff(ma);
5165 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5168 /* Try and create an isl_union_pw_multi_aff that is equivalent
5169 * to the given isl_union_map.
5170 * The isl_union_map is required to be single-valued in each space.
5171 * Otherwise, an error is produced.
5173 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5174 __isl_take isl_union_map *umap)
5176 isl_space *space;
5177 isl_union_pw_multi_aff *upma;
5179 space = isl_union_map_get_space(umap);
5180 upma = isl_union_pw_multi_aff_empty(space);
5181 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5182 upma = isl_union_pw_multi_aff_free(upma);
5183 isl_union_map_free(umap);
5185 return upma;
5188 /* Try and create an isl_union_pw_multi_aff that is equivalent
5189 * to the given isl_union_set.
5190 * The isl_union_set is required to be a singleton in each space.
5191 * Otherwise, an error is produced.
5193 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5194 __isl_take isl_union_set *uset)
5196 return isl_union_pw_multi_aff_from_union_map(uset);
5199 /* Return the piecewise affine expression "set ? 1 : 0".
5201 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5203 isl_pw_aff *pa;
5204 isl_space *space = isl_set_get_space(set);
5205 isl_local_space *ls = isl_local_space_from_space(space);
5206 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5207 isl_aff *one = isl_aff_zero_on_domain(ls);
5209 one = isl_aff_add_constant_si(one, 1);
5210 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5211 set = isl_set_complement(set);
5212 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5214 return pa;
5217 /* Plug in "subs" for dimension "type", "pos" of "aff".
5219 * Let i be the dimension to replace and let "subs" be of the form
5221 * f/d
5223 * and "aff" of the form
5225 * (a i + g)/m
5227 * The result is
5229 * (a f + d g')/(m d)
5231 * where g' is the result of plugging in "subs" in each of the integer
5232 * divisions in g.
5234 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5235 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5237 isl_ctx *ctx;
5238 isl_int v;
5240 aff = isl_aff_cow(aff);
5241 if (!aff || !subs)
5242 return isl_aff_free(aff);
5244 ctx = isl_aff_get_ctx(aff);
5245 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5246 isl_die(ctx, isl_error_invalid,
5247 "spaces don't match", return isl_aff_free(aff));
5248 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5249 isl_die(ctx, isl_error_unsupported,
5250 "cannot handle divs yet", return isl_aff_free(aff));
5252 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5253 if (!aff->ls)
5254 return isl_aff_free(aff);
5256 aff->v = isl_vec_cow(aff->v);
5257 if (!aff->v)
5258 return isl_aff_free(aff);
5260 pos += isl_local_space_offset(aff->ls, type);
5262 isl_int_init(v);
5263 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5264 aff->v->size, subs->v->size, v);
5265 isl_int_clear(v);
5267 return aff;
5270 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5271 * expressions in "maff".
5273 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5274 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5275 __isl_keep isl_aff *subs)
5277 int i;
5279 maff = isl_multi_aff_cow(maff);
5280 if (!maff || !subs)
5281 return isl_multi_aff_free(maff);
5283 if (type == isl_dim_in)
5284 type = isl_dim_set;
5286 for (i = 0; i < maff->n; ++i) {
5287 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5288 if (!maff->p[i])
5289 return isl_multi_aff_free(maff);
5292 return maff;
5295 /* Plug in "subs" for dimension "type", "pos" of "pma".
5297 * pma is of the form
5299 * A_i(v) -> M_i(v)
5301 * while subs is of the form
5303 * v' = B_j(v) -> S_j
5305 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5306 * has a contribution in the result, in particular
5308 * C_ij(S_j) -> M_i(S_j)
5310 * Note that plugging in S_j in C_ij may also result in an empty set
5311 * and this contribution should simply be discarded.
5313 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5314 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5315 __isl_keep isl_pw_aff *subs)
5317 int i, j, n;
5318 isl_pw_multi_aff *res;
5320 if (!pma || !subs)
5321 return isl_pw_multi_aff_free(pma);
5323 n = pma->n * subs->n;
5324 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5326 for (i = 0; i < pma->n; ++i) {
5327 for (j = 0; j < subs->n; ++j) {
5328 isl_set *common;
5329 isl_multi_aff *res_ij;
5330 int empty;
5332 common = isl_set_intersect(
5333 isl_set_copy(pma->p[i].set),
5334 isl_set_copy(subs->p[j].set));
5335 common = isl_set_substitute(common,
5336 type, pos, subs->p[j].aff);
5337 empty = isl_set_plain_is_empty(common);
5338 if (empty < 0 || empty) {
5339 isl_set_free(common);
5340 if (empty < 0)
5341 goto error;
5342 continue;
5345 res_ij = isl_multi_aff_substitute(
5346 isl_multi_aff_copy(pma->p[i].maff),
5347 type, pos, subs->p[j].aff);
5349 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5353 isl_pw_multi_aff_free(pma);
5354 return res;
5355 error:
5356 isl_pw_multi_aff_free(pma);
5357 isl_pw_multi_aff_free(res);
5358 return NULL;
5361 /* Compute the preimage of a range of dimensions in the affine expression "src"
5362 * under "ma" and put the result in "dst". The number of dimensions in "src"
5363 * that precede the range is given by "n_before". The number of dimensions
5364 * in the range is given by the number of output dimensions of "ma".
5365 * The number of dimensions that follow the range is given by "n_after".
5366 * If "has_denom" is set (to one),
5367 * then "src" and "dst" have an extra initial denominator.
5368 * "n_div_ma" is the number of existentials in "ma"
5369 * "n_div_bset" is the number of existentials in "src"
5370 * The resulting "dst" (which is assumed to have been allocated by
5371 * the caller) contains coefficients for both sets of existentials,
5372 * first those in "ma" and then those in "src".
5373 * f, c1, c2 and g are temporary objects that have been initialized
5374 * by the caller.
5376 * Let src represent the expression
5378 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5380 * and let ma represent the expressions
5382 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5384 * We start out with the following expression for dst:
5386 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5388 * with the multiplication factor f initially equal to 1
5389 * and f \sum_i b_i v_i kept separately.
5390 * For each x_i that we substitute, we multiply the numerator
5391 * (and denominator) of dst by c_1 = m_i and add the numerator
5392 * of the x_i expression multiplied by c_2 = f b_i,
5393 * after removing the common factors of c_1 and c_2.
5394 * The multiplication factor f also needs to be multiplied by c_1
5395 * for the next x_j, j > i.
5397 void isl_seq_preimage(isl_int *dst, isl_int *src,
5398 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5399 int n_div_ma, int n_div_bmap,
5400 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5402 int i;
5403 int n_param, n_in, n_out;
5404 int o_dst, o_src;
5406 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5407 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5408 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5410 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5411 o_dst = o_src = has_denom + 1 + n_param + n_before;
5412 isl_seq_clr(dst + o_dst, n_in);
5413 o_dst += n_in;
5414 o_src += n_out;
5415 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5416 o_dst += n_after;
5417 o_src += n_after;
5418 isl_seq_clr(dst + o_dst, n_div_ma);
5419 o_dst += n_div_ma;
5420 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5422 isl_int_set_si(f, 1);
5424 for (i = 0; i < n_out; ++i) {
5425 int offset = has_denom + 1 + n_param + n_before + i;
5427 if (isl_int_is_zero(src[offset]))
5428 continue;
5429 isl_int_set(c1, ma->p[i]->v->el[0]);
5430 isl_int_mul(c2, f, src[offset]);
5431 isl_int_gcd(g, c1, c2);
5432 isl_int_divexact(c1, c1, g);
5433 isl_int_divexact(c2, c2, g);
5435 isl_int_mul(f, f, c1);
5436 o_dst = has_denom;
5437 o_src = 1;
5438 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5439 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5440 o_dst += 1 + n_param;
5441 o_src += 1 + n_param;
5442 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5443 o_dst += n_before;
5444 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5445 c2, ma->p[i]->v->el + o_src, n_in);
5446 o_dst += n_in;
5447 o_src += n_in;
5448 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5449 o_dst += n_after;
5450 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5451 c2, ma->p[i]->v->el + o_src, n_div_ma);
5452 o_dst += n_div_ma;
5453 o_src += n_div_ma;
5454 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5455 if (has_denom)
5456 isl_int_mul(dst[0], dst[0], c1);
5460 /* Compute the pullback of "aff" by the function represented by "ma".
5461 * In other words, plug in "ma" in "aff". The result is an affine expression
5462 * defined over the domain space of "ma".
5464 * If "aff" is represented by
5466 * (a(p) + b x + c(divs))/d
5468 * and ma is represented by
5470 * x = D(p) + F(y) + G(divs')
5472 * then the result is
5474 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5476 * The divs in the local space of the input are similarly adjusted
5477 * through a call to isl_local_space_preimage_multi_aff.
5479 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5480 __isl_take isl_multi_aff *ma)
5482 isl_aff *res = NULL;
5483 isl_local_space *ls;
5484 int n_div_aff, n_div_ma;
5485 isl_int f, c1, c2, g;
5487 ma = isl_multi_aff_align_divs(ma);
5488 if (!aff || !ma)
5489 goto error;
5491 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5492 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5494 ls = isl_aff_get_domain_local_space(aff);
5495 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5496 res = isl_aff_alloc(ls);
5497 if (!res)
5498 goto error;
5500 isl_int_init(f);
5501 isl_int_init(c1);
5502 isl_int_init(c2);
5503 isl_int_init(g);
5505 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5506 f, c1, c2, g, 1);
5508 isl_int_clear(f);
5509 isl_int_clear(c1);
5510 isl_int_clear(c2);
5511 isl_int_clear(g);
5513 isl_aff_free(aff);
5514 isl_multi_aff_free(ma);
5515 res = isl_aff_normalize(res);
5516 return res;
5517 error:
5518 isl_aff_free(aff);
5519 isl_multi_aff_free(ma);
5520 isl_aff_free(res);
5521 return NULL;
5524 /* Compute the pullback of "aff1" by the function represented by "aff2".
5525 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5526 * defined over the domain space of "aff1".
5528 * The domain of "aff1" should match the range of "aff2", which means
5529 * that it should be single-dimensional.
5531 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5532 __isl_take isl_aff *aff2)
5534 isl_multi_aff *ma;
5536 ma = isl_multi_aff_from_aff(aff2);
5537 return isl_aff_pullback_multi_aff(aff1, ma);
5540 /* Compute the pullback of "ma1" by the function represented by "ma2".
5541 * In other words, plug in "ma2" in "ma1".
5543 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5545 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5546 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5548 int i;
5549 isl_space *space = NULL;
5551 ma2 = isl_multi_aff_align_divs(ma2);
5552 ma1 = isl_multi_aff_cow(ma1);
5553 if (!ma1 || !ma2)
5554 goto error;
5556 space = isl_space_join(isl_multi_aff_get_space(ma2),
5557 isl_multi_aff_get_space(ma1));
5559 for (i = 0; i < ma1->n; ++i) {
5560 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5561 isl_multi_aff_copy(ma2));
5562 if (!ma1->p[i])
5563 goto error;
5566 ma1 = isl_multi_aff_reset_space(ma1, space);
5567 isl_multi_aff_free(ma2);
5568 return ma1;
5569 error:
5570 isl_space_free(space);
5571 isl_multi_aff_free(ma2);
5572 isl_multi_aff_free(ma1);
5573 return NULL;
5576 /* Compute the pullback of "ma1" by the function represented by "ma2".
5577 * In other words, plug in "ma2" in "ma1".
5579 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5580 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5582 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5583 &isl_multi_aff_pullback_multi_aff_aligned);
5586 /* Extend the local space of "dst" to include the divs
5587 * in the local space of "src".
5589 * If "src" does not have any divs or if the local spaces of "dst" and
5590 * "src" are the same, then no extension is required.
5592 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5593 __isl_keep isl_aff *src)
5595 isl_ctx *ctx;
5596 int src_n_div, dst_n_div;
5597 int *exp1 = NULL;
5598 int *exp2 = NULL;
5599 isl_bool equal;
5600 isl_mat *div;
5602 if (!src || !dst)
5603 return isl_aff_free(dst);
5605 ctx = isl_aff_get_ctx(src);
5606 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5607 if (equal < 0)
5608 return isl_aff_free(dst);
5609 if (!equal)
5610 isl_die(ctx, isl_error_invalid,
5611 "spaces don't match", goto error);
5613 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5614 if (src_n_div == 0)
5615 return dst;
5616 equal = isl_local_space_is_equal(src->ls, dst->ls);
5617 if (equal < 0)
5618 return isl_aff_free(dst);
5619 if (equal)
5620 return dst;
5622 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5623 exp1 = isl_alloc_array(ctx, int, src_n_div);
5624 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5625 if (!exp1 || (dst_n_div && !exp2))
5626 goto error;
5628 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5629 dst = isl_aff_expand_divs(dst, div, exp2);
5630 free(exp1);
5631 free(exp2);
5633 return dst;
5634 error:
5635 free(exp1);
5636 free(exp2);
5637 return isl_aff_free(dst);
5640 /* Adjust the local spaces of the affine expressions in "maff"
5641 * such that they all have the save divs.
5643 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5644 __isl_take isl_multi_aff *maff)
5646 int i;
5648 if (!maff)
5649 return NULL;
5650 if (maff->n == 0)
5651 return maff;
5652 maff = isl_multi_aff_cow(maff);
5653 if (!maff)
5654 return NULL;
5656 for (i = 1; i < maff->n; ++i)
5657 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5658 for (i = 1; i < maff->n; ++i) {
5659 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5660 if (!maff->p[i])
5661 return isl_multi_aff_free(maff);
5664 return maff;
5667 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5669 aff = isl_aff_cow(aff);
5670 if (!aff)
5671 return NULL;
5673 aff->ls = isl_local_space_lift(aff->ls);
5674 if (!aff->ls)
5675 return isl_aff_free(aff);
5677 return aff;
5680 /* Lift "maff" to a space with extra dimensions such that the result
5681 * has no more existentially quantified variables.
5682 * If "ls" is not NULL, then *ls is assigned the local space that lies
5683 * at the basis of the lifting applied to "maff".
5685 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5686 __isl_give isl_local_space **ls)
5688 int i;
5689 isl_space *space;
5690 unsigned n_div;
5692 if (ls)
5693 *ls = NULL;
5695 if (!maff)
5696 return NULL;
5698 if (maff->n == 0) {
5699 if (ls) {
5700 isl_space *space = isl_multi_aff_get_domain_space(maff);
5701 *ls = isl_local_space_from_space(space);
5702 if (!*ls)
5703 return isl_multi_aff_free(maff);
5705 return maff;
5708 maff = isl_multi_aff_cow(maff);
5709 maff = isl_multi_aff_align_divs(maff);
5710 if (!maff)
5711 return NULL;
5713 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5714 space = isl_multi_aff_get_space(maff);
5715 space = isl_space_lift(isl_space_domain(space), n_div);
5716 space = isl_space_extend_domain_with_range(space,
5717 isl_multi_aff_get_space(maff));
5718 if (!space)
5719 return isl_multi_aff_free(maff);
5720 isl_space_free(maff->space);
5721 maff->space = space;
5723 if (ls) {
5724 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5725 if (!*ls)
5726 return isl_multi_aff_free(maff);
5729 for (i = 0; i < maff->n; ++i) {
5730 maff->p[i] = isl_aff_lift(maff->p[i]);
5731 if (!maff->p[i])
5732 goto error;
5735 return maff;
5736 error:
5737 if (ls)
5738 isl_local_space_free(*ls);
5739 return isl_multi_aff_free(maff);
5743 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5745 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5746 __isl_keep isl_pw_multi_aff *pma, int pos)
5748 int i;
5749 int n_out;
5750 isl_space *space;
5751 isl_pw_aff *pa;
5753 if (!pma)
5754 return NULL;
5756 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5757 if (pos < 0 || pos >= n_out)
5758 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5759 "index out of bounds", return NULL);
5761 space = isl_pw_multi_aff_get_space(pma);
5762 space = isl_space_drop_dims(space, isl_dim_out,
5763 pos + 1, n_out - pos - 1);
5764 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5766 pa = isl_pw_aff_alloc_size(space, pma->n);
5767 for (i = 0; i < pma->n; ++i) {
5768 isl_aff *aff;
5769 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5770 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5773 return pa;
5776 /* Return an isl_pw_multi_aff with the given "set" as domain and
5777 * an unnamed zero-dimensional range.
5779 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5780 __isl_take isl_set *set)
5782 isl_multi_aff *ma;
5783 isl_space *space;
5785 space = isl_set_get_space(set);
5786 space = isl_space_from_domain(space);
5787 ma = isl_multi_aff_zero(space);
5788 return isl_pw_multi_aff_alloc(set, ma);
5791 /* Add an isl_pw_multi_aff with the given "set" as domain and
5792 * an unnamed zero-dimensional range to *user.
5794 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5795 void *user)
5797 isl_union_pw_multi_aff **upma = user;
5798 isl_pw_multi_aff *pma;
5800 pma = isl_pw_multi_aff_from_domain(set);
5801 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5803 return isl_stat_ok;
5806 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5807 * an unnamed zero-dimensional range.
5809 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5810 __isl_take isl_union_set *uset)
5812 isl_space *space;
5813 isl_union_pw_multi_aff *upma;
5815 if (!uset)
5816 return NULL;
5818 space = isl_union_set_get_space(uset);
5819 upma = isl_union_pw_multi_aff_empty(space);
5821 if (isl_union_set_foreach_set(uset,
5822 &add_pw_multi_aff_from_domain, &upma) < 0)
5823 goto error;
5825 isl_union_set_free(uset);
5826 return upma;
5827 error:
5828 isl_union_set_free(uset);
5829 isl_union_pw_multi_aff_free(upma);
5830 return NULL;
5833 /* Convert "pma" to an isl_map and add it to *umap.
5835 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5836 void *user)
5838 isl_union_map **umap = user;
5839 isl_map *map;
5841 map = isl_map_from_pw_multi_aff(pma);
5842 *umap = isl_union_map_add_map(*umap, map);
5844 return isl_stat_ok;
5847 /* Construct a union map mapping the domain of the union
5848 * piecewise multi-affine expression to its range, with each dimension
5849 * in the range equated to the corresponding affine expression on its cell.
5851 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5852 __isl_take isl_union_pw_multi_aff *upma)
5854 isl_space *space;
5855 isl_union_map *umap;
5857 if (!upma)
5858 return NULL;
5860 space = isl_union_pw_multi_aff_get_space(upma);
5861 umap = isl_union_map_empty(space);
5863 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5864 &map_from_pw_multi_aff, &umap) < 0)
5865 goto error;
5867 isl_union_pw_multi_aff_free(upma);
5868 return umap;
5869 error:
5870 isl_union_pw_multi_aff_free(upma);
5871 isl_union_map_free(umap);
5872 return NULL;
5875 /* Local data for bin_entry and the callback "fn".
5877 struct isl_union_pw_multi_aff_bin_data {
5878 isl_union_pw_multi_aff *upma2;
5879 isl_union_pw_multi_aff *res;
5880 isl_pw_multi_aff *pma;
5881 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5884 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5885 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5887 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5889 struct isl_union_pw_multi_aff_bin_data *data = user;
5890 isl_stat r;
5892 data->pma = pma;
5893 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5894 data->fn, data);
5895 isl_pw_multi_aff_free(pma);
5897 return r;
5900 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5901 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5902 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5903 * as *entry. The callback should adjust data->res if desired.
5905 static __isl_give isl_union_pw_multi_aff *bin_op(
5906 __isl_take isl_union_pw_multi_aff *upma1,
5907 __isl_take isl_union_pw_multi_aff *upma2,
5908 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5910 isl_space *space;
5911 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5913 space = isl_union_pw_multi_aff_get_space(upma2);
5914 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5915 space = isl_union_pw_multi_aff_get_space(upma1);
5916 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5918 if (!upma1 || !upma2)
5919 goto error;
5921 data.upma2 = upma2;
5922 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5923 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5924 &bin_entry, &data) < 0)
5925 goto error;
5927 isl_union_pw_multi_aff_free(upma1);
5928 isl_union_pw_multi_aff_free(upma2);
5929 return data.res;
5930 error:
5931 isl_union_pw_multi_aff_free(upma1);
5932 isl_union_pw_multi_aff_free(upma2);
5933 isl_union_pw_multi_aff_free(data.res);
5934 return NULL;
5937 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5938 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5940 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5941 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5943 isl_space *space;
5945 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5946 isl_pw_multi_aff_get_space(pma2));
5947 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5948 &isl_multi_aff_range_product);
5951 /* Given two isl_pw_multi_affs A -> B and C -> D,
5952 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5954 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5955 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5957 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5958 &pw_multi_aff_range_product);
5961 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5962 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5964 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5965 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5967 isl_space *space;
5969 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5970 isl_pw_multi_aff_get_space(pma2));
5971 space = isl_space_flatten_range(space);
5972 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5973 &isl_multi_aff_flat_range_product);
5976 /* Given two isl_pw_multi_affs A -> B and C -> D,
5977 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5979 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5980 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5982 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5983 &pw_multi_aff_flat_range_product);
5986 /* If data->pma and "pma2" have the same domain space, then compute
5987 * their flat range product and the result to data->res.
5989 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5990 void *user)
5992 struct isl_union_pw_multi_aff_bin_data *data = user;
5994 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5995 pma2->dim, isl_dim_in)) {
5996 isl_pw_multi_aff_free(pma2);
5997 return isl_stat_ok;
6000 pma2 = isl_pw_multi_aff_flat_range_product(
6001 isl_pw_multi_aff_copy(data->pma), pma2);
6003 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6005 return isl_stat_ok;
6008 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6009 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6011 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6012 __isl_take isl_union_pw_multi_aff *upma1,
6013 __isl_take isl_union_pw_multi_aff *upma2)
6015 return bin_op(upma1, upma2, &flat_range_product_entry);
6018 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6019 * The parameters are assumed to have been aligned.
6021 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6022 * except that it works on two different isl_pw_* types.
6024 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6025 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6026 __isl_take isl_pw_aff *pa)
6028 int i, j, n;
6029 isl_pw_multi_aff *res = NULL;
6031 if (!pma || !pa)
6032 goto error;
6034 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6035 pa->dim, isl_dim_in))
6036 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6037 "domains don't match", goto error);
6038 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6039 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6040 "index out of bounds", goto error);
6042 n = pma->n * pa->n;
6043 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6045 for (i = 0; i < pma->n; ++i) {
6046 for (j = 0; j < pa->n; ++j) {
6047 isl_set *common;
6048 isl_multi_aff *res_ij;
6049 int empty;
6051 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6052 isl_set_copy(pa->p[j].set));
6053 empty = isl_set_plain_is_empty(common);
6054 if (empty < 0 || empty) {
6055 isl_set_free(common);
6056 if (empty < 0)
6057 goto error;
6058 continue;
6061 res_ij = isl_multi_aff_set_aff(
6062 isl_multi_aff_copy(pma->p[i].maff), pos,
6063 isl_aff_copy(pa->p[j].aff));
6064 res_ij = isl_multi_aff_gist(res_ij,
6065 isl_set_copy(common));
6067 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6071 isl_pw_multi_aff_free(pma);
6072 isl_pw_aff_free(pa);
6073 return res;
6074 error:
6075 isl_pw_multi_aff_free(pma);
6076 isl_pw_aff_free(pa);
6077 return isl_pw_multi_aff_free(res);
6080 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6082 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6083 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6084 __isl_take isl_pw_aff *pa)
6086 if (!pma || !pa)
6087 goto error;
6088 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
6089 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6090 if (!isl_space_has_named_params(pma->dim) ||
6091 !isl_space_has_named_params(pa->dim))
6092 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6093 "unaligned unnamed parameters", goto error);
6094 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6095 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6096 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6097 error:
6098 isl_pw_multi_aff_free(pma);
6099 isl_pw_aff_free(pa);
6100 return NULL;
6103 /* Do the parameters of "pa" match those of "space"?
6105 isl_bool isl_pw_aff_matching_params(__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_bool_error;
6114 pa_space = isl_pw_aff_get_space(pa);
6116 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6118 isl_space_free(pa_space);
6119 return match;
6122 /* Check that the domain space of "pa" matches "space".
6124 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6125 __isl_keep isl_space *space)
6127 isl_space *pa_space;
6128 isl_bool match;
6130 if (!pa || !space)
6131 return isl_stat_error;
6133 pa_space = isl_pw_aff_get_space(pa);
6135 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6136 if (match < 0)
6137 goto error;
6138 if (!match)
6139 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6140 "parameters don't match", goto error);
6141 match = isl_space_tuple_is_equal(space, isl_dim_in,
6142 pa_space, isl_dim_in);
6143 if (match < 0)
6144 goto error;
6145 if (!match)
6146 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6147 "domains don't match", goto error);
6148 isl_space_free(pa_space);
6149 return isl_stat_ok;
6150 error:
6151 isl_space_free(pa_space);
6152 return isl_stat_error;
6155 #undef BASE
6156 #define BASE pw_aff
6157 #undef DOMBASE
6158 #define DOMBASE set
6160 #include <isl_multi_templ.c>
6161 #include <isl_multi_apply_set.c>
6162 #include <isl_multi_coalesce.c>
6163 #include <isl_multi_gist.c>
6164 #include <isl_multi_hash.c>
6165 #include <isl_multi_intersect.c>
6167 /* Scale the elements of "pma" by the corresponding elements of "mv".
6169 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6170 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6172 int i;
6174 pma = isl_pw_multi_aff_cow(pma);
6175 if (!pma || !mv)
6176 goto error;
6177 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6178 mv->space, isl_dim_set))
6179 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6180 "spaces don't match", goto error);
6181 if (!isl_space_match(pma->dim, isl_dim_param,
6182 mv->space, isl_dim_param)) {
6183 pma = isl_pw_multi_aff_align_params(pma,
6184 isl_multi_val_get_space(mv));
6185 mv = isl_multi_val_align_params(mv,
6186 isl_pw_multi_aff_get_space(pma));
6187 if (!pma || !mv)
6188 goto error;
6191 for (i = 0; i < pma->n; ++i) {
6192 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6193 isl_multi_val_copy(mv));
6194 if (!pma->p[i].maff)
6195 goto error;
6198 isl_multi_val_free(mv);
6199 return pma;
6200 error:
6201 isl_multi_val_free(mv);
6202 isl_pw_multi_aff_free(pma);
6203 return NULL;
6206 /* This function is called for each entry of an isl_union_pw_multi_aff.
6207 * If the space of the entry matches that of data->mv,
6208 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6209 * Otherwise, return an empty isl_pw_multi_aff.
6211 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6212 __isl_take isl_pw_multi_aff *pma, void *user)
6214 isl_multi_val *mv = user;
6216 if (!pma)
6217 return NULL;
6218 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6219 mv->space, isl_dim_set)) {
6220 isl_space *space = isl_pw_multi_aff_get_space(pma);
6221 isl_pw_multi_aff_free(pma);
6222 return isl_pw_multi_aff_empty(space);
6225 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6228 /* Scale the elements of "upma" by the corresponding elements of "mv",
6229 * for those entries that match the space of "mv".
6231 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6232 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6234 upma = isl_union_pw_multi_aff_align_params(upma,
6235 isl_multi_val_get_space(mv));
6236 mv = isl_multi_val_align_params(mv,
6237 isl_union_pw_multi_aff_get_space(upma));
6238 if (!upma || !mv)
6239 goto error;
6241 return isl_union_pw_multi_aff_transform(upma,
6242 &union_pw_multi_aff_scale_multi_val_entry, mv);
6244 isl_multi_val_free(mv);
6245 return upma;
6246 error:
6247 isl_multi_val_free(mv);
6248 isl_union_pw_multi_aff_free(upma);
6249 return NULL;
6252 /* Construct and return a piecewise multi affine expression
6253 * in the given space with value zero in each of the output dimensions and
6254 * a universe domain.
6256 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6258 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6261 /* Construct and return a piecewise multi affine expression
6262 * that is equal to the given piecewise affine expression.
6264 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6265 __isl_take isl_pw_aff *pa)
6267 int i;
6268 isl_space *space;
6269 isl_pw_multi_aff *pma;
6271 if (!pa)
6272 return NULL;
6274 space = isl_pw_aff_get_space(pa);
6275 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6277 for (i = 0; i < pa->n; ++i) {
6278 isl_set *set;
6279 isl_multi_aff *ma;
6281 set = isl_set_copy(pa->p[i].set);
6282 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6283 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6286 isl_pw_aff_free(pa);
6287 return pma;
6290 /* Construct a set or map mapping the shared (parameter) domain
6291 * of the piecewise affine expressions to the range of "mpa"
6292 * with each dimension in the range equated to the
6293 * corresponding piecewise affine expression.
6295 static __isl_give isl_map *map_from_multi_pw_aff(
6296 __isl_take isl_multi_pw_aff *mpa)
6298 int i;
6299 isl_space *space;
6300 isl_map *map;
6302 if (!mpa)
6303 return NULL;
6305 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6306 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6307 "invalid space", goto error);
6309 space = isl_multi_pw_aff_get_domain_space(mpa);
6310 map = isl_map_universe(isl_space_from_domain(space));
6312 for (i = 0; i < mpa->n; ++i) {
6313 isl_pw_aff *pa;
6314 isl_map *map_i;
6316 pa = isl_pw_aff_copy(mpa->p[i]);
6317 map_i = map_from_pw_aff(pa);
6319 map = isl_map_flat_range_product(map, map_i);
6322 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6324 isl_multi_pw_aff_free(mpa);
6325 return map;
6326 error:
6327 isl_multi_pw_aff_free(mpa);
6328 return NULL;
6331 /* Construct a map mapping the shared domain
6332 * of the piecewise affine expressions to the range of "mpa"
6333 * with each dimension in the range equated to the
6334 * corresponding piecewise affine expression.
6336 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6338 if (!mpa)
6339 return NULL;
6340 if (isl_space_is_set(mpa->space))
6341 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6342 "space of input is not a map", goto error);
6344 return map_from_multi_pw_aff(mpa);
6345 error:
6346 isl_multi_pw_aff_free(mpa);
6347 return NULL;
6350 /* Construct a set mapping the shared parameter domain
6351 * of the piecewise affine expressions to the space of "mpa"
6352 * with each dimension in the range equated to the
6353 * corresponding piecewise affine expression.
6355 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6357 if (!mpa)
6358 return NULL;
6359 if (!isl_space_is_set(mpa->space))
6360 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6361 "space of input is not a set", goto error);
6363 return map_from_multi_pw_aff(mpa);
6364 error:
6365 isl_multi_pw_aff_free(mpa);
6366 return NULL;
6369 /* Construct and return a piecewise multi affine expression
6370 * that is equal to the given multi piecewise affine expression
6371 * on the shared domain of the piecewise affine expressions.
6373 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6374 __isl_take isl_multi_pw_aff *mpa)
6376 int i;
6377 isl_space *space;
6378 isl_pw_aff *pa;
6379 isl_pw_multi_aff *pma;
6381 if (!mpa)
6382 return NULL;
6384 space = isl_multi_pw_aff_get_space(mpa);
6386 if (mpa->n == 0) {
6387 isl_multi_pw_aff_free(mpa);
6388 return isl_pw_multi_aff_zero(space);
6391 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6392 pma = isl_pw_multi_aff_from_pw_aff(pa);
6394 for (i = 1; i < mpa->n; ++i) {
6395 isl_pw_multi_aff *pma_i;
6397 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6398 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6399 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6402 pma = isl_pw_multi_aff_reset_space(pma, space);
6404 isl_multi_pw_aff_free(mpa);
6405 return pma;
6408 /* Construct and return a multi piecewise affine expression
6409 * that is equal to the given multi affine expression.
6411 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6412 __isl_take isl_multi_aff *ma)
6414 int i, n;
6415 isl_multi_pw_aff *mpa;
6417 if (!ma)
6418 return NULL;
6420 n = isl_multi_aff_dim(ma, isl_dim_out);
6421 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6423 for (i = 0; i < n; ++i) {
6424 isl_pw_aff *pa;
6426 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6427 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6430 isl_multi_aff_free(ma);
6431 return mpa;
6434 /* Construct and return a multi piecewise affine expression
6435 * that is equal to the given piecewise multi affine expression.
6437 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6438 __isl_take isl_pw_multi_aff *pma)
6440 int i, n;
6441 isl_space *space;
6442 isl_multi_pw_aff *mpa;
6444 if (!pma)
6445 return NULL;
6447 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6448 space = isl_pw_multi_aff_get_space(pma);
6449 mpa = isl_multi_pw_aff_alloc(space);
6451 for (i = 0; i < n; ++i) {
6452 isl_pw_aff *pa;
6454 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6455 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6458 isl_pw_multi_aff_free(pma);
6459 return mpa;
6462 /* Do "pa1" and "pa2" represent the same function?
6464 * We first check if they are obviously equal.
6465 * If not, we convert them to maps and check if those are equal.
6467 * If "pa1" or "pa2" contain any NaNs, then they are considered
6468 * not to be the same. A NaN is not equal to anything, not even
6469 * to another NaN.
6471 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6472 __isl_keep isl_pw_aff *pa2)
6474 isl_bool equal;
6475 isl_bool has_nan;
6476 isl_map *map1, *map2;
6478 if (!pa1 || !pa2)
6479 return isl_bool_error;
6481 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6482 if (equal < 0 || equal)
6483 return equal;
6484 has_nan = either_involves_nan(pa1, pa2);
6485 if (has_nan < 0)
6486 return isl_bool_error;
6487 if (has_nan)
6488 return isl_bool_false;
6490 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6491 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6492 equal = isl_map_is_equal(map1, map2);
6493 isl_map_free(map1);
6494 isl_map_free(map2);
6496 return equal;
6499 /* Do "mpa1" and "mpa2" represent the same function?
6501 * Note that we cannot convert the entire isl_multi_pw_aff
6502 * to a map because the domains of the piecewise affine expressions
6503 * may not be the same.
6505 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6506 __isl_keep isl_multi_pw_aff *mpa2)
6508 int i;
6509 isl_bool equal;
6511 if (!mpa1 || !mpa2)
6512 return isl_bool_error;
6514 if (!isl_space_match(mpa1->space, isl_dim_param,
6515 mpa2->space, isl_dim_param)) {
6516 if (!isl_space_has_named_params(mpa1->space))
6517 return isl_bool_false;
6518 if (!isl_space_has_named_params(mpa2->space))
6519 return isl_bool_false;
6520 mpa1 = isl_multi_pw_aff_copy(mpa1);
6521 mpa2 = isl_multi_pw_aff_copy(mpa2);
6522 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6523 isl_multi_pw_aff_get_space(mpa2));
6524 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6525 isl_multi_pw_aff_get_space(mpa1));
6526 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6527 isl_multi_pw_aff_free(mpa1);
6528 isl_multi_pw_aff_free(mpa2);
6529 return equal;
6532 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6533 if (equal < 0 || !equal)
6534 return equal;
6536 for (i = 0; i < mpa1->n; ++i) {
6537 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6538 if (equal < 0 || !equal)
6539 return equal;
6542 return isl_bool_true;
6545 /* Do "pma1" and "pma2" represent the same function?
6547 * First check if they are obviously equal.
6548 * If not, then convert them to maps and check if those are equal.
6550 * If "pa1" or "pa2" contain any NaNs, then they are considered
6551 * not to be the same. A NaN is not equal to anything, not even
6552 * to another NaN.
6554 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6555 __isl_keep isl_pw_multi_aff *pma2)
6557 isl_bool equal;
6558 isl_bool has_nan;
6559 isl_map *map1, *map2;
6561 if (!pma1 || !pma2)
6562 return isl_bool_error;
6564 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6565 if (equal < 0 || equal)
6566 return equal;
6567 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6568 if (has_nan >= 0 && !has_nan)
6569 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6570 if (has_nan < 0 || has_nan)
6571 return isl_bool_not(has_nan);
6573 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6574 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6575 equal = isl_map_is_equal(map1, map2);
6576 isl_map_free(map1);
6577 isl_map_free(map2);
6579 return equal;
6582 /* Compute the pullback of "mpa" by the function represented by "ma".
6583 * In other words, plug in "ma" in "mpa".
6585 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6587 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6588 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6590 int i;
6591 isl_space *space = NULL;
6593 mpa = isl_multi_pw_aff_cow(mpa);
6594 if (!mpa || !ma)
6595 goto error;
6597 space = isl_space_join(isl_multi_aff_get_space(ma),
6598 isl_multi_pw_aff_get_space(mpa));
6599 if (!space)
6600 goto error;
6602 for (i = 0; i < mpa->n; ++i) {
6603 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6604 isl_multi_aff_copy(ma));
6605 if (!mpa->p[i])
6606 goto error;
6609 isl_multi_aff_free(ma);
6610 isl_space_free(mpa->space);
6611 mpa->space = space;
6612 return mpa;
6613 error:
6614 isl_space_free(space);
6615 isl_multi_pw_aff_free(mpa);
6616 isl_multi_aff_free(ma);
6617 return NULL;
6620 /* Compute the pullback of "mpa" by the function represented by "ma".
6621 * In other words, plug in "ma" in "mpa".
6623 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6624 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6626 if (!mpa || !ma)
6627 goto error;
6628 if (isl_space_match(mpa->space, isl_dim_param,
6629 ma->space, isl_dim_param))
6630 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6631 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6632 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6633 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6634 error:
6635 isl_multi_pw_aff_free(mpa);
6636 isl_multi_aff_free(ma);
6637 return NULL;
6640 /* Compute the pullback of "mpa" by the function represented by "pma".
6641 * In other words, plug in "pma" in "mpa".
6643 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6645 static __isl_give isl_multi_pw_aff *
6646 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6647 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6649 int i;
6650 isl_space *space = NULL;
6652 mpa = isl_multi_pw_aff_cow(mpa);
6653 if (!mpa || !pma)
6654 goto error;
6656 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6657 isl_multi_pw_aff_get_space(mpa));
6659 for (i = 0; i < mpa->n; ++i) {
6660 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6661 isl_pw_multi_aff_copy(pma));
6662 if (!mpa->p[i])
6663 goto error;
6666 isl_pw_multi_aff_free(pma);
6667 isl_space_free(mpa->space);
6668 mpa->space = space;
6669 return mpa;
6670 error:
6671 isl_space_free(space);
6672 isl_multi_pw_aff_free(mpa);
6673 isl_pw_multi_aff_free(pma);
6674 return NULL;
6677 /* Compute the pullback of "mpa" by the function represented by "pma".
6678 * In other words, plug in "pma" in "mpa".
6680 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6681 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6683 if (!mpa || !pma)
6684 goto error;
6685 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6686 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6687 mpa = isl_multi_pw_aff_align_params(mpa,
6688 isl_pw_multi_aff_get_space(pma));
6689 pma = isl_pw_multi_aff_align_params(pma,
6690 isl_multi_pw_aff_get_space(mpa));
6691 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6692 error:
6693 isl_multi_pw_aff_free(mpa);
6694 isl_pw_multi_aff_free(pma);
6695 return NULL;
6698 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6699 * with the domain of "aff". The domain of the result is the same
6700 * as that of "mpa".
6701 * "mpa" and "aff" are assumed to have been aligned.
6703 * We first extract the parametric constant from "aff", defined
6704 * over the correct domain.
6705 * Then we add the appropriate combinations of the members of "mpa".
6706 * Finally, we add the integer divisions through recursive calls.
6708 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6709 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6711 int i, n_in, n_div;
6712 isl_space *space;
6713 isl_val *v;
6714 isl_pw_aff *pa;
6715 isl_aff *tmp;
6717 n_in = isl_aff_dim(aff, isl_dim_in);
6718 n_div = isl_aff_dim(aff, isl_dim_div);
6720 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6721 tmp = isl_aff_copy(aff);
6722 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6723 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6724 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6725 isl_space_dim(space, isl_dim_set));
6726 tmp = isl_aff_reset_domain_space(tmp, space);
6727 pa = isl_pw_aff_from_aff(tmp);
6729 for (i = 0; i < n_in; ++i) {
6730 isl_pw_aff *pa_i;
6732 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6733 continue;
6734 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6735 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6736 pa_i = isl_pw_aff_scale_val(pa_i, v);
6737 pa = isl_pw_aff_add(pa, pa_i);
6740 for (i = 0; i < n_div; ++i) {
6741 isl_aff *div;
6742 isl_pw_aff *pa_i;
6744 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6745 continue;
6746 div = isl_aff_get_div(aff, i);
6747 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6748 isl_multi_pw_aff_copy(mpa), div);
6749 pa_i = isl_pw_aff_floor(pa_i);
6750 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6751 pa_i = isl_pw_aff_scale_val(pa_i, v);
6752 pa = isl_pw_aff_add(pa, pa_i);
6755 isl_multi_pw_aff_free(mpa);
6756 isl_aff_free(aff);
6758 return pa;
6761 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6762 * with the domain of "aff". The domain of the result is the same
6763 * as that of "mpa".
6765 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6766 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6768 if (!aff || !mpa)
6769 goto error;
6770 if (isl_space_match(aff->ls->dim, isl_dim_param,
6771 mpa->space, isl_dim_param))
6772 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6774 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6775 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6777 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6778 error:
6779 isl_aff_free(aff);
6780 isl_multi_pw_aff_free(mpa);
6781 return NULL;
6784 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6785 * with the domain of "pa". The domain of the result is the same
6786 * as that of "mpa".
6787 * "mpa" and "pa" are assumed to have been aligned.
6789 * We consider each piece in turn. Note that the domains of the
6790 * pieces are assumed to be disjoint and they remain disjoint
6791 * after taking the preimage (over the same function).
6793 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6794 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6796 isl_space *space;
6797 isl_pw_aff *res;
6798 int i;
6800 if (!mpa || !pa)
6801 goto error;
6803 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6804 isl_pw_aff_get_space(pa));
6805 res = isl_pw_aff_empty(space);
6807 for (i = 0; i < pa->n; ++i) {
6808 isl_pw_aff *pa_i;
6809 isl_set *domain;
6811 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6812 isl_multi_pw_aff_copy(mpa),
6813 isl_aff_copy(pa->p[i].aff));
6814 domain = isl_set_copy(pa->p[i].set);
6815 domain = isl_set_preimage_multi_pw_aff(domain,
6816 isl_multi_pw_aff_copy(mpa));
6817 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6818 res = isl_pw_aff_add_disjoint(res, pa_i);
6821 isl_pw_aff_free(pa);
6822 isl_multi_pw_aff_free(mpa);
6823 return res;
6824 error:
6825 isl_pw_aff_free(pa);
6826 isl_multi_pw_aff_free(mpa);
6827 return NULL;
6830 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6831 * with the domain of "pa". The domain of the result is the same
6832 * as that of "mpa".
6834 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6835 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6837 if (!pa || !mpa)
6838 goto error;
6839 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6840 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6842 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6843 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6845 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6846 error:
6847 isl_pw_aff_free(pa);
6848 isl_multi_pw_aff_free(mpa);
6849 return NULL;
6852 /* Compute the pullback of "pa" by the function represented by "mpa".
6853 * In other words, plug in "mpa" in "pa".
6854 * "pa" and "mpa" are assumed to have been aligned.
6856 * The pullback is computed by applying "pa" to "mpa".
6858 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6859 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6861 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6864 /* Compute the pullback of "pa" by the function represented by "mpa".
6865 * In other words, plug in "mpa" in "pa".
6867 * The pullback is computed by applying "pa" to "mpa".
6869 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6870 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6872 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6875 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6876 * In other words, plug in "mpa2" in "mpa1".
6878 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6880 * We pullback each member of "mpa1" in turn.
6882 static __isl_give isl_multi_pw_aff *
6883 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6884 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6886 int i;
6887 isl_space *space = NULL;
6889 mpa1 = isl_multi_pw_aff_cow(mpa1);
6890 if (!mpa1 || !mpa2)
6891 goto error;
6893 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6894 isl_multi_pw_aff_get_space(mpa1));
6896 for (i = 0; i < mpa1->n; ++i) {
6897 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6898 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6899 if (!mpa1->p[i])
6900 goto error;
6903 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6905 isl_multi_pw_aff_free(mpa2);
6906 return mpa1;
6907 error:
6908 isl_space_free(space);
6909 isl_multi_pw_aff_free(mpa1);
6910 isl_multi_pw_aff_free(mpa2);
6911 return NULL;
6914 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6915 * In other words, plug in "mpa2" in "mpa1".
6917 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6918 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6920 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6921 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6924 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6925 * of "mpa1" and "mpa2" live in the same space, construct map space
6926 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6927 * with this map space as extract argument.
6929 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6930 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6931 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6932 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6934 int match;
6935 isl_space *space1, *space2;
6936 isl_map *res;
6938 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6939 isl_multi_pw_aff_get_space(mpa2));
6940 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6941 isl_multi_pw_aff_get_space(mpa1));
6942 if (!mpa1 || !mpa2)
6943 goto error;
6944 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6945 mpa2->space, isl_dim_out);
6946 if (match < 0)
6947 goto error;
6948 if (!match)
6949 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6950 "range spaces don't match", goto error);
6951 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6952 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6953 space1 = isl_space_map_from_domain_and_range(space1, space2);
6955 res = order(mpa1, mpa2, space1);
6956 isl_multi_pw_aff_free(mpa1);
6957 isl_multi_pw_aff_free(mpa2);
6958 return res;
6959 error:
6960 isl_multi_pw_aff_free(mpa1);
6961 isl_multi_pw_aff_free(mpa2);
6962 return NULL;
6965 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6966 * where the function values are equal. "space" is the space of the result.
6967 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6969 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6970 * in the sequences are equal.
6972 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6973 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6974 __isl_take isl_space *space)
6976 int i, n;
6977 isl_map *res;
6979 res = isl_map_universe(space);
6981 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6982 for (i = 0; i < n; ++i) {
6983 isl_pw_aff *pa1, *pa2;
6984 isl_map *map;
6986 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6987 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6988 map = isl_pw_aff_eq_map(pa1, pa2);
6989 res = isl_map_intersect(res, map);
6992 return res;
6995 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6996 * where the function values are equal.
6998 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6999 __isl_take isl_multi_pw_aff *mpa2)
7001 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7002 &isl_multi_pw_aff_eq_map_on_space);
7005 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7006 * where the function values of "mpa1" is lexicographically satisfies "base"
7007 * compared to that of "mpa2". "space" is the space of the result.
7008 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7010 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7011 * if its i-th element satisfies "base" when compared to
7012 * the i-th element of "mpa2" while all previous elements are
7013 * pairwise equal.
7015 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7016 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7017 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7018 __isl_take isl_pw_aff *pa2),
7019 __isl_take isl_space *space)
7021 int i, n;
7022 isl_map *res, *rest;
7024 res = isl_map_empty(isl_space_copy(space));
7025 rest = isl_map_universe(space);
7027 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7028 for (i = 0; i < n; ++i) {
7029 isl_pw_aff *pa1, *pa2;
7030 isl_map *map;
7032 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7033 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7034 map = base(pa1, pa2);
7035 map = isl_map_intersect(map, isl_map_copy(rest));
7036 res = isl_map_union(res, map);
7038 if (i == n - 1)
7039 continue;
7041 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7042 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7043 map = isl_pw_aff_eq_map(pa1, pa2);
7044 rest = isl_map_intersect(rest, map);
7047 isl_map_free(rest);
7048 return res;
7051 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7052 * where the function value of "mpa1" is lexicographically less than that
7053 * of "mpa2". "space" is the space of the result.
7054 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7056 * "mpa1" is less than "mpa2" if its i-th element is smaller
7057 * than the i-th element of "mpa2" while all previous elements are
7058 * pairwise equal.
7060 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7061 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7062 __isl_take isl_space *space)
7064 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7065 &isl_pw_aff_lt_map, space);
7068 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7069 * where the function value of "mpa1" is lexicographically less than that
7070 * of "mpa2".
7072 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7073 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7075 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7076 &isl_multi_pw_aff_lex_lt_map_on_space);
7079 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7080 * where the function value of "mpa1" is lexicographically greater than that
7081 * of "mpa2". "space" is the space of the result.
7082 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7084 * "mpa1" is greater than "mpa2" if its i-th element is greater
7085 * than the i-th element of "mpa2" while all previous elements are
7086 * pairwise equal.
7088 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7089 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7090 __isl_take isl_space *space)
7092 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7093 &isl_pw_aff_gt_map, space);
7096 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7097 * where the function value of "mpa1" is lexicographically greater than that
7098 * of "mpa2".
7100 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7101 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7103 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7104 &isl_multi_pw_aff_lex_gt_map_on_space);
7107 /* Compare two isl_affs.
7109 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7110 * than "aff2" and 0 if they are equal.
7112 * The order is fairly arbitrary. We do consider expressions that only involve
7113 * earlier dimensions as "smaller".
7115 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7117 int cmp;
7118 int last1, last2;
7120 if (aff1 == aff2)
7121 return 0;
7123 if (!aff1)
7124 return -1;
7125 if (!aff2)
7126 return 1;
7128 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7129 if (cmp != 0)
7130 return cmp;
7132 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7133 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7134 if (last1 != last2)
7135 return last1 - last2;
7137 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7140 /* Compare two isl_pw_affs.
7142 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7143 * than "pa2" and 0 if they are equal.
7145 * The order is fairly arbitrary. We do consider expressions that only involve
7146 * earlier dimensions as "smaller".
7148 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7149 __isl_keep isl_pw_aff *pa2)
7151 int i;
7152 int cmp;
7154 if (pa1 == pa2)
7155 return 0;
7157 if (!pa1)
7158 return -1;
7159 if (!pa2)
7160 return 1;
7162 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7163 if (cmp != 0)
7164 return cmp;
7166 if (pa1->n != pa2->n)
7167 return pa1->n - pa2->n;
7169 for (i = 0; i < pa1->n; ++i) {
7170 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7171 if (cmp != 0)
7172 return cmp;
7173 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7174 if (cmp != 0)
7175 return cmp;
7178 return 0;
7181 /* Return a piecewise affine expression that is equal to "v" on "domain".
7183 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7184 __isl_take isl_val *v)
7186 isl_space *space;
7187 isl_local_space *ls;
7188 isl_aff *aff;
7190 space = isl_set_get_space(domain);
7191 ls = isl_local_space_from_space(space);
7192 aff = isl_aff_val_on_domain(ls, v);
7194 return isl_pw_aff_alloc(domain, aff);
7197 /* Return a multi affine expression that is equal to "mv" on domain
7198 * space "space".
7200 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7201 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7203 int i, n;
7204 isl_space *space2;
7205 isl_local_space *ls;
7206 isl_multi_aff *ma;
7208 if (!space || !mv)
7209 goto error;
7211 n = isl_multi_val_dim(mv, isl_dim_set);
7212 space2 = isl_multi_val_get_space(mv);
7213 space2 = isl_space_align_params(space2, isl_space_copy(space));
7214 space = isl_space_align_params(space, isl_space_copy(space2));
7215 space = isl_space_map_from_domain_and_range(space, space2);
7216 ma = isl_multi_aff_alloc(isl_space_copy(space));
7217 ls = isl_local_space_from_space(isl_space_domain(space));
7218 for (i = 0; i < n; ++i) {
7219 isl_val *v;
7220 isl_aff *aff;
7222 v = isl_multi_val_get_val(mv, i);
7223 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7224 ma = isl_multi_aff_set_aff(ma, i, aff);
7226 isl_local_space_free(ls);
7228 isl_multi_val_free(mv);
7229 return ma;
7230 error:
7231 isl_space_free(space);
7232 isl_multi_val_free(mv);
7233 return NULL;
7236 /* Return a piecewise multi-affine expression
7237 * that is equal to "mv" on "domain".
7239 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7240 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7242 isl_space *space;
7243 isl_multi_aff *ma;
7245 space = isl_set_get_space(domain);
7246 ma = isl_multi_aff_multi_val_on_space(space, mv);
7248 return isl_pw_multi_aff_alloc(domain, ma);
7251 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7252 * mv is the value that should be attained on each domain set
7253 * res collects the results
7255 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7256 isl_multi_val *mv;
7257 isl_union_pw_multi_aff *res;
7260 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7261 * and add it to data->res.
7263 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7264 void *user)
7266 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7267 isl_pw_multi_aff *pma;
7268 isl_multi_val *mv;
7270 mv = isl_multi_val_copy(data->mv);
7271 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7272 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7274 return data->res ? isl_stat_ok : isl_stat_error;
7277 /* Return a union piecewise multi-affine expression
7278 * that is equal to "mv" on "domain".
7280 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7281 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7283 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7284 isl_space *space;
7286 space = isl_union_set_get_space(domain);
7287 data.res = isl_union_pw_multi_aff_empty(space);
7288 data.mv = mv;
7289 if (isl_union_set_foreach_set(domain,
7290 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7291 data.res = isl_union_pw_multi_aff_free(data.res);
7292 isl_union_set_free(domain);
7293 isl_multi_val_free(mv);
7294 return data.res;
7297 /* Compute the pullback of data->pma by the function represented by "pma2",
7298 * provided the spaces match, and add the results to data->res.
7300 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7302 struct isl_union_pw_multi_aff_bin_data *data = user;
7304 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7305 pma2->dim, isl_dim_out)) {
7306 isl_pw_multi_aff_free(pma2);
7307 return isl_stat_ok;
7310 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7311 isl_pw_multi_aff_copy(data->pma), pma2);
7313 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7314 if (!data->res)
7315 return isl_stat_error;
7317 return isl_stat_ok;
7320 /* Compute the pullback of "upma1" by the function represented by "upma2".
7322 __isl_give isl_union_pw_multi_aff *
7323 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7324 __isl_take isl_union_pw_multi_aff *upma1,
7325 __isl_take isl_union_pw_multi_aff *upma2)
7327 return bin_op(upma1, upma2, &pullback_entry);
7330 /* Check that the domain space of "upa" matches "space".
7332 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7333 * can in principle never fail since the space "space" is that
7334 * of the isl_multi_union_pw_aff and is a set space such that
7335 * there is no domain space to match.
7337 * We check the parameters and double-check that "space" is
7338 * indeed that of a set.
7340 static isl_stat isl_union_pw_aff_check_match_domain_space(
7341 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7343 isl_space *upa_space;
7344 isl_bool match;
7346 if (!upa || !space)
7347 return isl_stat_error;
7349 match = isl_space_is_set(space);
7350 if (match < 0)
7351 return isl_stat_error;
7352 if (!match)
7353 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7354 "expecting set space", return -1);
7356 upa_space = isl_union_pw_aff_get_space(upa);
7357 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7358 if (match < 0)
7359 goto error;
7360 if (!match)
7361 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7362 "parameters don't match", goto error);
7364 isl_space_free(upa_space);
7365 return isl_stat_ok;
7366 error:
7367 isl_space_free(upa_space);
7368 return isl_stat_error;
7371 /* Do the parameters of "upa" match those of "space"?
7373 static isl_bool isl_union_pw_aff_matching_params(
7374 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7376 isl_space *upa_space;
7377 isl_bool match;
7379 if (!upa || !space)
7380 return isl_bool_error;
7382 upa_space = isl_union_pw_aff_get_space(upa);
7384 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7386 isl_space_free(upa_space);
7387 return match;
7390 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7391 * space represents the new parameters.
7392 * res collects the results.
7394 struct isl_union_pw_aff_reset_params_data {
7395 isl_space *space;
7396 isl_union_pw_aff *res;
7399 /* Replace the parameters of "pa" by data->space and
7400 * add the result to data->res.
7402 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7404 struct isl_union_pw_aff_reset_params_data *data = user;
7405 isl_space *space;
7407 space = isl_pw_aff_get_space(pa);
7408 space = isl_space_replace(space, isl_dim_param, data->space);
7409 pa = isl_pw_aff_reset_space(pa, space);
7410 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7412 return data->res ? isl_stat_ok : isl_stat_error;
7415 /* Replace the domain space of "upa" by "space".
7416 * Since a union expression does not have a (single) domain space,
7417 * "space" is necessarily a parameter space.
7419 * Since the order and the names of the parameters determine
7420 * the hash value, we need to create a new hash table.
7422 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7423 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7425 struct isl_union_pw_aff_reset_params_data data = { space };
7426 isl_bool match;
7428 match = isl_union_pw_aff_matching_params(upa, space);
7429 if (match < 0)
7430 upa = isl_union_pw_aff_free(upa);
7431 else if (match) {
7432 isl_space_free(space);
7433 return upa;
7436 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7437 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7438 data.res = isl_union_pw_aff_free(data.res);
7440 isl_union_pw_aff_free(upa);
7441 isl_space_free(space);
7442 return data.res;
7445 /* Return the floor of "pa".
7447 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7449 return isl_pw_aff_floor(pa);
7452 /* Given f, return floor(f).
7454 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7455 __isl_take isl_union_pw_aff *upa)
7457 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7460 /* Compute
7462 * upa mod m = upa - m * floor(upa/m)
7464 * with m an integer value.
7466 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7467 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7469 isl_union_pw_aff *res;
7471 if (!upa || !m)
7472 goto error;
7474 if (!isl_val_is_int(m))
7475 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7476 "expecting integer modulo", goto error);
7477 if (!isl_val_is_pos(m))
7478 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7479 "expecting positive modulo", goto error);
7481 res = isl_union_pw_aff_copy(upa);
7482 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7483 upa = isl_union_pw_aff_floor(upa);
7484 upa = isl_union_pw_aff_scale_val(upa, m);
7485 res = isl_union_pw_aff_sub(res, upa);
7487 return res;
7488 error:
7489 isl_val_free(m);
7490 isl_union_pw_aff_free(upa);
7491 return NULL;
7494 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7495 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7496 * needs to attain.
7497 * "res" collects the results.
7499 struct isl_union_pw_aff_aff_on_domain_data {
7500 isl_aff *aff;
7501 isl_union_pw_aff *res;
7504 /* Construct a piecewise affine expression that is equal to data->aff
7505 * on "domain" and add the result to data->res.
7507 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7509 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7510 isl_pw_aff *pa;
7511 isl_aff *aff;
7512 int dim;
7514 aff = isl_aff_copy(data->aff);
7515 dim = isl_set_dim(domain, isl_dim_set);
7516 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7517 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7518 pa = isl_pw_aff_alloc(domain, aff);
7519 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7521 return data->res ? isl_stat_ok : isl_stat_error;
7524 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7525 * pos is the output position that needs to be extracted.
7526 * res collects the results.
7528 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7529 int pos;
7530 isl_union_pw_aff *res;
7533 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7534 * (assuming it has such a dimension) and add it to data->res.
7536 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7538 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7539 int n_out;
7540 isl_pw_aff *pa;
7542 if (!pma)
7543 return isl_stat_error;
7545 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7546 if (data->pos >= n_out) {
7547 isl_pw_multi_aff_free(pma);
7548 return isl_stat_ok;
7551 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7552 isl_pw_multi_aff_free(pma);
7554 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7556 return data->res ? isl_stat_ok : isl_stat_error;
7559 /* Extract an isl_union_pw_aff corresponding to
7560 * output dimension "pos" of "upma".
7562 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7563 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7565 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7566 isl_space *space;
7568 if (!upma)
7569 return NULL;
7571 if (pos < 0)
7572 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7573 "cannot extract at negative position", return NULL);
7575 space = isl_union_pw_multi_aff_get_space(upma);
7576 data.res = isl_union_pw_aff_empty(space);
7577 data.pos = pos;
7578 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7579 &get_union_pw_aff, &data) < 0)
7580 data.res = isl_union_pw_aff_free(data.res);
7582 return data.res;
7585 /* Return a union piecewise affine expression
7586 * that is equal to "aff" on "domain".
7588 * Construct an isl_pw_aff on each of the sets in "domain" and
7589 * collect the results.
7591 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7592 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7594 struct isl_union_pw_aff_aff_on_domain_data data;
7595 isl_space *space;
7597 if (!domain || !aff)
7598 goto error;
7599 if (!isl_local_space_is_params(aff->ls))
7600 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7601 "expecting parametric expression", goto error);
7603 space = isl_union_set_get_space(domain);
7604 data.res = isl_union_pw_aff_empty(space);
7605 data.aff = aff;
7606 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7607 data.res = isl_union_pw_aff_free(data.res);
7608 isl_union_set_free(domain);
7609 isl_aff_free(aff);
7610 return data.res;
7611 error:
7612 isl_union_set_free(domain);
7613 isl_aff_free(aff);
7614 return NULL;
7617 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7618 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7619 * "res" collects the results.
7621 struct isl_union_pw_aff_val_on_domain_data {
7622 isl_val *v;
7623 isl_union_pw_aff *res;
7626 /* Construct a piecewise affine expression that is equal to data->v
7627 * on "domain" and add the result to data->res.
7629 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7631 struct isl_union_pw_aff_val_on_domain_data *data = user;
7632 isl_pw_aff *pa;
7633 isl_val *v;
7635 v = isl_val_copy(data->v);
7636 pa = isl_pw_aff_val_on_domain(domain, v);
7637 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7639 return data->res ? isl_stat_ok : isl_stat_error;
7642 /* Return a union piecewise affine expression
7643 * that is equal to "v" on "domain".
7645 * Construct an isl_pw_aff on each of the sets in "domain" and
7646 * collect the results.
7648 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7649 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7651 struct isl_union_pw_aff_val_on_domain_data data;
7652 isl_space *space;
7654 space = isl_union_set_get_space(domain);
7655 data.res = isl_union_pw_aff_empty(space);
7656 data.v = v;
7657 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7658 data.res = isl_union_pw_aff_free(data.res);
7659 isl_union_set_free(domain);
7660 isl_val_free(v);
7661 return data.res;
7664 /* Construct a piecewise multi affine expression
7665 * that is equal to "pa" and add it to upma.
7667 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7668 void *user)
7670 isl_union_pw_multi_aff **upma = user;
7671 isl_pw_multi_aff *pma;
7673 pma = isl_pw_multi_aff_from_pw_aff(pa);
7674 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7676 return *upma ? isl_stat_ok : isl_stat_error;
7679 /* Construct and return a union piecewise multi affine expression
7680 * that is equal to the given union piecewise affine expression.
7682 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7683 __isl_take isl_union_pw_aff *upa)
7685 isl_space *space;
7686 isl_union_pw_multi_aff *upma;
7688 if (!upa)
7689 return NULL;
7691 space = isl_union_pw_aff_get_space(upa);
7692 upma = isl_union_pw_multi_aff_empty(space);
7694 if (isl_union_pw_aff_foreach_pw_aff(upa,
7695 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7696 upma = isl_union_pw_multi_aff_free(upma);
7698 isl_union_pw_aff_free(upa);
7699 return upma;
7702 /* Compute the set of elements in the domain of "pa" where it is zero and
7703 * add this set to "uset".
7705 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7707 isl_union_set **uset = (isl_union_set **)user;
7709 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7711 return *uset ? isl_stat_ok : isl_stat_error;
7714 /* Return a union set containing those elements in the domain
7715 * of "upa" where it is zero.
7717 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7718 __isl_take isl_union_pw_aff *upa)
7720 isl_union_set *zero;
7722 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7723 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7724 zero = isl_union_set_free(zero);
7726 isl_union_pw_aff_free(upa);
7727 return zero;
7730 /* Convert "pa" to an isl_map and add it to *umap.
7732 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7734 isl_union_map **umap = user;
7735 isl_map *map;
7737 map = isl_map_from_pw_aff(pa);
7738 *umap = isl_union_map_add_map(*umap, map);
7740 return *umap ? isl_stat_ok : isl_stat_error;
7743 /* Construct a union map mapping the domain of the union
7744 * piecewise affine expression to its range, with the single output dimension
7745 * equated to the corresponding affine expressions on their cells.
7747 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7748 __isl_take isl_union_pw_aff *upa)
7750 isl_space *space;
7751 isl_union_map *umap;
7753 if (!upa)
7754 return NULL;
7756 space = isl_union_pw_aff_get_space(upa);
7757 umap = isl_union_map_empty(space);
7759 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7760 &umap) < 0)
7761 umap = isl_union_map_free(umap);
7763 isl_union_pw_aff_free(upa);
7764 return umap;
7767 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7768 * upma is the function that is plugged in.
7769 * pa is the current part of the function in which upma is plugged in.
7770 * res collects the results.
7772 struct isl_union_pw_aff_pullback_upma_data {
7773 isl_union_pw_multi_aff *upma;
7774 isl_pw_aff *pa;
7775 isl_union_pw_aff *res;
7778 /* Check if "pma" can be plugged into data->pa.
7779 * If so, perform the pullback and add the result to data->res.
7781 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7783 struct isl_union_pw_aff_pullback_upma_data *data = user;
7784 isl_pw_aff *pa;
7786 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7787 pma->dim, isl_dim_out)) {
7788 isl_pw_multi_aff_free(pma);
7789 return isl_stat_ok;
7792 pa = isl_pw_aff_copy(data->pa);
7793 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7795 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7797 return data->res ? isl_stat_ok : isl_stat_error;
7800 /* Check if any of the elements of data->upma can be plugged into pa,
7801 * add if so add the result to data->res.
7803 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7805 struct isl_union_pw_aff_pullback_upma_data *data = user;
7806 isl_stat r;
7808 data->pa = pa;
7809 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7810 &pa_pb_pma, data);
7811 isl_pw_aff_free(pa);
7813 return r;
7816 /* Compute the pullback of "upa" by the function represented by "upma".
7817 * In other words, plug in "upma" in "upa". The result contains
7818 * expressions defined over the domain space of "upma".
7820 * Run over all pairs of elements in "upa" and "upma", perform
7821 * the pullback when appropriate and collect the results.
7822 * If the hash value were based on the domain space rather than
7823 * the function space, then we could run through all elements
7824 * of "upma" and directly pick out the corresponding element of "upa".
7826 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7827 __isl_take isl_union_pw_aff *upa,
7828 __isl_take isl_union_pw_multi_aff *upma)
7830 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7831 isl_space *space;
7833 space = isl_union_pw_multi_aff_get_space(upma);
7834 upa = isl_union_pw_aff_align_params(upa, space);
7835 space = isl_union_pw_aff_get_space(upa);
7836 upma = isl_union_pw_multi_aff_align_params(upma, space);
7838 if (!upa || !upma)
7839 goto error;
7841 data.upma = upma;
7842 data.res = isl_union_pw_aff_alloc_same_size(upa);
7843 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7844 data.res = isl_union_pw_aff_free(data.res);
7846 isl_union_pw_aff_free(upa);
7847 isl_union_pw_multi_aff_free(upma);
7848 return data.res;
7849 error:
7850 isl_union_pw_aff_free(upa);
7851 isl_union_pw_multi_aff_free(upma);
7852 return NULL;
7855 #undef BASE
7856 #define BASE union_pw_aff
7857 #undef DOMBASE
7858 #define DOMBASE union_set
7860 #define NO_MOVE_DIMS
7861 #define NO_DIMS
7862 #define NO_DOMAIN
7863 #define NO_PRODUCT
7864 #define NO_SPLICE
7865 #define NO_ZERO
7866 #define NO_IDENTITY
7867 #define NO_GIST
7869 #include <isl_multi_templ.c>
7870 #include <isl_multi_apply_set.c>
7871 #include <isl_multi_apply_union_set.c>
7872 #include <isl_multi_coalesce.c>
7873 #include <isl_multi_floor.c>
7874 #include <isl_multi_gist.c>
7875 #include <isl_multi_intersect.c>
7877 /* Construct a multiple union piecewise affine expression
7878 * in the given space with value zero in each of the output dimensions.
7880 * Since there is no canonical zero value for
7881 * a union piecewise affine expression, we can only construct
7882 * zero-dimensional "zero" value.
7884 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7885 __isl_take isl_space *space)
7887 if (!space)
7888 return NULL;
7890 if (!isl_space_is_set(space))
7891 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7892 "expecting set space", goto error);
7893 if (isl_space_dim(space , isl_dim_out) != 0)
7894 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7895 "expecting 0D space", goto error);
7897 return isl_multi_union_pw_aff_alloc(space);
7898 error:
7899 isl_space_free(space);
7900 return NULL;
7903 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7904 * with the actual sum on the shared domain and
7905 * the defined expression on the symmetric difference of the domains.
7907 * We simply iterate over the elements in both arguments and
7908 * call isl_union_pw_aff_union_add on each of them.
7910 static __isl_give isl_multi_union_pw_aff *
7911 isl_multi_union_pw_aff_union_add_aligned(
7912 __isl_take isl_multi_union_pw_aff *mupa1,
7913 __isl_take isl_multi_union_pw_aff *mupa2)
7915 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7916 &isl_union_pw_aff_union_add);
7919 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7920 * with the actual sum on the shared domain and
7921 * the defined expression on the symmetric difference of the domains.
7923 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7924 __isl_take isl_multi_union_pw_aff *mupa1,
7925 __isl_take isl_multi_union_pw_aff *mupa2)
7927 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7928 &isl_multi_union_pw_aff_union_add_aligned);
7931 /* Construct and return a multi union piecewise affine expression
7932 * that is equal to the given multi affine expression.
7934 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7935 __isl_take isl_multi_aff *ma)
7937 isl_multi_pw_aff *mpa;
7939 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7940 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7943 /* Construct and return a multi union piecewise affine expression
7944 * that is equal to the given multi piecewise affine expression.
7946 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7947 __isl_take isl_multi_pw_aff *mpa)
7949 int i, n;
7950 isl_space *space;
7951 isl_multi_union_pw_aff *mupa;
7953 if (!mpa)
7954 return NULL;
7956 space = isl_multi_pw_aff_get_space(mpa);
7957 space = isl_space_range(space);
7958 mupa = isl_multi_union_pw_aff_alloc(space);
7960 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7961 for (i = 0; i < n; ++i) {
7962 isl_pw_aff *pa;
7963 isl_union_pw_aff *upa;
7965 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7966 upa = isl_union_pw_aff_from_pw_aff(pa);
7967 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7970 isl_multi_pw_aff_free(mpa);
7972 return mupa;
7975 /* Extract the range space of "pma" and assign it to *space.
7976 * If *space has already been set (through a previous call to this function),
7977 * then check that the range space is the same.
7979 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7981 isl_space **space = user;
7982 isl_space *pma_space;
7983 isl_bool equal;
7985 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7986 isl_pw_multi_aff_free(pma);
7988 if (!pma_space)
7989 return isl_stat_error;
7990 if (!*space) {
7991 *space = pma_space;
7992 return isl_stat_ok;
7995 equal = isl_space_is_equal(pma_space, *space);
7996 isl_space_free(pma_space);
7998 if (equal < 0)
7999 return isl_stat_error;
8000 if (!equal)
8001 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8002 "range spaces not the same", return isl_stat_error);
8003 return isl_stat_ok;
8006 /* Construct and return a multi union piecewise affine expression
8007 * that is equal to the given union piecewise multi affine expression.
8009 * In order to be able to perform the conversion, the input
8010 * needs to be non-empty and may only involve a single range space.
8012 __isl_give isl_multi_union_pw_aff *
8013 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8014 __isl_take isl_union_pw_multi_aff *upma)
8016 isl_space *space = NULL;
8017 isl_multi_union_pw_aff *mupa;
8018 int i, n;
8020 if (!upma)
8021 return NULL;
8022 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8023 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8024 "cannot extract range space from empty input",
8025 goto error);
8026 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8027 &space) < 0)
8028 goto error;
8030 if (!space)
8031 goto error;
8033 n = isl_space_dim(space, isl_dim_set);
8034 mupa = isl_multi_union_pw_aff_alloc(space);
8036 for (i = 0; i < n; ++i) {
8037 isl_union_pw_aff *upa;
8039 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8040 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8043 isl_union_pw_multi_aff_free(upma);
8044 return mupa;
8045 error:
8046 isl_space_free(space);
8047 isl_union_pw_multi_aff_free(upma);
8048 return NULL;
8051 /* Try and create an isl_multi_union_pw_aff that is equivalent
8052 * to the given isl_union_map.
8053 * The isl_union_map is required to be single-valued in each space.
8054 * Moreover, it cannot be empty and all range spaces need to be the same.
8055 * Otherwise, an error is produced.
8057 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8058 __isl_take isl_union_map *umap)
8060 isl_union_pw_multi_aff *upma;
8062 upma = isl_union_pw_multi_aff_from_union_map(umap);
8063 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8066 /* Return a multiple union piecewise affine expression
8067 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8068 * have been aligned.
8070 static __isl_give isl_multi_union_pw_aff *
8071 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8072 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8074 int i, n;
8075 isl_space *space;
8076 isl_multi_union_pw_aff *mupa;
8078 if (!domain || !mv)
8079 goto error;
8081 n = isl_multi_val_dim(mv, isl_dim_set);
8082 space = isl_multi_val_get_space(mv);
8083 mupa = isl_multi_union_pw_aff_alloc(space);
8084 for (i = 0; i < n; ++i) {
8085 isl_val *v;
8086 isl_union_pw_aff *upa;
8088 v = isl_multi_val_get_val(mv, i);
8089 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8091 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8094 isl_union_set_free(domain);
8095 isl_multi_val_free(mv);
8096 return mupa;
8097 error:
8098 isl_union_set_free(domain);
8099 isl_multi_val_free(mv);
8100 return NULL;
8103 /* Return a multiple union piecewise affine expression
8104 * that is equal to "mv" on "domain".
8106 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8107 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8109 if (!domain || !mv)
8110 goto error;
8111 if (isl_space_match(domain->dim, isl_dim_param,
8112 mv->space, isl_dim_param))
8113 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8114 domain, mv);
8115 domain = isl_union_set_align_params(domain,
8116 isl_multi_val_get_space(mv));
8117 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8118 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8119 error:
8120 isl_union_set_free(domain);
8121 isl_multi_val_free(mv);
8122 return NULL;
8125 /* Return a multiple union piecewise affine expression
8126 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8127 * have been aligned.
8129 static __isl_give isl_multi_union_pw_aff *
8130 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8131 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8133 int i, n;
8134 isl_space *space;
8135 isl_multi_union_pw_aff *mupa;
8137 if (!domain || !ma)
8138 goto error;
8140 n = isl_multi_aff_dim(ma, isl_dim_set);
8141 space = isl_multi_aff_get_space(ma);
8142 mupa = isl_multi_union_pw_aff_alloc(space);
8143 for (i = 0; i < n; ++i) {
8144 isl_aff *aff;
8145 isl_union_pw_aff *upa;
8147 aff = isl_multi_aff_get_aff(ma, i);
8148 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8149 aff);
8150 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8153 isl_union_set_free(domain);
8154 isl_multi_aff_free(ma);
8155 return mupa;
8156 error:
8157 isl_union_set_free(domain);
8158 isl_multi_aff_free(ma);
8159 return NULL;
8162 /* Return a multiple union piecewise affine expression
8163 * that is equal to "ma" on "domain".
8165 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8166 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8168 if (!domain || !ma)
8169 goto error;
8170 if (isl_space_match(domain->dim, isl_dim_param,
8171 ma->space, isl_dim_param))
8172 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8173 domain, ma);
8174 domain = isl_union_set_align_params(domain,
8175 isl_multi_aff_get_space(ma));
8176 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8177 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8178 error:
8179 isl_union_set_free(domain);
8180 isl_multi_aff_free(ma);
8181 return NULL;
8184 /* Return a union set containing those elements in the domains
8185 * of the elements of "mupa" where they are all zero.
8187 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8188 __isl_take isl_multi_union_pw_aff *mupa)
8190 int i, n;
8191 isl_union_pw_aff *upa;
8192 isl_union_set *zero;
8194 if (!mupa)
8195 return NULL;
8197 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8198 if (n == 0)
8199 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8200 "cannot determine zero set "
8201 "of zero-dimensional function", goto error);
8203 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8204 zero = isl_union_pw_aff_zero_union_set(upa);
8206 for (i = 1; i < n; ++i) {
8207 isl_union_set *zero_i;
8209 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8210 zero_i = isl_union_pw_aff_zero_union_set(upa);
8212 zero = isl_union_set_intersect(zero, zero_i);
8215 isl_multi_union_pw_aff_free(mupa);
8216 return zero;
8217 error:
8218 isl_multi_union_pw_aff_free(mupa);
8219 return NULL;
8222 /* Construct a union map mapping the shared domain
8223 * of the union piecewise affine expressions to the range of "mupa"
8224 * with each dimension in the range equated to the
8225 * corresponding union piecewise affine expression.
8227 * The input cannot be zero-dimensional as there is
8228 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8230 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8231 __isl_take isl_multi_union_pw_aff *mupa)
8233 int i, n;
8234 isl_space *space;
8235 isl_union_map *umap;
8236 isl_union_pw_aff *upa;
8238 if (!mupa)
8239 return NULL;
8241 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8242 if (n == 0)
8243 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8244 "cannot determine domain of zero-dimensional "
8245 "isl_multi_union_pw_aff", goto error);
8247 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8248 umap = isl_union_map_from_union_pw_aff(upa);
8250 for (i = 1; i < n; ++i) {
8251 isl_union_map *umap_i;
8253 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8254 umap_i = isl_union_map_from_union_pw_aff(upa);
8255 umap = isl_union_map_flat_range_product(umap, umap_i);
8258 space = isl_multi_union_pw_aff_get_space(mupa);
8259 umap = isl_union_map_reset_range_space(umap, space);
8261 isl_multi_union_pw_aff_free(mupa);
8262 return umap;
8263 error:
8264 isl_multi_union_pw_aff_free(mupa);
8265 return NULL;
8268 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8269 * "range" is the space from which to set the range space.
8270 * "res" collects the results.
8272 struct isl_union_pw_multi_aff_reset_range_space_data {
8273 isl_space *range;
8274 isl_union_pw_multi_aff *res;
8277 /* Replace the range space of "pma" by the range space of data->range and
8278 * add the result to data->res.
8280 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8282 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8283 isl_space *space;
8285 space = isl_pw_multi_aff_get_space(pma);
8286 space = isl_space_domain(space);
8287 space = isl_space_extend_domain_with_range(space,
8288 isl_space_copy(data->range));
8289 pma = isl_pw_multi_aff_reset_space(pma, space);
8290 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8292 return data->res ? isl_stat_ok : isl_stat_error;
8295 /* Replace the range space of all the piecewise affine expressions in "upma" by
8296 * the range space of "space".
8298 * This assumes that all these expressions have the same output dimension.
8300 * Since the spaces of the expressions change, so do their hash values.
8301 * We therefore need to create a new isl_union_pw_multi_aff.
8302 * Note that the hash value is currently computed based on the entire
8303 * space even though there can only be a single expression with a given
8304 * domain space.
8306 static __isl_give isl_union_pw_multi_aff *
8307 isl_union_pw_multi_aff_reset_range_space(
8308 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8310 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8311 isl_space *space_upma;
8313 space_upma = isl_union_pw_multi_aff_get_space(upma);
8314 data.res = isl_union_pw_multi_aff_empty(space_upma);
8315 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8316 &reset_range_space, &data) < 0)
8317 data.res = isl_union_pw_multi_aff_free(data.res);
8319 isl_space_free(space);
8320 isl_union_pw_multi_aff_free(upma);
8321 return data.res;
8324 /* Construct and return a union piecewise multi affine expression
8325 * that is equal to the given multi union piecewise affine expression.
8327 * In order to be able to perform the conversion, the input
8328 * needs to have a least one output dimension.
8330 __isl_give isl_union_pw_multi_aff *
8331 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8332 __isl_take isl_multi_union_pw_aff *mupa)
8334 int i, n;
8335 isl_space *space;
8336 isl_union_pw_multi_aff *upma;
8337 isl_union_pw_aff *upa;
8339 if (!mupa)
8340 return NULL;
8342 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8343 if (n == 0)
8344 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8345 "cannot determine domain of zero-dimensional "
8346 "isl_multi_union_pw_aff", goto error);
8348 space = isl_multi_union_pw_aff_get_space(mupa);
8349 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8350 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8352 for (i = 1; i < n; ++i) {
8353 isl_union_pw_multi_aff *upma_i;
8355 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8356 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8357 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8360 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8362 isl_multi_union_pw_aff_free(mupa);
8363 return upma;
8364 error:
8365 isl_multi_union_pw_aff_free(mupa);
8366 return NULL;
8369 /* Intersect the range of "mupa" with "range".
8370 * That is, keep only those domain elements that have a function value
8371 * in "range".
8373 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8374 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8376 isl_union_pw_multi_aff *upma;
8377 isl_union_set *domain;
8378 isl_space *space;
8379 int n;
8380 int match;
8382 if (!mupa || !range)
8383 goto error;
8385 space = isl_set_get_space(range);
8386 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8387 space, isl_dim_set);
8388 isl_space_free(space);
8389 if (match < 0)
8390 goto error;
8391 if (!match)
8392 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8393 "space don't match", goto error);
8394 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8395 if (n == 0)
8396 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8397 "cannot intersect range of zero-dimensional "
8398 "isl_multi_union_pw_aff", goto error);
8400 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8401 isl_multi_union_pw_aff_copy(mupa));
8402 domain = isl_union_set_from_set(range);
8403 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8404 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8406 return mupa;
8407 error:
8408 isl_multi_union_pw_aff_free(mupa);
8409 isl_set_free(range);
8410 return NULL;
8413 /* Return the shared domain of the elements of "mupa".
8415 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8416 __isl_take isl_multi_union_pw_aff *mupa)
8418 int i, n;
8419 isl_union_pw_aff *upa;
8420 isl_union_set *dom;
8422 if (!mupa)
8423 return NULL;
8425 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8426 if (n == 0)
8427 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8428 "cannot determine domain", goto error);
8430 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8431 dom = isl_union_pw_aff_domain(upa);
8432 for (i = 1; i < n; ++i) {
8433 isl_union_set *dom_i;
8435 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8436 dom_i = isl_union_pw_aff_domain(upa);
8437 dom = isl_union_set_intersect(dom, dom_i);
8440 isl_multi_union_pw_aff_free(mupa);
8441 return dom;
8442 error:
8443 isl_multi_union_pw_aff_free(mupa);
8444 return NULL;
8447 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8448 * In particular, the spaces have been aligned.
8449 * The result is defined over the shared domain of the elements of "mupa"
8451 * We first extract the parametric constant part of "aff" and
8452 * define that over the shared domain.
8453 * Then we iterate over all input dimensions of "aff" and add the corresponding
8454 * multiples of the elements of "mupa".
8455 * Finally, we consider the integer divisions, calling the function
8456 * recursively to obtain an isl_union_pw_aff corresponding to the
8457 * integer division argument.
8459 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8460 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8462 int i, n_in, n_div;
8463 isl_union_pw_aff *upa;
8464 isl_union_set *uset;
8465 isl_val *v;
8466 isl_aff *cst;
8468 n_in = isl_aff_dim(aff, isl_dim_in);
8469 n_div = isl_aff_dim(aff, isl_dim_div);
8471 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8472 cst = isl_aff_copy(aff);
8473 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8474 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8475 cst = isl_aff_project_domain_on_params(cst);
8476 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8478 for (i = 0; i < n_in; ++i) {
8479 isl_union_pw_aff *upa_i;
8481 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8482 continue;
8483 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8484 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8485 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8486 upa = isl_union_pw_aff_add(upa, upa_i);
8489 for (i = 0; i < n_div; ++i) {
8490 isl_aff *div;
8491 isl_union_pw_aff *upa_i;
8493 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8494 continue;
8495 div = isl_aff_get_div(aff, i);
8496 upa_i = multi_union_pw_aff_apply_aff(
8497 isl_multi_union_pw_aff_copy(mupa), div);
8498 upa_i = isl_union_pw_aff_floor(upa_i);
8499 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8500 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8501 upa = isl_union_pw_aff_add(upa, upa_i);
8504 isl_multi_union_pw_aff_free(mupa);
8505 isl_aff_free(aff);
8507 return upa;
8510 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8511 * with the domain of "aff".
8512 * Furthermore, the dimension of this space needs to be greater than zero.
8513 * The result is defined over the shared domain of the elements of "mupa"
8515 * We perform these checks and then hand over control to
8516 * multi_union_pw_aff_apply_aff.
8518 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8519 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8521 isl_space *space1, *space2;
8522 int equal;
8524 mupa = isl_multi_union_pw_aff_align_params(mupa,
8525 isl_aff_get_space(aff));
8526 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8527 if (!mupa || !aff)
8528 goto error;
8530 space1 = isl_multi_union_pw_aff_get_space(mupa);
8531 space2 = isl_aff_get_domain_space(aff);
8532 equal = isl_space_is_equal(space1, space2);
8533 isl_space_free(space1);
8534 isl_space_free(space2);
8535 if (equal < 0)
8536 goto error;
8537 if (!equal)
8538 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8539 "spaces don't match", goto error);
8540 if (isl_aff_dim(aff, isl_dim_in) == 0)
8541 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8542 "cannot determine domains", goto error);
8544 return multi_union_pw_aff_apply_aff(mupa, aff);
8545 error:
8546 isl_multi_union_pw_aff_free(mupa);
8547 isl_aff_free(aff);
8548 return NULL;
8551 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8552 * with the domain of "ma".
8553 * Furthermore, the dimension of this space needs to be greater than zero,
8554 * unless the dimension of the target space of "ma" is also zero.
8555 * The result is defined over the shared domain of the elements of "mupa"
8557 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8558 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8560 isl_space *space1, *space2;
8561 isl_multi_union_pw_aff *res;
8562 int equal;
8563 int i, n_out;
8565 mupa = isl_multi_union_pw_aff_align_params(mupa,
8566 isl_multi_aff_get_space(ma));
8567 ma = isl_multi_aff_align_params(ma,
8568 isl_multi_union_pw_aff_get_space(mupa));
8569 if (!mupa || !ma)
8570 goto error;
8572 space1 = isl_multi_union_pw_aff_get_space(mupa);
8573 space2 = isl_multi_aff_get_domain_space(ma);
8574 equal = isl_space_is_equal(space1, space2);
8575 isl_space_free(space1);
8576 isl_space_free(space2);
8577 if (equal < 0)
8578 goto error;
8579 if (!equal)
8580 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8581 "spaces don't match", goto error);
8582 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8583 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8584 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8585 "cannot determine domains", goto error);
8587 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8588 res = isl_multi_union_pw_aff_alloc(space1);
8590 for (i = 0; i < n_out; ++i) {
8591 isl_aff *aff;
8592 isl_union_pw_aff *upa;
8594 aff = isl_multi_aff_get_aff(ma, i);
8595 upa = multi_union_pw_aff_apply_aff(
8596 isl_multi_union_pw_aff_copy(mupa), aff);
8597 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8600 isl_multi_aff_free(ma);
8601 isl_multi_union_pw_aff_free(mupa);
8602 return res;
8603 error:
8604 isl_multi_union_pw_aff_free(mupa);
8605 isl_multi_aff_free(ma);
8606 return NULL;
8609 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8610 * with the domain of "pa".
8611 * Furthermore, the dimension of this space needs to be greater than zero.
8612 * The result is defined over the shared domain of the elements of "mupa"
8614 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8615 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8617 int i;
8618 int equal;
8619 isl_space *space, *space2;
8620 isl_union_pw_aff *upa;
8622 mupa = isl_multi_union_pw_aff_align_params(mupa,
8623 isl_pw_aff_get_space(pa));
8624 pa = isl_pw_aff_align_params(pa,
8625 isl_multi_union_pw_aff_get_space(mupa));
8626 if (!mupa || !pa)
8627 goto error;
8629 space = isl_multi_union_pw_aff_get_space(mupa);
8630 space2 = isl_pw_aff_get_domain_space(pa);
8631 equal = isl_space_is_equal(space, space2);
8632 isl_space_free(space);
8633 isl_space_free(space2);
8634 if (equal < 0)
8635 goto error;
8636 if (!equal)
8637 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8638 "spaces don't match", goto error);
8639 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8640 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8641 "cannot determine domains", goto error);
8643 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8644 upa = isl_union_pw_aff_empty(space);
8646 for (i = 0; i < pa->n; ++i) {
8647 isl_aff *aff;
8648 isl_set *domain;
8649 isl_multi_union_pw_aff *mupa_i;
8650 isl_union_pw_aff *upa_i;
8652 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8653 domain = isl_set_copy(pa->p[i].set);
8654 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8655 aff = isl_aff_copy(pa->p[i].aff);
8656 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8657 upa = isl_union_pw_aff_union_add(upa, upa_i);
8660 isl_multi_union_pw_aff_free(mupa);
8661 isl_pw_aff_free(pa);
8662 return upa;
8663 error:
8664 isl_multi_union_pw_aff_free(mupa);
8665 isl_pw_aff_free(pa);
8666 return NULL;
8669 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8670 * with the domain of "pma".
8671 * Furthermore, the dimension of this space needs to be greater than zero,
8672 * unless the dimension of the target space of "pma" is also zero.
8673 * The result is defined over the shared domain of the elements of "mupa"
8675 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8676 __isl_take isl_multi_union_pw_aff *mupa,
8677 __isl_take isl_pw_multi_aff *pma)
8679 isl_space *space1, *space2;
8680 isl_multi_union_pw_aff *res;
8681 int equal;
8682 int i, n_out;
8684 mupa = isl_multi_union_pw_aff_align_params(mupa,
8685 isl_pw_multi_aff_get_space(pma));
8686 pma = isl_pw_multi_aff_align_params(pma,
8687 isl_multi_union_pw_aff_get_space(mupa));
8688 if (!mupa || !pma)
8689 goto error;
8691 space1 = isl_multi_union_pw_aff_get_space(mupa);
8692 space2 = isl_pw_multi_aff_get_domain_space(pma);
8693 equal = isl_space_is_equal(space1, space2);
8694 isl_space_free(space1);
8695 isl_space_free(space2);
8696 if (equal < 0)
8697 goto error;
8698 if (!equal)
8699 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8700 "spaces don't match", goto error);
8701 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8702 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8703 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8704 "cannot determine domains", goto error);
8706 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8707 res = isl_multi_union_pw_aff_alloc(space1);
8709 for (i = 0; i < n_out; ++i) {
8710 isl_pw_aff *pa;
8711 isl_union_pw_aff *upa;
8713 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8714 upa = isl_multi_union_pw_aff_apply_pw_aff(
8715 isl_multi_union_pw_aff_copy(mupa), pa);
8716 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8719 isl_pw_multi_aff_free(pma);
8720 isl_multi_union_pw_aff_free(mupa);
8721 return res;
8722 error:
8723 isl_multi_union_pw_aff_free(mupa);
8724 isl_pw_multi_aff_free(pma);
8725 return NULL;
8728 /* Compute the pullback of "mupa" by the function represented by "upma".
8729 * In other words, plug in "upma" in "mupa". The result contains
8730 * expressions defined over the domain space of "upma".
8732 * Run over all elements of "mupa" and plug in "upma" in each of them.
8734 __isl_give isl_multi_union_pw_aff *
8735 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8736 __isl_take isl_multi_union_pw_aff *mupa,
8737 __isl_take isl_union_pw_multi_aff *upma)
8739 int i, n;
8741 mupa = isl_multi_union_pw_aff_align_params(mupa,
8742 isl_union_pw_multi_aff_get_space(upma));
8743 upma = isl_union_pw_multi_aff_align_params(upma,
8744 isl_multi_union_pw_aff_get_space(mupa));
8745 if (!mupa || !upma)
8746 goto error;
8748 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8749 for (i = 0; i < n; ++i) {
8750 isl_union_pw_aff *upa;
8752 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8753 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8754 isl_union_pw_multi_aff_copy(upma));
8755 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8758 isl_union_pw_multi_aff_free(upma);
8759 return mupa;
8760 error:
8761 isl_multi_union_pw_aff_free(mupa);
8762 isl_union_pw_multi_aff_free(upma);
8763 return NULL;
8766 /* Extract the sequence of elements in "mupa" with domain space "space"
8767 * (ignoring parameters).
8769 * For the elements of "mupa" that are not defined on the specified space,
8770 * the corresponding element in the result is empty.
8772 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8773 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8775 int i, n;
8776 isl_space *space_mpa = NULL;
8777 isl_multi_pw_aff *mpa;
8779 if (!mupa || !space)
8780 goto error;
8782 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8783 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8784 space = isl_space_drop_dims(space, isl_dim_param,
8785 0, isl_space_dim(space, isl_dim_param));
8786 space = isl_space_align_params(space,
8787 isl_space_copy(space_mpa));
8788 if (!space)
8789 goto error;
8791 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8792 space_mpa);
8793 mpa = isl_multi_pw_aff_alloc(space_mpa);
8795 space = isl_space_from_domain(space);
8796 space = isl_space_add_dims(space, isl_dim_out, 1);
8797 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8798 for (i = 0; i < n; ++i) {
8799 isl_union_pw_aff *upa;
8800 isl_pw_aff *pa;
8802 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8803 pa = isl_union_pw_aff_extract_pw_aff(upa,
8804 isl_space_copy(space));
8805 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8806 isl_union_pw_aff_free(upa);
8809 isl_space_free(space);
8810 return mpa;
8811 error:
8812 isl_space_free(space_mpa);
8813 isl_space_free(space);
8814 return NULL;