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