isl_multi_templ.c: extract out isl_multi_nan_templ.c
[isl.git] / isl_aff.c
blob681794c544c5367e7087d35b1faac3f8500ea5be
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 EL_BASE
34 #define EL_BASE aff
36 #include <isl_list_templ.c>
38 #undef EL_BASE
39 #define EL_BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef EL_BASE
44 #define EL_BASE pw_multi_aff
46 #include <isl_list_templ.c>
48 #undef EL_BASE
49 #define EL_BASE union_pw_aff
51 #include <isl_list_templ.c>
53 #undef EL_BASE
54 #define EL_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 isl_size 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 if (total < 0)
101 goto error;
102 v = isl_vec_alloc(ctx, 1 + 1 + total);
103 return isl_aff_alloc_vec(ls, v);
104 error:
105 isl_local_space_free(ls);
106 return NULL;
109 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
111 isl_aff *aff;
113 aff = isl_aff_alloc(ls);
114 if (!aff)
115 return NULL;
117 isl_int_set_si(aff->v->el[0], 1);
118 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
120 return aff;
123 /* Return a piecewise affine expression defined on the specified domain
124 * that is equal to zero.
126 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
128 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
131 /* Return an affine expression defined on the specified domain
132 * that represents NaN.
134 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
136 isl_aff *aff;
138 aff = isl_aff_alloc(ls);
139 if (!aff)
140 return NULL;
142 isl_seq_clr(aff->v->el, aff->v->size);
144 return aff;
147 /* Return a piecewise affine expression defined on the specified domain
148 * that represents NaN.
150 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
152 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
155 /* Return an affine expression that is equal to "val" on
156 * domain local space "ls".
158 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
159 __isl_take isl_val *val)
161 isl_aff *aff;
163 if (!ls || !val)
164 goto error;
165 if (!isl_val_is_rat(val))
166 isl_die(isl_val_get_ctx(val), isl_error_invalid,
167 "expecting rational value", goto error);
169 aff = isl_aff_alloc(isl_local_space_copy(ls));
170 if (!aff)
171 goto error;
173 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
174 isl_int_set(aff->v->el[1], val->n);
175 isl_int_set(aff->v->el[0], val->d);
177 isl_local_space_free(ls);
178 isl_val_free(val);
179 return aff;
180 error:
181 isl_local_space_free(ls);
182 isl_val_free(val);
183 return NULL;
186 /* Return an affine expression that is equal to the specified dimension
187 * in "ls".
189 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
190 enum isl_dim_type type, unsigned pos)
192 isl_space *space;
193 isl_aff *aff;
195 if (!ls)
196 return NULL;
198 space = isl_local_space_get_space(ls);
199 if (!space)
200 goto error;
201 if (isl_space_is_map(space))
202 isl_die(isl_space_get_ctx(space), isl_error_invalid,
203 "expecting (parameter) set space", goto error);
204 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
205 goto error;
207 isl_space_free(space);
208 aff = isl_aff_alloc(ls);
209 if (!aff)
210 return NULL;
212 pos += isl_local_space_offset(aff->ls, type);
214 isl_int_set_si(aff->v->el[0], 1);
215 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
216 isl_int_set_si(aff->v->el[1 + pos], 1);
218 return aff;
219 error:
220 isl_local_space_free(ls);
221 isl_space_free(space);
222 return NULL;
225 /* Return a piecewise affine expression that is equal to
226 * the specified dimension in "ls".
228 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
229 enum isl_dim_type type, unsigned pos)
231 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
234 /* Return an affine expression that is equal to the parameter
235 * in the domain space "space" with identifier "id".
237 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
238 __isl_take isl_space *space, __isl_take isl_id *id)
240 int pos;
241 isl_local_space *ls;
243 if (!space || !id)
244 goto error;
245 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
246 if (pos < 0)
247 isl_die(isl_space_get_ctx(space), isl_error_invalid,
248 "parameter not found in space", goto error);
249 isl_id_free(id);
250 ls = isl_local_space_from_space(space);
251 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
252 error:
253 isl_space_free(space);
254 isl_id_free(id);
255 return NULL;
258 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
260 if (!aff)
261 return NULL;
263 aff->ref++;
264 return aff;
267 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
269 if (!aff)
270 return NULL;
272 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
273 isl_vec_copy(aff->v));
276 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
278 if (!aff)
279 return NULL;
281 if (aff->ref == 1)
282 return aff;
283 aff->ref--;
284 return isl_aff_dup(aff);
287 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
289 if (!aff)
290 return NULL;
292 if (--aff->ref > 0)
293 return NULL;
295 isl_local_space_free(aff->ls);
296 isl_vec_free(aff->v);
298 free(aff);
300 return NULL;
303 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
305 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
308 /* Return a hash value that digests "aff".
310 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
312 uint32_t hash, ls_hash, v_hash;
314 if (!aff)
315 return 0;
317 hash = isl_hash_init();
318 ls_hash = isl_local_space_get_hash(aff->ls);
319 isl_hash_hash(hash, ls_hash);
320 v_hash = isl_vec_get_hash(aff->v);
321 isl_hash_hash(hash, v_hash);
323 return hash;
326 /* Externally, an isl_aff has a map space, but internally, the
327 * ls field corresponds to the domain of that space.
329 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
331 if (!aff)
332 return isl_size_error;
333 if (type == isl_dim_out)
334 return 1;
335 if (type == isl_dim_in)
336 type = isl_dim_set;
337 return isl_local_space_dim(aff->ls, type);
340 /* Return the position of the dimension of the given type and name
341 * in "aff".
342 * Return -1 if no such dimension can be found.
344 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
345 const char *name)
347 if (!aff)
348 return -1;
349 if (type == isl_dim_out)
350 return -1;
351 if (type == isl_dim_in)
352 type = isl_dim_set;
353 return isl_local_space_find_dim_by_name(aff->ls, type, name);
356 /* Return the domain space of "aff".
358 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
360 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
363 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
365 return isl_space_copy(isl_aff_peek_domain_space(aff));
368 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
370 isl_space *space;
371 if (!aff)
372 return NULL;
373 space = isl_local_space_get_space(aff->ls);
374 space = isl_space_from_domain(space);
375 space = isl_space_add_dims(space, isl_dim_out, 1);
376 return space;
379 __isl_give isl_local_space *isl_aff_get_domain_local_space(
380 __isl_keep isl_aff *aff)
382 return aff ? isl_local_space_copy(aff->ls) : NULL;
385 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
387 isl_local_space *ls;
388 if (!aff)
389 return NULL;
390 ls = isl_local_space_copy(aff->ls);
391 ls = isl_local_space_from_domain(ls);
392 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
393 return ls;
396 /* Return the local space of the domain of "aff".
397 * This may be either a copy or the local space itself
398 * if there is only one reference to "aff".
399 * This allows the local space to be modified inplace
400 * if both the expression and its local space have only a single reference.
401 * The caller is not allowed to modify "aff" between this call and
402 * a subsequent call to isl_aff_restore_domain_local_space.
403 * The only exception is that isl_aff_free can be called instead.
405 __isl_give isl_local_space *isl_aff_take_domain_local_space(
406 __isl_keep isl_aff *aff)
408 isl_local_space *ls;
410 if (!aff)
411 return NULL;
412 if (aff->ref != 1)
413 return isl_aff_get_domain_local_space(aff);
414 ls = aff->ls;
415 aff->ls = NULL;
416 return ls;
419 /* Set the local space of the domain of "aff" to "ls",
420 * where the local space of "aff" may be missing
421 * due to a preceding call to isl_aff_take_domain_local_space.
422 * However, in this case, "aff" only has a single reference and
423 * then the call to isl_aff_cow has no effect.
425 __isl_give isl_aff *isl_aff_restore_domain_local_space(
426 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
428 if (!aff || !ls)
429 goto error;
431 if (aff->ls == ls) {
432 isl_local_space_free(ls);
433 return aff;
436 aff = isl_aff_cow(aff);
437 if (!aff)
438 goto error;
439 isl_local_space_free(aff->ls);
440 aff->ls = ls;
442 return aff;
443 error:
444 isl_aff_free(aff);
445 isl_local_space_free(ls);
446 return NULL;
449 /* Externally, an isl_aff has a map space, but internally, the
450 * ls field corresponds to the domain of that space.
452 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
453 enum isl_dim_type type, unsigned pos)
455 if (!aff)
456 return NULL;
457 if (type == isl_dim_out)
458 return NULL;
459 if (type == isl_dim_in)
460 type = isl_dim_set;
461 return isl_local_space_get_dim_name(aff->ls, type, pos);
464 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
465 __isl_take isl_space *dim)
467 aff = isl_aff_cow(aff);
468 if (!aff || !dim)
469 goto error;
471 aff->ls = isl_local_space_reset_space(aff->ls, dim);
472 if (!aff->ls)
473 return isl_aff_free(aff);
475 return aff;
476 error:
477 isl_aff_free(aff);
478 isl_space_free(dim);
479 return NULL;
482 /* Reset the space of "aff". This function is called from isl_pw_templ.c
483 * and doesn't know if the space of an element object is represented
484 * directly or through its domain. It therefore passes along both.
486 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
487 __isl_take isl_space *space, __isl_take isl_space *domain)
489 isl_space_free(space);
490 return isl_aff_reset_domain_space(aff, domain);
493 /* Reorder the coefficients of the affine expression based
494 * on the given reordering.
495 * The reordering r is assumed to have been extended with the local
496 * variables.
498 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
499 __isl_take isl_reordering *r, int n_div)
501 isl_space *space;
502 isl_vec *res;
503 isl_size dim;
504 int i;
506 if (!vec || !r)
507 goto error;
509 space = isl_reordering_peek_space(r);
510 dim = isl_space_dim(space, isl_dim_all);
511 if (dim < 0)
512 goto error;
513 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
514 if (!res)
515 goto error;
516 isl_seq_cpy(res->el, vec->el, 2);
517 isl_seq_clr(res->el + 2, res->size - 2);
518 for (i = 0; i < r->len; ++i)
519 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
521 isl_reordering_free(r);
522 isl_vec_free(vec);
523 return res;
524 error:
525 isl_vec_free(vec);
526 isl_reordering_free(r);
527 return NULL;
530 /* Reorder the dimensions of the domain of "aff" according
531 * to the given reordering.
533 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
534 __isl_take isl_reordering *r)
536 aff = isl_aff_cow(aff);
537 if (!aff)
538 goto error;
540 r = isl_reordering_extend(r, aff->ls->div->n_row);
541 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
542 aff->ls->div->n_row);
543 aff->ls = isl_local_space_realign(aff->ls, r);
545 if (!aff->v || !aff->ls)
546 return isl_aff_free(aff);
548 return aff;
549 error:
550 isl_aff_free(aff);
551 isl_reordering_free(r);
552 return NULL;
555 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
556 __isl_take isl_space *model)
558 isl_bool equal_params;
560 if (!aff || !model)
561 goto error;
563 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
564 if (equal_params < 0)
565 goto error;
566 if (!equal_params) {
567 isl_reordering *exp;
569 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
570 exp = isl_reordering_extend_space(exp,
571 isl_aff_get_domain_space(aff));
572 aff = isl_aff_realign_domain(aff, exp);
575 isl_space_free(model);
576 return aff;
577 error:
578 isl_space_free(model);
579 isl_aff_free(aff);
580 return NULL;
583 /* Is "aff" obviously equal to zero?
585 * If the denominator is zero, then "aff" is not equal to zero.
587 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
589 int pos;
591 if (!aff)
592 return isl_bool_error;
594 if (isl_int_is_zero(aff->v->el[0]))
595 return isl_bool_false;
596 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
597 return isl_bool_ok(pos < 0);
600 /* Does "aff" represent NaN?
602 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
604 if (!aff)
605 return isl_bool_error;
607 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
610 /* Are "aff1" and "aff2" obviously equal?
612 * NaN is not equal to anything, not even to another NaN.
614 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
615 __isl_keep isl_aff *aff2)
617 isl_bool equal;
619 if (!aff1 || !aff2)
620 return isl_bool_error;
622 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
623 return isl_bool_false;
625 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
626 if (equal < 0 || !equal)
627 return equal;
629 return isl_vec_is_equal(aff1->v, aff2->v);
632 /* Return the common denominator of "aff" in "v".
634 * We cannot return anything meaningful in case of a NaN.
636 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
638 if (!aff)
639 return isl_stat_error;
640 if (isl_aff_is_nan(aff))
641 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
642 "cannot get denominator of NaN", return isl_stat_error);
643 isl_int_set(*v, aff->v->el[0]);
644 return isl_stat_ok;
647 /* Return the common denominator of "aff".
649 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
651 isl_ctx *ctx;
653 if (!aff)
654 return NULL;
656 ctx = isl_aff_get_ctx(aff);
657 if (isl_aff_is_nan(aff))
658 return isl_val_nan(ctx);
659 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
662 /* Return the constant term of "aff".
664 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
666 isl_ctx *ctx;
667 isl_val *v;
669 if (!aff)
670 return NULL;
672 ctx = isl_aff_get_ctx(aff);
673 if (isl_aff_is_nan(aff))
674 return isl_val_nan(ctx);
675 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
676 return isl_val_normalize(v);
679 /* Return the coefficient of the variable of type "type" at position "pos"
680 * of "aff".
682 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
683 enum isl_dim_type type, int pos)
685 isl_ctx *ctx;
686 isl_val *v;
688 if (!aff)
689 return NULL;
691 ctx = isl_aff_get_ctx(aff);
692 if (type == isl_dim_out)
693 isl_die(ctx, isl_error_invalid,
694 "output/set dimension does not have a coefficient",
695 return NULL);
696 if (type == isl_dim_in)
697 type = isl_dim_set;
699 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
700 return NULL;
702 if (isl_aff_is_nan(aff))
703 return isl_val_nan(ctx);
704 pos += isl_local_space_offset(aff->ls, type);
705 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
706 return isl_val_normalize(v);
709 /* Return the sign of the coefficient of the variable of type "type"
710 * at position "pos" of "aff".
712 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
713 int pos)
715 isl_ctx *ctx;
717 if (!aff)
718 return 0;
720 ctx = isl_aff_get_ctx(aff);
721 if (type == isl_dim_out)
722 isl_die(ctx, isl_error_invalid,
723 "output/set dimension does not have a coefficient",
724 return 0);
725 if (type == isl_dim_in)
726 type = isl_dim_set;
728 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
729 return 0;
731 pos += isl_local_space_offset(aff->ls, type);
732 return isl_int_sgn(aff->v->el[1 + pos]);
735 /* Replace the numerator of the constant term of "aff" by "v".
737 * A NaN is unaffected by this operation.
739 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
741 if (!aff)
742 return NULL;
743 if (isl_aff_is_nan(aff))
744 return aff;
745 aff = isl_aff_cow(aff);
746 if (!aff)
747 return NULL;
749 aff->v = isl_vec_cow(aff->v);
750 if (!aff->v)
751 return isl_aff_free(aff);
753 isl_int_set(aff->v->el[1], v);
755 return aff;
758 /* Replace the constant term of "aff" by "v".
760 * A NaN is unaffected by this operation.
762 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
763 __isl_take isl_val *v)
765 if (!aff || !v)
766 goto error;
768 if (isl_aff_is_nan(aff)) {
769 isl_val_free(v);
770 return aff;
773 if (!isl_val_is_rat(v))
774 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
775 "expecting rational value", goto error);
777 if (isl_int_eq(aff->v->el[1], v->n) &&
778 isl_int_eq(aff->v->el[0], v->d)) {
779 isl_val_free(v);
780 return aff;
783 aff = isl_aff_cow(aff);
784 if (!aff)
785 goto error;
786 aff->v = isl_vec_cow(aff->v);
787 if (!aff->v)
788 goto error;
790 if (isl_int_eq(aff->v->el[0], v->d)) {
791 isl_int_set(aff->v->el[1], v->n);
792 } else if (isl_int_is_one(v->d)) {
793 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
794 } else {
795 isl_seq_scale(aff->v->el + 1,
796 aff->v->el + 1, v->d, aff->v->size - 1);
797 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
798 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
799 aff->v = isl_vec_normalize(aff->v);
800 if (!aff->v)
801 goto error;
804 isl_val_free(v);
805 return aff;
806 error:
807 isl_aff_free(aff);
808 isl_val_free(v);
809 return NULL;
812 /* Add "v" to the constant term of "aff".
814 * A NaN is unaffected by this operation.
816 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
818 if (isl_int_is_zero(v))
819 return aff;
821 if (!aff)
822 return NULL;
823 if (isl_aff_is_nan(aff))
824 return aff;
825 aff = isl_aff_cow(aff);
826 if (!aff)
827 return NULL;
829 aff->v = isl_vec_cow(aff->v);
830 if (!aff->v)
831 return isl_aff_free(aff);
833 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
835 return aff;
838 /* Add "v" to the constant term of "aff".
840 * A NaN is unaffected by this operation.
842 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
843 __isl_take isl_val *v)
845 if (!aff || !v)
846 goto error;
848 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
849 isl_val_free(v);
850 return aff;
853 if (!isl_val_is_rat(v))
854 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
855 "expecting rational value", goto error);
857 aff = isl_aff_cow(aff);
858 if (!aff)
859 goto error;
861 aff->v = isl_vec_cow(aff->v);
862 if (!aff->v)
863 goto error;
865 if (isl_int_is_one(v->d)) {
866 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
867 } else if (isl_int_eq(aff->v->el[0], v->d)) {
868 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
869 aff->v = isl_vec_normalize(aff->v);
870 if (!aff->v)
871 goto error;
872 } else {
873 isl_seq_scale(aff->v->el + 1,
874 aff->v->el + 1, v->d, aff->v->size - 1);
875 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
876 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
877 aff->v = isl_vec_normalize(aff->v);
878 if (!aff->v)
879 goto error;
882 isl_val_free(v);
883 return aff;
884 error:
885 isl_aff_free(aff);
886 isl_val_free(v);
887 return NULL;
890 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
892 isl_int t;
894 isl_int_init(t);
895 isl_int_set_si(t, v);
896 aff = isl_aff_add_constant(aff, t);
897 isl_int_clear(t);
899 return aff;
902 /* Add "v" to the numerator of the constant term of "aff".
904 * A NaN is unaffected by this operation.
906 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
908 if (isl_int_is_zero(v))
909 return aff;
911 if (!aff)
912 return NULL;
913 if (isl_aff_is_nan(aff))
914 return aff;
915 aff = isl_aff_cow(aff);
916 if (!aff)
917 return NULL;
919 aff->v = isl_vec_cow(aff->v);
920 if (!aff->v)
921 return isl_aff_free(aff);
923 isl_int_add(aff->v->el[1], aff->v->el[1], v);
925 return aff;
928 /* Add "v" to the numerator of the constant term of "aff".
930 * A NaN is unaffected by this operation.
932 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
934 isl_int t;
936 if (v == 0)
937 return aff;
939 isl_int_init(t);
940 isl_int_set_si(t, v);
941 aff = isl_aff_add_constant_num(aff, t);
942 isl_int_clear(t);
944 return aff;
947 /* Replace the numerator of the constant term of "aff" by "v".
949 * A NaN is unaffected by this operation.
951 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
953 if (!aff)
954 return NULL;
955 if (isl_aff_is_nan(aff))
956 return aff;
957 aff = isl_aff_cow(aff);
958 if (!aff)
959 return NULL;
961 aff->v = isl_vec_cow(aff->v);
962 if (!aff->v)
963 return isl_aff_free(aff);
965 isl_int_set_si(aff->v->el[1], v);
967 return aff;
970 /* Replace the numerator of the coefficient of the variable of type "type"
971 * at position "pos" of "aff" by "v".
973 * A NaN is unaffected by this operation.
975 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
976 enum isl_dim_type type, int pos, isl_int v)
978 if (!aff)
979 return NULL;
981 if (type == isl_dim_out)
982 isl_die(aff->v->ctx, isl_error_invalid,
983 "output/set dimension does not have a coefficient",
984 return isl_aff_free(aff));
985 if (type == isl_dim_in)
986 type = isl_dim_set;
988 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
989 return isl_aff_free(aff);
991 if (isl_aff_is_nan(aff))
992 return aff;
993 aff = isl_aff_cow(aff);
994 if (!aff)
995 return NULL;
997 aff->v = isl_vec_cow(aff->v);
998 if (!aff->v)
999 return isl_aff_free(aff);
1001 pos += isl_local_space_offset(aff->ls, type);
1002 isl_int_set(aff->v->el[1 + pos], v);
1004 return aff;
1007 /* Replace the numerator of the coefficient of the variable of type "type"
1008 * at position "pos" of "aff" by "v".
1010 * A NaN is unaffected by this operation.
1012 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1013 enum isl_dim_type type, int pos, int v)
1015 if (!aff)
1016 return NULL;
1018 if (type == isl_dim_out)
1019 isl_die(aff->v->ctx, isl_error_invalid,
1020 "output/set dimension does not have a coefficient",
1021 return isl_aff_free(aff));
1022 if (type == isl_dim_in)
1023 type = isl_dim_set;
1025 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1026 return isl_aff_free(aff);
1028 if (isl_aff_is_nan(aff))
1029 return aff;
1030 pos += isl_local_space_offset(aff->ls, type);
1031 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1032 return aff;
1034 aff = isl_aff_cow(aff);
1035 if (!aff)
1036 return NULL;
1038 aff->v = isl_vec_cow(aff->v);
1039 if (!aff->v)
1040 return isl_aff_free(aff);
1042 isl_int_set_si(aff->v->el[1 + pos], v);
1044 return aff;
1047 /* Replace the coefficient of the variable of type "type" at position "pos"
1048 * of "aff" by "v".
1050 * A NaN is unaffected by this operation.
1052 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1053 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1055 if (!aff || !v)
1056 goto error;
1058 if (type == isl_dim_out)
1059 isl_die(aff->v->ctx, isl_error_invalid,
1060 "output/set dimension does not have a coefficient",
1061 goto error);
1062 if (type == isl_dim_in)
1063 type = isl_dim_set;
1065 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1066 return isl_aff_free(aff);
1068 if (isl_aff_is_nan(aff)) {
1069 isl_val_free(v);
1070 return aff;
1072 if (!isl_val_is_rat(v))
1073 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1074 "expecting rational value", goto error);
1076 pos += isl_local_space_offset(aff->ls, type);
1077 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1078 isl_int_eq(aff->v->el[0], v->d)) {
1079 isl_val_free(v);
1080 return aff;
1083 aff = isl_aff_cow(aff);
1084 if (!aff)
1085 goto error;
1086 aff->v = isl_vec_cow(aff->v);
1087 if (!aff->v)
1088 goto error;
1090 if (isl_int_eq(aff->v->el[0], v->d)) {
1091 isl_int_set(aff->v->el[1 + pos], v->n);
1092 } else if (isl_int_is_one(v->d)) {
1093 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1094 } else {
1095 isl_seq_scale(aff->v->el + 1,
1096 aff->v->el + 1, v->d, aff->v->size - 1);
1097 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1098 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1099 aff->v = isl_vec_normalize(aff->v);
1100 if (!aff->v)
1101 goto error;
1104 isl_val_free(v);
1105 return aff;
1106 error:
1107 isl_aff_free(aff);
1108 isl_val_free(v);
1109 return NULL;
1112 /* Add "v" to the coefficient of the variable of type "type"
1113 * at position "pos" of "aff".
1115 * A NaN is unaffected by this operation.
1117 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1118 enum isl_dim_type type, int pos, isl_int v)
1120 if (!aff)
1121 return NULL;
1123 if (type == isl_dim_out)
1124 isl_die(aff->v->ctx, isl_error_invalid,
1125 "output/set dimension does not have a coefficient",
1126 return isl_aff_free(aff));
1127 if (type == isl_dim_in)
1128 type = isl_dim_set;
1130 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1131 return isl_aff_free(aff);
1133 if (isl_aff_is_nan(aff))
1134 return aff;
1135 aff = isl_aff_cow(aff);
1136 if (!aff)
1137 return NULL;
1139 aff->v = isl_vec_cow(aff->v);
1140 if (!aff->v)
1141 return isl_aff_free(aff);
1143 pos += isl_local_space_offset(aff->ls, type);
1144 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1146 return aff;
1149 /* Add "v" to the coefficient of the variable of type "type"
1150 * at position "pos" of "aff".
1152 * A NaN is unaffected by this operation.
1154 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1155 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1157 if (!aff || !v)
1158 goto error;
1160 if (isl_val_is_zero(v)) {
1161 isl_val_free(v);
1162 return aff;
1165 if (type == isl_dim_out)
1166 isl_die(aff->v->ctx, isl_error_invalid,
1167 "output/set dimension does not have a coefficient",
1168 goto error);
1169 if (type == isl_dim_in)
1170 type = isl_dim_set;
1172 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1173 goto error;
1175 if (isl_aff_is_nan(aff)) {
1176 isl_val_free(v);
1177 return aff;
1179 if (!isl_val_is_rat(v))
1180 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1181 "expecting rational value", goto error);
1183 aff = isl_aff_cow(aff);
1184 if (!aff)
1185 goto error;
1187 aff->v = isl_vec_cow(aff->v);
1188 if (!aff->v)
1189 goto error;
1191 pos += isl_local_space_offset(aff->ls, type);
1192 if (isl_int_is_one(v->d)) {
1193 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1194 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1195 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1196 aff->v = isl_vec_normalize(aff->v);
1197 if (!aff->v)
1198 goto error;
1199 } else {
1200 isl_seq_scale(aff->v->el + 1,
1201 aff->v->el + 1, v->d, aff->v->size - 1);
1202 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1203 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1204 aff->v = isl_vec_normalize(aff->v);
1205 if (!aff->v)
1206 goto error;
1209 isl_val_free(v);
1210 return aff;
1211 error:
1212 isl_aff_free(aff);
1213 isl_val_free(v);
1214 return NULL;
1217 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1218 enum isl_dim_type type, int pos, int v)
1220 isl_int t;
1222 isl_int_init(t);
1223 isl_int_set_si(t, v);
1224 aff = isl_aff_add_coefficient(aff, type, pos, t);
1225 isl_int_clear(t);
1227 return aff;
1230 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1232 if (!aff)
1233 return NULL;
1235 return isl_local_space_get_div(aff->ls, pos);
1238 /* Return the negation of "aff".
1240 * As a special case, -NaN = NaN.
1242 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1244 if (!aff)
1245 return NULL;
1246 if (isl_aff_is_nan(aff))
1247 return aff;
1248 aff = isl_aff_cow(aff);
1249 if (!aff)
1250 return NULL;
1251 aff->v = isl_vec_cow(aff->v);
1252 if (!aff->v)
1253 return isl_aff_free(aff);
1255 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1257 return aff;
1260 /* Remove divs from the local space that do not appear in the affine
1261 * expression.
1262 * We currently only remove divs at the end.
1263 * Some intermediate divs may also not appear directly in the affine
1264 * expression, but we would also need to check that no other divs are
1265 * defined in terms of them.
1267 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1269 int pos;
1270 int off;
1271 isl_size n;
1273 if (!aff)
1274 return NULL;
1276 n = isl_local_space_dim(aff->ls, isl_dim_div);
1277 if (n < 0)
1278 return isl_aff_free(aff);
1279 off = isl_local_space_offset(aff->ls, isl_dim_div);
1281 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1282 if (pos == n)
1283 return aff;
1285 aff = isl_aff_cow(aff);
1286 if (!aff)
1287 return NULL;
1289 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1290 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1291 if (!aff->ls || !aff->v)
1292 return isl_aff_free(aff);
1294 return aff;
1297 /* Look for any divs in the aff->ls with a denominator equal to one
1298 * and plug them into the affine expression and any subsequent divs
1299 * that may reference the div.
1301 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1303 int i;
1304 isl_size n;
1305 int len;
1306 isl_int v;
1307 isl_vec *vec;
1308 isl_local_space *ls;
1309 unsigned pos;
1311 if (!aff)
1312 return NULL;
1314 n = isl_local_space_dim(aff->ls, isl_dim_div);
1315 if (n < 0)
1316 return isl_aff_free(aff);
1317 len = aff->v->size;
1318 for (i = 0; i < n; ++i) {
1319 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1320 continue;
1321 ls = isl_local_space_copy(aff->ls);
1322 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1323 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1324 vec = isl_vec_copy(aff->v);
1325 vec = isl_vec_cow(vec);
1326 if (!ls || !vec)
1327 goto error;
1329 isl_int_init(v);
1331 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1332 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1333 len, len, v);
1335 isl_int_clear(v);
1337 isl_vec_free(aff->v);
1338 aff->v = vec;
1339 isl_local_space_free(aff->ls);
1340 aff->ls = ls;
1343 return aff;
1344 error:
1345 isl_vec_free(vec);
1346 isl_local_space_free(ls);
1347 return isl_aff_free(aff);
1350 /* Look for any divs j that appear with a unit coefficient inside
1351 * the definitions of other divs i and plug them into the definitions
1352 * of the divs i.
1354 * In particular, an expression of the form
1356 * floor((f(..) + floor(g(..)/n))/m)
1358 * is simplified to
1360 * floor((n * f(..) + g(..))/(n * m))
1362 * This simplification is correct because we can move the expression
1363 * f(..) into the inner floor in the original expression to obtain
1365 * floor(floor((n * f(..) + g(..))/n)/m)
1367 * from which we can derive the simplified expression.
1369 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1371 int i, j;
1372 isl_size n;
1373 int off;
1375 if (!aff)
1376 return NULL;
1378 n = isl_local_space_dim(aff->ls, isl_dim_div);
1379 if (n < 0)
1380 return isl_aff_free(aff);
1381 off = isl_local_space_offset(aff->ls, isl_dim_div);
1382 for (i = 1; i < n; ++i) {
1383 for (j = 0; j < i; ++j) {
1384 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1385 continue;
1386 aff->ls = isl_local_space_substitute_seq(aff->ls,
1387 isl_dim_div, j, aff->ls->div->row[j],
1388 aff->v->size, i, 1);
1389 if (!aff->ls)
1390 return isl_aff_free(aff);
1394 return aff;
1397 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1399 * Even though this function is only called on isl_affs with a single
1400 * reference, we are careful to only change aff->v and aff->ls together.
1402 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1404 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1405 isl_local_space *ls;
1406 isl_vec *v;
1408 ls = isl_local_space_copy(aff->ls);
1409 ls = isl_local_space_swap_div(ls, a, b);
1410 v = isl_vec_copy(aff->v);
1411 v = isl_vec_cow(v);
1412 if (!ls || !v)
1413 goto error;
1415 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1416 isl_vec_free(aff->v);
1417 aff->v = v;
1418 isl_local_space_free(aff->ls);
1419 aff->ls = ls;
1421 return aff;
1422 error:
1423 isl_vec_free(v);
1424 isl_local_space_free(ls);
1425 return isl_aff_free(aff);
1428 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1430 * We currently do not actually remove div "b", but simply add its
1431 * coefficient to that of "a" and then zero it out.
1433 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1435 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1437 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1438 return aff;
1440 aff->v = isl_vec_cow(aff->v);
1441 if (!aff->v)
1442 return isl_aff_free(aff);
1444 isl_int_add(aff->v->el[1 + off + a],
1445 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1446 isl_int_set_si(aff->v->el[1 + off + b], 0);
1448 return aff;
1451 /* Sort the divs in the local space of "aff" according to
1452 * the comparison function "cmp_row" in isl_local_space.c,
1453 * combining the coefficients of identical divs.
1455 * Reordering divs does not change the semantics of "aff",
1456 * so there is no need to call isl_aff_cow.
1457 * Moreover, this function is currently only called on isl_affs
1458 * with a single reference.
1460 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1462 isl_size n;
1463 int i, j;
1465 n = isl_aff_dim(aff, isl_dim_div);
1466 if (n < 0)
1467 return isl_aff_free(aff);
1468 for (i = 1; i < n; ++i) {
1469 for (j = i - 1; j >= 0; --j) {
1470 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1471 if (cmp < 0)
1472 break;
1473 if (cmp == 0)
1474 aff = merge_divs(aff, j, j + 1);
1475 else
1476 aff = swap_div(aff, j, j + 1);
1477 if (!aff)
1478 return NULL;
1482 return aff;
1485 /* Normalize the representation of "aff".
1487 * This function should only be called of "new" isl_affs, i.e.,
1488 * with only a single reference. We therefore do not need to
1489 * worry about affecting other instances.
1491 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1493 if (!aff)
1494 return NULL;
1495 aff->v = isl_vec_normalize(aff->v);
1496 if (!aff->v)
1497 return isl_aff_free(aff);
1498 aff = plug_in_integral_divs(aff);
1499 aff = plug_in_unit_divs(aff);
1500 aff = sort_divs(aff);
1501 aff = isl_aff_remove_unused_divs(aff);
1502 return aff;
1505 /* Given f, return floor(f).
1506 * If f is an integer expression, then just return f.
1507 * If f is a constant, then return the constant floor(f).
1508 * Otherwise, if f = g/m, write g = q m + r,
1509 * create a new div d = [r/m] and return the expression q + d.
1510 * The coefficients in r are taken to lie between -m/2 and m/2.
1512 * reduce_div_coefficients performs the same normalization.
1514 * As a special case, floor(NaN) = NaN.
1516 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1518 int i;
1519 int size;
1520 isl_ctx *ctx;
1521 isl_vec *div;
1523 if (!aff)
1524 return NULL;
1526 if (isl_aff_is_nan(aff))
1527 return aff;
1528 if (isl_int_is_one(aff->v->el[0]))
1529 return aff;
1531 aff = isl_aff_cow(aff);
1532 if (!aff)
1533 return NULL;
1535 aff->v = isl_vec_cow(aff->v);
1536 if (!aff->v)
1537 return isl_aff_free(aff);
1539 if (isl_aff_is_cst(aff)) {
1540 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1541 isl_int_set_si(aff->v->el[0], 1);
1542 return aff;
1545 div = isl_vec_copy(aff->v);
1546 div = isl_vec_cow(div);
1547 if (!div)
1548 return isl_aff_free(aff);
1550 ctx = isl_aff_get_ctx(aff);
1551 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1552 for (i = 1; i < aff->v->size; ++i) {
1553 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1554 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1555 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1556 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1557 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1561 aff->ls = isl_local_space_add_div(aff->ls, div);
1562 if (!aff->ls)
1563 return isl_aff_free(aff);
1565 size = aff->v->size;
1566 aff->v = isl_vec_extend(aff->v, size + 1);
1567 if (!aff->v)
1568 return isl_aff_free(aff);
1569 isl_int_set_si(aff->v->el[0], 1);
1570 isl_int_set_si(aff->v->el[size], 1);
1572 aff = isl_aff_normalize(aff);
1574 return aff;
1577 /* Compute
1579 * aff mod m = aff - m * floor(aff/m)
1581 * with m an integer value.
1583 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1584 __isl_take isl_val *m)
1586 isl_aff *res;
1588 if (!aff || !m)
1589 goto error;
1591 if (!isl_val_is_int(m))
1592 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1593 "expecting integer modulo", goto error);
1595 res = isl_aff_copy(aff);
1596 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1597 aff = isl_aff_floor(aff);
1598 aff = isl_aff_scale_val(aff, m);
1599 res = isl_aff_sub(res, aff);
1601 return res;
1602 error:
1603 isl_aff_free(aff);
1604 isl_val_free(m);
1605 return NULL;
1608 /* Compute
1610 * pwaff mod m = pwaff - m * floor(pwaff/m)
1612 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1614 isl_pw_aff *res;
1616 res = isl_pw_aff_copy(pwaff);
1617 pwaff = isl_pw_aff_scale_down(pwaff, m);
1618 pwaff = isl_pw_aff_floor(pwaff);
1619 pwaff = isl_pw_aff_scale(pwaff, m);
1620 res = isl_pw_aff_sub(res, pwaff);
1622 return res;
1625 /* Compute
1627 * pa mod m = pa - m * floor(pa/m)
1629 * with m an integer value.
1631 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1632 __isl_take isl_val *m)
1634 if (!pa || !m)
1635 goto error;
1636 if (!isl_val_is_int(m))
1637 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1638 "expecting integer modulo", goto error);
1639 pa = isl_pw_aff_mod(pa, m->n);
1640 isl_val_free(m);
1641 return pa;
1642 error:
1643 isl_pw_aff_free(pa);
1644 isl_val_free(m);
1645 return NULL;
1648 /* Given f, return ceil(f).
1649 * If f is an integer expression, then just return f.
1650 * Otherwise, let f be the expression
1652 * e/m
1654 * then return
1656 * floor((e + m - 1)/m)
1658 * As a special case, ceil(NaN) = NaN.
1660 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1662 if (!aff)
1663 return NULL;
1665 if (isl_aff_is_nan(aff))
1666 return aff;
1667 if (isl_int_is_one(aff->v->el[0]))
1668 return aff;
1670 aff = isl_aff_cow(aff);
1671 if (!aff)
1672 return NULL;
1673 aff->v = isl_vec_cow(aff->v);
1674 if (!aff->v)
1675 return isl_aff_free(aff);
1677 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1678 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1679 aff = isl_aff_floor(aff);
1681 return aff;
1684 /* Apply the expansion computed by isl_merge_divs.
1685 * The expansion itself is given by "exp" while the resulting
1686 * list of divs is given by "div".
1688 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1689 __isl_take isl_mat *div, int *exp)
1691 isl_size old_n_div;
1692 isl_size new_n_div;
1693 int offset;
1695 aff = isl_aff_cow(aff);
1696 if (!aff || !div)
1697 goto error;
1699 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1700 new_n_div = isl_mat_rows(div);
1701 if (old_n_div < 0 || new_n_div < 0)
1702 goto error;
1703 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1705 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1706 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1707 if (!aff->v || !aff->ls)
1708 return isl_aff_free(aff);
1709 return aff;
1710 error:
1711 isl_aff_free(aff);
1712 isl_mat_free(div);
1713 return NULL;
1716 /* Add two affine expressions that live in the same local space.
1718 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1719 __isl_take isl_aff *aff2)
1721 isl_int gcd, f;
1723 aff1 = isl_aff_cow(aff1);
1724 if (!aff1 || !aff2)
1725 goto error;
1727 aff1->v = isl_vec_cow(aff1->v);
1728 if (!aff1->v)
1729 goto error;
1731 isl_int_init(gcd);
1732 isl_int_init(f);
1733 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1734 isl_int_divexact(f, aff2->v->el[0], gcd);
1735 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1736 isl_int_divexact(f, aff1->v->el[0], gcd);
1737 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1738 isl_int_divexact(f, aff2->v->el[0], gcd);
1739 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1740 isl_int_clear(f);
1741 isl_int_clear(gcd);
1743 isl_aff_free(aff2);
1744 return aff1;
1745 error:
1746 isl_aff_free(aff1);
1747 isl_aff_free(aff2);
1748 return NULL;
1751 /* Return the sum of "aff1" and "aff2".
1753 * If either of the two is NaN, then the result is NaN.
1755 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1756 __isl_take isl_aff *aff2)
1758 isl_ctx *ctx;
1759 int *exp1 = NULL;
1760 int *exp2 = NULL;
1761 isl_mat *div;
1762 isl_size n_div1, n_div2;
1764 if (!aff1 || !aff2)
1765 goto error;
1767 ctx = isl_aff_get_ctx(aff1);
1768 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1769 isl_die(ctx, isl_error_invalid,
1770 "spaces don't match", goto error);
1772 if (isl_aff_is_nan(aff1)) {
1773 isl_aff_free(aff2);
1774 return aff1;
1776 if (isl_aff_is_nan(aff2)) {
1777 isl_aff_free(aff1);
1778 return aff2;
1781 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1782 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1783 if (n_div1 < 0 || n_div2 < 0)
1784 goto error;
1785 if (n_div1 == 0 && n_div2 == 0)
1786 return add_expanded(aff1, aff2);
1788 exp1 = isl_alloc_array(ctx, int, n_div1);
1789 exp2 = isl_alloc_array(ctx, int, n_div2);
1790 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1791 goto error;
1793 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1794 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1795 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1796 free(exp1);
1797 free(exp2);
1799 return add_expanded(aff1, aff2);
1800 error:
1801 free(exp1);
1802 free(exp2);
1803 isl_aff_free(aff1);
1804 isl_aff_free(aff2);
1805 return NULL;
1808 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1809 __isl_take isl_aff *aff2)
1811 return isl_aff_add(aff1, isl_aff_neg(aff2));
1814 /* Return the result of scaling "aff" by a factor of "f".
1816 * As a special case, f * NaN = NaN.
1818 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1820 isl_int gcd;
1822 if (!aff)
1823 return NULL;
1824 if (isl_aff_is_nan(aff))
1825 return aff;
1827 if (isl_int_is_one(f))
1828 return aff;
1830 aff = isl_aff_cow(aff);
1831 if (!aff)
1832 return NULL;
1833 aff->v = isl_vec_cow(aff->v);
1834 if (!aff->v)
1835 return isl_aff_free(aff);
1837 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1838 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1839 return aff;
1842 isl_int_init(gcd);
1843 isl_int_gcd(gcd, aff->v->el[0], f);
1844 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1845 isl_int_divexact(gcd, f, gcd);
1846 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1847 isl_int_clear(gcd);
1849 return aff;
1852 /* Multiple "aff" by "v".
1854 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1855 __isl_take isl_val *v)
1857 if (!aff || !v)
1858 goto error;
1860 if (isl_val_is_one(v)) {
1861 isl_val_free(v);
1862 return aff;
1865 if (!isl_val_is_rat(v))
1866 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1867 "expecting rational factor", goto error);
1869 aff = isl_aff_scale(aff, v->n);
1870 aff = isl_aff_scale_down(aff, v->d);
1872 isl_val_free(v);
1873 return aff;
1874 error:
1875 isl_aff_free(aff);
1876 isl_val_free(v);
1877 return NULL;
1880 /* Return the result of scaling "aff" down by a factor of "f".
1882 * As a special case, NaN/f = NaN.
1884 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1886 isl_int gcd;
1888 if (!aff)
1889 return NULL;
1890 if (isl_aff_is_nan(aff))
1891 return aff;
1893 if (isl_int_is_one(f))
1894 return aff;
1896 aff = isl_aff_cow(aff);
1897 if (!aff)
1898 return NULL;
1900 if (isl_int_is_zero(f))
1901 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1902 "cannot scale down by zero", return isl_aff_free(aff));
1904 aff->v = isl_vec_cow(aff->v);
1905 if (!aff->v)
1906 return isl_aff_free(aff);
1908 isl_int_init(gcd);
1909 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1910 isl_int_gcd(gcd, gcd, f);
1911 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1912 isl_int_divexact(gcd, f, gcd);
1913 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1914 isl_int_clear(gcd);
1916 return aff;
1919 /* Divide "aff" by "v".
1921 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1922 __isl_take isl_val *v)
1924 if (!aff || !v)
1925 goto error;
1927 if (isl_val_is_one(v)) {
1928 isl_val_free(v);
1929 return aff;
1932 if (!isl_val_is_rat(v))
1933 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1934 "expecting rational factor", goto error);
1935 if (!isl_val_is_pos(v))
1936 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1937 "factor needs to be positive", goto error);
1939 aff = isl_aff_scale(aff, v->d);
1940 aff = isl_aff_scale_down(aff, v->n);
1942 isl_val_free(v);
1943 return aff;
1944 error:
1945 isl_aff_free(aff);
1946 isl_val_free(v);
1947 return NULL;
1950 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1952 isl_int v;
1954 if (f == 1)
1955 return aff;
1957 isl_int_init(v);
1958 isl_int_set_ui(v, f);
1959 aff = isl_aff_scale_down(aff, v);
1960 isl_int_clear(v);
1962 return aff;
1965 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1966 enum isl_dim_type type, unsigned pos, const char *s)
1968 aff = isl_aff_cow(aff);
1969 if (!aff)
1970 return NULL;
1971 if (type == isl_dim_out)
1972 isl_die(aff->v->ctx, isl_error_invalid,
1973 "cannot set name of output/set dimension",
1974 return isl_aff_free(aff));
1975 if (type == isl_dim_in)
1976 type = isl_dim_set;
1977 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1978 if (!aff->ls)
1979 return isl_aff_free(aff);
1981 return aff;
1984 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1985 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1987 aff = isl_aff_cow(aff);
1988 if (!aff)
1989 goto error;
1990 if (type == isl_dim_out)
1991 isl_die(aff->v->ctx, isl_error_invalid,
1992 "cannot set name of output/set dimension",
1993 goto error);
1994 if (type == isl_dim_in)
1995 type = isl_dim_set;
1996 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1997 if (!aff->ls)
1998 return isl_aff_free(aff);
2000 return aff;
2001 error:
2002 isl_id_free(id);
2003 isl_aff_free(aff);
2004 return NULL;
2007 /* Replace the identifier of the input tuple of "aff" by "id".
2008 * type is currently required to be equal to isl_dim_in
2010 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2011 enum isl_dim_type type, __isl_take isl_id *id)
2013 aff = isl_aff_cow(aff);
2014 if (!aff)
2015 goto error;
2016 if (type != isl_dim_in)
2017 isl_die(aff->v->ctx, isl_error_invalid,
2018 "cannot only set id of input tuple", goto error);
2019 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2020 if (!aff->ls)
2021 return isl_aff_free(aff);
2023 return aff;
2024 error:
2025 isl_id_free(id);
2026 isl_aff_free(aff);
2027 return NULL;
2030 /* Exploit the equalities in "eq" to simplify the affine expression
2031 * and the expressions of the integer divisions in the local space.
2032 * The integer divisions in this local space are assumed to appear
2033 * as regular dimensions in "eq".
2035 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2036 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2038 int i, j;
2039 unsigned o_div;
2040 unsigned n_div;
2042 if (!eq)
2043 goto error;
2044 if (eq->n_eq == 0) {
2045 isl_basic_set_free(eq);
2046 return aff;
2049 aff = isl_aff_cow(aff);
2050 if (!aff)
2051 goto error;
2053 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2054 isl_basic_set_copy(eq));
2055 aff->v = isl_vec_cow(aff->v);
2056 if (!aff->ls || !aff->v)
2057 goto error;
2059 o_div = isl_basic_set_offset(eq, isl_dim_div);
2060 n_div = eq->n_div;
2061 for (i = 0; i < eq->n_eq; ++i) {
2062 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2063 if (j < 0 || j == 0 || j >= o_div)
2064 continue;
2066 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2067 &aff->v->el[0]);
2070 isl_basic_set_free(eq);
2071 aff = isl_aff_normalize(aff);
2072 return aff;
2073 error:
2074 isl_basic_set_free(eq);
2075 isl_aff_free(aff);
2076 return NULL;
2079 /* Exploit the equalities in "eq" to simplify the affine expression
2080 * and the expressions of the integer divisions in the local space.
2082 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2083 __isl_take isl_basic_set *eq)
2085 isl_size n_div;
2087 if (!aff || !eq)
2088 goto error;
2089 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2090 if (n_div < 0)
2091 goto error;
2092 if (n_div > 0)
2093 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2094 return isl_aff_substitute_equalities_lifted(aff, eq);
2095 error:
2096 isl_basic_set_free(eq);
2097 isl_aff_free(aff);
2098 return NULL;
2101 /* Look for equalities among the variables shared by context and aff
2102 * and the integer divisions of aff, if any.
2103 * The equalities are then used to eliminate coefficients and/or integer
2104 * divisions from aff.
2106 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2107 __isl_take isl_set *context)
2109 isl_local_space *ls;
2110 isl_basic_set *hull;
2112 ls = isl_aff_get_domain_local_space(aff);
2113 context = isl_local_space_lift_set(ls, context);
2115 hull = isl_set_affine_hull(context);
2116 return isl_aff_substitute_equalities_lifted(aff, hull);
2119 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2120 __isl_take isl_set *context)
2122 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2123 dom_context = isl_set_intersect_params(dom_context, context);
2124 return isl_aff_gist(aff, dom_context);
2127 /* Return a basic set containing those elements in the space
2128 * of aff where it is positive. "rational" should not be set.
2130 * If "aff" is NaN, then it is not positive.
2132 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2133 int rational)
2135 isl_constraint *ineq;
2136 isl_basic_set *bset;
2137 isl_val *c;
2139 if (!aff)
2140 return NULL;
2141 if (isl_aff_is_nan(aff)) {
2142 isl_space *space = isl_aff_get_domain_space(aff);
2143 isl_aff_free(aff);
2144 return isl_basic_set_empty(space);
2146 if (rational)
2147 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2148 "rational sets not supported", goto error);
2150 ineq = isl_inequality_from_aff(aff);
2151 c = isl_constraint_get_constant_val(ineq);
2152 c = isl_val_sub_ui(c, 1);
2153 ineq = isl_constraint_set_constant_val(ineq, c);
2155 bset = isl_basic_set_from_constraint(ineq);
2156 bset = isl_basic_set_simplify(bset);
2157 return bset;
2158 error:
2159 isl_aff_free(aff);
2160 return NULL;
2163 /* Return a basic set containing those elements in the space
2164 * of aff where it is non-negative.
2165 * If "rational" is set, then return a rational basic set.
2167 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2169 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2170 __isl_take isl_aff *aff, int rational)
2172 isl_constraint *ineq;
2173 isl_basic_set *bset;
2175 if (!aff)
2176 return NULL;
2177 if (isl_aff_is_nan(aff)) {
2178 isl_space *space = isl_aff_get_domain_space(aff);
2179 isl_aff_free(aff);
2180 return isl_basic_set_empty(space);
2183 ineq = isl_inequality_from_aff(aff);
2185 bset = isl_basic_set_from_constraint(ineq);
2186 if (rational)
2187 bset = isl_basic_set_set_rational(bset);
2188 bset = isl_basic_set_simplify(bset);
2189 return bset;
2192 /* Return a basic set containing those elements in the space
2193 * of aff where it is non-negative.
2195 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2197 return aff_nonneg_basic_set(aff, 0);
2200 /* Return a basic set containing those elements in the domain space
2201 * of "aff" where it is positive.
2203 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2205 aff = isl_aff_add_constant_num_si(aff, -1);
2206 return isl_aff_nonneg_basic_set(aff);
2209 /* Return a basic set containing those elements in the domain space
2210 * of aff where it is negative.
2212 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2214 aff = isl_aff_neg(aff);
2215 return isl_aff_pos_basic_set(aff);
2218 /* Return a basic set containing those elements in the space
2219 * of aff where it is zero.
2220 * If "rational" is set, then return a rational basic set.
2222 * If "aff" is NaN, then it is not zero.
2224 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2225 int rational)
2227 isl_constraint *ineq;
2228 isl_basic_set *bset;
2230 if (!aff)
2231 return NULL;
2232 if (isl_aff_is_nan(aff)) {
2233 isl_space *space = isl_aff_get_domain_space(aff);
2234 isl_aff_free(aff);
2235 return isl_basic_set_empty(space);
2238 ineq = isl_equality_from_aff(aff);
2240 bset = isl_basic_set_from_constraint(ineq);
2241 if (rational)
2242 bset = isl_basic_set_set_rational(bset);
2243 bset = isl_basic_set_simplify(bset);
2244 return bset;
2247 /* Return a basic set containing those elements in the space
2248 * of aff where it is zero.
2250 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2252 return aff_zero_basic_set(aff, 0);
2255 /* Return a basic 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_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2259 __isl_take isl_aff *aff2)
2261 aff1 = isl_aff_sub(aff1, aff2);
2263 return isl_aff_nonneg_basic_set(aff1);
2266 /* Return a basic set containing those elements in the shared domain space
2267 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2269 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2270 __isl_take isl_aff *aff2)
2272 aff1 = isl_aff_sub(aff1, aff2);
2274 return isl_aff_pos_basic_set(aff1);
2277 /* Return a set containing those elements in the shared space
2278 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2280 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2281 __isl_take isl_aff *aff2)
2283 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2286 /* Return a set containing those elements in the shared domain space
2287 * of aff1 and aff2 where aff1 is greater than aff2.
2289 * If either of the two inputs is NaN, then the result is empty,
2290 * as comparisons with NaN always return false.
2292 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2293 __isl_take isl_aff *aff2)
2295 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2298 /* Return a basic set containing those elements in the shared space
2299 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2301 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2302 __isl_take isl_aff *aff2)
2304 return isl_aff_ge_basic_set(aff2, aff1);
2307 /* Return a basic set containing those elements in the shared domain space
2308 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2310 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2311 __isl_take isl_aff *aff2)
2313 return isl_aff_gt_basic_set(aff2, aff1);
2316 /* Return a set containing those elements in the shared space
2317 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2319 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2320 __isl_take isl_aff *aff2)
2322 return isl_aff_ge_set(aff2, aff1);
2325 /* Return a set containing those elements in the shared domain space
2326 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2328 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2329 __isl_take isl_aff *aff2)
2331 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2334 /* Return a basic set containing those elements in the shared space
2335 * of aff1 and aff2 where aff1 and aff2 are equal.
2337 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2338 __isl_take isl_aff *aff2)
2340 aff1 = isl_aff_sub(aff1, aff2);
2342 return isl_aff_zero_basic_set(aff1);
2345 /* Return a set containing those elements in the shared space
2346 * of aff1 and aff2 where aff1 and aff2 are equal.
2348 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2349 __isl_take isl_aff *aff2)
2351 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2354 /* Return a set containing those elements in the shared domain space
2355 * of aff1 and aff2 where aff1 and aff2 are not equal.
2357 * If either of the two inputs is NaN, then the result is empty,
2358 * as comparisons with NaN always return false.
2360 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2361 __isl_take isl_aff *aff2)
2363 isl_set *set_lt, *set_gt;
2365 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2366 isl_aff_copy(aff2));
2367 set_gt = isl_aff_gt_set(aff1, aff2);
2368 return isl_set_union_disjoint(set_lt, set_gt);
2371 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2372 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2374 aff1 = isl_aff_add(aff1, aff2);
2375 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2376 return aff1;
2379 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2381 if (!aff)
2382 return -1;
2384 return 0;
2387 #undef TYPE
2388 #define TYPE isl_aff
2389 static
2390 #include "check_type_range_templ.c"
2392 /* Check whether the given affine expression has non-zero coefficient
2393 * for any dimension in the given range or if any of these dimensions
2394 * appear with non-zero coefficients in any of the integer divisions
2395 * involved in the affine expression.
2397 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2398 enum isl_dim_type type, unsigned first, unsigned n)
2400 int i;
2401 int *active = NULL;
2402 isl_bool involves = isl_bool_false;
2404 if (!aff)
2405 return isl_bool_error;
2406 if (n == 0)
2407 return isl_bool_false;
2408 if (isl_aff_check_range(aff, type, first, n) < 0)
2409 return isl_bool_error;
2411 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2412 if (!active)
2413 goto error;
2415 first += isl_local_space_offset(aff->ls, type) - 1;
2416 for (i = 0; i < n; ++i)
2417 if (active[first + i]) {
2418 involves = isl_bool_true;
2419 break;
2422 free(active);
2424 return involves;
2425 error:
2426 free(active);
2427 return isl_bool_error;
2430 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2431 enum isl_dim_type type, unsigned first, unsigned n)
2433 isl_ctx *ctx;
2435 if (!aff)
2436 return NULL;
2437 if (type == isl_dim_out)
2438 isl_die(aff->v->ctx, isl_error_invalid,
2439 "cannot drop output/set dimension",
2440 return isl_aff_free(aff));
2441 if (type == isl_dim_in)
2442 type = isl_dim_set;
2443 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2444 return aff;
2446 ctx = isl_aff_get_ctx(aff);
2447 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2448 return isl_aff_free(aff);
2450 aff = isl_aff_cow(aff);
2451 if (!aff)
2452 return NULL;
2454 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2455 if (!aff->ls)
2456 return isl_aff_free(aff);
2458 first += 1 + isl_local_space_offset(aff->ls, type);
2459 aff->v = isl_vec_drop_els(aff->v, first, n);
2460 if (!aff->v)
2461 return isl_aff_free(aff);
2463 return aff;
2466 /* Drop the "n" domain dimensions starting at "first" from "aff",
2467 * after checking that they do not appear in the affine expression.
2469 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2470 unsigned n)
2472 isl_bool involves;
2474 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2475 if (involves < 0)
2476 return isl_aff_free(aff);
2477 if (involves)
2478 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2479 "affine expression involves some of the domain dimensions",
2480 return isl_aff_free(aff));
2481 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2484 /* Project the domain of the affine expression onto its parameter space.
2485 * The affine expression may not involve any of the domain dimensions.
2487 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2489 isl_space *space;
2490 isl_size n;
2492 n = isl_aff_dim(aff, isl_dim_in);
2493 if (n < 0)
2494 return isl_aff_free(aff);
2495 aff = drop_domain(aff, 0, n);
2496 space = isl_aff_get_domain_space(aff);
2497 space = isl_space_params(space);
2498 aff = isl_aff_reset_domain_space(aff, space);
2499 return aff;
2502 /* Check that the domain of "aff" is a product.
2504 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2506 isl_bool is_product;
2508 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2509 if (is_product < 0)
2510 return isl_stat_error;
2511 if (!is_product)
2512 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2513 "domain is not a product", return isl_stat_error);
2514 return isl_stat_ok;
2517 /* Given an affine function with a domain of the form [A -> B] that
2518 * does not depend on B, return the same function on domain A.
2520 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2522 isl_space *space;
2523 isl_size n, n_in;
2525 if (check_domain_product(aff) < 0)
2526 return isl_aff_free(aff);
2527 space = isl_aff_get_domain_space(aff);
2528 n = isl_space_dim(space, isl_dim_set);
2529 space = isl_space_factor_domain(space);
2530 n_in = isl_space_dim(space, isl_dim_set);
2531 if (n < 0 || n_in < 0)
2532 aff = isl_aff_free(aff);
2533 else
2534 aff = drop_domain(aff, n_in, n - n_in);
2535 aff = isl_aff_reset_domain_space(aff, space);
2536 return aff;
2539 /* Convert an affine expression defined over a parameter domain
2540 * into one that is defined over a zero-dimensional set.
2542 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2544 isl_local_space *ls;
2546 ls = isl_aff_take_domain_local_space(aff);
2547 ls = isl_local_space_set_from_params(ls);
2548 aff = isl_aff_restore_domain_local_space(aff, ls);
2550 return aff;
2553 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2554 enum isl_dim_type type, unsigned first, unsigned n)
2556 isl_ctx *ctx;
2558 if (!aff)
2559 return NULL;
2560 if (type == isl_dim_out)
2561 isl_die(aff->v->ctx, isl_error_invalid,
2562 "cannot insert output/set dimensions",
2563 return isl_aff_free(aff));
2564 if (type == isl_dim_in)
2565 type = isl_dim_set;
2566 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2567 return aff;
2569 ctx = isl_aff_get_ctx(aff);
2570 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2571 return isl_aff_free(aff);
2573 aff = isl_aff_cow(aff);
2574 if (!aff)
2575 return NULL;
2577 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2578 if (!aff->ls)
2579 return isl_aff_free(aff);
2581 first += 1 + isl_local_space_offset(aff->ls, type);
2582 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2583 if (!aff->v)
2584 return isl_aff_free(aff);
2586 return aff;
2589 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2590 enum isl_dim_type type, unsigned n)
2592 isl_size pos;
2594 pos = isl_aff_dim(aff, type);
2595 if (pos < 0)
2596 return isl_aff_free(aff);
2598 return isl_aff_insert_dims(aff, type, pos, n);
2601 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2602 enum isl_dim_type type, unsigned n)
2604 isl_size pos;
2606 pos = isl_pw_aff_dim(pwaff, type);
2607 if (pos < 0)
2608 return isl_pw_aff_free(pwaff);
2610 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2613 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2614 * to dimensions of "dst_type" at "dst_pos".
2616 * We only support moving input dimensions to parameters and vice versa.
2618 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2619 enum isl_dim_type dst_type, unsigned dst_pos,
2620 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2622 unsigned g_dst_pos;
2623 unsigned g_src_pos;
2625 if (!aff)
2626 return NULL;
2627 if (n == 0 &&
2628 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2629 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2630 return aff;
2632 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2633 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2634 "cannot move output/set dimension",
2635 return isl_aff_free(aff));
2636 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2637 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2638 "cannot move divs", return isl_aff_free(aff));
2639 if (dst_type == isl_dim_in)
2640 dst_type = isl_dim_set;
2641 if (src_type == isl_dim_in)
2642 src_type = isl_dim_set;
2644 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2645 return isl_aff_free(aff);
2646 if (dst_type == src_type)
2647 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2648 "moving dims within the same type not supported",
2649 return isl_aff_free(aff));
2651 aff = isl_aff_cow(aff);
2652 if (!aff)
2653 return NULL;
2655 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2656 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2657 if (dst_type > src_type)
2658 g_dst_pos -= n;
2660 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2661 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2662 src_type, src_pos, n);
2663 if (!aff->v || !aff->ls)
2664 return isl_aff_free(aff);
2666 aff = sort_divs(aff);
2668 return aff;
2671 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2673 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2674 return isl_pw_aff_alloc(dom, aff);
2677 #define isl_aff_involves_nan isl_aff_is_nan
2679 #undef PW
2680 #define PW isl_pw_aff
2681 #undef EL
2682 #define EL isl_aff
2683 #undef EL_IS_ZERO
2684 #define EL_IS_ZERO is_empty
2685 #undef ZERO
2686 #define ZERO empty
2687 #undef IS_ZERO
2688 #define IS_ZERO is_empty
2689 #undef FIELD
2690 #define FIELD aff
2691 #undef DEFAULT_IS_ZERO
2692 #define DEFAULT_IS_ZERO 0
2694 #define NO_OPT
2695 #define NO_LIFT
2696 #define NO_MORPH
2698 #include <isl_pw_templ.c>
2699 #include <isl_pw_eval.c>
2700 #include <isl_pw_hash.c>
2701 #include <isl_pw_union_opt.c>
2703 #undef BASE
2704 #define BASE pw_aff
2706 #include <isl_union_single.c>
2707 #include <isl_union_neg.c>
2709 static __isl_give isl_set *align_params_pw_pw_set_and(
2710 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2711 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2712 __isl_take isl_pw_aff *pwaff2))
2714 isl_bool equal_params;
2716 if (!pwaff1 || !pwaff2)
2717 goto error;
2718 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2719 if (equal_params < 0)
2720 goto error;
2721 if (equal_params)
2722 return fn(pwaff1, pwaff2);
2723 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2724 isl_pw_aff_check_named_params(pwaff2) < 0)
2725 goto error;
2726 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2727 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2728 return fn(pwaff1, pwaff2);
2729 error:
2730 isl_pw_aff_free(pwaff1);
2731 isl_pw_aff_free(pwaff2);
2732 return NULL;
2735 /* Align the parameters of the to isl_pw_aff arguments and
2736 * then apply a function "fn" on them that returns an isl_map.
2738 static __isl_give isl_map *align_params_pw_pw_map_and(
2739 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2740 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2741 __isl_take isl_pw_aff *pa2))
2743 isl_bool equal_params;
2745 if (!pa1 || !pa2)
2746 goto error;
2747 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2748 if (equal_params < 0)
2749 goto error;
2750 if (equal_params)
2751 return fn(pa1, pa2);
2752 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2753 isl_pw_aff_check_named_params(pa2) < 0)
2754 goto error;
2755 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2756 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2757 return fn(pa1, pa2);
2758 error:
2759 isl_pw_aff_free(pa1);
2760 isl_pw_aff_free(pa2);
2761 return NULL;
2764 /* Compute a piecewise quasi-affine expression with a domain that
2765 * is the union of those of pwaff1 and pwaff2 and such that on each
2766 * cell, the quasi-affine expression is the maximum of those of pwaff1
2767 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2768 * cell, then the associated expression is the defined one.
2770 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2771 __isl_take isl_pw_aff *pwaff2)
2773 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2776 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2777 __isl_take isl_pw_aff *pwaff2)
2779 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2780 &pw_aff_union_max);
2783 /* Compute a piecewise quasi-affine expression with a domain that
2784 * is the union of those of pwaff1 and pwaff2 and such that on each
2785 * cell, the quasi-affine expression is the minimum of those of pwaff1
2786 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2787 * cell, then the associated expression is the defined one.
2789 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2790 __isl_take isl_pw_aff *pwaff2)
2792 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2795 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2796 __isl_take isl_pw_aff *pwaff2)
2798 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2799 &pw_aff_union_min);
2802 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2803 __isl_take isl_pw_aff *pwaff2, int max)
2805 if (max)
2806 return isl_pw_aff_union_max(pwaff1, pwaff2);
2807 else
2808 return isl_pw_aff_union_min(pwaff1, pwaff2);
2811 /* Return a set containing those elements in the domain
2812 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2813 * does not satisfy "fn" (if complement is 1).
2815 * The pieces with a NaN never belong to the result since
2816 * NaN does not satisfy any property.
2818 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2819 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2820 int complement)
2822 int i;
2823 isl_set *set;
2825 if (!pwaff)
2826 return NULL;
2828 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2830 for (i = 0; i < pwaff->n; ++i) {
2831 isl_basic_set *bset;
2832 isl_set *set_i, *locus;
2833 isl_bool rational;
2835 if (isl_aff_is_nan(pwaff->p[i].aff))
2836 continue;
2838 rational = isl_set_has_rational(pwaff->p[i].set);
2839 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2840 locus = isl_set_from_basic_set(bset);
2841 set_i = isl_set_copy(pwaff->p[i].set);
2842 if (complement)
2843 set_i = isl_set_subtract(set_i, locus);
2844 else
2845 set_i = isl_set_intersect(set_i, locus);
2846 set = isl_set_union_disjoint(set, set_i);
2849 isl_pw_aff_free(pwaff);
2851 return set;
2854 /* Return a set containing those elements in the domain
2855 * of "pa" where it is positive.
2857 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2859 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2862 /* Return a set containing those elements in the domain
2863 * of pwaff where it is non-negative.
2865 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2867 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2870 /* Return a set containing those elements in the domain
2871 * of pwaff where it is zero.
2873 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2875 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2878 /* Return a set containing those elements in the domain
2879 * of pwaff where it is not zero.
2881 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2883 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2886 /* Return a set containing those elements in the shared domain
2887 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2889 * We compute the difference on the shared domain and then construct
2890 * the set of values where this difference is non-negative.
2891 * If strict is set, we first subtract 1 from the difference.
2892 * If equal is set, we only return the elements where pwaff1 and pwaff2
2893 * are equal.
2895 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2896 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2898 isl_set *set1, *set2;
2900 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2901 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2902 set1 = isl_set_intersect(set1, set2);
2903 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2904 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2905 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2907 if (strict) {
2908 isl_space *space = isl_set_get_space(set1);
2909 isl_aff *aff;
2910 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2911 aff = isl_aff_add_constant_si(aff, -1);
2912 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2913 } else
2914 isl_set_free(set1);
2916 if (equal)
2917 return isl_pw_aff_zero_set(pwaff1);
2918 return isl_pw_aff_nonneg_set(pwaff1);
2921 /* Return a set containing those elements in the shared domain
2922 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2924 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2925 __isl_take isl_pw_aff *pwaff2)
2927 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2930 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2931 __isl_take isl_pw_aff *pwaff2)
2933 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2936 /* Return a set containing those elements in the shared domain
2937 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2939 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2940 __isl_take isl_pw_aff *pwaff2)
2942 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2945 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2946 __isl_take isl_pw_aff *pwaff2)
2948 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2951 /* Return a set containing those elements in the shared domain
2952 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2954 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2955 __isl_take isl_pw_aff *pwaff2)
2957 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2960 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2961 __isl_take isl_pw_aff *pwaff2)
2963 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2966 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2967 __isl_take isl_pw_aff *pwaff2)
2969 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2972 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2973 __isl_take isl_pw_aff *pwaff2)
2975 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2978 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2979 * where the function values are ordered in the same way as "order",
2980 * which returns a set in the shared domain of its two arguments.
2981 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2983 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2984 * We first pull back the two functions such that they are defined on
2985 * the domain [A -> B]. Then we apply "order", resulting in a set
2986 * in the space [A -> B]. Finally, we unwrap this set to obtain
2987 * a map in the space A -> B.
2989 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2990 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2991 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2992 __isl_take isl_pw_aff *pa2))
2994 isl_space *space1, *space2;
2995 isl_multi_aff *ma;
2996 isl_set *set;
2998 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2999 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3000 space1 = isl_space_map_from_domain_and_range(space1, space2);
3001 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3002 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3003 ma = isl_multi_aff_range_map(space1);
3004 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3005 set = order(pa1, pa2);
3007 return isl_set_unwrap(set);
3010 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3011 * where the function values are equal.
3012 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3014 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3015 __isl_take isl_pw_aff *pa2)
3017 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3020 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3021 * where the function values are equal.
3023 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3024 __isl_take isl_pw_aff *pa2)
3026 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3029 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3030 * where the function value of "pa1" is less than the function value of "pa2".
3031 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3033 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3034 __isl_take isl_pw_aff *pa2)
3036 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3039 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3040 * where the function value of "pa1" is less than the function value of "pa2".
3042 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3043 __isl_take isl_pw_aff *pa2)
3045 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3048 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3049 * where the function value of "pa1" is greater than the function value
3050 * of "pa2".
3051 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3053 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3054 __isl_take isl_pw_aff *pa2)
3056 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3059 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3060 * where the function value of "pa1" is greater than the function value
3061 * of "pa2".
3063 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3064 __isl_take isl_pw_aff *pa2)
3066 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3069 /* Return a set containing those elements in the shared domain
3070 * of the elements of list1 and list2 where each element in list1
3071 * has the relation specified by "fn" with each element in list2.
3073 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3074 __isl_take isl_pw_aff_list *list2,
3075 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3076 __isl_take isl_pw_aff *pwaff2))
3078 int i, j;
3079 isl_ctx *ctx;
3080 isl_set *set;
3082 if (!list1 || !list2)
3083 goto error;
3085 ctx = isl_pw_aff_list_get_ctx(list1);
3086 if (list1->n < 1 || list2->n < 1)
3087 isl_die(ctx, isl_error_invalid,
3088 "list should contain at least one element", goto error);
3090 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3091 for (i = 0; i < list1->n; ++i)
3092 for (j = 0; j < list2->n; ++j) {
3093 isl_set *set_ij;
3095 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3096 isl_pw_aff_copy(list2->p[j]));
3097 set = isl_set_intersect(set, set_ij);
3100 isl_pw_aff_list_free(list1);
3101 isl_pw_aff_list_free(list2);
3102 return set;
3103 error:
3104 isl_pw_aff_list_free(list1);
3105 isl_pw_aff_list_free(list2);
3106 return NULL;
3109 /* Return a set containing those elements in the shared domain
3110 * of the elements of list1 and list2 where each element in list1
3111 * is equal to each element in list2.
3113 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3114 __isl_take isl_pw_aff_list *list2)
3116 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3119 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3120 __isl_take isl_pw_aff_list *list2)
3122 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3125 /* Return a set containing those elements in the shared domain
3126 * of the elements of list1 and list2 where each element in list1
3127 * is less than or equal to each element in list2.
3129 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3130 __isl_take isl_pw_aff_list *list2)
3132 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3135 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3136 __isl_take isl_pw_aff_list *list2)
3138 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3141 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3142 __isl_take isl_pw_aff_list *list2)
3144 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3147 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3148 __isl_take isl_pw_aff_list *list2)
3150 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3154 /* Return a set containing those elements in the shared domain
3155 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3157 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3158 __isl_take isl_pw_aff *pwaff2)
3160 isl_set *set_lt, *set_gt;
3162 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3163 isl_pw_aff_copy(pwaff2));
3164 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3165 return isl_set_union_disjoint(set_lt, set_gt);
3168 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3169 __isl_take isl_pw_aff *pwaff2)
3171 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3174 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3175 isl_int v)
3177 int i;
3179 if (isl_int_is_one(v))
3180 return pwaff;
3181 if (!isl_int_is_pos(v))
3182 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3183 "factor needs to be positive",
3184 return isl_pw_aff_free(pwaff));
3185 pwaff = isl_pw_aff_cow(pwaff);
3186 if (!pwaff)
3187 return NULL;
3188 if (pwaff->n == 0)
3189 return pwaff;
3191 for (i = 0; i < pwaff->n; ++i) {
3192 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3193 if (!pwaff->p[i].aff)
3194 return isl_pw_aff_free(pwaff);
3197 return pwaff;
3200 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3202 int i;
3204 pwaff = isl_pw_aff_cow(pwaff);
3205 if (!pwaff)
3206 return NULL;
3207 if (pwaff->n == 0)
3208 return pwaff;
3210 for (i = 0; i < pwaff->n; ++i) {
3211 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3212 if (!pwaff->p[i].aff)
3213 return isl_pw_aff_free(pwaff);
3216 return pwaff;
3219 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3221 int i;
3223 pwaff = isl_pw_aff_cow(pwaff);
3224 if (!pwaff)
3225 return NULL;
3226 if (pwaff->n == 0)
3227 return pwaff;
3229 for (i = 0; i < pwaff->n; ++i) {
3230 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3231 if (!pwaff->p[i].aff)
3232 return isl_pw_aff_free(pwaff);
3235 return pwaff;
3238 /* Assuming that "cond1" and "cond2" are disjoint,
3239 * return an affine expression that is equal to pwaff1 on cond1
3240 * and to pwaff2 on cond2.
3242 static __isl_give isl_pw_aff *isl_pw_aff_select(
3243 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3244 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3246 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3247 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3249 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3252 /* Return an affine expression that is equal to pwaff_true for elements
3253 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3254 * is zero.
3255 * That is, return cond ? pwaff_true : pwaff_false;
3257 * If "cond" involves and NaN, then we conservatively return a NaN
3258 * on its entire domain. In principle, we could consider the pieces
3259 * where it is NaN separately from those where it is not.
3261 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3262 * then only use the domain of "cond" to restrict the domain.
3264 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3265 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3267 isl_set *cond_true, *cond_false;
3268 isl_bool equal;
3270 if (!cond)
3271 goto error;
3272 if (isl_pw_aff_involves_nan(cond)) {
3273 isl_space *space = isl_pw_aff_get_domain_space(cond);
3274 isl_local_space *ls = isl_local_space_from_space(space);
3275 isl_pw_aff_free(cond);
3276 isl_pw_aff_free(pwaff_true);
3277 isl_pw_aff_free(pwaff_false);
3278 return isl_pw_aff_nan_on_domain(ls);
3281 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3282 isl_pw_aff_get_space(pwaff_false));
3283 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3284 isl_pw_aff_get_space(pwaff_true));
3285 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3286 if (equal < 0)
3287 goto error;
3288 if (equal) {
3289 isl_set *dom;
3291 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3292 isl_pw_aff_free(pwaff_false);
3293 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3296 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3297 cond_false = isl_pw_aff_zero_set(cond);
3298 return isl_pw_aff_select(cond_true, pwaff_true,
3299 cond_false, pwaff_false);
3300 error:
3301 isl_pw_aff_free(cond);
3302 isl_pw_aff_free(pwaff_true);
3303 isl_pw_aff_free(pwaff_false);
3304 return NULL;
3307 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3309 int pos;
3311 if (!aff)
3312 return isl_bool_error;
3314 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3315 return isl_bool_ok(pos == -1);
3318 /* Check whether pwaff is a piecewise constant.
3320 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3322 int i;
3324 if (!pwaff)
3325 return isl_bool_error;
3327 for (i = 0; i < pwaff->n; ++i) {
3328 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3329 if (is_cst < 0 || !is_cst)
3330 return is_cst;
3333 return isl_bool_true;
3336 /* Are all elements of "mpa" piecewise constants?
3338 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3340 int i;
3342 if (!mpa)
3343 return isl_bool_error;
3345 for (i = 0; i < mpa->n; ++i) {
3346 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3347 if (is_cst < 0 || !is_cst)
3348 return is_cst;
3351 return isl_bool_true;
3354 /* Return the product of "aff1" and "aff2".
3356 * If either of the two is NaN, then the result is NaN.
3358 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3360 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3361 __isl_take isl_aff *aff2)
3363 if (!aff1 || !aff2)
3364 goto error;
3366 if (isl_aff_is_nan(aff1)) {
3367 isl_aff_free(aff2);
3368 return aff1;
3370 if (isl_aff_is_nan(aff2)) {
3371 isl_aff_free(aff1);
3372 return aff2;
3375 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3376 return isl_aff_mul(aff2, aff1);
3378 if (!isl_aff_is_cst(aff2))
3379 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3380 "at least one affine expression should be constant",
3381 goto error);
3383 aff1 = isl_aff_cow(aff1);
3384 if (!aff1 || !aff2)
3385 goto error;
3387 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3388 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3390 isl_aff_free(aff2);
3391 return aff1;
3392 error:
3393 isl_aff_free(aff1);
3394 isl_aff_free(aff2);
3395 return NULL;
3398 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3400 * If either of the two is NaN, then the result is NaN.
3402 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3403 __isl_take isl_aff *aff2)
3405 int is_cst;
3406 int neg;
3408 if (!aff1 || !aff2)
3409 goto error;
3411 if (isl_aff_is_nan(aff1)) {
3412 isl_aff_free(aff2);
3413 return aff1;
3415 if (isl_aff_is_nan(aff2)) {
3416 isl_aff_free(aff1);
3417 return aff2;
3420 is_cst = isl_aff_is_cst(aff2);
3421 if (is_cst < 0)
3422 goto error;
3423 if (!is_cst)
3424 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3425 "second argument should be a constant", goto error);
3427 if (!aff2)
3428 goto error;
3430 neg = isl_int_is_neg(aff2->v->el[1]);
3431 if (neg) {
3432 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3433 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3436 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3437 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3439 if (neg) {
3440 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3441 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3444 isl_aff_free(aff2);
3445 return aff1;
3446 error:
3447 isl_aff_free(aff1);
3448 isl_aff_free(aff2);
3449 return NULL;
3452 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3455 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3458 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3459 __isl_take isl_pw_aff *pwaff2)
3461 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3464 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3465 __isl_take isl_pw_aff *pwaff2)
3467 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3470 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3471 __isl_take isl_pw_aff *pwaff2)
3473 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3476 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3477 __isl_take isl_pw_aff *pwaff2)
3479 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3482 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3483 __isl_take isl_pw_aff *pa2)
3485 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3488 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3490 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3491 __isl_take isl_pw_aff *pa2)
3493 int is_cst;
3495 is_cst = isl_pw_aff_is_cst(pa2);
3496 if (is_cst < 0)
3497 goto error;
3498 if (!is_cst)
3499 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3500 "second argument should be a piecewise constant",
3501 goto error);
3502 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3503 error:
3504 isl_pw_aff_free(pa1);
3505 isl_pw_aff_free(pa2);
3506 return NULL;
3509 /* Compute the quotient of the integer division of "pa1" by "pa2"
3510 * with rounding towards zero.
3511 * "pa2" is assumed to be a piecewise constant.
3513 * In particular, return
3515 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3518 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3519 __isl_take isl_pw_aff *pa2)
3521 int is_cst;
3522 isl_set *cond;
3523 isl_pw_aff *f, *c;
3525 is_cst = isl_pw_aff_is_cst(pa2);
3526 if (is_cst < 0)
3527 goto error;
3528 if (!is_cst)
3529 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3530 "second argument should be a piecewise constant",
3531 goto error);
3533 pa1 = isl_pw_aff_div(pa1, pa2);
3535 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3536 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3537 c = isl_pw_aff_ceil(pa1);
3538 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3539 error:
3540 isl_pw_aff_free(pa1);
3541 isl_pw_aff_free(pa2);
3542 return NULL;
3545 /* Compute the remainder of the integer division of "pa1" by "pa2"
3546 * with rounding towards zero.
3547 * "pa2" is assumed to be a piecewise constant.
3549 * In particular, return
3551 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3554 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3555 __isl_take isl_pw_aff *pa2)
3557 int is_cst;
3558 isl_pw_aff *res;
3560 is_cst = isl_pw_aff_is_cst(pa2);
3561 if (is_cst < 0)
3562 goto error;
3563 if (!is_cst)
3564 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3565 "second argument should be a piecewise constant",
3566 goto error);
3567 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3568 res = isl_pw_aff_mul(pa2, res);
3569 res = isl_pw_aff_sub(pa1, res);
3570 return res;
3571 error:
3572 isl_pw_aff_free(pa1);
3573 isl_pw_aff_free(pa2);
3574 return NULL;
3577 /* Does either of "pa1" or "pa2" involve any NaN2?
3579 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3580 __isl_keep isl_pw_aff *pa2)
3582 isl_bool has_nan;
3584 has_nan = isl_pw_aff_involves_nan(pa1);
3585 if (has_nan < 0 || has_nan)
3586 return has_nan;
3587 return isl_pw_aff_involves_nan(pa2);
3590 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3591 * by a NaN on their shared domain.
3593 * In principle, the result could be refined to only being NaN
3594 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3596 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3597 __isl_take isl_pw_aff *pa2)
3599 isl_local_space *ls;
3600 isl_set *dom;
3601 isl_pw_aff *pa;
3603 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3604 ls = isl_local_space_from_space(isl_set_get_space(dom));
3605 pa = isl_pw_aff_nan_on_domain(ls);
3606 pa = isl_pw_aff_intersect_domain(pa, dom);
3608 return pa;
3611 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3612 __isl_take isl_pw_aff *pwaff2)
3614 isl_set *le;
3615 isl_set *dom;
3617 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3618 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3619 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3620 isl_pw_aff_copy(pwaff2));
3621 dom = isl_set_subtract(dom, isl_set_copy(le));
3622 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3625 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3626 __isl_take isl_pw_aff *pwaff2)
3628 isl_set *ge;
3629 isl_set *dom;
3631 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3632 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3633 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3634 isl_pw_aff_copy(pwaff2));
3635 dom = isl_set_subtract(dom, isl_set_copy(ge));
3636 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3639 /* Return an expression for the minimum (if "max" is not set) or
3640 * the maximum (if "max" is set) of "pa1" and "pa2".
3641 * If either expression involves any NaN, then return a NaN
3642 * on the shared domain as result.
3644 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3645 __isl_take isl_pw_aff *pa2, int max)
3647 isl_bool has_nan;
3649 has_nan = either_involves_nan(pa1, pa2);
3650 if (has_nan < 0)
3651 pa1 = isl_pw_aff_free(pa1);
3652 else if (has_nan)
3653 return replace_by_nan(pa1, pa2);
3655 if (max)
3656 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3657 else
3658 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3661 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3663 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3664 __isl_take isl_pw_aff *pwaff2)
3666 return pw_aff_min_max(pwaff1, pwaff2, 0);
3669 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3671 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3672 __isl_take isl_pw_aff *pwaff2)
3674 return pw_aff_min_max(pwaff1, pwaff2, 1);
3677 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3678 __isl_take isl_pw_aff_list *list,
3679 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3680 __isl_take isl_pw_aff *pwaff2))
3682 int i;
3683 isl_ctx *ctx;
3684 isl_pw_aff *res;
3686 if (!list)
3687 return NULL;
3689 ctx = isl_pw_aff_list_get_ctx(list);
3690 if (list->n < 1)
3691 isl_die(ctx, isl_error_invalid,
3692 "list should contain at least one element", goto error);
3694 res = isl_pw_aff_copy(list->p[0]);
3695 for (i = 1; i < list->n; ++i)
3696 res = fn(res, isl_pw_aff_copy(list->p[i]));
3698 isl_pw_aff_list_free(list);
3699 return res;
3700 error:
3701 isl_pw_aff_list_free(list);
3702 return NULL;
3705 /* Return an isl_pw_aff that maps each element in the intersection of the
3706 * domains of the elements of list to the minimal corresponding affine
3707 * expression.
3709 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3711 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3714 /* Return an isl_pw_aff that maps each element in the intersection of the
3715 * domains of the elements of list to the maximal corresponding affine
3716 * expression.
3718 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3720 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3723 /* Mark the domains of "pwaff" as rational.
3725 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3727 int i;
3729 pwaff = isl_pw_aff_cow(pwaff);
3730 if (!pwaff)
3731 return NULL;
3732 if (pwaff->n == 0)
3733 return pwaff;
3735 for (i = 0; i < pwaff->n; ++i) {
3736 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3737 if (!pwaff->p[i].set)
3738 return isl_pw_aff_free(pwaff);
3741 return pwaff;
3744 /* Mark the domains of the elements of "list" as rational.
3746 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3747 __isl_take isl_pw_aff_list *list)
3749 int i, n;
3751 if (!list)
3752 return NULL;
3753 if (list->n == 0)
3754 return list;
3756 n = list->n;
3757 for (i = 0; i < n; ++i) {
3758 isl_pw_aff *pa;
3760 pa = isl_pw_aff_list_get_pw_aff(list, i);
3761 pa = isl_pw_aff_set_rational(pa);
3762 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3765 return list;
3768 /* Do the parameters of "aff" match those of "space"?
3770 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3771 __isl_keep isl_space *space)
3773 isl_space *aff_space;
3774 isl_bool match;
3776 if (!aff || !space)
3777 return isl_bool_error;
3779 aff_space = isl_aff_get_domain_space(aff);
3781 match = isl_space_has_equal_params(space, aff_space);
3783 isl_space_free(aff_space);
3784 return match;
3787 /* Check that the domain space of "aff" matches "space".
3789 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3790 __isl_keep isl_space *space)
3792 isl_space *aff_space;
3793 isl_bool match;
3795 if (!aff || !space)
3796 return isl_stat_error;
3798 aff_space = isl_aff_get_domain_space(aff);
3800 match = isl_space_has_equal_params(space, aff_space);
3801 if (match < 0)
3802 goto error;
3803 if (!match)
3804 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3805 "parameters don't match", goto error);
3806 match = isl_space_tuple_is_equal(space, isl_dim_in,
3807 aff_space, isl_dim_set);
3808 if (match < 0)
3809 goto error;
3810 if (!match)
3811 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3812 "domains don't match", goto error);
3813 isl_space_free(aff_space);
3814 return isl_stat_ok;
3815 error:
3816 isl_space_free(aff_space);
3817 return isl_stat_error;
3820 #undef BASE
3821 #define BASE aff
3822 #undef DOMBASE
3823 #define DOMBASE set
3825 #include <isl_multi_no_explicit_domain.c>
3826 #include <isl_multi_templ.c>
3827 #include <isl_multi_apply_set.c>
3828 #include <isl_multi_arith_templ.c>
3829 #include <isl_multi_cmp.c>
3830 #include <isl_multi_dims.c>
3831 #include <isl_multi_floor.c>
3832 #include <isl_multi_from_base_templ.c>
3833 #include <isl_multi_gist.c>
3834 #include <isl_multi_identity_templ.c>
3835 #include <isl_multi_move_dims_templ.c>
3836 #include <isl_multi_nan_templ.c>
3837 #include <isl_multi_product_templ.c>
3838 #include <isl_multi_splice_templ.c>
3839 #include <isl_multi_zero_templ.c>
3841 /* Construct an isl_multi_aff living in "space" that corresponds
3842 * to the affine transformation matrix "mat".
3844 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3845 __isl_take isl_space *space, __isl_take isl_mat *mat)
3847 isl_ctx *ctx;
3848 isl_local_space *ls = NULL;
3849 isl_multi_aff *ma = NULL;
3850 isl_size n_row, n_col, n_out, total;
3851 int i;
3853 if (!space || !mat)
3854 goto error;
3856 ctx = isl_mat_get_ctx(mat);
3858 n_row = isl_mat_rows(mat);
3859 n_col = isl_mat_cols(mat);
3860 n_out = isl_space_dim(space, isl_dim_out);
3861 total = isl_space_dim(space, isl_dim_all);
3862 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3863 goto error;
3864 if (n_row < 1)
3865 isl_die(ctx, isl_error_invalid,
3866 "insufficient number of rows", goto error);
3867 if (n_col < 1)
3868 isl_die(ctx, isl_error_invalid,
3869 "insufficient number of columns", goto error);
3870 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3871 isl_die(ctx, isl_error_invalid,
3872 "dimension mismatch", goto error);
3874 ma = isl_multi_aff_zero(isl_space_copy(space));
3875 ls = isl_local_space_from_space(isl_space_domain(space));
3877 for (i = 0; i < n_row - 1; ++i) {
3878 isl_vec *v;
3879 isl_aff *aff;
3881 v = isl_vec_alloc(ctx, 1 + n_col);
3882 if (!v)
3883 goto error;
3884 isl_int_set(v->el[0], mat->row[0][0]);
3885 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3886 v = isl_vec_normalize(v);
3887 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3888 ma = isl_multi_aff_set_aff(ma, i, aff);
3891 isl_local_space_free(ls);
3892 isl_mat_free(mat);
3893 return ma;
3894 error:
3895 isl_local_space_free(ls);
3896 isl_mat_free(mat);
3897 isl_multi_aff_free(ma);
3898 return NULL;
3901 /* Remove any internal structure of the domain of "ma".
3902 * If there is any such internal structure in the input,
3903 * then the name of the corresponding space is also removed.
3905 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3906 __isl_take isl_multi_aff *ma)
3908 isl_space *space;
3910 if (!ma)
3911 return NULL;
3913 if (!ma->space->nested[0])
3914 return ma;
3916 space = isl_multi_aff_get_space(ma);
3917 space = isl_space_flatten_domain(space);
3918 ma = isl_multi_aff_reset_space(ma, space);
3920 return ma;
3923 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3924 * of the space to its domain.
3926 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3928 int i;
3929 isl_size n_in;
3930 isl_local_space *ls;
3931 isl_multi_aff *ma;
3933 if (!space)
3934 return NULL;
3935 if (!isl_space_is_map(space))
3936 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3937 "not a map space", goto error);
3939 n_in = isl_space_dim(space, isl_dim_in);
3940 if (n_in < 0)
3941 goto error;
3942 space = isl_space_domain_map(space);
3944 ma = isl_multi_aff_alloc(isl_space_copy(space));
3945 if (n_in == 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_in; ++i) {
3953 isl_aff *aff;
3955 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3956 isl_dim_set, 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_multi_aff that maps a wrapped copy
3967 * of the space to its range.
3969 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3971 int i;
3972 isl_size n_in, n_out;
3973 isl_local_space *ls;
3974 isl_multi_aff *ma;
3976 if (!space)
3977 return NULL;
3978 if (!isl_space_is_map(space))
3979 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3980 "not a map space", goto error);
3982 n_in = isl_space_dim(space, isl_dim_in);
3983 n_out = isl_space_dim(space, isl_dim_out);
3984 if (n_in < 0 || n_out < 0)
3985 goto error;
3986 space = isl_space_range_map(space);
3988 ma = isl_multi_aff_alloc(isl_space_copy(space));
3989 if (n_out == 0) {
3990 isl_space_free(space);
3991 return ma;
3994 space = isl_space_domain(space);
3995 ls = isl_local_space_from_space(space);
3996 for (i = 0; i < n_out; ++i) {
3997 isl_aff *aff;
3999 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4000 isl_dim_set, n_in + i);
4001 ma = isl_multi_aff_set_aff(ma, i, aff);
4003 isl_local_space_free(ls);
4004 return ma;
4005 error:
4006 isl_space_free(space);
4007 return NULL;
4010 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4011 * of the space to its range.
4013 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4014 __isl_take isl_space *space)
4016 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4019 /* Given the space of a set and a range of set dimensions,
4020 * construct an isl_multi_aff that projects out those dimensions.
4022 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4023 __isl_take isl_space *space, enum isl_dim_type type,
4024 unsigned first, unsigned n)
4026 int i;
4027 isl_size dim;
4028 isl_local_space *ls;
4029 isl_multi_aff *ma;
4031 if (!space)
4032 return NULL;
4033 if (!isl_space_is_set(space))
4034 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4035 "expecting set space", goto error);
4036 if (type != isl_dim_set)
4037 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4038 "only set dimensions can be projected out", goto error);
4039 if (isl_space_check_range(space, type, first, n) < 0)
4040 goto error;
4042 dim = isl_space_dim(space, isl_dim_set);
4043 if (dim < 0)
4044 goto error;
4046 space = isl_space_from_domain(space);
4047 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4049 if (dim == n)
4050 return isl_multi_aff_alloc(space);
4052 ma = isl_multi_aff_alloc(isl_space_copy(space));
4053 space = isl_space_domain(space);
4054 ls = isl_local_space_from_space(space);
4056 for (i = 0; i < first; ++i) {
4057 isl_aff *aff;
4059 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4060 isl_dim_set, i);
4061 ma = isl_multi_aff_set_aff(ma, i, aff);
4064 for (i = 0; i < dim - (first + n); ++i) {
4065 isl_aff *aff;
4067 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4068 isl_dim_set, first + n + i);
4069 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4072 isl_local_space_free(ls);
4073 return ma;
4074 error:
4075 isl_space_free(space);
4076 return NULL;
4079 /* Given the space of a set and a range of set dimensions,
4080 * construct an isl_pw_multi_aff that projects out those dimensions.
4082 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4083 __isl_take isl_space *space, enum isl_dim_type type,
4084 unsigned first, unsigned n)
4086 isl_multi_aff *ma;
4088 ma = isl_multi_aff_project_out_map(space, type, first, n);
4089 return isl_pw_multi_aff_from_multi_aff(ma);
4092 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4093 * domain.
4095 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4096 __isl_take isl_multi_aff *ma)
4098 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4099 return isl_pw_multi_aff_alloc(dom, ma);
4102 /* Create a piecewise multi-affine expression in the given space that maps each
4103 * input dimension to the corresponding output dimension.
4105 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4106 __isl_take isl_space *space)
4108 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4111 /* Exploit the equalities in "eq" to simplify the affine expressions.
4113 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4114 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4116 int i;
4118 maff = isl_multi_aff_cow(maff);
4119 if (!maff || !eq)
4120 goto error;
4122 for (i = 0; i < maff->n; ++i) {
4123 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4124 isl_basic_set_copy(eq));
4125 if (!maff->u.p[i])
4126 goto error;
4129 isl_basic_set_free(eq);
4130 return maff;
4131 error:
4132 isl_basic_set_free(eq);
4133 isl_multi_aff_free(maff);
4134 return NULL;
4137 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4138 isl_int f)
4140 int i;
4142 maff = isl_multi_aff_cow(maff);
4143 if (!maff)
4144 return NULL;
4146 for (i = 0; i < maff->n; ++i) {
4147 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4148 if (!maff->u.p[i])
4149 return isl_multi_aff_free(maff);
4152 return maff;
4155 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4156 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4158 maff1 = isl_multi_aff_add(maff1, maff2);
4159 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4160 return maff1;
4163 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4165 if (!maff)
4166 return -1;
4168 return 0;
4171 /* Return the set of domain elements where "ma1" is lexicographically
4172 * smaller than or equal to "ma2".
4174 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4175 __isl_take isl_multi_aff *ma2)
4177 return isl_multi_aff_lex_ge_set(ma2, ma1);
4180 /* Return the set of domain elements where "ma1" is lexicographically
4181 * smaller than "ma2".
4183 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4184 __isl_take isl_multi_aff *ma2)
4186 return isl_multi_aff_lex_gt_set(ma2, ma1);
4189 /* Return the set of domain elements where "ma1" and "ma2"
4190 * satisfy "order".
4192 static __isl_give isl_set *isl_multi_aff_order_set(
4193 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4194 __isl_give isl_map *order(__isl_take isl_space *set_space))
4196 isl_space *space;
4197 isl_map *map1, *map2;
4198 isl_map *map, *ge;
4200 map1 = isl_map_from_multi_aff_internal(ma1);
4201 map2 = isl_map_from_multi_aff_internal(ma2);
4202 map = isl_map_range_product(map1, map2);
4203 space = isl_space_range(isl_map_get_space(map));
4204 space = isl_space_domain(isl_space_unwrap(space));
4205 ge = order(space);
4206 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4208 return isl_map_domain(map);
4211 /* Return the set of domain elements where "ma1" is lexicographically
4212 * greater than or equal to "ma2".
4214 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4215 __isl_take isl_multi_aff *ma2)
4217 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4220 /* Return the set of domain elements where "ma1" is lexicographically
4221 * greater than "ma2".
4223 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4224 __isl_take isl_multi_aff *ma2)
4226 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4229 #undef PW
4230 #define PW isl_pw_multi_aff
4231 #undef EL
4232 #define EL isl_multi_aff
4233 #undef EL_IS_ZERO
4234 #define EL_IS_ZERO is_empty
4235 #undef ZERO
4236 #define ZERO empty
4237 #undef IS_ZERO
4238 #define IS_ZERO is_empty
4239 #undef FIELD
4240 #define FIELD maff
4241 #undef DEFAULT_IS_ZERO
4242 #define DEFAULT_IS_ZERO 0
4244 #define NO_SUB
4245 #define NO_OPT
4246 #define NO_INSERT_DIMS
4247 #define NO_LIFT
4248 #define NO_MORPH
4250 #include <isl_pw_templ.c>
4251 #include <isl_pw_union_opt.c>
4253 #undef NO_SUB
4255 #undef BASE
4256 #define BASE pw_multi_aff
4258 #include <isl_union_multi.c>
4259 #include <isl_union_neg.c>
4261 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4262 __isl_take isl_pw_multi_aff *pma1,
4263 __isl_take isl_pw_multi_aff *pma2)
4265 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4266 &isl_multi_aff_lex_ge_set);
4269 /* Given two piecewise multi affine expressions, return a piecewise
4270 * multi-affine expression defined on the union of the definition domains
4271 * of the inputs that is equal to the lexicographic maximum of the two
4272 * inputs on each cell. If only one of the two inputs is defined on
4273 * a given cell, then it is considered to be the maximum.
4275 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4276 __isl_take isl_pw_multi_aff *pma1,
4277 __isl_take isl_pw_multi_aff *pma2)
4279 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4280 &pw_multi_aff_union_lexmax);
4283 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4284 __isl_take isl_pw_multi_aff *pma1,
4285 __isl_take isl_pw_multi_aff *pma2)
4287 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4288 &isl_multi_aff_lex_le_set);
4291 /* Given two piecewise multi affine expressions, return a piecewise
4292 * multi-affine expression defined on the union of the definition domains
4293 * of the inputs that is equal to the lexicographic minimum of the two
4294 * inputs on each cell. If only one of the two inputs is defined on
4295 * a given cell, then it is considered to be the minimum.
4297 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4298 __isl_take isl_pw_multi_aff *pma1,
4299 __isl_take isl_pw_multi_aff *pma2)
4301 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4302 &pw_multi_aff_union_lexmin);
4305 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4306 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4308 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4309 &isl_multi_aff_add);
4312 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4313 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4315 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4316 &pw_multi_aff_add);
4319 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4320 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4322 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4323 &isl_multi_aff_sub);
4326 /* Subtract "pma2" from "pma1" and return the result.
4328 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4329 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4331 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4332 &pw_multi_aff_sub);
4335 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4336 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4338 return isl_pw_multi_aff_union_add_(pma1, pma2);
4341 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4342 * with the actual sum on the shared domain and
4343 * the defined expression on the symmetric difference of the domains.
4345 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4346 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4348 return isl_union_pw_aff_union_add_(upa1, upa2);
4351 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4352 * with the actual sum on the shared domain and
4353 * the defined expression on the symmetric difference of the domains.
4355 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4356 __isl_take isl_union_pw_multi_aff *upma1,
4357 __isl_take isl_union_pw_multi_aff *upma2)
4359 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4362 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4363 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4365 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4366 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4368 int i, j, n;
4369 isl_space *space;
4370 isl_pw_multi_aff *res;
4372 if (!pma1 || !pma2)
4373 goto error;
4375 n = pma1->n * pma2->n;
4376 space = isl_space_product(isl_space_copy(pma1->dim),
4377 isl_space_copy(pma2->dim));
4378 res = isl_pw_multi_aff_alloc_size(space, n);
4380 for (i = 0; i < pma1->n; ++i) {
4381 for (j = 0; j < pma2->n; ++j) {
4382 isl_set *domain;
4383 isl_multi_aff *ma;
4385 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4386 isl_set_copy(pma2->p[j].set));
4387 ma = isl_multi_aff_product(
4388 isl_multi_aff_copy(pma1->p[i].maff),
4389 isl_multi_aff_copy(pma2->p[j].maff));
4390 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4394 isl_pw_multi_aff_free(pma1);
4395 isl_pw_multi_aff_free(pma2);
4396 return res;
4397 error:
4398 isl_pw_multi_aff_free(pma1);
4399 isl_pw_multi_aff_free(pma2);
4400 return NULL;
4403 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4404 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4406 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4407 &pw_multi_aff_product);
4410 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4411 * denominator "denom".
4412 * "denom" is allowed to be negative, in which case the actual denominator
4413 * is -denom and the expressions are added instead.
4415 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4416 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4418 int i, first;
4419 int sign;
4420 isl_int d;
4422 first = isl_seq_first_non_zero(c, n);
4423 if (first == -1)
4424 return aff;
4426 sign = isl_int_sgn(denom);
4427 isl_int_init(d);
4428 isl_int_abs(d, denom);
4429 for (i = first; i < n; ++i) {
4430 isl_aff *aff_i;
4432 if (isl_int_is_zero(c[i]))
4433 continue;
4434 aff_i = isl_multi_aff_get_aff(ma, i);
4435 aff_i = isl_aff_scale(aff_i, c[i]);
4436 aff_i = isl_aff_scale_down(aff_i, d);
4437 if (sign >= 0)
4438 aff = isl_aff_sub(aff, aff_i);
4439 else
4440 aff = isl_aff_add(aff, aff_i);
4442 isl_int_clear(d);
4444 return aff;
4447 /* Extract an affine expression that expresses the output dimension "pos"
4448 * of "bmap" in terms of the parameters and input dimensions from
4449 * equality "eq".
4450 * Note that this expression may involve integer divisions defined
4451 * in terms of parameters and input dimensions.
4452 * The equality may also involve references to earlier (but not later)
4453 * output dimensions. These are replaced by the corresponding elements
4454 * in "ma".
4456 * If the equality is of the form
4458 * f(i) + h(j) + a x + g(i) = 0,
4460 * with f(i) a linear combinations of the parameters and input dimensions,
4461 * g(i) a linear combination of integer divisions defined in terms of the same
4462 * and h(j) a linear combinations of earlier output dimensions,
4463 * then the affine expression is
4465 * (-f(i) - g(i))/a - h(j)/a
4467 * If the equality is of the form
4469 * f(i) + h(j) - a x + g(i) = 0,
4471 * then the affine expression is
4473 * (f(i) + g(i))/a - h(j)/(-a)
4476 * If "div" refers to an integer division (i.e., it is smaller than
4477 * the number of integer divisions), then the equality constraint
4478 * does involve an integer division (the one at position "div") that
4479 * is defined in terms of output dimensions. However, this integer
4480 * division can be eliminated by exploiting a pair of constraints
4481 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4482 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4483 * -l + x >= 0.
4484 * In particular, let
4486 * x = e(i) + m floor(...)
4488 * with e(i) the expression derived above and floor(...) the integer
4489 * division involving output dimensions.
4490 * From
4492 * l <= x <= l + n,
4494 * we have
4496 * 0 <= x - l <= n
4498 * This means
4500 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4501 * = (e(i) - l) mod m
4503 * Therefore,
4505 * x - l = (e(i) - l) mod m
4507 * or
4509 * x = ((e(i) - l) mod m) + l
4511 * The variable "shift" below contains the expression -l, which may
4512 * also involve a linear combination of earlier output dimensions.
4514 static __isl_give isl_aff *extract_aff_from_equality(
4515 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4516 __isl_keep isl_multi_aff *ma)
4518 unsigned o_out;
4519 isl_size n_div, n_out;
4520 isl_ctx *ctx;
4521 isl_local_space *ls;
4522 isl_aff *aff, *shift;
4523 isl_val *mod;
4525 ctx = isl_basic_map_get_ctx(bmap);
4526 ls = isl_basic_map_get_local_space(bmap);
4527 ls = isl_local_space_domain(ls);
4528 aff = isl_aff_alloc(isl_local_space_copy(ls));
4529 if (!aff)
4530 goto error;
4531 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4532 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4533 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4534 if (n_out < 0 || n_div < 0)
4535 goto error;
4536 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4537 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4538 isl_seq_cpy(aff->v->el + 1 + o_out,
4539 bmap->eq[eq] + o_out + n_out, n_div);
4540 } else {
4541 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4542 isl_seq_neg(aff->v->el + 1 + o_out,
4543 bmap->eq[eq] + o_out + n_out, n_div);
4545 if (div < n_div)
4546 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4547 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4548 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4549 bmap->eq[eq][o_out + pos]);
4550 if (div < n_div) {
4551 shift = isl_aff_alloc(isl_local_space_copy(ls));
4552 if (!shift)
4553 goto error;
4554 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4555 isl_seq_cpy(shift->v->el + 1 + o_out,
4556 bmap->ineq[ineq] + o_out + n_out, n_div);
4557 isl_int_set_si(shift->v->el[0], 1);
4558 shift = subtract_initial(shift, ma, pos,
4559 bmap->ineq[ineq] + o_out, ctx->negone);
4560 aff = isl_aff_add(aff, isl_aff_copy(shift));
4561 mod = isl_val_int_from_isl_int(ctx,
4562 bmap->eq[eq][o_out + n_out + div]);
4563 mod = isl_val_abs(mod);
4564 aff = isl_aff_mod_val(aff, mod);
4565 aff = isl_aff_sub(aff, shift);
4568 isl_local_space_free(ls);
4569 return aff;
4570 error:
4571 isl_local_space_free(ls);
4572 isl_aff_free(aff);
4573 return NULL;
4576 /* Given a basic map with output dimensions defined
4577 * in terms of the parameters input dimensions and earlier
4578 * output dimensions using an equality (and possibly a pair on inequalities),
4579 * extract an isl_aff that expresses output dimension "pos" in terms
4580 * of the parameters and input dimensions.
4581 * Note that this expression may involve integer divisions defined
4582 * in terms of parameters and input dimensions.
4583 * "ma" contains the expressions corresponding to earlier output dimensions.
4585 * This function shares some similarities with
4586 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4588 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4589 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4591 int eq, div, ineq;
4592 isl_aff *aff;
4594 if (!bmap)
4595 return NULL;
4596 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4597 if (eq >= bmap->n_eq)
4598 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4599 "unable to find suitable equality", return NULL);
4600 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4602 aff = isl_aff_remove_unused_divs(aff);
4603 return aff;
4606 /* Given a basic map where each output dimension is defined
4607 * in terms of the parameters and input dimensions using an equality,
4608 * extract an isl_multi_aff that expresses the output dimensions in terms
4609 * of the parameters and input dimensions.
4611 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4612 __isl_take isl_basic_map *bmap)
4614 int i;
4615 isl_size n_out;
4616 isl_multi_aff *ma;
4618 if (!bmap)
4619 return NULL;
4621 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4622 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4623 if (n_out < 0)
4624 ma = isl_multi_aff_free(ma);
4626 for (i = 0; i < n_out; ++i) {
4627 isl_aff *aff;
4629 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4630 ma = isl_multi_aff_set_aff(ma, i, aff);
4633 isl_basic_map_free(bmap);
4635 return ma;
4638 /* Given a basic set where each set dimension is defined
4639 * in terms of the parameters using an equality,
4640 * extract an isl_multi_aff that expresses the set dimensions in terms
4641 * of the parameters.
4643 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4644 __isl_take isl_basic_set *bset)
4646 return extract_isl_multi_aff_from_basic_map(bset);
4649 /* Create an isl_pw_multi_aff that is equivalent to
4650 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4651 * The given basic map is such that each output dimension is defined
4652 * in terms of the parameters and input dimensions using an equality.
4654 * Since some applications expect the result of isl_pw_multi_aff_from_map
4655 * to only contain integer affine expressions, we compute the floor
4656 * of the expression before returning.
4658 * Remove all constraints involving local variables without
4659 * an explicit representation (resulting in the removal of those
4660 * local variables) prior to the actual extraction to ensure
4661 * that the local spaces in which the resulting affine expressions
4662 * are created do not contain any unknown local variables.
4663 * Removing such constraints is safe because constraints involving
4664 * unknown local variables are not used to determine whether
4665 * a basic map is obviously single-valued.
4667 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4668 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4670 isl_multi_aff *ma;
4672 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4673 ma = extract_isl_multi_aff_from_basic_map(bmap);
4674 ma = isl_multi_aff_floor(ma);
4675 return isl_pw_multi_aff_alloc(domain, ma);
4678 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4679 * This obviously only works if the input "map" is single-valued.
4680 * If so, we compute the lexicographic minimum of the image in the form
4681 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4682 * to its lexicographic minimum.
4683 * If the input is not single-valued, we produce an error.
4685 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4686 __isl_take isl_map *map)
4688 int i;
4689 int sv;
4690 isl_pw_multi_aff *pma;
4692 sv = isl_map_is_single_valued(map);
4693 if (sv < 0)
4694 goto error;
4695 if (!sv)
4696 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4697 "map is not single-valued", goto error);
4698 map = isl_map_make_disjoint(map);
4699 if (!map)
4700 return NULL;
4702 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4704 for (i = 0; i < map->n; ++i) {
4705 isl_pw_multi_aff *pma_i;
4706 isl_basic_map *bmap;
4707 bmap = isl_basic_map_copy(map->p[i]);
4708 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4709 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4712 isl_map_free(map);
4713 return pma;
4714 error:
4715 isl_map_free(map);
4716 return NULL;
4719 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4720 * taking into account that the output dimension at position "d"
4721 * can be represented as
4723 * x = floor((e(...) + c1) / m)
4725 * given that constraint "i" is of the form
4727 * e(...) + c1 - m x >= 0
4730 * Let "map" be of the form
4732 * A -> B
4734 * We construct a mapping
4736 * A -> [A -> x = floor(...)]
4738 * apply that to the map, obtaining
4740 * [A -> x = floor(...)] -> B
4742 * and equate dimension "d" to x.
4743 * We then compute a isl_pw_multi_aff representation of the resulting map
4744 * and plug in the mapping above.
4746 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4747 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4749 isl_ctx *ctx;
4750 isl_space *space = NULL;
4751 isl_local_space *ls;
4752 isl_multi_aff *ma;
4753 isl_aff *aff;
4754 isl_vec *v;
4755 isl_map *insert;
4756 int offset;
4757 isl_size n;
4758 isl_size n_in;
4759 isl_pw_multi_aff *pma;
4760 isl_bool is_set;
4762 is_set = isl_map_is_set(map);
4763 if (is_set < 0)
4764 goto error;
4766 offset = isl_basic_map_offset(hull, isl_dim_out);
4767 ctx = isl_map_get_ctx(map);
4768 space = isl_space_domain(isl_map_get_space(map));
4769 n_in = isl_space_dim(space, isl_dim_set);
4770 n = isl_space_dim(space, isl_dim_all);
4771 if (n_in < 0 || n < 0)
4772 goto error;
4774 v = isl_vec_alloc(ctx, 1 + 1 + n);
4775 if (v) {
4776 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4777 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4779 isl_basic_map_free(hull);
4781 ls = isl_local_space_from_space(isl_space_copy(space));
4782 aff = isl_aff_alloc_vec(ls, v);
4783 aff = isl_aff_floor(aff);
4784 if (is_set) {
4785 isl_space_free(space);
4786 ma = isl_multi_aff_from_aff(aff);
4787 } else {
4788 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4789 ma = isl_multi_aff_range_product(ma,
4790 isl_multi_aff_from_aff(aff));
4793 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4794 map = isl_map_apply_domain(map, insert);
4795 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4796 pma = isl_pw_multi_aff_from_map(map);
4797 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4799 return pma;
4800 error:
4801 isl_space_free(space);
4802 isl_map_free(map);
4803 isl_basic_map_free(hull);
4804 return NULL;
4807 /* Is constraint "c" of the form
4809 * e(...) + c1 - m x >= 0
4811 * or
4813 * -e(...) + c2 + m x >= 0
4815 * where m > 1 and e only depends on parameters and input dimemnsions?
4817 * "offset" is the offset of the output dimensions
4818 * "pos" is the position of output dimension x.
4820 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4822 if (isl_int_is_zero(c[offset + d]))
4823 return 0;
4824 if (isl_int_is_one(c[offset + d]))
4825 return 0;
4826 if (isl_int_is_negone(c[offset + d]))
4827 return 0;
4828 if (isl_seq_first_non_zero(c + offset, d) != -1)
4829 return 0;
4830 if (isl_seq_first_non_zero(c + offset + d + 1,
4831 total - (offset + d + 1)) != -1)
4832 return 0;
4833 return 1;
4836 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4838 * As a special case, we first check if there is any pair of constraints,
4839 * shared by all the basic maps in "map" that force a given dimension
4840 * to be equal to the floor of some affine combination of the input dimensions.
4842 * In particular, if we can find two constraints
4844 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4846 * and
4848 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4850 * where m > 1 and e only depends on parameters and input dimemnsions,
4851 * and such that
4853 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4855 * then we know that we can take
4857 * x = floor((e(...) + c1) / m)
4859 * without having to perform any computation.
4861 * Note that we know that
4863 * c1 + c2 >= 1
4865 * If c1 + c2 were 0, then we would have detected an equality during
4866 * simplification. If c1 + c2 were negative, then we would have detected
4867 * a contradiction.
4869 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4870 __isl_take isl_map *map)
4872 int d;
4873 isl_size dim;
4874 int i, j, n;
4875 int offset;
4876 isl_size total;
4877 isl_int sum;
4878 isl_basic_map *hull;
4880 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4881 dim = isl_map_dim(map, isl_dim_out);
4882 total = isl_basic_map_dim(hull, isl_dim_all);
4883 if (dim < 0 || total < 0)
4884 goto error;
4886 isl_int_init(sum);
4887 offset = isl_basic_map_offset(hull, isl_dim_out);
4888 n = hull->n_ineq;
4889 for (d = 0; d < dim; ++d) {
4890 for (i = 0; i < n; ++i) {
4891 if (!is_potential_div_constraint(hull->ineq[i],
4892 offset, d, 1 + total))
4893 continue;
4894 for (j = i + 1; j < n; ++j) {
4895 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4896 hull->ineq[j] + 1, total))
4897 continue;
4898 isl_int_add(sum, hull->ineq[i][0],
4899 hull->ineq[j][0]);
4900 if (isl_int_abs_lt(sum,
4901 hull->ineq[i][offset + d]))
4902 break;
4905 if (j >= n)
4906 continue;
4907 isl_int_clear(sum);
4908 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4909 j = i;
4910 return pw_multi_aff_from_map_div(map, hull, d, j);
4913 isl_int_clear(sum);
4914 isl_basic_map_free(hull);
4915 return pw_multi_aff_from_map_base(map);
4916 error:
4917 isl_map_free(map);
4918 isl_basic_map_free(hull);
4919 return NULL;
4922 /* Given an affine expression
4924 * [A -> B] -> f(A,B)
4926 * construct an isl_multi_aff
4928 * [A -> B] -> B'
4930 * such that dimension "d" in B' is set to "aff" and the remaining
4931 * dimensions are set equal to the corresponding dimensions in B.
4932 * "n_in" is the dimension of the space A.
4933 * "n_out" is the dimension of the space B.
4935 * If "is_set" is set, then the affine expression is of the form
4937 * [B] -> f(B)
4939 * and we construct an isl_multi_aff
4941 * B -> B'
4943 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4944 unsigned n_in, unsigned n_out, int is_set)
4946 int i;
4947 isl_multi_aff *ma;
4948 isl_space *space, *space2;
4949 isl_local_space *ls;
4951 space = isl_aff_get_domain_space(aff);
4952 ls = isl_local_space_from_space(isl_space_copy(space));
4953 space2 = isl_space_copy(space);
4954 if (!is_set)
4955 space2 = isl_space_range(isl_space_unwrap(space2));
4956 space = isl_space_map_from_domain_and_range(space, space2);
4957 ma = isl_multi_aff_alloc(space);
4958 ma = isl_multi_aff_set_aff(ma, d, aff);
4960 for (i = 0; i < n_out; ++i) {
4961 if (i == d)
4962 continue;
4963 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4964 isl_dim_set, n_in + i);
4965 ma = isl_multi_aff_set_aff(ma, i, aff);
4968 isl_local_space_free(ls);
4970 return ma;
4973 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4974 * taking into account that the dimension at position "d" can be written as
4976 * x = m a + f(..) (1)
4978 * where m is equal to "gcd".
4979 * "i" is the index of the equality in "hull" that defines f(..).
4980 * In particular, the equality is of the form
4982 * f(..) - x + m g(existentials) = 0
4984 * or
4986 * -f(..) + x + m g(existentials) = 0
4988 * We basically plug (1) into "map", resulting in a map with "a"
4989 * in the range instead of "x". The corresponding isl_pw_multi_aff
4990 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4992 * Specifically, given the input map
4994 * A -> B
4996 * We first wrap it into a set
4998 * [A -> B]
5000 * and define (1) on top of the corresponding space, resulting in "aff".
5001 * We use this to create an isl_multi_aff that maps the output position "d"
5002 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5003 * We plug this into the wrapped map, unwrap the result and compute the
5004 * corresponding isl_pw_multi_aff.
5005 * The result is an expression
5007 * A -> T(A)
5009 * We adjust that to
5011 * A -> [A -> T(A)]
5013 * so that we can plug that into "aff", after extending the latter to
5014 * a mapping
5016 * [A -> B] -> B'
5019 * If "map" is actually a set, then there is no "A" space, meaning
5020 * that we do not need to perform any wrapping, and that the result
5021 * of the recursive call is of the form
5023 * [T]
5025 * which is plugged into a mapping of the form
5027 * B -> B'
5029 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5030 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5031 isl_int gcd)
5033 isl_set *set;
5034 isl_space *space;
5035 isl_local_space *ls;
5036 isl_aff *aff;
5037 isl_multi_aff *ma;
5038 isl_pw_multi_aff *pma, *id;
5039 isl_size n_in;
5040 unsigned o_out;
5041 isl_size n_out;
5042 isl_bool is_set;
5044 is_set = isl_map_is_set(map);
5045 if (is_set < 0)
5046 goto error;
5048 n_in = isl_basic_map_dim(hull, isl_dim_in);
5049 n_out = isl_basic_map_dim(hull, isl_dim_out);
5050 if (n_in < 0 || n_out < 0)
5051 goto error;
5052 o_out = isl_basic_map_offset(hull, isl_dim_out);
5054 if (is_set)
5055 set = map;
5056 else
5057 set = isl_map_wrap(map);
5058 space = isl_space_map_from_set(isl_set_get_space(set));
5059 ma = isl_multi_aff_identity(space);
5060 ls = isl_local_space_from_space(isl_set_get_space(set));
5061 aff = isl_aff_alloc(ls);
5062 if (aff) {
5063 isl_int_set_si(aff->v->el[0], 1);
5064 if (isl_int_is_one(hull->eq[i][o_out + d]))
5065 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5066 aff->v->size - 1);
5067 else
5068 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5069 aff->v->size - 1);
5070 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5072 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5073 set = isl_set_preimage_multi_aff(set, ma);
5075 ma = range_map(aff, d, n_in, n_out, is_set);
5077 if (is_set)
5078 map = set;
5079 else
5080 map = isl_set_unwrap(set);
5081 pma = isl_pw_multi_aff_from_map(map);
5083 if (!is_set) {
5084 space = isl_pw_multi_aff_get_domain_space(pma);
5085 space = isl_space_map_from_set(space);
5086 id = isl_pw_multi_aff_identity(space);
5087 pma = isl_pw_multi_aff_range_product(id, pma);
5089 id = isl_pw_multi_aff_from_multi_aff(ma);
5090 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5092 isl_basic_map_free(hull);
5093 return pma;
5094 error:
5095 isl_map_free(map);
5096 isl_basic_map_free(hull);
5097 return NULL;
5100 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5101 * "hull" contains the equalities valid for "map".
5103 * Check if any of the output dimensions is "strided".
5104 * That is, we check if it can be written as
5106 * x = m a + f(..)
5108 * with m greater than 1, a some combination of existentially quantified
5109 * variables and f an expression in the parameters and input dimensions.
5110 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5112 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5113 * special case.
5115 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5116 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5118 int i, j;
5119 isl_size n_out;
5120 unsigned o_out;
5121 isl_size n_div;
5122 unsigned o_div;
5123 isl_int gcd;
5125 n_div = isl_basic_map_dim(hull, isl_dim_div);
5126 n_out = isl_basic_map_dim(hull, isl_dim_out);
5127 if (n_div < 0 || n_out < 0)
5128 goto error;
5130 if (n_div == 0) {
5131 isl_basic_map_free(hull);
5132 return pw_multi_aff_from_map_check_div(map);
5135 isl_int_init(gcd);
5137 o_div = isl_basic_map_offset(hull, isl_dim_div);
5138 o_out = isl_basic_map_offset(hull, isl_dim_out);
5140 for (i = 0; i < n_out; ++i) {
5141 for (j = 0; j < hull->n_eq; ++j) {
5142 isl_int *eq = hull->eq[j];
5143 isl_pw_multi_aff *res;
5145 if (!isl_int_is_one(eq[o_out + i]) &&
5146 !isl_int_is_negone(eq[o_out + i]))
5147 continue;
5148 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5149 continue;
5150 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5151 n_out - (i + 1)) != -1)
5152 continue;
5153 isl_seq_gcd(eq + o_div, n_div, &gcd);
5154 if (isl_int_is_zero(gcd))
5155 continue;
5156 if (isl_int_is_one(gcd))
5157 continue;
5159 res = pw_multi_aff_from_map_stride(map, hull,
5160 i, j, gcd);
5161 isl_int_clear(gcd);
5162 return res;
5166 isl_int_clear(gcd);
5167 isl_basic_map_free(hull);
5168 return pw_multi_aff_from_map_check_div(map);
5169 error:
5170 isl_map_free(map);
5171 isl_basic_map_free(hull);
5172 return NULL;
5175 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5177 * As a special case, we first check if all output dimensions are uniquely
5178 * defined in terms of the parameters and input dimensions over the entire
5179 * domain. If so, we extract the desired isl_pw_multi_aff directly
5180 * from the affine hull of "map" and its domain.
5182 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5183 * special cases.
5185 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5187 isl_bool sv;
5188 isl_size n;
5189 isl_basic_map *hull;
5191 n = isl_map_n_basic_map(map);
5192 if (n < 0)
5193 goto error;
5195 if (n == 1) {
5196 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5197 hull = isl_basic_map_plain_affine_hull(hull);
5198 sv = isl_basic_map_plain_is_single_valued(hull);
5199 if (sv >= 0 && sv)
5200 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5201 hull);
5202 isl_basic_map_free(hull);
5204 map = isl_map_detect_equalities(map);
5205 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5206 sv = isl_basic_map_plain_is_single_valued(hull);
5207 if (sv >= 0 && sv)
5208 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5209 if (sv >= 0)
5210 return pw_multi_aff_from_map_check_strides(map, hull);
5211 isl_basic_map_free(hull);
5212 error:
5213 isl_map_free(map);
5214 return NULL;
5217 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5219 return isl_pw_multi_aff_from_map(set);
5222 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5223 * add it to *user.
5225 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5227 isl_union_pw_multi_aff **upma = user;
5228 isl_pw_multi_aff *pma;
5230 pma = isl_pw_multi_aff_from_map(map);
5231 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5233 return *upma ? isl_stat_ok : isl_stat_error;
5236 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5237 * domain.
5239 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5240 __isl_take isl_aff *aff)
5242 isl_multi_aff *ma;
5243 isl_pw_multi_aff *pma;
5245 ma = isl_multi_aff_from_aff(aff);
5246 pma = isl_pw_multi_aff_from_multi_aff(ma);
5247 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5250 /* Try and create an isl_union_pw_multi_aff that is equivalent
5251 * to the given isl_union_map.
5252 * The isl_union_map is required to be single-valued in each space.
5253 * Otherwise, an error is produced.
5255 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5256 __isl_take isl_union_map *umap)
5258 isl_space *space;
5259 isl_union_pw_multi_aff *upma;
5261 space = isl_union_map_get_space(umap);
5262 upma = isl_union_pw_multi_aff_empty(space);
5263 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5264 upma = isl_union_pw_multi_aff_free(upma);
5265 isl_union_map_free(umap);
5267 return upma;
5270 /* Try and create an isl_union_pw_multi_aff that is equivalent
5271 * to the given isl_union_set.
5272 * The isl_union_set is required to be a singleton in each space.
5273 * Otherwise, an error is produced.
5275 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5276 __isl_take isl_union_set *uset)
5278 return isl_union_pw_multi_aff_from_union_map(uset);
5281 /* Return the piecewise affine expression "set ? 1 : 0".
5283 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5285 isl_pw_aff *pa;
5286 isl_space *space = isl_set_get_space(set);
5287 isl_local_space *ls = isl_local_space_from_space(space);
5288 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5289 isl_aff *one = isl_aff_zero_on_domain(ls);
5291 one = isl_aff_add_constant_si(one, 1);
5292 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5293 set = isl_set_complement(set);
5294 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5296 return pa;
5299 /* Plug in "subs" for dimension "type", "pos" of "aff".
5301 * Let i be the dimension to replace and let "subs" be of the form
5303 * f/d
5305 * and "aff" of the form
5307 * (a i + g)/m
5309 * The result is
5311 * (a f + d g')/(m d)
5313 * where g' is the result of plugging in "subs" in each of the integer
5314 * divisions in g.
5316 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5317 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5319 isl_ctx *ctx;
5320 isl_int v;
5321 isl_size n_div;
5323 aff = isl_aff_cow(aff);
5324 if (!aff || !subs)
5325 return isl_aff_free(aff);
5327 ctx = isl_aff_get_ctx(aff);
5328 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5329 isl_die(ctx, isl_error_invalid,
5330 "spaces don't match", return isl_aff_free(aff));
5331 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
5332 if (n_div < 0)
5333 return isl_aff_free(aff);
5334 if (n_div != 0)
5335 isl_die(ctx, isl_error_unsupported,
5336 "cannot handle divs yet", return isl_aff_free(aff));
5338 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5339 if (!aff->ls)
5340 return isl_aff_free(aff);
5342 aff->v = isl_vec_cow(aff->v);
5343 if (!aff->v)
5344 return isl_aff_free(aff);
5346 pos += isl_local_space_offset(aff->ls, type);
5348 isl_int_init(v);
5349 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5350 aff->v->size, subs->v->size, v);
5351 isl_int_clear(v);
5353 return aff;
5356 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5357 * expressions in "maff".
5359 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5360 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5361 __isl_keep isl_aff *subs)
5363 int i;
5365 maff = isl_multi_aff_cow(maff);
5366 if (!maff || !subs)
5367 return isl_multi_aff_free(maff);
5369 if (type == isl_dim_in)
5370 type = isl_dim_set;
5372 for (i = 0; i < maff->n; ++i) {
5373 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5374 type, pos, subs);
5375 if (!maff->u.p[i])
5376 return isl_multi_aff_free(maff);
5379 return maff;
5382 /* Plug in "subs" for dimension "type", "pos" of "pma".
5384 * pma is of the form
5386 * A_i(v) -> M_i(v)
5388 * while subs is of the form
5390 * v' = B_j(v) -> S_j
5392 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5393 * has a contribution in the result, in particular
5395 * C_ij(S_j) -> M_i(S_j)
5397 * Note that plugging in S_j in C_ij may also result in an empty set
5398 * and this contribution should simply be discarded.
5400 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5401 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5402 __isl_keep isl_pw_aff *subs)
5404 int i, j, n;
5405 isl_pw_multi_aff *res;
5407 if (!pma || !subs)
5408 return isl_pw_multi_aff_free(pma);
5410 n = pma->n * subs->n;
5411 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5413 for (i = 0; i < pma->n; ++i) {
5414 for (j = 0; j < subs->n; ++j) {
5415 isl_set *common;
5416 isl_multi_aff *res_ij;
5417 int empty;
5419 common = isl_set_intersect(
5420 isl_set_copy(pma->p[i].set),
5421 isl_set_copy(subs->p[j].set));
5422 common = isl_set_substitute(common,
5423 type, pos, subs->p[j].aff);
5424 empty = isl_set_plain_is_empty(common);
5425 if (empty < 0 || empty) {
5426 isl_set_free(common);
5427 if (empty < 0)
5428 goto error;
5429 continue;
5432 res_ij = isl_multi_aff_substitute(
5433 isl_multi_aff_copy(pma->p[i].maff),
5434 type, pos, subs->p[j].aff);
5436 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5440 isl_pw_multi_aff_free(pma);
5441 return res;
5442 error:
5443 isl_pw_multi_aff_free(pma);
5444 isl_pw_multi_aff_free(res);
5445 return NULL;
5448 /* Compute the preimage of a range of dimensions in the affine expression "src"
5449 * under "ma" and put the result in "dst". The number of dimensions in "src"
5450 * that precede the range is given by "n_before". The number of dimensions
5451 * in the range is given by the number of output dimensions of "ma".
5452 * The number of dimensions that follow the range is given by "n_after".
5453 * If "has_denom" is set (to one),
5454 * then "src" and "dst" have an extra initial denominator.
5455 * "n_div_ma" is the number of existentials in "ma"
5456 * "n_div_bset" is the number of existentials in "src"
5457 * The resulting "dst" (which is assumed to have been allocated by
5458 * the caller) contains coefficients for both sets of existentials,
5459 * first those in "ma" and then those in "src".
5460 * f, c1, c2 and g are temporary objects that have been initialized
5461 * by the caller.
5463 * Let src represent the expression
5465 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5467 * and let ma represent the expressions
5469 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5471 * We start out with the following expression for dst:
5473 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5475 * with the multiplication factor f initially equal to 1
5476 * and f \sum_i b_i v_i kept separately.
5477 * For each x_i that we substitute, we multiply the numerator
5478 * (and denominator) of dst by c_1 = m_i and add the numerator
5479 * of the x_i expression multiplied by c_2 = f b_i,
5480 * after removing the common factors of c_1 and c_2.
5481 * The multiplication factor f also needs to be multiplied by c_1
5482 * for the next x_j, j > i.
5484 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5485 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5486 int n_div_ma, int n_div_bmap,
5487 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5489 int i;
5490 isl_size n_param, n_in, n_out;
5491 int o_dst, o_src;
5493 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5494 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5495 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5496 if (n_param < 0 || n_in < 0 || n_out < 0)
5497 return isl_stat_error;
5499 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5500 o_dst = o_src = has_denom + 1 + n_param + n_before;
5501 isl_seq_clr(dst + o_dst, n_in);
5502 o_dst += n_in;
5503 o_src += n_out;
5504 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5505 o_dst += n_after;
5506 o_src += n_after;
5507 isl_seq_clr(dst + o_dst, n_div_ma);
5508 o_dst += n_div_ma;
5509 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5511 isl_int_set_si(f, 1);
5513 for (i = 0; i < n_out; ++i) {
5514 int offset = has_denom + 1 + n_param + n_before + i;
5516 if (isl_int_is_zero(src[offset]))
5517 continue;
5518 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5519 isl_int_mul(c2, f, src[offset]);
5520 isl_int_gcd(g, c1, c2);
5521 isl_int_divexact(c1, c1, g);
5522 isl_int_divexact(c2, c2, g);
5524 isl_int_mul(f, f, c1);
5525 o_dst = has_denom;
5526 o_src = 1;
5527 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5528 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5529 o_dst += 1 + n_param;
5530 o_src += 1 + n_param;
5531 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5532 o_dst += n_before;
5533 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5534 c2, ma->u.p[i]->v->el + o_src, n_in);
5535 o_dst += n_in;
5536 o_src += n_in;
5537 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5538 o_dst += n_after;
5539 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5540 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5541 o_dst += n_div_ma;
5542 o_src += n_div_ma;
5543 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5544 if (has_denom)
5545 isl_int_mul(dst[0], dst[0], c1);
5548 return isl_stat_ok;
5551 /* Compute the pullback of "aff" by the function represented by "ma".
5552 * In other words, plug in "ma" in "aff". The result is an affine expression
5553 * defined over the domain space of "ma".
5555 * If "aff" is represented by
5557 * (a(p) + b x + c(divs))/d
5559 * and ma is represented by
5561 * x = D(p) + F(y) + G(divs')
5563 * then the result is
5565 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5567 * The divs in the local space of the input are similarly adjusted
5568 * through a call to isl_local_space_preimage_multi_aff.
5570 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5571 __isl_take isl_multi_aff *ma)
5573 isl_aff *res = NULL;
5574 isl_local_space *ls;
5575 isl_size n_div_aff, n_div_ma;
5576 isl_int f, c1, c2, g;
5578 ma = isl_multi_aff_align_divs(ma);
5579 if (!aff || !ma)
5580 goto error;
5582 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5583 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5584 if (n_div_aff < 0 || n_div_ma < 0)
5585 goto error;
5587 ls = isl_aff_get_domain_local_space(aff);
5588 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5589 res = isl_aff_alloc(ls);
5590 if (!res)
5591 goto error;
5593 isl_int_init(f);
5594 isl_int_init(c1);
5595 isl_int_init(c2);
5596 isl_int_init(g);
5598 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5599 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5600 res = isl_aff_free(res);
5602 isl_int_clear(f);
5603 isl_int_clear(c1);
5604 isl_int_clear(c2);
5605 isl_int_clear(g);
5607 isl_aff_free(aff);
5608 isl_multi_aff_free(ma);
5609 res = isl_aff_normalize(res);
5610 return res;
5611 error:
5612 isl_aff_free(aff);
5613 isl_multi_aff_free(ma);
5614 isl_aff_free(res);
5615 return NULL;
5618 /* Compute the pullback of "aff1" by the function represented by "aff2".
5619 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5620 * defined over the domain space of "aff1".
5622 * The domain of "aff1" should match the range of "aff2", which means
5623 * that it should be single-dimensional.
5625 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5626 __isl_take isl_aff *aff2)
5628 isl_multi_aff *ma;
5630 ma = isl_multi_aff_from_aff(aff2);
5631 return isl_aff_pullback_multi_aff(aff1, ma);
5634 /* Compute the pullback of "ma1" by the function represented by "ma2".
5635 * In other words, plug in "ma2" in "ma1".
5637 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5639 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5640 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5642 int i;
5643 isl_space *space = NULL;
5645 ma2 = isl_multi_aff_align_divs(ma2);
5646 ma1 = isl_multi_aff_cow(ma1);
5647 if (!ma1 || !ma2)
5648 goto error;
5650 space = isl_space_join(isl_multi_aff_get_space(ma2),
5651 isl_multi_aff_get_space(ma1));
5653 for (i = 0; i < ma1->n; ++i) {
5654 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5655 isl_multi_aff_copy(ma2));
5656 if (!ma1->u.p[i])
5657 goto error;
5660 ma1 = isl_multi_aff_reset_space(ma1, space);
5661 isl_multi_aff_free(ma2);
5662 return ma1;
5663 error:
5664 isl_space_free(space);
5665 isl_multi_aff_free(ma2);
5666 isl_multi_aff_free(ma1);
5667 return NULL;
5670 /* Compute the pullback of "ma1" by the function represented by "ma2".
5671 * In other words, plug in "ma2" in "ma1".
5673 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5674 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5676 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5677 &isl_multi_aff_pullback_multi_aff_aligned);
5680 /* Extend the local space of "dst" to include the divs
5681 * in the local space of "src".
5683 * If "src" does not have any divs or if the local spaces of "dst" and
5684 * "src" are the same, then no extension is required.
5686 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5687 __isl_keep isl_aff *src)
5689 isl_ctx *ctx;
5690 isl_size src_n_div, dst_n_div;
5691 int *exp1 = NULL;
5692 int *exp2 = NULL;
5693 isl_bool equal;
5694 isl_mat *div;
5696 if (!src || !dst)
5697 return isl_aff_free(dst);
5699 ctx = isl_aff_get_ctx(src);
5700 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5701 if (equal < 0)
5702 return isl_aff_free(dst);
5703 if (!equal)
5704 isl_die(ctx, isl_error_invalid,
5705 "spaces don't match", goto error);
5707 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5708 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5709 if (src_n_div == 0)
5710 return dst;
5711 equal = isl_local_space_is_equal(src->ls, dst->ls);
5712 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5713 return isl_aff_free(dst);
5714 if (equal)
5715 return dst;
5717 exp1 = isl_alloc_array(ctx, int, src_n_div);
5718 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5719 if (!exp1 || (dst_n_div && !exp2))
5720 goto error;
5722 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5723 dst = isl_aff_expand_divs(dst, div, exp2);
5724 free(exp1);
5725 free(exp2);
5727 return dst;
5728 error:
5729 free(exp1);
5730 free(exp2);
5731 return isl_aff_free(dst);
5734 /* Adjust the local spaces of the affine expressions in "maff"
5735 * such that they all have the save divs.
5737 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5738 __isl_take isl_multi_aff *maff)
5740 int i;
5742 if (!maff)
5743 return NULL;
5744 if (maff->n == 0)
5745 return maff;
5746 maff = isl_multi_aff_cow(maff);
5747 if (!maff)
5748 return NULL;
5750 for (i = 1; i < maff->n; ++i)
5751 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5752 for (i = 1; i < maff->n; ++i) {
5753 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5754 if (!maff->u.p[i])
5755 return isl_multi_aff_free(maff);
5758 return maff;
5761 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5763 aff = isl_aff_cow(aff);
5764 if (!aff)
5765 return NULL;
5767 aff->ls = isl_local_space_lift(aff->ls);
5768 if (!aff->ls)
5769 return isl_aff_free(aff);
5771 return aff;
5774 /* Lift "maff" to a space with extra dimensions such that the result
5775 * has no more existentially quantified variables.
5776 * If "ls" is not NULL, then *ls is assigned the local space that lies
5777 * at the basis of the lifting applied to "maff".
5779 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5780 __isl_give isl_local_space **ls)
5782 int i;
5783 isl_space *space;
5784 isl_size n_div;
5786 if (ls)
5787 *ls = NULL;
5789 if (!maff)
5790 return NULL;
5792 if (maff->n == 0) {
5793 if (ls) {
5794 isl_space *space = isl_multi_aff_get_domain_space(maff);
5795 *ls = isl_local_space_from_space(space);
5796 if (!*ls)
5797 return isl_multi_aff_free(maff);
5799 return maff;
5802 maff = isl_multi_aff_cow(maff);
5803 maff = isl_multi_aff_align_divs(maff);
5804 if (!maff)
5805 return NULL;
5807 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5808 if (n_div < 0)
5809 return isl_multi_aff_free(maff);
5810 space = isl_multi_aff_get_space(maff);
5811 space = isl_space_lift(isl_space_domain(space), n_div);
5812 space = isl_space_extend_domain_with_range(space,
5813 isl_multi_aff_get_space(maff));
5814 if (!space)
5815 return isl_multi_aff_free(maff);
5816 isl_space_free(maff->space);
5817 maff->space = space;
5819 if (ls) {
5820 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5821 if (!*ls)
5822 return isl_multi_aff_free(maff);
5825 for (i = 0; i < maff->n; ++i) {
5826 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5827 if (!maff->u.p[i])
5828 goto error;
5831 return maff;
5832 error:
5833 if (ls)
5834 isl_local_space_free(*ls);
5835 return isl_multi_aff_free(maff);
5838 #undef TYPE
5839 #define TYPE isl_pw_multi_aff
5840 static
5841 #include "check_type_range_templ.c"
5843 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5845 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5846 __isl_keep isl_pw_multi_aff *pma, int pos)
5848 int i;
5849 isl_size n_out;
5850 isl_space *space;
5851 isl_pw_aff *pa;
5853 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
5854 return NULL;
5856 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5857 if (n_out < 0)
5858 return NULL;
5860 space = isl_pw_multi_aff_get_space(pma);
5861 space = isl_space_drop_dims(space, isl_dim_out,
5862 pos + 1, n_out - pos - 1);
5863 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5865 pa = isl_pw_aff_alloc_size(space, pma->n);
5866 for (i = 0; i < pma->n; ++i) {
5867 isl_aff *aff;
5868 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5869 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5872 return pa;
5875 /* Return an isl_pw_multi_aff with the given "set" as domain and
5876 * an unnamed zero-dimensional range.
5878 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5879 __isl_take isl_set *set)
5881 isl_multi_aff *ma;
5882 isl_space *space;
5884 space = isl_set_get_space(set);
5885 space = isl_space_from_domain(space);
5886 ma = isl_multi_aff_zero(space);
5887 return isl_pw_multi_aff_alloc(set, ma);
5890 /* Add an isl_pw_multi_aff with the given "set" as domain and
5891 * an unnamed zero-dimensional range to *user.
5893 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5894 void *user)
5896 isl_union_pw_multi_aff **upma = user;
5897 isl_pw_multi_aff *pma;
5899 pma = isl_pw_multi_aff_from_domain(set);
5900 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5902 return isl_stat_ok;
5905 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5906 * an unnamed zero-dimensional range.
5908 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5909 __isl_take isl_union_set *uset)
5911 isl_space *space;
5912 isl_union_pw_multi_aff *upma;
5914 if (!uset)
5915 return NULL;
5917 space = isl_union_set_get_space(uset);
5918 upma = isl_union_pw_multi_aff_empty(space);
5920 if (isl_union_set_foreach_set(uset,
5921 &add_pw_multi_aff_from_domain, &upma) < 0)
5922 goto error;
5924 isl_union_set_free(uset);
5925 return upma;
5926 error:
5927 isl_union_set_free(uset);
5928 isl_union_pw_multi_aff_free(upma);
5929 return NULL;
5932 /* Local data for bin_entry and the callback "fn".
5934 struct isl_union_pw_multi_aff_bin_data {
5935 isl_union_pw_multi_aff *upma2;
5936 isl_union_pw_multi_aff *res;
5937 isl_pw_multi_aff *pma;
5938 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5941 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5942 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5944 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5946 struct isl_union_pw_multi_aff_bin_data *data = user;
5947 isl_stat r;
5949 data->pma = pma;
5950 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5951 data->fn, data);
5952 isl_pw_multi_aff_free(pma);
5954 return r;
5957 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5958 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5959 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5960 * as *entry. The callback should adjust data->res if desired.
5962 static __isl_give isl_union_pw_multi_aff *bin_op(
5963 __isl_take isl_union_pw_multi_aff *upma1,
5964 __isl_take isl_union_pw_multi_aff *upma2,
5965 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5967 isl_space *space;
5968 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5970 space = isl_union_pw_multi_aff_get_space(upma2);
5971 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5972 space = isl_union_pw_multi_aff_get_space(upma1);
5973 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5975 if (!upma1 || !upma2)
5976 goto error;
5978 data.upma2 = upma2;
5979 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5980 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5981 &bin_entry, &data) < 0)
5982 goto error;
5984 isl_union_pw_multi_aff_free(upma1);
5985 isl_union_pw_multi_aff_free(upma2);
5986 return data.res;
5987 error:
5988 isl_union_pw_multi_aff_free(upma1);
5989 isl_union_pw_multi_aff_free(upma2);
5990 isl_union_pw_multi_aff_free(data.res);
5991 return NULL;
5994 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5995 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5997 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5998 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6000 isl_space *space;
6002 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6003 isl_pw_multi_aff_get_space(pma2));
6004 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6005 &isl_multi_aff_range_product);
6008 /* Given two isl_pw_multi_affs A -> B and C -> D,
6009 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6011 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6012 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6014 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6015 &pw_multi_aff_range_product);
6018 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6019 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6021 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6022 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6024 isl_space *space;
6026 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6027 isl_pw_multi_aff_get_space(pma2));
6028 space = isl_space_flatten_range(space);
6029 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6030 &isl_multi_aff_flat_range_product);
6033 /* Given two isl_pw_multi_affs A -> B and C -> D,
6034 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6036 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6037 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6039 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6040 &pw_multi_aff_flat_range_product);
6043 /* If data->pma and "pma2" have the same domain space, then compute
6044 * their flat range product and the result to data->res.
6046 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6047 void *user)
6049 struct isl_union_pw_multi_aff_bin_data *data = user;
6051 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6052 pma2->dim, isl_dim_in)) {
6053 isl_pw_multi_aff_free(pma2);
6054 return isl_stat_ok;
6057 pma2 = isl_pw_multi_aff_flat_range_product(
6058 isl_pw_multi_aff_copy(data->pma), pma2);
6060 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6062 return isl_stat_ok;
6065 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6066 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6068 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6069 __isl_take isl_union_pw_multi_aff *upma1,
6070 __isl_take isl_union_pw_multi_aff *upma2)
6072 return bin_op(upma1, upma2, &flat_range_product_entry);
6075 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6076 * The parameters are assumed to have been aligned.
6078 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6079 * except that it works on two different isl_pw_* types.
6081 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6082 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6083 __isl_take isl_pw_aff *pa)
6085 int i, j, n;
6086 isl_pw_multi_aff *res = NULL;
6088 if (!pma || !pa)
6089 goto error;
6091 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6092 pa->dim, isl_dim_in))
6093 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6094 "domains don't match", goto error);
6095 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6096 goto error;
6098 n = pma->n * pa->n;
6099 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6101 for (i = 0; i < pma->n; ++i) {
6102 for (j = 0; j < pa->n; ++j) {
6103 isl_set *common;
6104 isl_multi_aff *res_ij;
6105 int empty;
6107 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6108 isl_set_copy(pa->p[j].set));
6109 empty = isl_set_plain_is_empty(common);
6110 if (empty < 0 || empty) {
6111 isl_set_free(common);
6112 if (empty < 0)
6113 goto error;
6114 continue;
6117 res_ij = isl_multi_aff_set_aff(
6118 isl_multi_aff_copy(pma->p[i].maff), pos,
6119 isl_aff_copy(pa->p[j].aff));
6120 res_ij = isl_multi_aff_gist(res_ij,
6121 isl_set_copy(common));
6123 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6127 isl_pw_multi_aff_free(pma);
6128 isl_pw_aff_free(pa);
6129 return res;
6130 error:
6131 isl_pw_multi_aff_free(pma);
6132 isl_pw_aff_free(pa);
6133 return isl_pw_multi_aff_free(res);
6136 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6138 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6139 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6140 __isl_take isl_pw_aff *pa)
6142 isl_bool equal_params;
6144 if (!pma || !pa)
6145 goto error;
6146 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6147 if (equal_params < 0)
6148 goto error;
6149 if (equal_params)
6150 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6151 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6152 isl_pw_aff_check_named_params(pa) < 0)
6153 goto error;
6154 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6155 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6156 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6157 error:
6158 isl_pw_multi_aff_free(pma);
6159 isl_pw_aff_free(pa);
6160 return NULL;
6163 /* Do the parameters of "pa" match those of "space"?
6165 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6166 __isl_keep isl_space *space)
6168 isl_space *pa_space;
6169 isl_bool match;
6171 if (!pa || !space)
6172 return isl_bool_error;
6174 pa_space = isl_pw_aff_get_space(pa);
6176 match = isl_space_has_equal_params(space, pa_space);
6178 isl_space_free(pa_space);
6179 return match;
6182 /* Check that the domain space of "pa" matches "space".
6184 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6185 __isl_keep isl_space *space)
6187 isl_space *pa_space;
6188 isl_bool match;
6190 if (!pa || !space)
6191 return isl_stat_error;
6193 pa_space = isl_pw_aff_get_space(pa);
6195 match = isl_space_has_equal_params(space, pa_space);
6196 if (match < 0)
6197 goto error;
6198 if (!match)
6199 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6200 "parameters don't match", goto error);
6201 match = isl_space_tuple_is_equal(space, isl_dim_in,
6202 pa_space, isl_dim_in);
6203 if (match < 0)
6204 goto error;
6205 if (!match)
6206 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6207 "domains don't match", goto error);
6208 isl_space_free(pa_space);
6209 return isl_stat_ok;
6210 error:
6211 isl_space_free(pa_space);
6212 return isl_stat_error;
6215 #undef BASE
6216 #define BASE pw_aff
6217 #undef DOMBASE
6218 #define DOMBASE set
6220 #include <isl_multi_explicit_domain.c>
6221 #include <isl_multi_pw_aff_explicit_domain.c>
6222 #include <isl_multi_templ.c>
6223 #include <isl_multi_apply_set.c>
6224 #include <isl_multi_arith_templ.c>
6225 #include <isl_multi_coalesce.c>
6226 #include <isl_multi_domain_templ.c>
6227 #include <isl_multi_dims.c>
6228 #include <isl_multi_from_base_templ.c>
6229 #include <isl_multi_gist.c>
6230 #include <isl_multi_hash.c>
6231 #include <isl_multi_identity_templ.c>
6232 #include <isl_multi_align_set.c>
6233 #include <isl_multi_intersect.c>
6234 #include <isl_multi_move_dims_templ.c>
6235 #include <isl_multi_nan_templ.c>
6236 #include <isl_multi_param_templ.c>
6237 #include <isl_multi_product_templ.c>
6238 #include <isl_multi_splice_templ.c>
6239 #include <isl_multi_zero_templ.c>
6241 /* Does "mpa" have a non-trivial explicit domain?
6243 * The explicit domain, if present, is trivial if it represents
6244 * an (obviously) universe set.
6246 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6247 __isl_keep isl_multi_pw_aff *mpa)
6249 if (!mpa)
6250 return isl_bool_error;
6251 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6252 return isl_bool_false;
6253 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6256 /* Scale the elements of "pma" by the corresponding elements of "mv".
6258 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6259 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6261 int i;
6262 isl_bool equal_params;
6264 pma = isl_pw_multi_aff_cow(pma);
6265 if (!pma || !mv)
6266 goto error;
6267 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6268 mv->space, isl_dim_set))
6269 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6270 "spaces don't match", goto error);
6271 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6272 if (equal_params < 0)
6273 goto error;
6274 if (!equal_params) {
6275 pma = isl_pw_multi_aff_align_params(pma,
6276 isl_multi_val_get_space(mv));
6277 mv = isl_multi_val_align_params(mv,
6278 isl_pw_multi_aff_get_space(pma));
6279 if (!pma || !mv)
6280 goto error;
6283 for (i = 0; i < pma->n; ++i) {
6284 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6285 isl_multi_val_copy(mv));
6286 if (!pma->p[i].maff)
6287 goto error;
6290 isl_multi_val_free(mv);
6291 return pma;
6292 error:
6293 isl_multi_val_free(mv);
6294 isl_pw_multi_aff_free(pma);
6295 return NULL;
6298 /* This function is called for each entry of an isl_union_pw_multi_aff.
6299 * If the space of the entry matches that of data->mv,
6300 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6301 * Otherwise, return an empty isl_pw_multi_aff.
6303 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6304 __isl_take isl_pw_multi_aff *pma, void *user)
6306 isl_multi_val *mv = user;
6308 if (!pma)
6309 return NULL;
6310 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6311 mv->space, isl_dim_set)) {
6312 isl_space *space = isl_pw_multi_aff_get_space(pma);
6313 isl_pw_multi_aff_free(pma);
6314 return isl_pw_multi_aff_empty(space);
6317 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6320 /* Scale the elements of "upma" by the corresponding elements of "mv",
6321 * for those entries that match the space of "mv".
6323 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6324 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6326 upma = isl_union_pw_multi_aff_align_params(upma,
6327 isl_multi_val_get_space(mv));
6328 mv = isl_multi_val_align_params(mv,
6329 isl_union_pw_multi_aff_get_space(upma));
6330 if (!upma || !mv)
6331 goto error;
6333 return isl_union_pw_multi_aff_transform(upma,
6334 &union_pw_multi_aff_scale_multi_val_entry, mv);
6336 isl_multi_val_free(mv);
6337 return upma;
6338 error:
6339 isl_multi_val_free(mv);
6340 isl_union_pw_multi_aff_free(upma);
6341 return NULL;
6344 /* Construct and return a piecewise multi affine expression
6345 * in the given space with value zero in each of the output dimensions and
6346 * a universe domain.
6348 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6350 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6353 /* Construct and return a piecewise multi affine expression
6354 * that is equal to the given piecewise affine expression.
6356 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6357 __isl_take isl_pw_aff *pa)
6359 int i;
6360 isl_space *space;
6361 isl_pw_multi_aff *pma;
6363 if (!pa)
6364 return NULL;
6366 space = isl_pw_aff_get_space(pa);
6367 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6369 for (i = 0; i < pa->n; ++i) {
6370 isl_set *set;
6371 isl_multi_aff *ma;
6373 set = isl_set_copy(pa->p[i].set);
6374 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6375 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6378 isl_pw_aff_free(pa);
6379 return pma;
6382 /* Construct and return a piecewise multi affine expression
6383 * that is equal to the given multi piecewise affine expression
6384 * on the shared domain of the piecewise affine expressions,
6385 * in the special case of a 0D multi piecewise affine expression.
6387 * Create a piecewise multi affine expression with the explicit domain of
6388 * the 0D multi piecewise affine expression as domain.
6390 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6391 __isl_take isl_multi_pw_aff *mpa)
6393 isl_space *space;
6394 isl_set *dom;
6395 isl_multi_aff *ma;
6397 space = isl_multi_pw_aff_get_space(mpa);
6398 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6399 isl_multi_pw_aff_free(mpa);
6401 ma = isl_multi_aff_zero(space);
6402 return isl_pw_multi_aff_alloc(dom, ma);
6405 /* Construct and return a piecewise multi affine expression
6406 * that is equal to the given multi piecewise affine expression
6407 * on the shared domain of the piecewise affine expressions.
6409 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6410 __isl_take isl_multi_pw_aff *mpa)
6412 int i;
6413 isl_space *space;
6414 isl_pw_aff *pa;
6415 isl_pw_multi_aff *pma;
6417 if (!mpa)
6418 return NULL;
6420 if (mpa->n == 0)
6421 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6423 space = isl_multi_pw_aff_get_space(mpa);
6424 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6425 pma = isl_pw_multi_aff_from_pw_aff(pa);
6427 for (i = 1; i < mpa->n; ++i) {
6428 isl_pw_multi_aff *pma_i;
6430 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6431 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6432 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6435 pma = isl_pw_multi_aff_reset_space(pma, space);
6437 isl_multi_pw_aff_free(mpa);
6438 return pma;
6441 /* Construct and return a multi piecewise affine expression
6442 * that is equal to the given multi affine expression.
6444 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6445 __isl_take isl_multi_aff *ma)
6447 int i;
6448 isl_size n;
6449 isl_multi_pw_aff *mpa;
6451 n = isl_multi_aff_dim(ma, isl_dim_out);
6452 if (n < 0)
6453 ma = isl_multi_aff_free(ma);
6454 if (!ma)
6455 return NULL;
6457 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6459 for (i = 0; i < n; ++i) {
6460 isl_pw_aff *pa;
6462 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6463 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6466 isl_multi_aff_free(ma);
6467 return mpa;
6470 /* Construct and return a multi piecewise affine expression
6471 * that is equal to the given piecewise multi affine expression.
6473 * If the resulting multi piecewise affine expression has
6474 * an explicit domain, then assign it the domain of the input.
6475 * In other cases, the domain is stored in the individual elements.
6477 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6478 __isl_take isl_pw_multi_aff *pma)
6480 int i;
6481 isl_size n;
6482 isl_space *space;
6483 isl_multi_pw_aff *mpa;
6485 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6486 if (n < 0)
6487 pma = isl_pw_multi_aff_free(pma);
6488 space = isl_pw_multi_aff_get_space(pma);
6489 mpa = isl_multi_pw_aff_alloc(space);
6491 for (i = 0; i < n; ++i) {
6492 isl_pw_aff *pa;
6494 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6495 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6497 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6498 isl_set *dom;
6500 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6501 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6504 isl_pw_multi_aff_free(pma);
6505 return mpa;
6508 /* Do "pa1" and "pa2" represent the same function?
6510 * We first check if they are obviously equal.
6511 * If not, we convert them to maps and check if those are equal.
6513 * If "pa1" or "pa2" contain any NaNs, then they are considered
6514 * not to be the same. A NaN is not equal to anything, not even
6515 * to another NaN.
6517 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6518 __isl_keep isl_pw_aff *pa2)
6520 isl_bool equal;
6521 isl_bool has_nan;
6522 isl_map *map1, *map2;
6524 if (!pa1 || !pa2)
6525 return isl_bool_error;
6527 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6528 if (equal < 0 || equal)
6529 return equal;
6530 has_nan = either_involves_nan(pa1, pa2);
6531 if (has_nan < 0)
6532 return isl_bool_error;
6533 if (has_nan)
6534 return isl_bool_false;
6536 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6537 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6538 equal = isl_map_is_equal(map1, map2);
6539 isl_map_free(map1);
6540 isl_map_free(map2);
6542 return equal;
6545 /* Do "mpa1" and "mpa2" represent the same function?
6547 * Note that we cannot convert the entire isl_multi_pw_aff
6548 * to a map because the domains of the piecewise affine expressions
6549 * may not be the same.
6551 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6552 __isl_keep isl_multi_pw_aff *mpa2)
6554 int i;
6555 isl_bool equal, equal_params;
6557 if (!mpa1 || !mpa2)
6558 return isl_bool_error;
6560 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6561 if (equal_params < 0)
6562 return isl_bool_error;
6563 if (!equal_params) {
6564 if (!isl_space_has_named_params(mpa1->space))
6565 return isl_bool_false;
6566 if (!isl_space_has_named_params(mpa2->space))
6567 return isl_bool_false;
6568 mpa1 = isl_multi_pw_aff_copy(mpa1);
6569 mpa2 = isl_multi_pw_aff_copy(mpa2);
6570 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6571 isl_multi_pw_aff_get_space(mpa2));
6572 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6573 isl_multi_pw_aff_get_space(mpa1));
6574 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6575 isl_multi_pw_aff_free(mpa1);
6576 isl_multi_pw_aff_free(mpa2);
6577 return equal;
6580 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6581 if (equal < 0 || !equal)
6582 return equal;
6584 for (i = 0; i < mpa1->n; ++i) {
6585 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6586 if (equal < 0 || !equal)
6587 return equal;
6590 return isl_bool_true;
6593 /* Do "pma1" and "pma2" represent the same function?
6595 * First check if they are obviously equal.
6596 * If not, then convert them to maps and check if those are equal.
6598 * If "pa1" or "pa2" contain any NaNs, then they are considered
6599 * not to be the same. A NaN is not equal to anything, not even
6600 * to another NaN.
6602 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6603 __isl_keep isl_pw_multi_aff *pma2)
6605 isl_bool equal;
6606 isl_bool has_nan;
6607 isl_map *map1, *map2;
6609 if (!pma1 || !pma2)
6610 return isl_bool_error;
6612 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6613 if (equal < 0 || equal)
6614 return equal;
6615 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6616 if (has_nan >= 0 && !has_nan)
6617 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6618 if (has_nan < 0 || has_nan)
6619 return isl_bool_not(has_nan);
6621 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6622 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6623 equal = isl_map_is_equal(map1, map2);
6624 isl_map_free(map1);
6625 isl_map_free(map2);
6627 return equal;
6630 /* Compute the pullback of "mpa" by the function represented by "ma".
6631 * In other words, plug in "ma" in "mpa".
6633 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6635 * If "mpa" has an explicit domain, then it is this domain
6636 * that needs to undergo a pullback, i.e., a preimage.
6638 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6639 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6641 int i;
6642 isl_space *space = NULL;
6644 mpa = isl_multi_pw_aff_cow(mpa);
6645 if (!mpa || !ma)
6646 goto error;
6648 space = isl_space_join(isl_multi_aff_get_space(ma),
6649 isl_multi_pw_aff_get_space(mpa));
6650 if (!space)
6651 goto error;
6653 for (i = 0; i < mpa->n; ++i) {
6654 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6655 isl_multi_aff_copy(ma));
6656 if (!mpa->u.p[i])
6657 goto error;
6659 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6660 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6661 isl_multi_aff_copy(ma));
6662 if (!mpa->u.dom)
6663 goto error;
6666 isl_multi_aff_free(ma);
6667 isl_space_free(mpa->space);
6668 mpa->space = space;
6669 return mpa;
6670 error:
6671 isl_space_free(space);
6672 isl_multi_pw_aff_free(mpa);
6673 isl_multi_aff_free(ma);
6674 return NULL;
6677 /* Compute the pullback of "mpa" by the function represented by "ma".
6678 * In other words, plug in "ma" in "mpa".
6680 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6681 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6683 isl_bool equal_params;
6685 if (!mpa || !ma)
6686 goto error;
6687 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6688 if (equal_params < 0)
6689 goto error;
6690 if (equal_params)
6691 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6692 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6693 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6694 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6695 error:
6696 isl_multi_pw_aff_free(mpa);
6697 isl_multi_aff_free(ma);
6698 return NULL;
6701 /* Compute the pullback of "mpa" by the function represented by "pma".
6702 * In other words, plug in "pma" in "mpa".
6704 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6706 * If "mpa" has an explicit domain, then it is this domain
6707 * that needs to undergo a pullback, i.e., a preimage.
6709 static __isl_give isl_multi_pw_aff *
6710 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6711 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6713 int i;
6714 isl_space *space = NULL;
6716 mpa = isl_multi_pw_aff_cow(mpa);
6717 if (!mpa || !pma)
6718 goto error;
6720 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6721 isl_multi_pw_aff_get_space(mpa));
6723 for (i = 0; i < mpa->n; ++i) {
6724 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6725 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6726 if (!mpa->u.p[i])
6727 goto error;
6729 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6730 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6731 isl_pw_multi_aff_copy(pma));
6732 if (!mpa->u.dom)
6733 goto error;
6736 isl_pw_multi_aff_free(pma);
6737 isl_space_free(mpa->space);
6738 mpa->space = space;
6739 return mpa;
6740 error:
6741 isl_space_free(space);
6742 isl_multi_pw_aff_free(mpa);
6743 isl_pw_multi_aff_free(pma);
6744 return NULL;
6747 /* Compute the pullback of "mpa" by the function represented by "pma".
6748 * In other words, plug in "pma" in "mpa".
6750 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6751 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6753 isl_bool equal_params;
6755 if (!mpa || !pma)
6756 goto error;
6757 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6758 if (equal_params < 0)
6759 goto error;
6760 if (equal_params)
6761 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6762 mpa = isl_multi_pw_aff_align_params(mpa,
6763 isl_pw_multi_aff_get_space(pma));
6764 pma = isl_pw_multi_aff_align_params(pma,
6765 isl_multi_pw_aff_get_space(mpa));
6766 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6767 error:
6768 isl_multi_pw_aff_free(mpa);
6769 isl_pw_multi_aff_free(pma);
6770 return NULL;
6773 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6774 * with the domain of "aff". The domain of the result is the same
6775 * as that of "mpa".
6776 * "mpa" and "aff" are assumed to have been aligned.
6778 * We first extract the parametric constant from "aff", defined
6779 * over the correct domain.
6780 * Then we add the appropriate combinations of the members of "mpa".
6781 * Finally, we add the integer divisions through recursive calls.
6783 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6784 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6786 int i;
6787 isl_size n_in, n_div, n_mpa_in;
6788 isl_space *space;
6789 isl_val *v;
6790 isl_pw_aff *pa;
6791 isl_aff *tmp;
6793 n_in = isl_aff_dim(aff, isl_dim_in);
6794 n_div = isl_aff_dim(aff, isl_dim_div);
6795 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
6796 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
6797 goto error;
6799 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6800 tmp = isl_aff_copy(aff);
6801 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6802 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6803 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
6804 tmp = isl_aff_reset_domain_space(tmp, space);
6805 pa = isl_pw_aff_from_aff(tmp);
6807 for (i = 0; i < n_in; ++i) {
6808 isl_pw_aff *pa_i;
6810 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6811 continue;
6812 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6813 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6814 pa_i = isl_pw_aff_scale_val(pa_i, v);
6815 pa = isl_pw_aff_add(pa, pa_i);
6818 for (i = 0; i < n_div; ++i) {
6819 isl_aff *div;
6820 isl_pw_aff *pa_i;
6822 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6823 continue;
6824 div = isl_aff_get_div(aff, i);
6825 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6826 isl_multi_pw_aff_copy(mpa), div);
6827 pa_i = isl_pw_aff_floor(pa_i);
6828 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6829 pa_i = isl_pw_aff_scale_val(pa_i, v);
6830 pa = isl_pw_aff_add(pa, pa_i);
6833 isl_multi_pw_aff_free(mpa);
6834 isl_aff_free(aff);
6836 return pa;
6837 error:
6838 isl_multi_pw_aff_free(mpa);
6839 isl_aff_free(aff);
6840 return NULL;
6843 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6844 * with the domain of "aff". The domain of the result is the same
6845 * as that of "mpa".
6847 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6848 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6850 isl_bool equal_params;
6852 if (!aff || !mpa)
6853 goto error;
6854 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6855 if (equal_params < 0)
6856 goto error;
6857 if (equal_params)
6858 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6860 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6861 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6863 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6864 error:
6865 isl_aff_free(aff);
6866 isl_multi_pw_aff_free(mpa);
6867 return NULL;
6870 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6871 * with the domain of "pa". The domain of the result is the same
6872 * as that of "mpa".
6873 * "mpa" and "pa" are assumed to have been aligned.
6875 * We consider each piece in turn. Note that the domains of the
6876 * pieces are assumed to be disjoint and they remain disjoint
6877 * after taking the preimage (over the same function).
6879 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6880 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6882 isl_space *space;
6883 isl_pw_aff *res;
6884 int i;
6886 if (!mpa || !pa)
6887 goto error;
6889 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6890 isl_pw_aff_get_space(pa));
6891 res = isl_pw_aff_empty(space);
6893 for (i = 0; i < pa->n; ++i) {
6894 isl_pw_aff *pa_i;
6895 isl_set *domain;
6897 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6898 isl_multi_pw_aff_copy(mpa),
6899 isl_aff_copy(pa->p[i].aff));
6900 domain = isl_set_copy(pa->p[i].set);
6901 domain = isl_set_preimage_multi_pw_aff(domain,
6902 isl_multi_pw_aff_copy(mpa));
6903 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6904 res = isl_pw_aff_add_disjoint(res, pa_i);
6907 isl_pw_aff_free(pa);
6908 isl_multi_pw_aff_free(mpa);
6909 return res;
6910 error:
6911 isl_pw_aff_free(pa);
6912 isl_multi_pw_aff_free(mpa);
6913 return NULL;
6916 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6917 * with the domain of "pa". The domain of the result is the same
6918 * as that of "mpa".
6920 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6921 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6923 isl_bool equal_params;
6925 if (!pa || !mpa)
6926 goto error;
6927 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6928 if (equal_params < 0)
6929 goto error;
6930 if (equal_params)
6931 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6933 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6934 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6936 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6937 error:
6938 isl_pw_aff_free(pa);
6939 isl_multi_pw_aff_free(mpa);
6940 return NULL;
6943 /* Compute the pullback of "pa" by the function represented by "mpa".
6944 * In other words, plug in "mpa" in "pa".
6945 * "pa" and "mpa" are assumed to have been aligned.
6947 * The pullback is computed by applying "pa" to "mpa".
6949 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6950 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6952 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6955 /* Compute the pullback of "pa" by the function represented by "mpa".
6956 * In other words, plug in "mpa" in "pa".
6958 * The pullback is computed by applying "pa" to "mpa".
6960 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6961 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6963 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6966 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6967 * In other words, plug in "mpa2" in "mpa1".
6969 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6971 * We pullback each member of "mpa1" in turn.
6973 * If "mpa1" has an explicit domain, then it is this domain
6974 * that needs to undergo a pullback instead, i.e., a preimage.
6976 static __isl_give isl_multi_pw_aff *
6977 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6978 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6980 int i;
6981 isl_space *space = NULL;
6983 mpa1 = isl_multi_pw_aff_cow(mpa1);
6984 if (!mpa1 || !mpa2)
6985 goto error;
6987 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6988 isl_multi_pw_aff_get_space(mpa1));
6990 for (i = 0; i < mpa1->n; ++i) {
6991 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6992 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
6993 if (!mpa1->u.p[i])
6994 goto error;
6997 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
6998 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
6999 isl_multi_pw_aff_copy(mpa2));
7000 if (!mpa1->u.dom)
7001 goto error;
7003 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7005 isl_multi_pw_aff_free(mpa2);
7006 return mpa1;
7007 error:
7008 isl_space_free(space);
7009 isl_multi_pw_aff_free(mpa1);
7010 isl_multi_pw_aff_free(mpa2);
7011 return NULL;
7014 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7015 * In other words, plug in "mpa2" in "mpa1".
7017 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7018 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7020 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7021 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7024 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7025 * of "mpa1" and "mpa2" live in the same space, construct map space
7026 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7027 * with this map space as extract argument.
7029 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7030 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7031 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7032 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7034 int match;
7035 isl_space *space1, *space2;
7036 isl_map *res;
7038 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7039 isl_multi_pw_aff_get_space(mpa2));
7040 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7041 isl_multi_pw_aff_get_space(mpa1));
7042 if (!mpa1 || !mpa2)
7043 goto error;
7044 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7045 mpa2->space, isl_dim_out);
7046 if (match < 0)
7047 goto error;
7048 if (!match)
7049 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7050 "range spaces don't match", goto error);
7051 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7052 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7053 space1 = isl_space_map_from_domain_and_range(space1, space2);
7055 res = order(mpa1, mpa2, space1);
7056 isl_multi_pw_aff_free(mpa1);
7057 isl_multi_pw_aff_free(mpa2);
7058 return res;
7059 error:
7060 isl_multi_pw_aff_free(mpa1);
7061 isl_multi_pw_aff_free(mpa2);
7062 return NULL;
7065 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7066 * where the function values are equal. "space" is the space of the result.
7067 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7069 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7070 * in the sequences are equal.
7072 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7073 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7074 __isl_take isl_space *space)
7076 int i;
7077 isl_size n;
7078 isl_map *res;
7080 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7081 if (n < 0)
7082 space = isl_space_free(space);
7083 res = isl_map_universe(space);
7085 for (i = 0; i < n; ++i) {
7086 isl_pw_aff *pa1, *pa2;
7087 isl_map *map;
7089 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7090 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7091 map = isl_pw_aff_eq_map(pa1, pa2);
7092 res = isl_map_intersect(res, map);
7095 return res;
7098 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7099 * where the function values are equal.
7101 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7102 __isl_take isl_multi_pw_aff *mpa2)
7104 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7105 &isl_multi_pw_aff_eq_map_on_space);
7108 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7109 * where the function values of "mpa1" is lexicographically satisfies "base"
7110 * compared to that of "mpa2". "space" is the space of the result.
7111 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7113 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7114 * if its i-th element satisfies "base" when compared to
7115 * the i-th element of "mpa2" while all previous elements are
7116 * pairwise equal.
7118 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7119 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7120 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7121 __isl_take isl_pw_aff *pa2),
7122 __isl_take isl_space *space)
7124 int i;
7125 isl_size n;
7126 isl_map *res, *rest;
7128 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7129 if (n < 0)
7130 space = isl_space_free(space);
7131 res = isl_map_empty(isl_space_copy(space));
7132 rest = isl_map_universe(space);
7134 for (i = 0; i < n; ++i) {
7135 isl_pw_aff *pa1, *pa2;
7136 isl_map *map;
7138 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7139 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7140 map = base(pa1, pa2);
7141 map = isl_map_intersect(map, isl_map_copy(rest));
7142 res = isl_map_union(res, map);
7144 if (i == n - 1)
7145 continue;
7147 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7148 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7149 map = isl_pw_aff_eq_map(pa1, pa2);
7150 rest = isl_map_intersect(rest, map);
7153 isl_map_free(rest);
7154 return res;
7157 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7158 * where the function value of "mpa1" is lexicographically less than that
7159 * of "mpa2". "space" is the space of the result.
7160 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7162 * "mpa1" is less than "mpa2" if its i-th element is smaller
7163 * than the i-th element of "mpa2" while all previous elements are
7164 * pairwise equal.
7166 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7167 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7168 __isl_take isl_space *space)
7170 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7171 &isl_pw_aff_lt_map, space);
7174 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7175 * where the function value of "mpa1" is lexicographically less than that
7176 * of "mpa2".
7178 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7179 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7181 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7182 &isl_multi_pw_aff_lex_lt_map_on_space);
7185 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7186 * where the function value of "mpa1" is lexicographically greater than that
7187 * of "mpa2". "space" is the space of the result.
7188 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7190 * "mpa1" is greater than "mpa2" if its i-th element is greater
7191 * than the i-th element of "mpa2" while all previous elements are
7192 * pairwise equal.
7194 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7195 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7196 __isl_take isl_space *space)
7198 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7199 &isl_pw_aff_gt_map, space);
7202 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7203 * where the function value of "mpa1" is lexicographically greater than that
7204 * of "mpa2".
7206 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7207 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7209 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7210 &isl_multi_pw_aff_lex_gt_map_on_space);
7213 /* Compare two isl_affs.
7215 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7216 * than "aff2" and 0 if they are equal.
7218 * The order is fairly arbitrary. We do consider expressions that only involve
7219 * earlier dimensions as "smaller".
7221 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7223 int cmp;
7224 int last1, last2;
7226 if (aff1 == aff2)
7227 return 0;
7229 if (!aff1)
7230 return -1;
7231 if (!aff2)
7232 return 1;
7234 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7235 if (cmp != 0)
7236 return cmp;
7238 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7239 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7240 if (last1 != last2)
7241 return last1 - last2;
7243 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7246 /* Compare two isl_pw_affs.
7248 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7249 * than "pa2" and 0 if they are equal.
7251 * The order is fairly arbitrary. We do consider expressions that only involve
7252 * earlier dimensions as "smaller".
7254 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7255 __isl_keep isl_pw_aff *pa2)
7257 int i;
7258 int cmp;
7260 if (pa1 == pa2)
7261 return 0;
7263 if (!pa1)
7264 return -1;
7265 if (!pa2)
7266 return 1;
7268 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7269 if (cmp != 0)
7270 return cmp;
7272 if (pa1->n != pa2->n)
7273 return pa1->n - pa2->n;
7275 for (i = 0; i < pa1->n; ++i) {
7276 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7277 if (cmp != 0)
7278 return cmp;
7279 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7280 if (cmp != 0)
7281 return cmp;
7284 return 0;
7287 /* Return a piecewise affine expression that is equal to "v" on "domain".
7289 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7290 __isl_take isl_val *v)
7292 isl_space *space;
7293 isl_local_space *ls;
7294 isl_aff *aff;
7296 space = isl_set_get_space(domain);
7297 ls = isl_local_space_from_space(space);
7298 aff = isl_aff_val_on_domain(ls, v);
7300 return isl_pw_aff_alloc(domain, aff);
7303 /* Return a multi affine expression that is equal to "mv" on domain
7304 * space "space".
7306 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7307 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7309 int i;
7310 isl_size n;
7311 isl_space *space2;
7312 isl_local_space *ls;
7313 isl_multi_aff *ma;
7315 n = isl_multi_val_dim(mv, isl_dim_set);
7316 if (!space || n < 0)
7317 goto error;
7319 space2 = isl_multi_val_get_space(mv);
7320 space2 = isl_space_align_params(space2, isl_space_copy(space));
7321 space = isl_space_align_params(space, isl_space_copy(space2));
7322 space = isl_space_map_from_domain_and_range(space, space2);
7323 ma = isl_multi_aff_alloc(isl_space_copy(space));
7324 ls = isl_local_space_from_space(isl_space_domain(space));
7325 for (i = 0; i < n; ++i) {
7326 isl_val *v;
7327 isl_aff *aff;
7329 v = isl_multi_val_get_val(mv, i);
7330 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7331 ma = isl_multi_aff_set_aff(ma, i, aff);
7333 isl_local_space_free(ls);
7335 isl_multi_val_free(mv);
7336 return ma;
7337 error:
7338 isl_space_free(space);
7339 isl_multi_val_free(mv);
7340 return NULL;
7343 /* Return a piecewise multi-affine expression
7344 * that is equal to "mv" on "domain".
7346 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7347 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7349 isl_space *space;
7350 isl_multi_aff *ma;
7352 space = isl_set_get_space(domain);
7353 ma = isl_multi_aff_multi_val_on_space(space, mv);
7355 return isl_pw_multi_aff_alloc(domain, ma);
7358 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7359 * mv is the value that should be attained on each domain set
7360 * res collects the results
7362 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7363 isl_multi_val *mv;
7364 isl_union_pw_multi_aff *res;
7367 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7368 * and add it to data->res.
7370 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7371 void *user)
7373 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7374 isl_pw_multi_aff *pma;
7375 isl_multi_val *mv;
7377 mv = isl_multi_val_copy(data->mv);
7378 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7379 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7381 return data->res ? isl_stat_ok : isl_stat_error;
7384 /* Return a union piecewise multi-affine expression
7385 * that is equal to "mv" on "domain".
7387 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7388 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7390 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7391 isl_space *space;
7393 space = isl_union_set_get_space(domain);
7394 data.res = isl_union_pw_multi_aff_empty(space);
7395 data.mv = mv;
7396 if (isl_union_set_foreach_set(domain,
7397 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7398 data.res = isl_union_pw_multi_aff_free(data.res);
7399 isl_union_set_free(domain);
7400 isl_multi_val_free(mv);
7401 return data.res;
7404 /* Compute the pullback of data->pma by the function represented by "pma2",
7405 * provided the spaces match, and add the results to data->res.
7407 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7409 struct isl_union_pw_multi_aff_bin_data *data = user;
7411 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7412 pma2->dim, isl_dim_out)) {
7413 isl_pw_multi_aff_free(pma2);
7414 return isl_stat_ok;
7417 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7418 isl_pw_multi_aff_copy(data->pma), pma2);
7420 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7421 if (!data->res)
7422 return isl_stat_error;
7424 return isl_stat_ok;
7427 /* Compute the pullback of "upma1" by the function represented by "upma2".
7429 __isl_give isl_union_pw_multi_aff *
7430 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7431 __isl_take isl_union_pw_multi_aff *upma1,
7432 __isl_take isl_union_pw_multi_aff *upma2)
7434 return bin_op(upma1, upma2, &pullback_entry);
7437 /* Check that the domain space of "upa" matches "space".
7439 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7440 * can in principle never fail since the space "space" is that
7441 * of the isl_multi_union_pw_aff and is a set space such that
7442 * there is no domain space to match.
7444 * We check the parameters and double-check that "space" is
7445 * indeed that of a set.
7447 static isl_stat isl_union_pw_aff_check_match_domain_space(
7448 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7450 isl_space *upa_space;
7451 isl_bool match;
7453 if (!upa || !space)
7454 return isl_stat_error;
7456 match = isl_space_is_set(space);
7457 if (match < 0)
7458 return isl_stat_error;
7459 if (!match)
7460 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7461 "expecting set space", return isl_stat_error);
7463 upa_space = isl_union_pw_aff_get_space(upa);
7464 match = isl_space_has_equal_params(space, upa_space);
7465 if (match < 0)
7466 goto error;
7467 if (!match)
7468 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7469 "parameters don't match", goto error);
7471 isl_space_free(upa_space);
7472 return isl_stat_ok;
7473 error:
7474 isl_space_free(upa_space);
7475 return isl_stat_error;
7478 /* Do the parameters of "upa" match those of "space"?
7480 static isl_bool isl_union_pw_aff_matching_params(
7481 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7483 isl_space *upa_space;
7484 isl_bool match;
7486 if (!upa || !space)
7487 return isl_bool_error;
7489 upa_space = isl_union_pw_aff_get_space(upa);
7491 match = isl_space_has_equal_params(space, upa_space);
7493 isl_space_free(upa_space);
7494 return match;
7497 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7498 * space represents the new parameters.
7499 * res collects the results.
7501 struct isl_union_pw_aff_reset_params_data {
7502 isl_space *space;
7503 isl_union_pw_aff *res;
7506 /* Replace the parameters of "pa" by data->space and
7507 * add the result to data->res.
7509 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7511 struct isl_union_pw_aff_reset_params_data *data = user;
7512 isl_space *space;
7514 space = isl_pw_aff_get_space(pa);
7515 space = isl_space_replace_params(space, data->space);
7516 pa = isl_pw_aff_reset_space(pa, space);
7517 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7519 return data->res ? isl_stat_ok : isl_stat_error;
7522 /* Replace the domain space of "upa" by "space".
7523 * Since a union expression does not have a (single) domain space,
7524 * "space" is necessarily a parameter space.
7526 * Since the order and the names of the parameters determine
7527 * the hash value, we need to create a new hash table.
7529 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7530 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7532 struct isl_union_pw_aff_reset_params_data data = { space };
7533 isl_bool match;
7535 match = isl_union_pw_aff_matching_params(upa, space);
7536 if (match < 0)
7537 upa = isl_union_pw_aff_free(upa);
7538 else if (match) {
7539 isl_space_free(space);
7540 return upa;
7543 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7544 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7545 data.res = isl_union_pw_aff_free(data.res);
7547 isl_union_pw_aff_free(upa);
7548 isl_space_free(space);
7549 return data.res;
7552 /* Return the floor of "pa".
7554 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7556 return isl_pw_aff_floor(pa);
7559 /* Given f, return floor(f).
7561 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7562 __isl_take isl_union_pw_aff *upa)
7564 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7567 /* Compute
7569 * upa mod m = upa - m * floor(upa/m)
7571 * with m an integer value.
7573 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7574 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7576 isl_union_pw_aff *res;
7578 if (!upa || !m)
7579 goto error;
7581 if (!isl_val_is_int(m))
7582 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7583 "expecting integer modulo", goto error);
7584 if (!isl_val_is_pos(m))
7585 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7586 "expecting positive modulo", goto error);
7588 res = isl_union_pw_aff_copy(upa);
7589 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7590 upa = isl_union_pw_aff_floor(upa);
7591 upa = isl_union_pw_aff_scale_val(upa, m);
7592 res = isl_union_pw_aff_sub(res, upa);
7594 return res;
7595 error:
7596 isl_val_free(m);
7597 isl_union_pw_aff_free(upa);
7598 return NULL;
7601 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7602 * pos is the output position that needs to be extracted.
7603 * res collects the results.
7605 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7606 int pos;
7607 isl_union_pw_aff *res;
7610 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7611 * (assuming it has such a dimension) and add it to data->res.
7613 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7615 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7616 isl_size n_out;
7617 isl_pw_aff *pa;
7619 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7620 if (n_out < 0)
7621 return isl_stat_error;
7622 if (data->pos >= n_out) {
7623 isl_pw_multi_aff_free(pma);
7624 return isl_stat_ok;
7627 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7628 isl_pw_multi_aff_free(pma);
7630 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7632 return data->res ? isl_stat_ok : isl_stat_error;
7635 /* Extract an isl_union_pw_aff corresponding to
7636 * output dimension "pos" of "upma".
7638 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7639 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7641 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7642 isl_space *space;
7644 if (!upma)
7645 return NULL;
7647 if (pos < 0)
7648 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7649 "cannot extract at negative position", return NULL);
7651 space = isl_union_pw_multi_aff_get_space(upma);
7652 data.res = isl_union_pw_aff_empty(space);
7653 data.pos = pos;
7654 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7655 &get_union_pw_aff, &data) < 0)
7656 data.res = isl_union_pw_aff_free(data.res);
7658 return data.res;
7661 /* Return a union piecewise affine expression
7662 * that is equal to "aff" on "domain".
7664 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7665 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7667 isl_pw_aff *pa;
7669 pa = isl_pw_aff_from_aff(aff);
7670 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7673 /* Return a union piecewise affine expression
7674 * that is equal to the parameter identified by "id" on "domain".
7676 * Make sure the parameter appears in the space passed to
7677 * isl_aff_param_on_domain_space_id.
7679 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7680 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7682 isl_space *space;
7683 isl_aff *aff;
7685 space = isl_union_set_get_space(domain);
7686 space = isl_space_add_param_id(space, isl_id_copy(id));
7687 aff = isl_aff_param_on_domain_space_id(space, id);
7688 return isl_union_pw_aff_aff_on_domain(domain, aff);
7691 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7692 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7693 * needs to attain.
7694 * "res" collects the results.
7696 struct isl_union_pw_aff_pw_aff_on_domain_data {
7697 isl_pw_aff *pa;
7698 isl_union_pw_aff *res;
7701 /* Construct a piecewise affine expression that is equal to data->pa
7702 * on "domain" and add the result to data->res.
7704 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7706 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7707 isl_pw_aff *pa;
7708 isl_size dim;
7710 pa = isl_pw_aff_copy(data->pa);
7711 dim = isl_set_dim(domain, isl_dim_set);
7712 if (dim < 0)
7713 pa = isl_pw_aff_free(pa);
7714 pa = isl_pw_aff_from_range(pa);
7715 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7716 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7717 pa = isl_pw_aff_intersect_domain(pa, domain);
7718 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7720 return data->res ? isl_stat_ok : isl_stat_error;
7723 /* Return a union piecewise affine expression
7724 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7725 * have been aligned.
7727 * Construct an isl_pw_aff on each of the sets in "domain" and
7728 * collect the results.
7730 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7731 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7733 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7734 isl_space *space;
7736 space = isl_union_set_get_space(domain);
7737 data.res = isl_union_pw_aff_empty(space);
7738 data.pa = pa;
7739 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7740 data.res = isl_union_pw_aff_free(data.res);
7741 isl_union_set_free(domain);
7742 isl_pw_aff_free(pa);
7743 return data.res;
7746 /* Return a union piecewise affine expression
7747 * that is equal to "pa" on "domain".
7749 * Check that "pa" is a parametric expression,
7750 * align the parameters if needed and call
7751 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7753 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7754 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7756 isl_bool is_set;
7757 isl_bool equal_params;
7758 isl_space *domain_space, *pa_space;
7760 pa_space = isl_pw_aff_peek_space(pa);
7761 is_set = isl_space_is_set(pa_space);
7762 if (is_set < 0)
7763 goto error;
7764 if (!is_set)
7765 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7766 "expecting parametric expression", goto error);
7768 domain_space = isl_union_set_get_space(domain);
7769 pa_space = isl_pw_aff_get_space(pa);
7770 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7771 if (equal_params >= 0 && !equal_params) {
7772 isl_space *space;
7774 space = isl_space_align_params(domain_space, pa_space);
7775 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7776 domain = isl_union_set_align_params(domain, space);
7777 } else {
7778 isl_space_free(domain_space);
7779 isl_space_free(pa_space);
7782 if (equal_params < 0)
7783 goto error;
7784 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7785 error:
7786 isl_union_set_free(domain);
7787 isl_pw_aff_free(pa);
7788 return NULL;
7791 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7792 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7793 * "res" collects the results.
7795 struct isl_union_pw_aff_val_on_domain_data {
7796 isl_val *v;
7797 isl_union_pw_aff *res;
7800 /* Construct a piecewise affine expression that is equal to data->v
7801 * on "domain" and add the result to data->res.
7803 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7805 struct isl_union_pw_aff_val_on_domain_data *data = user;
7806 isl_pw_aff *pa;
7807 isl_val *v;
7809 v = isl_val_copy(data->v);
7810 pa = isl_pw_aff_val_on_domain(domain, v);
7811 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7813 return data->res ? isl_stat_ok : isl_stat_error;
7816 /* Return a union piecewise affine expression
7817 * that is equal to "v" on "domain".
7819 * Construct an isl_pw_aff on each of the sets in "domain" and
7820 * collect the results.
7822 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7823 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7825 struct isl_union_pw_aff_val_on_domain_data data;
7826 isl_space *space;
7828 space = isl_union_set_get_space(domain);
7829 data.res = isl_union_pw_aff_empty(space);
7830 data.v = v;
7831 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7832 data.res = isl_union_pw_aff_free(data.res);
7833 isl_union_set_free(domain);
7834 isl_val_free(v);
7835 return data.res;
7838 /* Construct a piecewise multi affine expression
7839 * that is equal to "pa" and add it to upma.
7841 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7842 void *user)
7844 isl_union_pw_multi_aff **upma = user;
7845 isl_pw_multi_aff *pma;
7847 pma = isl_pw_multi_aff_from_pw_aff(pa);
7848 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7850 return *upma ? isl_stat_ok : isl_stat_error;
7853 /* Construct and return a union piecewise multi affine expression
7854 * that is equal to the given union piecewise affine expression.
7856 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7857 __isl_take isl_union_pw_aff *upa)
7859 isl_space *space;
7860 isl_union_pw_multi_aff *upma;
7862 if (!upa)
7863 return NULL;
7865 space = isl_union_pw_aff_get_space(upa);
7866 upma = isl_union_pw_multi_aff_empty(space);
7868 if (isl_union_pw_aff_foreach_pw_aff(upa,
7869 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7870 upma = isl_union_pw_multi_aff_free(upma);
7872 isl_union_pw_aff_free(upa);
7873 return upma;
7876 /* Compute the set of elements in the domain of "pa" where it is zero and
7877 * add this set to "uset".
7879 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7881 isl_union_set **uset = (isl_union_set **)user;
7883 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7885 return *uset ? isl_stat_ok : isl_stat_error;
7888 /* Return a union set containing those elements in the domain
7889 * of "upa" where it is zero.
7891 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7892 __isl_take isl_union_pw_aff *upa)
7894 isl_union_set *zero;
7896 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7897 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7898 zero = isl_union_set_free(zero);
7900 isl_union_pw_aff_free(upa);
7901 return zero;
7904 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7905 * upma is the function that is plugged in.
7906 * pa is the current part of the function in which upma is plugged in.
7907 * res collects the results.
7909 struct isl_union_pw_aff_pullback_upma_data {
7910 isl_union_pw_multi_aff *upma;
7911 isl_pw_aff *pa;
7912 isl_union_pw_aff *res;
7915 /* Check if "pma" can be plugged into data->pa.
7916 * If so, perform the pullback and add the result to data->res.
7918 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7920 struct isl_union_pw_aff_pullback_upma_data *data = user;
7921 isl_pw_aff *pa;
7923 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7924 pma->dim, isl_dim_out)) {
7925 isl_pw_multi_aff_free(pma);
7926 return isl_stat_ok;
7929 pa = isl_pw_aff_copy(data->pa);
7930 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7932 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7934 return data->res ? isl_stat_ok : isl_stat_error;
7937 /* Check if any of the elements of data->upma can be plugged into pa,
7938 * add if so add the result to data->res.
7940 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7942 struct isl_union_pw_aff_pullback_upma_data *data = user;
7943 isl_stat r;
7945 data->pa = pa;
7946 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7947 &pa_pb_pma, data);
7948 isl_pw_aff_free(pa);
7950 return r;
7953 /* Compute the pullback of "upa" by the function represented by "upma".
7954 * In other words, plug in "upma" in "upa". The result contains
7955 * expressions defined over the domain space of "upma".
7957 * Run over all pairs of elements in "upa" and "upma", perform
7958 * the pullback when appropriate and collect the results.
7959 * If the hash value were based on the domain space rather than
7960 * the function space, then we could run through all elements
7961 * of "upma" and directly pick out the corresponding element of "upa".
7963 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7964 __isl_take isl_union_pw_aff *upa,
7965 __isl_take isl_union_pw_multi_aff *upma)
7967 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7968 isl_space *space;
7970 space = isl_union_pw_multi_aff_get_space(upma);
7971 upa = isl_union_pw_aff_align_params(upa, space);
7972 space = isl_union_pw_aff_get_space(upa);
7973 upma = isl_union_pw_multi_aff_align_params(upma, space);
7975 if (!upa || !upma)
7976 goto error;
7978 data.upma = upma;
7979 data.res = isl_union_pw_aff_alloc_same_size(upa);
7980 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7981 data.res = isl_union_pw_aff_free(data.res);
7983 isl_union_pw_aff_free(upa);
7984 isl_union_pw_multi_aff_free(upma);
7985 return data.res;
7986 error:
7987 isl_union_pw_aff_free(upa);
7988 isl_union_pw_multi_aff_free(upma);
7989 return NULL;
7992 #undef BASE
7993 #define BASE union_pw_aff
7994 #undef DOMBASE
7995 #define DOMBASE union_set
7997 #include <isl_multi_explicit_domain.c>
7998 #include <isl_multi_union_pw_aff_explicit_domain.c>
7999 #include <isl_multi_templ.c>
8000 #include <isl_multi_apply_set.c>
8001 #include <isl_multi_apply_union_set.c>
8002 #include <isl_multi_arith_templ.c>
8003 #include <isl_multi_coalesce.c>
8004 #include <isl_multi_floor.c>
8005 #include <isl_multi_from_base_templ.c>
8006 #include <isl_multi_gist.c>
8007 #include <isl_multi_align_set.c>
8008 #include <isl_multi_align_union_set.c>
8009 #include <isl_multi_intersect.c>
8010 #include <isl_multi_nan_templ.c>
8012 /* Does "mupa" have a non-trivial explicit domain?
8014 * The explicit domain, if present, is trivial if it represents
8015 * an (obviously) universe parameter set.
8017 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8018 __isl_keep isl_multi_union_pw_aff *mupa)
8020 isl_bool is_params, trivial;
8021 isl_set *set;
8023 if (!mupa)
8024 return isl_bool_error;
8025 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8026 return isl_bool_false;
8027 is_params = isl_union_set_is_params(mupa->u.dom);
8028 if (is_params < 0 || !is_params)
8029 return isl_bool_not(is_params);
8030 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8031 trivial = isl_set_plain_is_universe(set);
8032 isl_set_free(set);
8033 return isl_bool_not(trivial);
8036 /* Construct a multiple union piecewise affine expression
8037 * in the given space with value zero in each of the output dimensions.
8039 * Since there is no canonical zero value for
8040 * a union piecewise affine expression, we can only construct
8041 * a zero-dimensional "zero" value.
8043 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8044 __isl_take isl_space *space)
8046 isl_bool params;
8047 isl_size dim;
8049 if (!space)
8050 return NULL;
8052 params = isl_space_is_params(space);
8053 if (params < 0)
8054 goto error;
8055 if (params)
8056 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8057 "expecting proper set space", goto error);
8058 if (!isl_space_is_set(space))
8059 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8060 "expecting set space", goto error);
8061 dim = isl_space_dim(space, isl_dim_out);
8062 if (dim < 0)
8063 goto error;
8064 if (dim != 0)
8065 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8066 "expecting 0D space", goto error);
8068 return isl_multi_union_pw_aff_alloc(space);
8069 error:
8070 isl_space_free(space);
8071 return NULL;
8074 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8075 * with the actual sum on the shared domain and
8076 * the defined expression on the symmetric difference of the domains.
8078 * We simply iterate over the elements in both arguments and
8079 * call isl_union_pw_aff_union_add on each of them, if there is
8080 * at least one element.
8082 * Otherwise, the two expressions have an explicit domain and
8083 * the union of these explicit domains is computed.
8084 * This assumes that the explicit domains are either both in terms
8085 * of specific domains elements or both in terms of parameters.
8086 * However, if one of the expressions does not have any constraints
8087 * on its explicit domain, then this is allowed as well and the result
8088 * is the expression with no constraints on its explicit domain.
8090 static __isl_give isl_multi_union_pw_aff *
8091 isl_multi_union_pw_aff_union_add_aligned(
8092 __isl_take isl_multi_union_pw_aff *mupa1,
8093 __isl_take isl_multi_union_pw_aff *mupa2)
8095 isl_bool has_domain, is_params1, is_params2;
8097 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8098 goto error;
8099 if (mupa1->n > 0)
8100 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8101 &isl_union_pw_aff_union_add);
8102 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8103 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8104 goto error;
8106 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8107 if (has_domain < 0)
8108 goto error;
8109 if (!has_domain) {
8110 isl_multi_union_pw_aff_free(mupa2);
8111 return mupa1;
8113 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8114 if (has_domain < 0)
8115 goto error;
8116 if (!has_domain) {
8117 isl_multi_union_pw_aff_free(mupa1);
8118 return mupa2;
8121 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8122 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8123 if (is_params1 < 0 || is_params2 < 0)
8124 goto error;
8125 if (is_params1 != is_params2)
8126 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8127 isl_error_invalid,
8128 "cannot compute union of concrete domain and "
8129 "parameter constraints", goto error);
8130 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8131 if (!mupa1)
8132 goto error;
8133 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8134 isl_union_set_copy(mupa2->u.dom));
8135 if (!mupa1->u.dom)
8136 goto error;
8137 isl_multi_union_pw_aff_free(mupa2);
8138 return mupa1;
8139 error:
8140 isl_multi_union_pw_aff_free(mupa1);
8141 isl_multi_union_pw_aff_free(mupa2);
8142 return NULL;
8145 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8146 * with the actual sum on the shared domain and
8147 * the defined expression on the symmetric difference of the domains.
8149 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8150 __isl_take isl_multi_union_pw_aff *mupa1,
8151 __isl_take isl_multi_union_pw_aff *mupa2)
8153 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8154 &isl_multi_union_pw_aff_union_add_aligned);
8157 /* Construct and return a multi union piecewise affine expression
8158 * that is equal to the given multi affine expression.
8160 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8161 __isl_take isl_multi_aff *ma)
8163 isl_multi_pw_aff *mpa;
8165 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8166 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8169 /* Construct and return a multi union piecewise affine expression
8170 * that is equal to the given multi piecewise affine expression.
8172 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8173 __isl_take isl_multi_pw_aff *mpa)
8175 int i;
8176 isl_size n;
8177 isl_space *space;
8178 isl_multi_union_pw_aff *mupa;
8180 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8181 if (n < 0)
8182 mpa = isl_multi_pw_aff_free(mpa);
8183 if (!mpa)
8184 return NULL;
8186 space = isl_multi_pw_aff_get_space(mpa);
8187 space = isl_space_range(space);
8188 mupa = isl_multi_union_pw_aff_alloc(space);
8190 for (i = 0; i < n; ++i) {
8191 isl_pw_aff *pa;
8192 isl_union_pw_aff *upa;
8194 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8195 upa = isl_union_pw_aff_from_pw_aff(pa);
8196 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8199 isl_multi_pw_aff_free(mpa);
8201 return mupa;
8204 /* Extract the range space of "pma" and assign it to *space.
8205 * If *space has already been set (through a previous call to this function),
8206 * then check that the range space is the same.
8208 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8210 isl_space **space = user;
8211 isl_space *pma_space;
8212 isl_bool equal;
8214 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8215 isl_pw_multi_aff_free(pma);
8217 if (!pma_space)
8218 return isl_stat_error;
8219 if (!*space) {
8220 *space = pma_space;
8221 return isl_stat_ok;
8224 equal = isl_space_is_equal(pma_space, *space);
8225 isl_space_free(pma_space);
8227 if (equal < 0)
8228 return isl_stat_error;
8229 if (!equal)
8230 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8231 "range spaces not the same", return isl_stat_error);
8232 return isl_stat_ok;
8235 /* Construct and return a multi union piecewise affine expression
8236 * that is equal to the given union piecewise multi affine expression.
8238 * In order to be able to perform the conversion, the input
8239 * needs to be non-empty and may only involve a single range space.
8241 * If the resulting multi union piecewise affine expression has
8242 * an explicit domain, then assign it the domain of the input.
8243 * In other cases, the domain is stored in the individual elements.
8245 __isl_give isl_multi_union_pw_aff *
8246 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8247 __isl_take isl_union_pw_multi_aff *upma)
8249 isl_space *space = NULL;
8250 isl_multi_union_pw_aff *mupa;
8251 int i;
8252 isl_size n;
8254 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8255 if (n < 0)
8256 goto error;
8257 if (n == 0)
8258 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8259 "cannot extract range space from empty input",
8260 goto error);
8261 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8262 &space) < 0)
8263 goto error;
8265 if (!space)
8266 goto error;
8268 n = isl_space_dim(space, isl_dim_set);
8269 if (n < 0)
8270 space = isl_space_free(space);
8271 mupa = isl_multi_union_pw_aff_alloc(space);
8273 for (i = 0; i < n; ++i) {
8274 isl_union_pw_aff *upa;
8276 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8277 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8279 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8280 isl_union_set *dom;
8281 isl_union_pw_multi_aff *copy;
8283 copy = isl_union_pw_multi_aff_copy(upma);
8284 dom = isl_union_pw_multi_aff_domain(copy);
8285 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8288 isl_union_pw_multi_aff_free(upma);
8289 return mupa;
8290 error:
8291 isl_space_free(space);
8292 isl_union_pw_multi_aff_free(upma);
8293 return NULL;
8296 /* Try and create an isl_multi_union_pw_aff that is equivalent
8297 * to the given isl_union_map.
8298 * The isl_union_map is required to be single-valued in each space.
8299 * Moreover, it cannot be empty and all range spaces need to be the same.
8300 * Otherwise, an error is produced.
8302 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8303 __isl_take isl_union_map *umap)
8305 isl_union_pw_multi_aff *upma;
8307 upma = isl_union_pw_multi_aff_from_union_map(umap);
8308 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8311 /* Return a multiple union piecewise affine expression
8312 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8313 * have been aligned.
8315 * If the resulting multi union piecewise affine expression has
8316 * an explicit domain, then assign it the input domain.
8317 * In other cases, the domain is stored in the individual elements.
8319 static __isl_give isl_multi_union_pw_aff *
8320 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8321 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8323 int i;
8324 isl_size n;
8325 isl_space *space;
8326 isl_multi_union_pw_aff *mupa;
8328 n = isl_multi_val_dim(mv, isl_dim_set);
8329 if (!domain || n < 0)
8330 goto error;
8332 space = isl_multi_val_get_space(mv);
8333 mupa = isl_multi_union_pw_aff_alloc(space);
8334 for (i = 0; i < n; ++i) {
8335 isl_val *v;
8336 isl_union_pw_aff *upa;
8338 v = isl_multi_val_get_val(mv, i);
8339 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8341 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8343 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8344 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8345 isl_union_set_copy(domain));
8347 isl_union_set_free(domain);
8348 isl_multi_val_free(mv);
8349 return mupa;
8350 error:
8351 isl_union_set_free(domain);
8352 isl_multi_val_free(mv);
8353 return NULL;
8356 /* Return a multiple union piecewise affine expression
8357 * that is equal to "mv" on "domain".
8359 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8360 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8362 isl_bool equal_params;
8364 if (!domain || !mv)
8365 goto error;
8366 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8367 if (equal_params < 0)
8368 goto error;
8369 if (equal_params)
8370 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8371 domain, mv);
8372 domain = isl_union_set_align_params(domain,
8373 isl_multi_val_get_space(mv));
8374 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8375 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8376 error:
8377 isl_union_set_free(domain);
8378 isl_multi_val_free(mv);
8379 return NULL;
8382 /* Return a multiple union piecewise affine expression
8383 * that is equal to "ma" on "domain".
8385 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8386 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8388 isl_pw_multi_aff *pma;
8390 pma = isl_pw_multi_aff_from_multi_aff(ma);
8391 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8394 /* Return a multiple union piecewise affine expression
8395 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8396 * have been aligned.
8398 * If the resulting multi union piecewise affine expression has
8399 * an explicit domain, then assign it the input domain.
8400 * In other cases, the domain is stored in the individual elements.
8402 static __isl_give isl_multi_union_pw_aff *
8403 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8404 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8406 int i;
8407 isl_size n;
8408 isl_space *space;
8409 isl_multi_union_pw_aff *mupa;
8411 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8412 if (!domain || n < 0)
8413 goto error;
8414 space = isl_pw_multi_aff_get_space(pma);
8415 mupa = isl_multi_union_pw_aff_alloc(space);
8416 for (i = 0; i < n; ++i) {
8417 isl_pw_aff *pa;
8418 isl_union_pw_aff *upa;
8420 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8421 upa = isl_union_pw_aff_pw_aff_on_domain(
8422 isl_union_set_copy(domain), pa);
8423 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8425 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8426 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8427 isl_union_set_copy(domain));
8429 isl_union_set_free(domain);
8430 isl_pw_multi_aff_free(pma);
8431 return mupa;
8432 error:
8433 isl_union_set_free(domain);
8434 isl_pw_multi_aff_free(pma);
8435 return NULL;
8438 /* Return a multiple union piecewise affine expression
8439 * that is equal to "pma" on "domain".
8441 __isl_give isl_multi_union_pw_aff *
8442 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8443 __isl_take isl_pw_multi_aff *pma)
8445 isl_bool equal_params;
8446 isl_space *space;
8448 space = isl_pw_multi_aff_peek_space(pma);
8449 equal_params = isl_union_set_space_has_equal_params(domain, space);
8450 if (equal_params < 0)
8451 goto error;
8452 if (equal_params)
8453 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8454 domain, pma);
8455 domain = isl_union_set_align_params(domain,
8456 isl_pw_multi_aff_get_space(pma));
8457 pma = isl_pw_multi_aff_align_params(pma,
8458 isl_union_set_get_space(domain));
8459 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8460 pma);
8461 error:
8462 isl_union_set_free(domain);
8463 isl_pw_multi_aff_free(pma);
8464 return NULL;
8467 /* Return a union set containing those elements in the domains
8468 * of the elements of "mupa" where they are all zero.
8470 * If there are no elements, then simply return the entire domain.
8472 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8473 __isl_take isl_multi_union_pw_aff *mupa)
8475 int i;
8476 isl_size n;
8477 isl_union_pw_aff *upa;
8478 isl_union_set *zero;
8480 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8481 if (n < 0)
8482 mupa = isl_multi_union_pw_aff_free(mupa);
8483 if (!mupa)
8484 return NULL;
8486 if (n == 0)
8487 return isl_multi_union_pw_aff_domain(mupa);
8489 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8490 zero = isl_union_pw_aff_zero_union_set(upa);
8492 for (i = 1; i < n; ++i) {
8493 isl_union_set *zero_i;
8495 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8496 zero_i = isl_union_pw_aff_zero_union_set(upa);
8498 zero = isl_union_set_intersect(zero, zero_i);
8501 isl_multi_union_pw_aff_free(mupa);
8502 return zero;
8505 /* Construct a union map mapping the shared domain
8506 * of the union piecewise affine expressions to the range of "mupa"
8507 * in the special case of a 0D multi union piecewise affine expression.
8509 * Construct a map between the explicit domain of "mupa" and
8510 * the range space.
8511 * Note that this assumes that the domain consists of explicit elements.
8513 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8514 __isl_take isl_multi_union_pw_aff *mupa)
8516 isl_bool is_params;
8517 isl_space *space;
8518 isl_union_set *dom, *ran;
8520 space = isl_multi_union_pw_aff_get_space(mupa);
8521 dom = isl_multi_union_pw_aff_domain(mupa);
8522 ran = isl_union_set_from_set(isl_set_universe(space));
8524 is_params = isl_union_set_is_params(dom);
8525 if (is_params < 0)
8526 dom = isl_union_set_free(dom);
8527 else if (is_params)
8528 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8529 "cannot create union map from expression without "
8530 "explicit domain elements",
8531 dom = isl_union_set_free(dom));
8533 return isl_union_map_from_domain_and_range(dom, ran);
8536 /* Construct a union map mapping the shared domain
8537 * of the union piecewise affine expressions to the range of "mupa"
8538 * with each dimension in the range equated to the
8539 * corresponding union piecewise affine expression.
8541 * If the input is zero-dimensional, then construct a mapping
8542 * from its explicit domain.
8544 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8545 __isl_take isl_multi_union_pw_aff *mupa)
8547 int i;
8548 isl_size n;
8549 isl_space *space;
8550 isl_union_map *umap;
8551 isl_union_pw_aff *upa;
8553 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8554 if (n < 0)
8555 mupa = isl_multi_union_pw_aff_free(mupa);
8556 if (!mupa)
8557 return NULL;
8559 if (n == 0)
8560 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8562 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8563 umap = isl_union_map_from_union_pw_aff(upa);
8565 for (i = 1; i < n; ++i) {
8566 isl_union_map *umap_i;
8568 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8569 umap_i = isl_union_map_from_union_pw_aff(upa);
8570 umap = isl_union_map_flat_range_product(umap, umap_i);
8573 space = isl_multi_union_pw_aff_get_space(mupa);
8574 umap = isl_union_map_reset_range_space(umap, space);
8576 isl_multi_union_pw_aff_free(mupa);
8577 return umap;
8580 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8581 * "range" is the space from which to set the range space.
8582 * "res" collects the results.
8584 struct isl_union_pw_multi_aff_reset_range_space_data {
8585 isl_space *range;
8586 isl_union_pw_multi_aff *res;
8589 /* Replace the range space of "pma" by the range space of data->range and
8590 * add the result to data->res.
8592 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8594 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8595 isl_space *space;
8597 space = isl_pw_multi_aff_get_space(pma);
8598 space = isl_space_domain(space);
8599 space = isl_space_extend_domain_with_range(space,
8600 isl_space_copy(data->range));
8601 pma = isl_pw_multi_aff_reset_space(pma, space);
8602 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8604 return data->res ? isl_stat_ok : isl_stat_error;
8607 /* Replace the range space of all the piecewise affine expressions in "upma" by
8608 * the range space of "space".
8610 * This assumes that all these expressions have the same output dimension.
8612 * Since the spaces of the expressions change, so do their hash values.
8613 * We therefore need to create a new isl_union_pw_multi_aff.
8614 * Note that the hash value is currently computed based on the entire
8615 * space even though there can only be a single expression with a given
8616 * domain space.
8618 static __isl_give isl_union_pw_multi_aff *
8619 isl_union_pw_multi_aff_reset_range_space(
8620 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8622 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8623 isl_space *space_upma;
8625 space_upma = isl_union_pw_multi_aff_get_space(upma);
8626 data.res = isl_union_pw_multi_aff_empty(space_upma);
8627 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8628 &reset_range_space, &data) < 0)
8629 data.res = isl_union_pw_multi_aff_free(data.res);
8631 isl_space_free(space);
8632 isl_union_pw_multi_aff_free(upma);
8633 return data.res;
8636 /* Construct and return a union piecewise multi affine expression
8637 * that is equal to the given multi union piecewise affine expression,
8638 * in the special case of a 0D multi union piecewise affine expression.
8640 * Construct a union piecewise multi affine expression
8641 * on top of the explicit domain of the input.
8643 __isl_give isl_union_pw_multi_aff *
8644 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8645 __isl_take isl_multi_union_pw_aff *mupa)
8647 isl_space *space;
8648 isl_multi_val *mv;
8649 isl_union_set *domain;
8651 space = isl_multi_union_pw_aff_get_space(mupa);
8652 mv = isl_multi_val_zero(space);
8653 domain = isl_multi_union_pw_aff_domain(mupa);
8654 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8657 /* Construct and return a union piecewise multi affine expression
8658 * that is equal to the given multi union piecewise affine expression.
8660 * If the input is zero-dimensional, then
8661 * construct a union piecewise multi affine expression
8662 * on top of the explicit domain of the input.
8664 __isl_give isl_union_pw_multi_aff *
8665 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8666 __isl_take isl_multi_union_pw_aff *mupa)
8668 int i;
8669 isl_size n;
8670 isl_space *space;
8671 isl_union_pw_multi_aff *upma;
8672 isl_union_pw_aff *upa;
8674 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8675 if (n < 0)
8676 mupa = isl_multi_union_pw_aff_free(mupa);
8677 if (!mupa)
8678 return NULL;
8680 if (n == 0)
8681 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8683 space = isl_multi_union_pw_aff_get_space(mupa);
8684 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8685 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8687 for (i = 1; i < n; ++i) {
8688 isl_union_pw_multi_aff *upma_i;
8690 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8691 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8692 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8695 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8697 isl_multi_union_pw_aff_free(mupa);
8698 return upma;
8701 /* Intersect the range of "mupa" with "range",
8702 * in the special case where "mupa" is 0D.
8704 * Intersect the domain of "mupa" with the constraints on the parameters
8705 * of "range".
8707 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8708 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8710 range = isl_set_params(range);
8711 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8712 return mupa;
8715 /* Intersect the range of "mupa" with "range".
8716 * That is, keep only those domain elements that have a function value
8717 * in "range".
8719 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8720 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8722 isl_union_pw_multi_aff *upma;
8723 isl_union_set *domain;
8724 isl_space *space;
8725 isl_size n;
8726 int match;
8728 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8729 if (n < 0 || !range)
8730 goto error;
8732 space = isl_set_get_space(range);
8733 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8734 space, isl_dim_set);
8735 isl_space_free(space);
8736 if (match < 0)
8737 goto error;
8738 if (!match)
8739 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8740 "space don't match", goto error);
8741 if (n == 0)
8742 return mupa_intersect_range_0D(mupa, range);
8744 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8745 isl_multi_union_pw_aff_copy(mupa));
8746 domain = isl_union_set_from_set(range);
8747 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8748 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8750 return mupa;
8751 error:
8752 isl_multi_union_pw_aff_free(mupa);
8753 isl_set_free(range);
8754 return NULL;
8757 /* Return the shared domain of the elements of "mupa",
8758 * in the special case where "mupa" is zero-dimensional.
8760 * Return the explicit domain of "mupa".
8761 * Note that this domain may be a parameter set, either
8762 * because "mupa" is meant to live in a set space or
8763 * because no explicit domain has been set.
8765 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8766 __isl_take isl_multi_union_pw_aff *mupa)
8768 isl_union_set *dom;
8770 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8771 isl_multi_union_pw_aff_free(mupa);
8773 return dom;
8776 /* Return the shared domain of the elements of "mupa".
8778 * If "mupa" is zero-dimensional, then return its explicit domain.
8780 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8781 __isl_take isl_multi_union_pw_aff *mupa)
8783 int i;
8784 isl_size n;
8785 isl_union_pw_aff *upa;
8786 isl_union_set *dom;
8788 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8789 if (n < 0)
8790 mupa = isl_multi_union_pw_aff_free(mupa);
8791 if (!mupa)
8792 return NULL;
8794 if (n == 0)
8795 return isl_multi_union_pw_aff_domain_0D(mupa);
8797 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8798 dom = isl_union_pw_aff_domain(upa);
8799 for (i = 1; i < n; ++i) {
8800 isl_union_set *dom_i;
8802 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8803 dom_i = isl_union_pw_aff_domain(upa);
8804 dom = isl_union_set_intersect(dom, dom_i);
8807 isl_multi_union_pw_aff_free(mupa);
8808 return dom;
8811 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8812 * In particular, the spaces have been aligned.
8813 * The result is defined over the shared domain of the elements of "mupa"
8815 * We first extract the parametric constant part of "aff" and
8816 * define that over the shared domain.
8817 * Then we iterate over all input dimensions of "aff" and add the corresponding
8818 * multiples of the elements of "mupa".
8819 * Finally, we consider the integer divisions, calling the function
8820 * recursively to obtain an isl_union_pw_aff corresponding to the
8821 * integer division argument.
8823 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8824 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8826 int i;
8827 isl_size n_in, n_div;
8828 isl_union_pw_aff *upa;
8829 isl_union_set *uset;
8830 isl_val *v;
8831 isl_aff *cst;
8833 n_in = isl_aff_dim(aff, isl_dim_in);
8834 n_div = isl_aff_dim(aff, isl_dim_div);
8835 if (n_in < 0 || n_div < 0)
8836 goto error;
8838 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8839 cst = isl_aff_copy(aff);
8840 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8841 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8842 cst = isl_aff_project_domain_on_params(cst);
8843 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8845 for (i = 0; i < n_in; ++i) {
8846 isl_union_pw_aff *upa_i;
8848 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8849 continue;
8850 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8851 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8852 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8853 upa = isl_union_pw_aff_add(upa, upa_i);
8856 for (i = 0; i < n_div; ++i) {
8857 isl_aff *div;
8858 isl_union_pw_aff *upa_i;
8860 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8861 continue;
8862 div = isl_aff_get_div(aff, i);
8863 upa_i = multi_union_pw_aff_apply_aff(
8864 isl_multi_union_pw_aff_copy(mupa), div);
8865 upa_i = isl_union_pw_aff_floor(upa_i);
8866 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8867 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8868 upa = isl_union_pw_aff_add(upa, upa_i);
8871 isl_multi_union_pw_aff_free(mupa);
8872 isl_aff_free(aff);
8874 return upa;
8875 error:
8876 isl_multi_union_pw_aff_free(mupa);
8877 isl_aff_free(aff);
8878 return NULL;
8881 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8882 * with the domain of "aff".
8883 * Furthermore, the dimension of this space needs to be greater than zero.
8884 * The result is defined over the shared domain of the elements of "mupa"
8886 * We perform these checks and then hand over control to
8887 * multi_union_pw_aff_apply_aff.
8889 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8890 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8892 isl_size dim;
8893 isl_space *space1, *space2;
8894 isl_bool equal;
8896 mupa = isl_multi_union_pw_aff_align_params(mupa,
8897 isl_aff_get_space(aff));
8898 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8899 if (!mupa || !aff)
8900 goto error;
8902 space1 = isl_multi_union_pw_aff_get_space(mupa);
8903 space2 = isl_aff_get_domain_space(aff);
8904 equal = isl_space_is_equal(space1, space2);
8905 isl_space_free(space1);
8906 isl_space_free(space2);
8907 if (equal < 0)
8908 goto error;
8909 if (!equal)
8910 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8911 "spaces don't match", goto error);
8912 dim = isl_aff_dim(aff, isl_dim_in);
8913 if (dim < 0)
8914 goto error;
8915 if (dim == 0)
8916 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8917 "cannot determine domains", goto error);
8919 return multi_union_pw_aff_apply_aff(mupa, aff);
8920 error:
8921 isl_multi_union_pw_aff_free(mupa);
8922 isl_aff_free(aff);
8923 return NULL;
8926 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8927 * The space of "mupa" is known to be compatible with the domain of "ma".
8929 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8930 * on the domain of "mupa".
8932 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
8933 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8935 isl_union_set *dom;
8937 dom = isl_multi_union_pw_aff_domain(mupa);
8938 ma = isl_multi_aff_project_domain_on_params(ma);
8940 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
8943 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8944 * with the domain of "ma".
8945 * The result is defined over the shared domain of the elements of "mupa"
8947 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8948 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8950 isl_space *space1, *space2;
8951 isl_multi_union_pw_aff *res;
8952 isl_bool equal;
8953 int i;
8954 isl_size n_in, n_out;
8956 mupa = isl_multi_union_pw_aff_align_params(mupa,
8957 isl_multi_aff_get_space(ma));
8958 ma = isl_multi_aff_align_params(ma,
8959 isl_multi_union_pw_aff_get_space(mupa));
8960 n_in = isl_multi_aff_dim(ma, isl_dim_in);
8961 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8962 if (!mupa || n_in < 0 || n_out < 0)
8963 goto error;
8965 space1 = isl_multi_union_pw_aff_get_space(mupa);
8966 space2 = isl_multi_aff_get_domain_space(ma);
8967 equal = isl_space_is_equal(space1, space2);
8968 isl_space_free(space1);
8969 isl_space_free(space2);
8970 if (equal < 0)
8971 goto error;
8972 if (!equal)
8973 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8974 "spaces don't match", goto error);
8975 if (n_in == 0)
8976 return mupa_apply_multi_aff_0D(mupa, ma);
8978 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8979 res = isl_multi_union_pw_aff_alloc(space1);
8981 for (i = 0; i < n_out; ++i) {
8982 isl_aff *aff;
8983 isl_union_pw_aff *upa;
8985 aff = isl_multi_aff_get_aff(ma, i);
8986 upa = multi_union_pw_aff_apply_aff(
8987 isl_multi_union_pw_aff_copy(mupa), aff);
8988 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8991 isl_multi_aff_free(ma);
8992 isl_multi_union_pw_aff_free(mupa);
8993 return res;
8994 error:
8995 isl_multi_union_pw_aff_free(mupa);
8996 isl_multi_aff_free(ma);
8997 return NULL;
9000 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9001 * The space of "mupa" is known to be compatible with the domain of "pa".
9003 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9004 * on the domain of "mupa".
9006 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9007 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9009 isl_union_set *dom;
9011 dom = isl_multi_union_pw_aff_domain(mupa);
9012 pa = isl_pw_aff_project_domain_on_params(pa);
9014 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9017 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9018 * with the domain of "pa".
9019 * Furthermore, the dimension of this space needs to be greater than zero.
9020 * The result is defined over the shared domain of the elements of "mupa"
9022 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9023 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9025 int i;
9026 isl_bool equal;
9027 isl_size n_in;
9028 isl_space *space, *space2;
9029 isl_union_pw_aff *upa;
9031 mupa = isl_multi_union_pw_aff_align_params(mupa,
9032 isl_pw_aff_get_space(pa));
9033 pa = isl_pw_aff_align_params(pa,
9034 isl_multi_union_pw_aff_get_space(mupa));
9035 if (!mupa || !pa)
9036 goto error;
9038 space = isl_multi_union_pw_aff_get_space(mupa);
9039 space2 = isl_pw_aff_get_domain_space(pa);
9040 equal = isl_space_is_equal(space, space2);
9041 isl_space_free(space);
9042 isl_space_free(space2);
9043 if (equal < 0)
9044 goto error;
9045 if (!equal)
9046 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9047 "spaces don't match", goto error);
9048 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9049 if (n_in < 0)
9050 goto error;
9051 if (n_in == 0)
9052 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9054 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9055 upa = isl_union_pw_aff_empty(space);
9057 for (i = 0; i < pa->n; ++i) {
9058 isl_aff *aff;
9059 isl_set *domain;
9060 isl_multi_union_pw_aff *mupa_i;
9061 isl_union_pw_aff *upa_i;
9063 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9064 domain = isl_set_copy(pa->p[i].set);
9065 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9066 aff = isl_aff_copy(pa->p[i].aff);
9067 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9068 upa = isl_union_pw_aff_union_add(upa, upa_i);
9071 isl_multi_union_pw_aff_free(mupa);
9072 isl_pw_aff_free(pa);
9073 return upa;
9074 error:
9075 isl_multi_union_pw_aff_free(mupa);
9076 isl_pw_aff_free(pa);
9077 return NULL;
9080 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9081 * The space of "mupa" is known to be compatible with the domain of "pma".
9083 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9084 * on the domain of "mupa".
9086 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9087 __isl_take isl_multi_union_pw_aff *mupa,
9088 __isl_take isl_pw_multi_aff *pma)
9090 isl_union_set *dom;
9092 dom = isl_multi_union_pw_aff_domain(mupa);
9093 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9095 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9098 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9099 * with the domain of "pma".
9100 * The result is defined over the shared domain of the elements of "mupa"
9102 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9103 __isl_take isl_multi_union_pw_aff *mupa,
9104 __isl_take isl_pw_multi_aff *pma)
9106 isl_space *space1, *space2;
9107 isl_multi_union_pw_aff *res;
9108 isl_bool equal;
9109 int i;
9110 isl_size n_in, n_out;
9112 mupa = isl_multi_union_pw_aff_align_params(mupa,
9113 isl_pw_multi_aff_get_space(pma));
9114 pma = isl_pw_multi_aff_align_params(pma,
9115 isl_multi_union_pw_aff_get_space(mupa));
9116 if (!mupa || !pma)
9117 goto error;
9119 space1 = isl_multi_union_pw_aff_get_space(mupa);
9120 space2 = isl_pw_multi_aff_get_domain_space(pma);
9121 equal = isl_space_is_equal(space1, space2);
9122 isl_space_free(space1);
9123 isl_space_free(space2);
9124 if (equal < 0)
9125 goto error;
9126 if (!equal)
9127 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9128 "spaces don't match", goto error);
9129 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9130 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9131 if (n_in < 0 || n_out < 0)
9132 goto error;
9133 if (n_in == 0)
9134 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9136 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9137 res = isl_multi_union_pw_aff_alloc(space1);
9139 for (i = 0; i < n_out; ++i) {
9140 isl_pw_aff *pa;
9141 isl_union_pw_aff *upa;
9143 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9144 upa = isl_multi_union_pw_aff_apply_pw_aff(
9145 isl_multi_union_pw_aff_copy(mupa), pa);
9146 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9149 isl_pw_multi_aff_free(pma);
9150 isl_multi_union_pw_aff_free(mupa);
9151 return res;
9152 error:
9153 isl_multi_union_pw_aff_free(mupa);
9154 isl_pw_multi_aff_free(pma);
9155 return NULL;
9158 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9159 * If the explicit domain only keeps track of constraints on the parameters,
9160 * then only update those constraints.
9162 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9163 __isl_take isl_multi_union_pw_aff *mupa,
9164 __isl_keep isl_union_pw_multi_aff *upma)
9166 isl_bool is_params;
9168 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9169 return isl_multi_union_pw_aff_free(mupa);
9171 mupa = isl_multi_union_pw_aff_cow(mupa);
9172 if (!mupa)
9173 return NULL;
9175 is_params = isl_union_set_is_params(mupa->u.dom);
9176 if (is_params < 0)
9177 return isl_multi_union_pw_aff_free(mupa);
9179 upma = isl_union_pw_multi_aff_copy(upma);
9180 if (is_params)
9181 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9182 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9183 else
9184 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9185 mupa->u.dom, upma);
9186 if (!mupa->u.dom)
9187 return isl_multi_union_pw_aff_free(mupa);
9188 return mupa;
9191 /* Compute the pullback of "mupa" by the function represented by "upma".
9192 * In other words, plug in "upma" in "mupa". The result contains
9193 * expressions defined over the domain space of "upma".
9195 * Run over all elements of "mupa" and plug in "upma" in each of them.
9197 * If "mupa" has an explicit domain, then it is this domain
9198 * that needs to undergo a pullback instead, i.e., a preimage.
9200 __isl_give isl_multi_union_pw_aff *
9201 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9202 __isl_take isl_multi_union_pw_aff *mupa,
9203 __isl_take isl_union_pw_multi_aff *upma)
9205 int i;
9206 isl_size n;
9208 mupa = isl_multi_union_pw_aff_align_params(mupa,
9209 isl_union_pw_multi_aff_get_space(upma));
9210 upma = isl_union_pw_multi_aff_align_params(upma,
9211 isl_multi_union_pw_aff_get_space(mupa));
9212 mupa = isl_multi_union_pw_aff_cow(mupa);
9213 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9214 if (n < 0 || !upma)
9215 goto error;
9217 for (i = 0; i < n; ++i) {
9218 isl_union_pw_aff *upa;
9220 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9221 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9222 isl_union_pw_multi_aff_copy(upma));
9223 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9226 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9227 mupa = preimage_explicit_domain(mupa, upma);
9229 isl_union_pw_multi_aff_free(upma);
9230 return mupa;
9231 error:
9232 isl_multi_union_pw_aff_free(mupa);
9233 isl_union_pw_multi_aff_free(upma);
9234 return NULL;
9237 /* Extract the sequence of elements in "mupa" with domain space "space"
9238 * (ignoring parameters).
9240 * For the elements of "mupa" that are not defined on the specified space,
9241 * the corresponding element in the result is empty.
9243 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9244 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9246 int i;
9247 isl_size n;
9248 isl_space *space_mpa;
9249 isl_multi_pw_aff *mpa;
9251 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9252 if (n < 0 || !space)
9253 goto error;
9255 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9256 space = isl_space_replace_params(space, space_mpa);
9257 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9258 space_mpa);
9259 mpa = isl_multi_pw_aff_alloc(space_mpa);
9261 space = isl_space_from_domain(space);
9262 space = isl_space_add_dims(space, isl_dim_out, 1);
9263 for (i = 0; i < n; ++i) {
9264 isl_union_pw_aff *upa;
9265 isl_pw_aff *pa;
9267 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9268 pa = isl_union_pw_aff_extract_pw_aff(upa,
9269 isl_space_copy(space));
9270 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9271 isl_union_pw_aff_free(upa);
9274 isl_space_free(space);
9275 return mpa;
9276 error:
9277 isl_space_free(space);
9278 return NULL;
9281 /* Evaluate the affine function "aff" in the void point "pnt".
9282 * In particular, return the value NaN.
9284 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9285 __isl_take isl_point *pnt)
9287 isl_ctx *ctx;
9289 ctx = isl_point_get_ctx(pnt);
9290 isl_aff_free(aff);
9291 isl_point_free(pnt);
9292 return isl_val_nan(ctx);
9295 /* Evaluate the affine expression "aff"
9296 * in the coordinates (with denominator) "pnt".
9298 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9299 __isl_keep isl_vec *pnt)
9301 isl_int n, d;
9302 isl_ctx *ctx;
9303 isl_val *v;
9305 if (!aff || !pnt)
9306 return NULL;
9308 ctx = isl_vec_get_ctx(aff);
9309 isl_int_init(n);
9310 isl_int_init(d);
9311 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9312 isl_int_mul(d, aff->el[0], pnt->el[0]);
9313 v = isl_val_rat_from_isl_int(ctx, n, d);
9314 v = isl_val_normalize(v);
9315 isl_int_clear(n);
9316 isl_int_clear(d);
9318 return v;
9321 /* Check that the domain space of "aff" is equal to "space".
9323 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9324 __isl_keep isl_space *space)
9326 isl_bool ok;
9328 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9329 if (ok < 0)
9330 return isl_stat_error;
9331 if (!ok)
9332 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9333 "incompatible spaces", return isl_stat_error);
9334 return isl_stat_ok;
9337 /* Evaluate the affine function "aff" in "pnt".
9339 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9340 __isl_take isl_point *pnt)
9342 isl_bool is_void;
9343 isl_val *v;
9344 isl_local_space *ls;
9346 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9347 goto error;
9348 is_void = isl_point_is_void(pnt);
9349 if (is_void < 0)
9350 goto error;
9351 if (is_void)
9352 return eval_void(aff, pnt);
9354 ls = isl_aff_get_domain_local_space(aff);
9355 pnt = isl_local_space_lift_point(ls, pnt);
9357 v = eval(aff->v, isl_point_peek_vec(pnt));
9359 isl_aff_free(aff);
9360 isl_point_free(pnt);
9362 return v;
9363 error:
9364 isl_aff_free(aff);
9365 isl_point_free(pnt);
9366 return NULL;