isl_space_check_named_params: change error message
[isl.git] / isl_aff.c
blobcb98e9bd23f139da969487d9d719c1cc04f43026
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
25 #include <isl/id.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
54 __isl_take isl_vec *v)
56 isl_aff *aff;
58 if (!ls || !v)
59 goto error;
61 aff = isl_calloc_type(v->ctx, struct isl_aff);
62 if (!aff)
63 goto error;
65 aff->ref = 1;
66 aff->ls = ls;
67 aff->v = v;
69 return aff;
70 error:
71 isl_local_space_free(ls);
72 isl_vec_free(v);
73 return NULL;
76 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
78 isl_ctx *ctx;
79 isl_vec *v;
80 unsigned total;
82 if (!ls)
83 return NULL;
85 ctx = isl_local_space_get_ctx(ls);
86 if (!isl_local_space_divs_known(ls))
87 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
88 goto error);
89 if (!isl_local_space_is_set(ls))
90 isl_die(ctx, isl_error_invalid,
91 "domain of affine expression should be a set",
92 goto error);
94 total = isl_local_space_dim(ls, isl_dim_all);
95 v = isl_vec_alloc(ctx, 1 + 1 + total);
96 return isl_aff_alloc_vec(ls, v);
97 error:
98 isl_local_space_free(ls);
99 return NULL;
102 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
104 isl_aff *aff;
106 aff = isl_aff_alloc(ls);
107 if (!aff)
108 return NULL;
110 isl_int_set_si(aff->v->el[0], 1);
111 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
113 return aff;
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
129 isl_aff *aff;
131 aff = isl_aff_alloc(ls);
132 if (!aff)
133 return NULL;
135 isl_seq_clr(aff->v->el, aff->v->size);
137 return aff;
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
152 __isl_take isl_val *val)
154 isl_aff *aff;
156 if (!ls || !val)
157 goto error;
158 if (!isl_val_is_rat(val))
159 isl_die(isl_val_get_ctx(val), isl_error_invalid,
160 "expecting rational value", goto error);
162 aff = isl_aff_alloc(isl_local_space_copy(ls));
163 if (!aff)
164 goto error;
166 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
167 isl_int_set(aff->v->el[1], val->n);
168 isl_int_set(aff->v->el[0], val->d);
170 isl_local_space_free(ls);
171 isl_val_free(val);
172 return aff;
173 error:
174 isl_local_space_free(ls);
175 isl_val_free(val);
176 return NULL;
179 /* Return an affine expression that is equal to the specified dimension
180 * in "ls".
182 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
183 enum isl_dim_type type, unsigned pos)
185 isl_space *space;
186 isl_aff *aff;
188 if (!ls)
189 return NULL;
191 space = isl_local_space_get_space(ls);
192 if (!space)
193 goto error;
194 if (isl_space_is_map(space))
195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
196 "expecting (parameter) set space", goto error);
197 if (pos >= isl_local_space_dim(ls, type))
198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
199 "position out of bounds", goto error);
201 isl_space_free(space);
202 aff = isl_aff_alloc(ls);
203 if (!aff)
204 return NULL;
206 pos += isl_local_space_offset(aff->ls, type);
208 isl_int_set_si(aff->v->el[0], 1);
209 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
210 isl_int_set_si(aff->v->el[1 + pos], 1);
212 return aff;
213 error:
214 isl_local_space_free(ls);
215 isl_space_free(space);
216 return NULL;
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
223 enum isl_dim_type type, unsigned pos)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
228 /* Return an affine expression that is equal to the parameter
229 * in the domain space "space" with identifier "id".
231 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
232 __isl_take isl_space *space, __isl_take isl_id *id)
234 int pos;
235 isl_local_space *ls;
237 if (!space || !id)
238 goto error;
239 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
240 if (pos < 0)
241 isl_die(isl_space_get_ctx(space), isl_error_invalid,
242 "parameter not found in space", goto error);
243 isl_id_free(id);
244 ls = isl_local_space_from_space(space);
245 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
246 error:
247 isl_space_free(space);
248 isl_id_free(id);
249 return NULL;
252 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
254 if (!aff)
255 return NULL;
257 aff->ref++;
258 return aff;
261 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
263 if (!aff)
264 return NULL;
266 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
267 isl_vec_copy(aff->v));
270 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
272 if (!aff)
273 return NULL;
275 if (aff->ref == 1)
276 return aff;
277 aff->ref--;
278 return isl_aff_dup(aff);
281 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
283 if (!aff)
284 return NULL;
286 if (--aff->ref > 0)
287 return NULL;
289 isl_local_space_free(aff->ls);
290 isl_vec_free(aff->v);
292 free(aff);
294 return NULL;
297 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
299 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
302 /* Return a hash value that digests "aff".
304 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
306 uint32_t hash, ls_hash, v_hash;
308 if (!aff)
309 return 0;
311 hash = isl_hash_init();
312 ls_hash = isl_local_space_get_hash(aff->ls);
313 isl_hash_hash(hash, ls_hash);
314 v_hash = isl_vec_get_hash(aff->v);
315 isl_hash_hash(hash, v_hash);
317 return hash;
320 /* Externally, an isl_aff has a map space, but internally, the
321 * ls field corresponds to the domain of that space.
323 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
325 if (!aff)
326 return 0;
327 if (type == isl_dim_out)
328 return 1;
329 if (type == isl_dim_in)
330 type = isl_dim_set;
331 return isl_local_space_dim(aff->ls, type);
334 /* Return the position of the dimension of the given type and name
335 * in "aff".
336 * Return -1 if no such dimension can be found.
338 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
339 const char *name)
341 if (!aff)
342 return -1;
343 if (type == isl_dim_out)
344 return -1;
345 if (type == isl_dim_in)
346 type = isl_dim_set;
347 return isl_local_space_find_dim_by_name(aff->ls, type, name);
350 /* Return the domain space of "aff".
352 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
354 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
357 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
359 return isl_space_copy(isl_aff_peek_domain_space(aff));
362 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
364 isl_space *space;
365 if (!aff)
366 return NULL;
367 space = isl_local_space_get_space(aff->ls);
368 space = isl_space_from_domain(space);
369 space = isl_space_add_dims(space, isl_dim_out, 1);
370 return space;
373 __isl_give isl_local_space *isl_aff_get_domain_local_space(
374 __isl_keep isl_aff *aff)
376 return aff ? isl_local_space_copy(aff->ls) : NULL;
379 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
381 isl_local_space *ls;
382 if (!aff)
383 return NULL;
384 ls = isl_local_space_copy(aff->ls);
385 ls = isl_local_space_from_domain(ls);
386 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
387 return ls;
390 /* Return the local space of the domain of "aff".
391 * This may be either a copy or the local space itself
392 * if there is only one reference to "aff".
393 * This allows the local space to be modified inplace
394 * if both the expression and its local space have only a single reference.
395 * The caller is not allowed to modify "aff" between this call and
396 * a subsequent call to isl_aff_restore_domain_local_space.
397 * The only exception is that isl_aff_free can be called instead.
399 __isl_give isl_local_space *isl_aff_take_domain_local_space(
400 __isl_keep isl_aff *aff)
402 isl_local_space *ls;
404 if (!aff)
405 return NULL;
406 if (aff->ref != 1)
407 return isl_aff_get_domain_local_space(aff);
408 ls = aff->ls;
409 aff->ls = NULL;
410 return ls;
413 /* Set the local space of the domain of "aff" to "ls",
414 * where the local space of "aff" may be missing
415 * due to a preceding call to isl_aff_take_domain_local_space.
416 * However, in this case, "aff" only has a single reference and
417 * then the call to isl_aff_cow has no effect.
419 __isl_give isl_aff *isl_aff_restore_domain_local_space(
420 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
422 if (!aff || !ls)
423 goto error;
425 if (aff->ls == ls) {
426 isl_local_space_free(ls);
427 return aff;
430 aff = isl_aff_cow(aff);
431 if (!aff)
432 goto error;
433 isl_local_space_free(aff->ls);
434 aff->ls = ls;
436 return aff;
437 error:
438 isl_aff_free(aff);
439 isl_local_space_free(ls);
440 return NULL;
443 /* Externally, an isl_aff has a map space, but internally, the
444 * ls field corresponds to the domain of that space.
446 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
447 enum isl_dim_type type, unsigned pos)
449 if (!aff)
450 return NULL;
451 if (type == isl_dim_out)
452 return NULL;
453 if (type == isl_dim_in)
454 type = isl_dim_set;
455 return isl_local_space_get_dim_name(aff->ls, type, pos);
458 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
459 __isl_take isl_space *dim)
461 aff = isl_aff_cow(aff);
462 if (!aff || !dim)
463 goto error;
465 aff->ls = isl_local_space_reset_space(aff->ls, dim);
466 if (!aff->ls)
467 return isl_aff_free(aff);
469 return aff;
470 error:
471 isl_aff_free(aff);
472 isl_space_free(dim);
473 return NULL;
476 /* Reset the space of "aff". This function is called from isl_pw_templ.c
477 * and doesn't know if the space of an element object is represented
478 * directly or through its domain. It therefore passes along both.
480 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
481 __isl_take isl_space *space, __isl_take isl_space *domain)
483 isl_space_free(space);
484 return isl_aff_reset_domain_space(aff, domain);
487 /* Reorder the coefficients of the affine expression based
488 * on the given reordering.
489 * The reordering r is assumed to have been extended with the local
490 * variables.
492 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
493 __isl_take isl_reordering *r, int n_div)
495 isl_vec *res;
496 int i;
498 if (!vec || !r)
499 goto error;
501 res = isl_vec_alloc(vec->ctx,
502 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
503 if (!res)
504 goto error;
505 isl_seq_cpy(res->el, vec->el, 2);
506 isl_seq_clr(res->el + 2, res->size - 2);
507 for (i = 0; i < r->len; ++i)
508 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
510 isl_reordering_free(r);
511 isl_vec_free(vec);
512 return res;
513 error:
514 isl_vec_free(vec);
515 isl_reordering_free(r);
516 return NULL;
519 /* Reorder the dimensions of the domain of "aff" according
520 * to the given reordering.
522 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
523 __isl_take isl_reordering *r)
525 aff = isl_aff_cow(aff);
526 if (!aff)
527 goto error;
529 r = isl_reordering_extend(r, aff->ls->div->n_row);
530 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
531 aff->ls->div->n_row);
532 aff->ls = isl_local_space_realign(aff->ls, r);
534 if (!aff->v || !aff->ls)
535 return isl_aff_free(aff);
537 return aff;
538 error:
539 isl_aff_free(aff);
540 isl_reordering_free(r);
541 return NULL;
544 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
545 __isl_take isl_space *model)
547 isl_bool equal_params;
549 if (!aff || !model)
550 goto error;
552 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
553 if (equal_params < 0)
554 goto error;
555 if (!equal_params) {
556 isl_reordering *exp;
558 model = isl_space_drop_dims(model, isl_dim_in,
559 0, isl_space_dim(model, isl_dim_in));
560 model = isl_space_drop_dims(model, isl_dim_out,
561 0, isl_space_dim(model, isl_dim_out));
562 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
563 exp = isl_reordering_extend_space(exp,
564 isl_aff_get_domain_space(aff));
565 aff = isl_aff_realign_domain(aff, exp);
568 isl_space_free(model);
569 return aff;
570 error:
571 isl_space_free(model);
572 isl_aff_free(aff);
573 return NULL;
576 /* Is "aff" obviously equal to zero?
578 * If the denominator is zero, then "aff" is not equal to zero.
580 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
582 if (!aff)
583 return isl_bool_error;
585 if (isl_int_is_zero(aff->v->el[0]))
586 return isl_bool_false;
587 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
590 /* Does "aff" represent NaN?
592 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
594 if (!aff)
595 return isl_bool_error;
597 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
600 /* Are "aff1" and "aff2" obviously equal?
602 * NaN is not equal to anything, not even to another NaN.
604 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
605 __isl_keep isl_aff *aff2)
607 isl_bool equal;
609 if (!aff1 || !aff2)
610 return isl_bool_error;
612 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
613 return isl_bool_false;
615 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
616 if (equal < 0 || !equal)
617 return equal;
619 return isl_vec_is_equal(aff1->v, aff2->v);
622 /* Return the common denominator of "aff" in "v".
624 * We cannot return anything meaningful in case of a NaN.
626 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
628 if (!aff)
629 return isl_stat_error;
630 if (isl_aff_is_nan(aff))
631 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
632 "cannot get denominator of NaN", return isl_stat_error);
633 isl_int_set(*v, aff->v->el[0]);
634 return isl_stat_ok;
637 /* Return the common denominator of "aff".
639 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
641 isl_ctx *ctx;
643 if (!aff)
644 return NULL;
646 ctx = isl_aff_get_ctx(aff);
647 if (isl_aff_is_nan(aff))
648 return isl_val_nan(ctx);
649 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
652 /* Return the constant term of "aff".
654 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
656 isl_ctx *ctx;
657 isl_val *v;
659 if (!aff)
660 return NULL;
662 ctx = isl_aff_get_ctx(aff);
663 if (isl_aff_is_nan(aff))
664 return isl_val_nan(ctx);
665 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
666 return isl_val_normalize(v);
669 /* Return the coefficient of the variable of type "type" at position "pos"
670 * of "aff".
672 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
673 enum isl_dim_type type, int pos)
675 isl_ctx *ctx;
676 isl_val *v;
678 if (!aff)
679 return NULL;
681 ctx = isl_aff_get_ctx(aff);
682 if (type == isl_dim_out)
683 isl_die(ctx, isl_error_invalid,
684 "output/set dimension does not have a coefficient",
685 return NULL);
686 if (type == isl_dim_in)
687 type = isl_dim_set;
689 if (pos >= isl_local_space_dim(aff->ls, type))
690 isl_die(ctx, isl_error_invalid,
691 "position out of bounds", return NULL);
693 if (isl_aff_is_nan(aff))
694 return isl_val_nan(ctx);
695 pos += isl_local_space_offset(aff->ls, type);
696 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
697 return isl_val_normalize(v);
700 /* Return the sign of the coefficient of the variable of type "type"
701 * at position "pos" of "aff".
703 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
704 int pos)
706 isl_ctx *ctx;
708 if (!aff)
709 return 0;
711 ctx = isl_aff_get_ctx(aff);
712 if (type == isl_dim_out)
713 isl_die(ctx, isl_error_invalid,
714 "output/set dimension does not have a coefficient",
715 return 0);
716 if (type == isl_dim_in)
717 type = isl_dim_set;
719 if (pos >= isl_local_space_dim(aff->ls, type))
720 isl_die(ctx, isl_error_invalid,
721 "position out of bounds", return 0);
723 pos += isl_local_space_offset(aff->ls, type);
724 return isl_int_sgn(aff->v->el[1 + pos]);
727 /* Replace the numerator of the constant term of "aff" by "v".
729 * A NaN is unaffected by this operation.
731 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
733 if (!aff)
734 return NULL;
735 if (isl_aff_is_nan(aff))
736 return aff;
737 aff = isl_aff_cow(aff);
738 if (!aff)
739 return NULL;
741 aff->v = isl_vec_cow(aff->v);
742 if (!aff->v)
743 return isl_aff_free(aff);
745 isl_int_set(aff->v->el[1], v);
747 return aff;
750 /* Replace the constant term of "aff" by "v".
752 * A NaN is unaffected by this operation.
754 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
755 __isl_take isl_val *v)
757 if (!aff || !v)
758 goto error;
760 if (isl_aff_is_nan(aff)) {
761 isl_val_free(v);
762 return aff;
765 if (!isl_val_is_rat(v))
766 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
767 "expecting rational value", goto error);
769 if (isl_int_eq(aff->v->el[1], v->n) &&
770 isl_int_eq(aff->v->el[0], v->d)) {
771 isl_val_free(v);
772 return aff;
775 aff = isl_aff_cow(aff);
776 if (!aff)
777 goto error;
778 aff->v = isl_vec_cow(aff->v);
779 if (!aff->v)
780 goto error;
782 if (isl_int_eq(aff->v->el[0], v->d)) {
783 isl_int_set(aff->v->el[1], v->n);
784 } else if (isl_int_is_one(v->d)) {
785 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
786 } else {
787 isl_seq_scale(aff->v->el + 1,
788 aff->v->el + 1, v->d, aff->v->size - 1);
789 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
790 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
791 aff->v = isl_vec_normalize(aff->v);
792 if (!aff->v)
793 goto error;
796 isl_val_free(v);
797 return aff;
798 error:
799 isl_aff_free(aff);
800 isl_val_free(v);
801 return NULL;
804 /* Add "v" to the constant term of "aff".
806 * A NaN is unaffected by this operation.
808 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
810 if (isl_int_is_zero(v))
811 return aff;
813 if (!aff)
814 return NULL;
815 if (isl_aff_is_nan(aff))
816 return aff;
817 aff = isl_aff_cow(aff);
818 if (!aff)
819 return NULL;
821 aff->v = isl_vec_cow(aff->v);
822 if (!aff->v)
823 return isl_aff_free(aff);
825 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
827 return aff;
830 /* Add "v" to the constant term of "aff".
832 * A NaN is unaffected by this operation.
834 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
835 __isl_take isl_val *v)
837 if (!aff || !v)
838 goto error;
840 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
841 isl_val_free(v);
842 return aff;
845 if (!isl_val_is_rat(v))
846 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
847 "expecting rational value", goto error);
849 aff = isl_aff_cow(aff);
850 if (!aff)
851 goto error;
853 aff->v = isl_vec_cow(aff->v);
854 if (!aff->v)
855 goto error;
857 if (isl_int_is_one(v->d)) {
858 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
859 } else if (isl_int_eq(aff->v->el[0], v->d)) {
860 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
861 aff->v = isl_vec_normalize(aff->v);
862 if (!aff->v)
863 goto error;
864 } else {
865 isl_seq_scale(aff->v->el + 1,
866 aff->v->el + 1, v->d, aff->v->size - 1);
867 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
868 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
869 aff->v = isl_vec_normalize(aff->v);
870 if (!aff->v)
871 goto error;
874 isl_val_free(v);
875 return aff;
876 error:
877 isl_aff_free(aff);
878 isl_val_free(v);
879 return NULL;
882 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
884 isl_int t;
886 isl_int_init(t);
887 isl_int_set_si(t, v);
888 aff = isl_aff_add_constant(aff, t);
889 isl_int_clear(t);
891 return aff;
894 /* Add "v" to the numerator of the constant term of "aff".
896 * A NaN is unaffected by this operation.
898 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
900 if (isl_int_is_zero(v))
901 return aff;
903 if (!aff)
904 return NULL;
905 if (isl_aff_is_nan(aff))
906 return aff;
907 aff = isl_aff_cow(aff);
908 if (!aff)
909 return NULL;
911 aff->v = isl_vec_cow(aff->v);
912 if (!aff->v)
913 return isl_aff_free(aff);
915 isl_int_add(aff->v->el[1], aff->v->el[1], v);
917 return aff;
920 /* Add "v" to the numerator of the constant term of "aff".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
926 isl_int t;
928 if (v == 0)
929 return aff;
931 isl_int_init(t);
932 isl_int_set_si(t, v);
933 aff = isl_aff_add_constant_num(aff, t);
934 isl_int_clear(t);
936 return aff;
939 /* Replace the numerator of the constant term of "aff" by "v".
941 * A NaN is unaffected by this operation.
943 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
945 if (!aff)
946 return NULL;
947 if (isl_aff_is_nan(aff))
948 return aff;
949 aff = isl_aff_cow(aff);
950 if (!aff)
951 return NULL;
953 aff->v = isl_vec_cow(aff->v);
954 if (!aff->v)
955 return isl_aff_free(aff);
957 isl_int_set_si(aff->v->el[1], v);
959 return aff;
962 /* Replace the numerator of the coefficient of the variable of type "type"
963 * at position "pos" of "aff" by "v".
965 * A NaN is unaffected by this operation.
967 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
968 enum isl_dim_type type, int pos, isl_int v)
970 if (!aff)
971 return NULL;
973 if (type == isl_dim_out)
974 isl_die(aff->v->ctx, isl_error_invalid,
975 "output/set dimension does not have a coefficient",
976 return isl_aff_free(aff));
977 if (type == isl_dim_in)
978 type = isl_dim_set;
980 if (pos >= isl_local_space_dim(aff->ls, type))
981 isl_die(aff->v->ctx, isl_error_invalid,
982 "position out of bounds", return isl_aff_free(aff));
984 if (isl_aff_is_nan(aff))
985 return aff;
986 aff = isl_aff_cow(aff);
987 if (!aff)
988 return NULL;
990 aff->v = isl_vec_cow(aff->v);
991 if (!aff->v)
992 return isl_aff_free(aff);
994 pos += isl_local_space_offset(aff->ls, type);
995 isl_int_set(aff->v->el[1 + pos], v);
997 return aff;
1000 /* Replace the numerator of the coefficient of the variable of type "type"
1001 * at position "pos" of "aff" by "v".
1003 * A NaN is unaffected by this operation.
1005 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1006 enum isl_dim_type type, int pos, int v)
1008 if (!aff)
1009 return NULL;
1011 if (type == isl_dim_out)
1012 isl_die(aff->v->ctx, isl_error_invalid,
1013 "output/set dimension does not have a coefficient",
1014 return isl_aff_free(aff));
1015 if (type == isl_dim_in)
1016 type = isl_dim_set;
1018 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1019 isl_die(aff->v->ctx, isl_error_invalid,
1020 "position out of bounds", return isl_aff_free(aff));
1022 if (isl_aff_is_nan(aff))
1023 return aff;
1024 pos += isl_local_space_offset(aff->ls, type);
1025 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1026 return aff;
1028 aff = isl_aff_cow(aff);
1029 if (!aff)
1030 return NULL;
1032 aff->v = isl_vec_cow(aff->v);
1033 if (!aff->v)
1034 return isl_aff_free(aff);
1036 isl_int_set_si(aff->v->el[1 + pos], v);
1038 return aff;
1041 /* Replace the coefficient of the variable of type "type" at position "pos"
1042 * of "aff" by "v".
1044 * A NaN is unaffected by this operation.
1046 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1047 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1049 if (!aff || !v)
1050 goto error;
1052 if (type == isl_dim_out)
1053 isl_die(aff->v->ctx, isl_error_invalid,
1054 "output/set dimension does not have a coefficient",
1055 goto error);
1056 if (type == isl_dim_in)
1057 type = isl_dim_set;
1059 if (pos >= isl_local_space_dim(aff->ls, type))
1060 isl_die(aff->v->ctx, isl_error_invalid,
1061 "position out of bounds", goto error);
1063 if (isl_aff_is_nan(aff)) {
1064 isl_val_free(v);
1065 return aff;
1067 if (!isl_val_is_rat(v))
1068 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1069 "expecting rational value", goto error);
1071 pos += isl_local_space_offset(aff->ls, type);
1072 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1073 isl_int_eq(aff->v->el[0], v->d)) {
1074 isl_val_free(v);
1075 return aff;
1078 aff = isl_aff_cow(aff);
1079 if (!aff)
1080 goto error;
1081 aff->v = isl_vec_cow(aff->v);
1082 if (!aff->v)
1083 goto error;
1085 if (isl_int_eq(aff->v->el[0], v->d)) {
1086 isl_int_set(aff->v->el[1 + pos], v->n);
1087 } else if (isl_int_is_one(v->d)) {
1088 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1089 } else {
1090 isl_seq_scale(aff->v->el + 1,
1091 aff->v->el + 1, v->d, aff->v->size - 1);
1092 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1093 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1094 aff->v = isl_vec_normalize(aff->v);
1095 if (!aff->v)
1096 goto error;
1099 isl_val_free(v);
1100 return aff;
1101 error:
1102 isl_aff_free(aff);
1103 isl_val_free(v);
1104 return NULL;
1107 /* Add "v" to the coefficient of the variable of type "type"
1108 * at position "pos" of "aff".
1110 * A NaN is unaffected by this operation.
1112 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1113 enum isl_dim_type type, int pos, isl_int v)
1115 if (!aff)
1116 return NULL;
1118 if (type == isl_dim_out)
1119 isl_die(aff->v->ctx, isl_error_invalid,
1120 "output/set dimension does not have a coefficient",
1121 return isl_aff_free(aff));
1122 if (type == isl_dim_in)
1123 type = isl_dim_set;
1125 if (pos >= isl_local_space_dim(aff->ls, type))
1126 isl_die(aff->v->ctx, isl_error_invalid,
1127 "position out of bounds", return isl_aff_free(aff));
1129 if (isl_aff_is_nan(aff))
1130 return aff;
1131 aff = isl_aff_cow(aff);
1132 if (!aff)
1133 return NULL;
1135 aff->v = isl_vec_cow(aff->v);
1136 if (!aff->v)
1137 return isl_aff_free(aff);
1139 pos += isl_local_space_offset(aff->ls, type);
1140 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1142 return aff;
1145 /* Add "v" to the coefficient of the variable of type "type"
1146 * at position "pos" of "aff".
1148 * A NaN is unaffected by this operation.
1150 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1151 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1153 if (!aff || !v)
1154 goto error;
1156 if (isl_val_is_zero(v)) {
1157 isl_val_free(v);
1158 return aff;
1161 if (type == isl_dim_out)
1162 isl_die(aff->v->ctx, isl_error_invalid,
1163 "output/set dimension does not have a coefficient",
1164 goto error);
1165 if (type == isl_dim_in)
1166 type = isl_dim_set;
1168 if (pos >= isl_local_space_dim(aff->ls, type))
1169 isl_die(aff->v->ctx, isl_error_invalid,
1170 "position out of bounds", goto error);
1172 if (isl_aff_is_nan(aff)) {
1173 isl_val_free(v);
1174 return aff;
1176 if (!isl_val_is_rat(v))
1177 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1178 "expecting rational value", goto error);
1180 aff = isl_aff_cow(aff);
1181 if (!aff)
1182 goto error;
1184 aff->v = isl_vec_cow(aff->v);
1185 if (!aff->v)
1186 goto error;
1188 pos += isl_local_space_offset(aff->ls, type);
1189 if (isl_int_is_one(v->d)) {
1190 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1191 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1192 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1193 aff->v = isl_vec_normalize(aff->v);
1194 if (!aff->v)
1195 goto error;
1196 } else {
1197 isl_seq_scale(aff->v->el + 1,
1198 aff->v->el + 1, v->d, aff->v->size - 1);
1199 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1200 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1201 aff->v = isl_vec_normalize(aff->v);
1202 if (!aff->v)
1203 goto error;
1206 isl_val_free(v);
1207 return aff;
1208 error:
1209 isl_aff_free(aff);
1210 isl_val_free(v);
1211 return NULL;
1214 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1215 enum isl_dim_type type, int pos, int v)
1217 isl_int t;
1219 isl_int_init(t);
1220 isl_int_set_si(t, v);
1221 aff = isl_aff_add_coefficient(aff, type, pos, t);
1222 isl_int_clear(t);
1224 return aff;
1227 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1229 if (!aff)
1230 return NULL;
1232 return isl_local_space_get_div(aff->ls, pos);
1235 /* Return the negation of "aff".
1237 * As a special case, -NaN = NaN.
1239 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1241 if (!aff)
1242 return NULL;
1243 if (isl_aff_is_nan(aff))
1244 return aff;
1245 aff = isl_aff_cow(aff);
1246 if (!aff)
1247 return NULL;
1248 aff->v = isl_vec_cow(aff->v);
1249 if (!aff->v)
1250 return isl_aff_free(aff);
1252 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1254 return aff;
1257 /* Remove divs from the local space that do not appear in the affine
1258 * expression.
1259 * We currently only remove divs at the end.
1260 * Some intermediate divs may also not appear directly in the affine
1261 * expression, but we would also need to check that no other divs are
1262 * defined in terms of them.
1264 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1266 int pos;
1267 int off;
1268 int n;
1270 if (!aff)
1271 return NULL;
1273 n = isl_local_space_dim(aff->ls, isl_dim_div);
1274 off = isl_local_space_offset(aff->ls, isl_dim_div);
1276 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1277 if (pos == n)
1278 return aff;
1280 aff = isl_aff_cow(aff);
1281 if (!aff)
1282 return NULL;
1284 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1285 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1286 if (!aff->ls || !aff->v)
1287 return isl_aff_free(aff);
1289 return aff;
1292 /* Look for any divs in the aff->ls with a denominator equal to one
1293 * and plug them into the affine expression and any subsequent divs
1294 * that may reference the div.
1296 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1298 int i, n;
1299 int len;
1300 isl_int v;
1301 isl_vec *vec;
1302 isl_local_space *ls;
1303 unsigned pos;
1305 if (!aff)
1306 return NULL;
1308 n = isl_local_space_dim(aff->ls, isl_dim_div);
1309 len = aff->v->size;
1310 for (i = 0; i < n; ++i) {
1311 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1312 continue;
1313 ls = isl_local_space_copy(aff->ls);
1314 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1315 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1316 vec = isl_vec_copy(aff->v);
1317 vec = isl_vec_cow(vec);
1318 if (!ls || !vec)
1319 goto error;
1321 isl_int_init(v);
1323 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1324 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1325 len, len, v);
1327 isl_int_clear(v);
1329 isl_vec_free(aff->v);
1330 aff->v = vec;
1331 isl_local_space_free(aff->ls);
1332 aff->ls = ls;
1335 return aff;
1336 error:
1337 isl_vec_free(vec);
1338 isl_local_space_free(ls);
1339 return isl_aff_free(aff);
1342 /* Look for any divs j that appear with a unit coefficient inside
1343 * the definitions of other divs i and plug them into the definitions
1344 * of the divs i.
1346 * In particular, an expression of the form
1348 * floor((f(..) + floor(g(..)/n))/m)
1350 * is simplified to
1352 * floor((n * f(..) + g(..))/(n * m))
1354 * This simplification is correct because we can move the expression
1355 * f(..) into the inner floor in the original expression to obtain
1357 * floor(floor((n * f(..) + g(..))/n)/m)
1359 * from which we can derive the simplified expression.
1361 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1363 int i, j, n;
1364 int off;
1366 if (!aff)
1367 return NULL;
1369 n = isl_local_space_dim(aff->ls, isl_dim_div);
1370 off = isl_local_space_offset(aff->ls, isl_dim_div);
1371 for (i = 1; i < n; ++i) {
1372 for (j = 0; j < i; ++j) {
1373 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1374 continue;
1375 aff->ls = isl_local_space_substitute_seq(aff->ls,
1376 isl_dim_div, j, aff->ls->div->row[j],
1377 aff->v->size, i, 1);
1378 if (!aff->ls)
1379 return isl_aff_free(aff);
1383 return aff;
1386 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1388 * Even though this function is only called on isl_affs with a single
1389 * reference, we are careful to only change aff->v and aff->ls together.
1391 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1393 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1394 isl_local_space *ls;
1395 isl_vec *v;
1397 ls = isl_local_space_copy(aff->ls);
1398 ls = isl_local_space_swap_div(ls, a, b);
1399 v = isl_vec_copy(aff->v);
1400 v = isl_vec_cow(v);
1401 if (!ls || !v)
1402 goto error;
1404 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1405 isl_vec_free(aff->v);
1406 aff->v = v;
1407 isl_local_space_free(aff->ls);
1408 aff->ls = ls;
1410 return aff;
1411 error:
1412 isl_vec_free(v);
1413 isl_local_space_free(ls);
1414 return isl_aff_free(aff);
1417 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1419 * We currently do not actually remove div "b", but simply add its
1420 * coefficient to that of "a" and then zero it out.
1422 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1424 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1426 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1427 return aff;
1429 aff->v = isl_vec_cow(aff->v);
1430 if (!aff->v)
1431 return isl_aff_free(aff);
1433 isl_int_add(aff->v->el[1 + off + a],
1434 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1435 isl_int_set_si(aff->v->el[1 + off + b], 0);
1437 return aff;
1440 /* Sort the divs in the local space of "aff" according to
1441 * the comparison function "cmp_row" in isl_local_space.c,
1442 * combining the coefficients of identical divs.
1444 * Reordering divs does not change the semantics of "aff",
1445 * so there is no need to call isl_aff_cow.
1446 * Moreover, this function is currently only called on isl_affs
1447 * with a single reference.
1449 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1451 int i, j, n;
1453 if (!aff)
1454 return NULL;
1456 n = isl_aff_dim(aff, isl_dim_div);
1457 for (i = 1; i < n; ++i) {
1458 for (j = i - 1; j >= 0; --j) {
1459 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1460 if (cmp < 0)
1461 break;
1462 if (cmp == 0)
1463 aff = merge_divs(aff, j, j + 1);
1464 else
1465 aff = swap_div(aff, j, j + 1);
1466 if (!aff)
1467 return NULL;
1471 return aff;
1474 /* Normalize the representation of "aff".
1476 * This function should only be called of "new" isl_affs, i.e.,
1477 * with only a single reference. We therefore do not need to
1478 * worry about affecting other instances.
1480 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1482 if (!aff)
1483 return NULL;
1484 aff->v = isl_vec_normalize(aff->v);
1485 if (!aff->v)
1486 return isl_aff_free(aff);
1487 aff = plug_in_integral_divs(aff);
1488 aff = plug_in_unit_divs(aff);
1489 aff = sort_divs(aff);
1490 aff = isl_aff_remove_unused_divs(aff);
1491 return aff;
1494 /* Given f, return floor(f).
1495 * If f is an integer expression, then just return f.
1496 * If f is a constant, then return the constant floor(f).
1497 * Otherwise, if f = g/m, write g = q m + r,
1498 * create a new div d = [r/m] and return the expression q + d.
1499 * The coefficients in r are taken to lie between -m/2 and m/2.
1501 * reduce_div_coefficients performs the same normalization.
1503 * As a special case, floor(NaN) = NaN.
1505 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1507 int i;
1508 int size;
1509 isl_ctx *ctx;
1510 isl_vec *div;
1512 if (!aff)
1513 return NULL;
1515 if (isl_aff_is_nan(aff))
1516 return aff;
1517 if (isl_int_is_one(aff->v->el[0]))
1518 return aff;
1520 aff = isl_aff_cow(aff);
1521 if (!aff)
1522 return NULL;
1524 aff->v = isl_vec_cow(aff->v);
1525 if (!aff->v)
1526 return isl_aff_free(aff);
1528 if (isl_aff_is_cst(aff)) {
1529 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1530 isl_int_set_si(aff->v->el[0], 1);
1531 return aff;
1534 div = isl_vec_copy(aff->v);
1535 div = isl_vec_cow(div);
1536 if (!div)
1537 return isl_aff_free(aff);
1539 ctx = isl_aff_get_ctx(aff);
1540 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1541 for (i = 1; i < aff->v->size; ++i) {
1542 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1543 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1544 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1545 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1546 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1550 aff->ls = isl_local_space_add_div(aff->ls, div);
1551 if (!aff->ls)
1552 return isl_aff_free(aff);
1554 size = aff->v->size;
1555 aff->v = isl_vec_extend(aff->v, size + 1);
1556 if (!aff->v)
1557 return isl_aff_free(aff);
1558 isl_int_set_si(aff->v->el[0], 1);
1559 isl_int_set_si(aff->v->el[size], 1);
1561 aff = isl_aff_normalize(aff);
1563 return aff;
1566 /* Compute
1568 * aff mod m = aff - m * floor(aff/m)
1570 * with m an integer value.
1572 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1573 __isl_take isl_val *m)
1575 isl_aff *res;
1577 if (!aff || !m)
1578 goto error;
1580 if (!isl_val_is_int(m))
1581 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1582 "expecting integer modulo", goto error);
1584 res = isl_aff_copy(aff);
1585 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1586 aff = isl_aff_floor(aff);
1587 aff = isl_aff_scale_val(aff, m);
1588 res = isl_aff_sub(res, aff);
1590 return res;
1591 error:
1592 isl_aff_free(aff);
1593 isl_val_free(m);
1594 return NULL;
1597 /* Compute
1599 * pwaff mod m = pwaff - m * floor(pwaff/m)
1601 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1603 isl_pw_aff *res;
1605 res = isl_pw_aff_copy(pwaff);
1606 pwaff = isl_pw_aff_scale_down(pwaff, m);
1607 pwaff = isl_pw_aff_floor(pwaff);
1608 pwaff = isl_pw_aff_scale(pwaff, m);
1609 res = isl_pw_aff_sub(res, pwaff);
1611 return res;
1614 /* Compute
1616 * pa mod m = pa - m * floor(pa/m)
1618 * with m an integer value.
1620 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1621 __isl_take isl_val *m)
1623 if (!pa || !m)
1624 goto error;
1625 if (!isl_val_is_int(m))
1626 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1627 "expecting integer modulo", goto error);
1628 pa = isl_pw_aff_mod(pa, m->n);
1629 isl_val_free(m);
1630 return pa;
1631 error:
1632 isl_pw_aff_free(pa);
1633 isl_val_free(m);
1634 return NULL;
1637 /* Given f, return ceil(f).
1638 * If f is an integer expression, then just return f.
1639 * Otherwise, let f be the expression
1641 * e/m
1643 * then return
1645 * floor((e + m - 1)/m)
1647 * As a special case, ceil(NaN) = NaN.
1649 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1651 if (!aff)
1652 return NULL;
1654 if (isl_aff_is_nan(aff))
1655 return aff;
1656 if (isl_int_is_one(aff->v->el[0]))
1657 return aff;
1659 aff = isl_aff_cow(aff);
1660 if (!aff)
1661 return NULL;
1662 aff->v = isl_vec_cow(aff->v);
1663 if (!aff->v)
1664 return isl_aff_free(aff);
1666 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1667 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1668 aff = isl_aff_floor(aff);
1670 return aff;
1673 /* Apply the expansion computed by isl_merge_divs.
1674 * The expansion itself is given by "exp" while the resulting
1675 * list of divs is given by "div".
1677 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1678 __isl_take isl_mat *div, int *exp)
1680 int old_n_div;
1681 int new_n_div;
1682 int offset;
1684 aff = isl_aff_cow(aff);
1685 if (!aff || !div)
1686 goto error;
1688 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1689 new_n_div = isl_mat_rows(div);
1690 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1692 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1693 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1694 if (!aff->v || !aff->ls)
1695 return isl_aff_free(aff);
1696 return aff;
1697 error:
1698 isl_aff_free(aff);
1699 isl_mat_free(div);
1700 return NULL;
1703 /* Add two affine expressions that live in the same local space.
1705 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1706 __isl_take isl_aff *aff2)
1708 isl_int gcd, f;
1710 aff1 = isl_aff_cow(aff1);
1711 if (!aff1 || !aff2)
1712 goto error;
1714 aff1->v = isl_vec_cow(aff1->v);
1715 if (!aff1->v)
1716 goto error;
1718 isl_int_init(gcd);
1719 isl_int_init(f);
1720 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1721 isl_int_divexact(f, aff2->v->el[0], gcd);
1722 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1723 isl_int_divexact(f, aff1->v->el[0], gcd);
1724 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1725 isl_int_divexact(f, aff2->v->el[0], gcd);
1726 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1727 isl_int_clear(f);
1728 isl_int_clear(gcd);
1730 isl_aff_free(aff2);
1731 return aff1;
1732 error:
1733 isl_aff_free(aff1);
1734 isl_aff_free(aff2);
1735 return NULL;
1738 /* Return the sum of "aff1" and "aff2".
1740 * If either of the two is NaN, then the result is NaN.
1742 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1743 __isl_take isl_aff *aff2)
1745 isl_ctx *ctx;
1746 int *exp1 = NULL;
1747 int *exp2 = NULL;
1748 isl_mat *div;
1749 int n_div1, n_div2;
1751 if (!aff1 || !aff2)
1752 goto error;
1754 ctx = isl_aff_get_ctx(aff1);
1755 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1756 isl_die(ctx, isl_error_invalid,
1757 "spaces don't match", goto error);
1759 if (isl_aff_is_nan(aff1)) {
1760 isl_aff_free(aff2);
1761 return aff1;
1763 if (isl_aff_is_nan(aff2)) {
1764 isl_aff_free(aff1);
1765 return aff2;
1768 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1769 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1770 if (n_div1 == 0 && n_div2 == 0)
1771 return add_expanded(aff1, aff2);
1773 exp1 = isl_alloc_array(ctx, int, n_div1);
1774 exp2 = isl_alloc_array(ctx, int, n_div2);
1775 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1776 goto error;
1778 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1779 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1780 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1781 free(exp1);
1782 free(exp2);
1784 return add_expanded(aff1, aff2);
1785 error:
1786 free(exp1);
1787 free(exp2);
1788 isl_aff_free(aff1);
1789 isl_aff_free(aff2);
1790 return NULL;
1793 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1794 __isl_take isl_aff *aff2)
1796 return isl_aff_add(aff1, isl_aff_neg(aff2));
1799 /* Return the result of scaling "aff" by a factor of "f".
1801 * As a special case, f * NaN = NaN.
1803 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1805 isl_int gcd;
1807 if (!aff)
1808 return NULL;
1809 if (isl_aff_is_nan(aff))
1810 return aff;
1812 if (isl_int_is_one(f))
1813 return aff;
1815 aff = isl_aff_cow(aff);
1816 if (!aff)
1817 return NULL;
1818 aff->v = isl_vec_cow(aff->v);
1819 if (!aff->v)
1820 return isl_aff_free(aff);
1822 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1823 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1824 return aff;
1827 isl_int_init(gcd);
1828 isl_int_gcd(gcd, aff->v->el[0], f);
1829 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1830 isl_int_divexact(gcd, f, gcd);
1831 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1832 isl_int_clear(gcd);
1834 return aff;
1837 /* Multiple "aff" by "v".
1839 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1840 __isl_take isl_val *v)
1842 if (!aff || !v)
1843 goto error;
1845 if (isl_val_is_one(v)) {
1846 isl_val_free(v);
1847 return aff;
1850 if (!isl_val_is_rat(v))
1851 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1852 "expecting rational factor", goto error);
1854 aff = isl_aff_scale(aff, v->n);
1855 aff = isl_aff_scale_down(aff, v->d);
1857 isl_val_free(v);
1858 return aff;
1859 error:
1860 isl_aff_free(aff);
1861 isl_val_free(v);
1862 return NULL;
1865 /* Return the result of scaling "aff" down by a factor of "f".
1867 * As a special case, NaN/f = NaN.
1869 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1871 isl_int gcd;
1873 if (!aff)
1874 return NULL;
1875 if (isl_aff_is_nan(aff))
1876 return aff;
1878 if (isl_int_is_one(f))
1879 return aff;
1881 aff = isl_aff_cow(aff);
1882 if (!aff)
1883 return NULL;
1885 if (isl_int_is_zero(f))
1886 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1887 "cannot scale down by zero", return isl_aff_free(aff));
1889 aff->v = isl_vec_cow(aff->v);
1890 if (!aff->v)
1891 return isl_aff_free(aff);
1893 isl_int_init(gcd);
1894 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1895 isl_int_gcd(gcd, gcd, f);
1896 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1897 isl_int_divexact(gcd, f, gcd);
1898 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1899 isl_int_clear(gcd);
1901 return aff;
1904 /* Divide "aff" by "v".
1906 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1907 __isl_take isl_val *v)
1909 if (!aff || !v)
1910 goto error;
1912 if (isl_val_is_one(v)) {
1913 isl_val_free(v);
1914 return aff;
1917 if (!isl_val_is_rat(v))
1918 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1919 "expecting rational factor", goto error);
1920 if (!isl_val_is_pos(v))
1921 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1922 "factor needs to be positive", goto error);
1924 aff = isl_aff_scale(aff, v->d);
1925 aff = isl_aff_scale_down(aff, v->n);
1927 isl_val_free(v);
1928 return aff;
1929 error:
1930 isl_aff_free(aff);
1931 isl_val_free(v);
1932 return NULL;
1935 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1937 isl_int v;
1939 if (f == 1)
1940 return aff;
1942 isl_int_init(v);
1943 isl_int_set_ui(v, f);
1944 aff = isl_aff_scale_down(aff, v);
1945 isl_int_clear(v);
1947 return aff;
1950 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1951 enum isl_dim_type type, unsigned pos, const char *s)
1953 aff = isl_aff_cow(aff);
1954 if (!aff)
1955 return NULL;
1956 if (type == isl_dim_out)
1957 isl_die(aff->v->ctx, isl_error_invalid,
1958 "cannot set name of output/set dimension",
1959 return isl_aff_free(aff));
1960 if (type == isl_dim_in)
1961 type = isl_dim_set;
1962 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1963 if (!aff->ls)
1964 return isl_aff_free(aff);
1966 return aff;
1969 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1970 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1972 aff = isl_aff_cow(aff);
1973 if (!aff)
1974 goto error;
1975 if (type == isl_dim_out)
1976 isl_die(aff->v->ctx, isl_error_invalid,
1977 "cannot set name of output/set dimension",
1978 goto error);
1979 if (type == isl_dim_in)
1980 type = isl_dim_set;
1981 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1982 if (!aff->ls)
1983 return isl_aff_free(aff);
1985 return aff;
1986 error:
1987 isl_id_free(id);
1988 isl_aff_free(aff);
1989 return NULL;
1992 /* Replace the identifier of the input tuple of "aff" by "id".
1993 * type is currently required to be equal to isl_dim_in
1995 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1996 enum isl_dim_type type, __isl_take isl_id *id)
1998 aff = isl_aff_cow(aff);
1999 if (!aff)
2000 goto error;
2001 if (type != isl_dim_out)
2002 isl_die(aff->v->ctx, isl_error_invalid,
2003 "cannot only set id of input tuple", goto error);
2004 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2005 if (!aff->ls)
2006 return isl_aff_free(aff);
2008 return aff;
2009 error:
2010 isl_id_free(id);
2011 isl_aff_free(aff);
2012 return NULL;
2015 /* Exploit the equalities in "eq" to simplify the affine expression
2016 * and the expressions of the integer divisions in the local space.
2017 * The integer divisions in this local space are assumed to appear
2018 * as regular dimensions in "eq".
2020 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2021 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2023 int i, j;
2024 unsigned total;
2025 unsigned n_div;
2027 if (!eq)
2028 goto error;
2029 if (eq->n_eq == 0) {
2030 isl_basic_set_free(eq);
2031 return aff;
2034 aff = isl_aff_cow(aff);
2035 if (!aff)
2036 goto error;
2038 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2039 isl_basic_set_copy(eq));
2040 aff->v = isl_vec_cow(aff->v);
2041 if (!aff->ls || !aff->v)
2042 goto error;
2044 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2045 n_div = eq->n_div;
2046 for (i = 0; i < eq->n_eq; ++i) {
2047 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2048 if (j < 0 || j == 0 || j >= total)
2049 continue;
2051 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2052 &aff->v->el[0]);
2055 isl_basic_set_free(eq);
2056 aff = isl_aff_normalize(aff);
2057 return aff;
2058 error:
2059 isl_basic_set_free(eq);
2060 isl_aff_free(aff);
2061 return NULL;
2064 /* Exploit the equalities in "eq" to simplify the affine expression
2065 * and the expressions of the integer divisions in the local space.
2067 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2068 __isl_take isl_basic_set *eq)
2070 int n_div;
2072 if (!aff || !eq)
2073 goto error;
2074 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2075 if (n_div > 0)
2076 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2077 return isl_aff_substitute_equalities_lifted(aff, eq);
2078 error:
2079 isl_basic_set_free(eq);
2080 isl_aff_free(aff);
2081 return NULL;
2084 /* Look for equalities among the variables shared by context and aff
2085 * and the integer divisions of aff, if any.
2086 * The equalities are then used to eliminate coefficients and/or integer
2087 * divisions from aff.
2089 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2090 __isl_take isl_set *context)
2092 isl_basic_set *hull;
2093 int n_div;
2095 if (!aff)
2096 goto error;
2097 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2098 if (n_div > 0) {
2099 isl_basic_set *bset;
2100 isl_local_space *ls;
2101 context = isl_set_add_dims(context, isl_dim_set, n_div);
2102 ls = isl_aff_get_domain_local_space(aff);
2103 bset = isl_basic_set_from_local_space(ls);
2104 bset = isl_basic_set_lift(bset);
2105 bset = isl_basic_set_flatten(bset);
2106 context = isl_set_intersect(context,
2107 isl_set_from_basic_set(bset));
2110 hull = isl_set_affine_hull(context);
2111 return isl_aff_substitute_equalities_lifted(aff, hull);
2112 error:
2113 isl_aff_free(aff);
2114 isl_set_free(context);
2115 return NULL;
2118 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2119 __isl_take isl_set *context)
2121 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2122 dom_context = isl_set_intersect_params(dom_context, context);
2123 return isl_aff_gist(aff, dom_context);
2126 /* Return a basic set containing those elements in the space
2127 * of aff where it is positive. "rational" should not be set.
2129 * If "aff" is NaN, then it is not positive.
2131 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2132 int rational)
2134 isl_constraint *ineq;
2135 isl_basic_set *bset;
2136 isl_val *c;
2138 if (!aff)
2139 return NULL;
2140 if (isl_aff_is_nan(aff)) {
2141 isl_space *space = isl_aff_get_domain_space(aff);
2142 isl_aff_free(aff);
2143 return isl_basic_set_empty(space);
2145 if (rational)
2146 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2147 "rational sets not supported", goto error);
2149 ineq = isl_inequality_from_aff(aff);
2150 c = isl_constraint_get_constant_val(ineq);
2151 c = isl_val_sub_ui(c, 1);
2152 ineq = isl_constraint_set_constant_val(ineq, c);
2154 bset = isl_basic_set_from_constraint(ineq);
2155 bset = isl_basic_set_simplify(bset);
2156 return bset;
2157 error:
2158 isl_aff_free(aff);
2159 return NULL;
2162 /* Return a basic set containing those elements in the space
2163 * of aff where it is non-negative.
2164 * If "rational" is set, then return a rational basic set.
2166 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2168 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2169 __isl_take isl_aff *aff, int rational)
2171 isl_constraint *ineq;
2172 isl_basic_set *bset;
2174 if (!aff)
2175 return NULL;
2176 if (isl_aff_is_nan(aff)) {
2177 isl_space *space = isl_aff_get_domain_space(aff);
2178 isl_aff_free(aff);
2179 return isl_basic_set_empty(space);
2182 ineq = isl_inequality_from_aff(aff);
2184 bset = isl_basic_set_from_constraint(ineq);
2185 if (rational)
2186 bset = isl_basic_set_set_rational(bset);
2187 bset = isl_basic_set_simplify(bset);
2188 return bset;
2191 /* Return a basic set containing those elements in the space
2192 * of aff where it is non-negative.
2194 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2196 return aff_nonneg_basic_set(aff, 0);
2199 /* Return a basic set containing those elements in the domain space
2200 * of "aff" where it is positive.
2202 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2204 aff = isl_aff_add_constant_num_si(aff, -1);
2205 return isl_aff_nonneg_basic_set(aff);
2208 /* Return a basic set containing those elements in the domain space
2209 * of aff where it is negative.
2211 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2213 aff = isl_aff_neg(aff);
2214 return isl_aff_pos_basic_set(aff);
2217 /* Return a basic set containing those elements in the space
2218 * of aff where it is zero.
2219 * If "rational" is set, then return a rational basic set.
2221 * If "aff" is NaN, then it is not zero.
2223 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2224 int rational)
2226 isl_constraint *ineq;
2227 isl_basic_set *bset;
2229 if (!aff)
2230 return NULL;
2231 if (isl_aff_is_nan(aff)) {
2232 isl_space *space = isl_aff_get_domain_space(aff);
2233 isl_aff_free(aff);
2234 return isl_basic_set_empty(space);
2237 ineq = isl_equality_from_aff(aff);
2239 bset = isl_basic_set_from_constraint(ineq);
2240 if (rational)
2241 bset = isl_basic_set_set_rational(bset);
2242 bset = isl_basic_set_simplify(bset);
2243 return bset;
2246 /* Return a basic set containing those elements in the space
2247 * of aff where it is zero.
2249 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2251 return aff_zero_basic_set(aff, 0);
2254 /* Return a basic set containing those elements in the shared space
2255 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2257 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2258 __isl_take isl_aff *aff2)
2260 aff1 = isl_aff_sub(aff1, aff2);
2262 return isl_aff_nonneg_basic_set(aff1);
2265 /* Return a basic set containing those elements in the shared domain space
2266 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2268 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2269 __isl_take isl_aff *aff2)
2271 aff1 = isl_aff_sub(aff1, aff2);
2273 return isl_aff_pos_basic_set(aff1);
2276 /* Return a set containing those elements in the shared space
2277 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2279 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2280 __isl_take isl_aff *aff2)
2282 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2285 /* Return a set containing those elements in the shared domain space
2286 * of aff1 and aff2 where aff1 is greater than aff2.
2288 * If either of the two inputs is NaN, then the result is empty,
2289 * as comparisons with NaN always return false.
2291 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2292 __isl_take isl_aff *aff2)
2294 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2297 /* Return a basic set containing those elements in the shared space
2298 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2300 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2301 __isl_take isl_aff *aff2)
2303 return isl_aff_ge_basic_set(aff2, aff1);
2306 /* Return a basic set containing those elements in the shared domain space
2307 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2309 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2310 __isl_take isl_aff *aff2)
2312 return isl_aff_gt_basic_set(aff2, aff1);
2315 /* Return a set containing those elements in the shared space
2316 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2318 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2319 __isl_take isl_aff *aff2)
2321 return isl_aff_ge_set(aff2, aff1);
2324 /* Return a set containing those elements in the shared domain space
2325 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2327 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2328 __isl_take isl_aff *aff2)
2330 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2333 /* Return a basic set containing those elements in the shared space
2334 * of aff1 and aff2 where aff1 and aff2 are equal.
2336 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2337 __isl_take isl_aff *aff2)
2339 aff1 = isl_aff_sub(aff1, aff2);
2341 return isl_aff_zero_basic_set(aff1);
2344 /* Return a set containing those elements in the shared space
2345 * of aff1 and aff2 where aff1 and aff2 are equal.
2347 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2348 __isl_take isl_aff *aff2)
2350 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2353 /* Return a set containing those elements in the shared domain space
2354 * of aff1 and aff2 where aff1 and aff2 are not equal.
2356 * If either of the two inputs is NaN, then the result is empty,
2357 * as comparisons with NaN always return false.
2359 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2360 __isl_take isl_aff *aff2)
2362 isl_set *set_lt, *set_gt;
2364 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2365 isl_aff_copy(aff2));
2366 set_gt = isl_aff_gt_set(aff1, aff2);
2367 return isl_set_union_disjoint(set_lt, set_gt);
2370 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2371 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2373 aff1 = isl_aff_add(aff1, aff2);
2374 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2375 return aff1;
2378 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2380 if (!aff)
2381 return -1;
2383 return 0;
2386 /* Check whether the given affine expression has non-zero coefficient
2387 * for any dimension in the given range or if any of these dimensions
2388 * appear with non-zero coefficients in any of the integer divisions
2389 * involved in the affine expression.
2391 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2392 enum isl_dim_type type, unsigned first, unsigned n)
2394 int i;
2395 isl_ctx *ctx;
2396 int *active = NULL;
2397 isl_bool involves = isl_bool_false;
2399 if (!aff)
2400 return isl_bool_error;
2401 if (n == 0)
2402 return isl_bool_false;
2404 ctx = isl_aff_get_ctx(aff);
2405 if (first + n > isl_aff_dim(aff, type))
2406 isl_die(ctx, isl_error_invalid,
2407 "range out of bounds", return isl_bool_error);
2409 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2410 if (!active)
2411 goto error;
2413 first += isl_local_space_offset(aff->ls, type) - 1;
2414 for (i = 0; i < n; ++i)
2415 if (active[first + i]) {
2416 involves = isl_bool_true;
2417 break;
2420 free(active);
2422 return involves;
2423 error:
2424 free(active);
2425 return isl_bool_error;
2428 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2429 enum isl_dim_type type, unsigned first, unsigned n)
2431 isl_ctx *ctx;
2433 if (!aff)
2434 return NULL;
2435 if (type == isl_dim_out)
2436 isl_die(aff->v->ctx, isl_error_invalid,
2437 "cannot drop output/set dimension",
2438 return isl_aff_free(aff));
2439 if (type == isl_dim_in)
2440 type = isl_dim_set;
2441 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2442 return aff;
2444 ctx = isl_aff_get_ctx(aff);
2445 if (first + n > isl_local_space_dim(aff->ls, type))
2446 isl_die(ctx, isl_error_invalid, "range out of bounds",
2447 return isl_aff_free(aff));
2449 aff = isl_aff_cow(aff);
2450 if (!aff)
2451 return NULL;
2453 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2454 if (!aff->ls)
2455 return isl_aff_free(aff);
2457 first += 1 + isl_local_space_offset(aff->ls, type);
2458 aff->v = isl_vec_drop_els(aff->v, first, n);
2459 if (!aff->v)
2460 return isl_aff_free(aff);
2462 return aff;
2465 /* Drop the "n" domain dimensions starting at "first" from "aff",
2466 * after checking that they do not appear in the affine expression.
2468 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2469 unsigned n)
2471 isl_bool involves;
2473 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2474 if (involves < 0)
2475 return isl_aff_free(aff);
2476 if (involves)
2477 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2478 "affine expression involves some of the domain dimensions",
2479 return isl_aff_free(aff));
2480 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2483 /* Project the domain of the affine expression onto its parameter space.
2484 * The affine expression may not involve any of the domain dimensions.
2486 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2488 isl_space *space;
2489 unsigned n;
2491 n = isl_aff_dim(aff, isl_dim_in);
2492 aff = drop_domain(aff, 0, n);
2493 space = isl_aff_get_domain_space(aff);
2494 space = isl_space_params(space);
2495 aff = isl_aff_reset_domain_space(aff, space);
2496 return aff;
2499 /* Check that the domain of "aff" is a product.
2501 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2503 isl_bool is_product;
2505 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2506 if (is_product < 0)
2507 return isl_stat_error;
2508 if (!is_product)
2509 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2510 "domain is not a product", return isl_stat_error);
2511 return isl_stat_ok;
2514 /* Given an affine function with a domain of the form [A -> B] that
2515 * does not depend on B, return the same function on domain A.
2517 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2519 isl_space *space;
2520 int n, n_in;
2522 if (check_domain_product(aff) < 0)
2523 return isl_aff_free(aff);
2524 space = isl_aff_get_domain_space(aff);
2525 n = isl_space_dim(space, isl_dim_set);
2526 space = isl_space_factor_domain(space);
2527 n_in = isl_space_dim(space, isl_dim_set);
2528 aff = drop_domain(aff, n_in, n - n_in);
2529 aff = isl_aff_reset_domain_space(aff, space);
2530 return aff;
2533 /* Convert an affine expression defined over a parameter domain
2534 * into one that is defined over a zero-dimensional set.
2536 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2538 isl_local_space *ls;
2540 ls = isl_aff_take_domain_local_space(aff);
2541 ls = isl_local_space_set_from_params(ls);
2542 aff = isl_aff_restore_domain_local_space(aff, ls);
2544 return aff;
2547 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2548 enum isl_dim_type type, unsigned first, unsigned n)
2550 isl_ctx *ctx;
2552 if (!aff)
2553 return NULL;
2554 if (type == isl_dim_out)
2555 isl_die(aff->v->ctx, isl_error_invalid,
2556 "cannot insert output/set dimensions",
2557 return isl_aff_free(aff));
2558 if (type == isl_dim_in)
2559 type = isl_dim_set;
2560 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2561 return aff;
2563 ctx = isl_aff_get_ctx(aff);
2564 if (first > isl_local_space_dim(aff->ls, type))
2565 isl_die(ctx, isl_error_invalid, "position out of bounds",
2566 return isl_aff_free(aff));
2568 aff = isl_aff_cow(aff);
2569 if (!aff)
2570 return NULL;
2572 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2573 if (!aff->ls)
2574 return isl_aff_free(aff);
2576 first += 1 + isl_local_space_offset(aff->ls, type);
2577 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2578 if (!aff->v)
2579 return isl_aff_free(aff);
2581 return aff;
2584 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2585 enum isl_dim_type type, unsigned n)
2587 unsigned pos;
2589 pos = isl_aff_dim(aff, type);
2591 return isl_aff_insert_dims(aff, type, pos, n);
2594 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2595 enum isl_dim_type type, unsigned n)
2597 unsigned pos;
2599 pos = isl_pw_aff_dim(pwaff, type);
2601 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2604 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2605 * to dimensions of "dst_type" at "dst_pos".
2607 * We only support moving input dimensions to parameters and vice versa.
2609 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2610 enum isl_dim_type dst_type, unsigned dst_pos,
2611 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2613 unsigned g_dst_pos;
2614 unsigned g_src_pos;
2616 if (!aff)
2617 return NULL;
2618 if (n == 0 &&
2619 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2620 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2621 return aff;
2623 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2624 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2625 "cannot move output/set dimension",
2626 return isl_aff_free(aff));
2627 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2628 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2629 "cannot move divs", return isl_aff_free(aff));
2630 if (dst_type == isl_dim_in)
2631 dst_type = isl_dim_set;
2632 if (src_type == isl_dim_in)
2633 src_type = isl_dim_set;
2635 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2636 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2637 "range out of bounds", return isl_aff_free(aff));
2638 if (dst_type == src_type)
2639 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2640 "moving dims within the same type not supported",
2641 return isl_aff_free(aff));
2643 aff = isl_aff_cow(aff);
2644 if (!aff)
2645 return NULL;
2647 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2648 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2649 if (dst_type > src_type)
2650 g_dst_pos -= n;
2652 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2653 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2654 src_type, src_pos, n);
2655 if (!aff->v || !aff->ls)
2656 return isl_aff_free(aff);
2658 aff = sort_divs(aff);
2660 return aff;
2663 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2665 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2666 return isl_pw_aff_alloc(dom, aff);
2669 #define isl_aff_involves_nan isl_aff_is_nan
2671 #undef PW
2672 #define PW isl_pw_aff
2673 #undef EL
2674 #define EL isl_aff
2675 #undef EL_IS_ZERO
2676 #define EL_IS_ZERO is_empty
2677 #undef ZERO
2678 #define ZERO empty
2679 #undef IS_ZERO
2680 #define IS_ZERO is_empty
2681 #undef FIELD
2682 #define FIELD aff
2683 #undef DEFAULT_IS_ZERO
2684 #define DEFAULT_IS_ZERO 0
2686 #define NO_OPT
2687 #define NO_LIFT
2688 #define NO_MORPH
2690 #include <isl_pw_templ.c>
2691 #include <isl_pw_eval.c>
2692 #include <isl_pw_hash.c>
2693 #include <isl_pw_union_opt.c>
2695 #undef UNION
2696 #define UNION isl_union_pw_aff
2697 #undef PART
2698 #define PART isl_pw_aff
2699 #undef PARTS
2700 #define PARTS pw_aff
2702 #include <isl_union_single.c>
2703 #include <isl_union_neg.c>
2705 static __isl_give isl_set *align_params_pw_pw_set_and(
2706 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2707 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2708 __isl_take isl_pw_aff *pwaff2))
2710 isl_bool equal_params;
2712 if (!pwaff1 || !pwaff2)
2713 goto error;
2714 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2715 if (equal_params < 0)
2716 goto error;
2717 if (equal_params)
2718 return fn(pwaff1, pwaff2);
2719 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2720 isl_pw_aff_check_named_params(pwaff2) < 0)
2721 goto error;
2722 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2723 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2724 return fn(pwaff1, pwaff2);
2725 error:
2726 isl_pw_aff_free(pwaff1);
2727 isl_pw_aff_free(pwaff2);
2728 return NULL;
2731 /* Align the parameters of the to isl_pw_aff arguments and
2732 * then apply a function "fn" on them that returns an isl_map.
2734 static __isl_give isl_map *align_params_pw_pw_map_and(
2735 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2736 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2737 __isl_take isl_pw_aff *pa2))
2739 isl_bool equal_params;
2741 if (!pa1 || !pa2)
2742 goto error;
2743 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2744 if (equal_params < 0)
2745 goto error;
2746 if (equal_params)
2747 return fn(pa1, pa2);
2748 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2749 isl_pw_aff_check_named_params(pa2) < 0)
2750 goto error;
2751 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2752 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2753 return fn(pa1, pa2);
2754 error:
2755 isl_pw_aff_free(pa1);
2756 isl_pw_aff_free(pa2);
2757 return NULL;
2760 /* Compute a piecewise quasi-affine expression with a domain that
2761 * is the union of those of pwaff1 and pwaff2 and such that on each
2762 * cell, the quasi-affine expression is the maximum of those of pwaff1
2763 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2764 * cell, then the associated expression is the defined one.
2766 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2767 __isl_take isl_pw_aff *pwaff2)
2769 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2772 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2773 __isl_take isl_pw_aff *pwaff2)
2775 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2776 &pw_aff_union_max);
2779 /* Compute a piecewise quasi-affine expression with a domain that
2780 * is the union of those of pwaff1 and pwaff2 and such that on each
2781 * cell, the quasi-affine expression is the minimum of those of pwaff1
2782 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2783 * cell, then the associated expression is the defined one.
2785 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2786 __isl_take isl_pw_aff *pwaff2)
2788 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2791 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2792 __isl_take isl_pw_aff *pwaff2)
2794 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2795 &pw_aff_union_min);
2798 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2799 __isl_take isl_pw_aff *pwaff2, int max)
2801 if (max)
2802 return isl_pw_aff_union_max(pwaff1, pwaff2);
2803 else
2804 return isl_pw_aff_union_min(pwaff1, pwaff2);
2807 /* Construct a map with as domain the domain of pwaff and
2808 * one-dimensional range corresponding to the affine expressions.
2810 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2812 int i;
2813 isl_space *dim;
2814 isl_map *map;
2816 if (!pwaff)
2817 return NULL;
2819 dim = isl_pw_aff_get_space(pwaff);
2820 map = isl_map_empty(dim);
2822 for (i = 0; i < pwaff->n; ++i) {
2823 isl_basic_map *bmap;
2824 isl_map *map_i;
2826 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2827 map_i = isl_map_from_basic_map(bmap);
2828 map_i = isl_map_intersect_domain(map_i,
2829 isl_set_copy(pwaff->p[i].set));
2830 map = isl_map_union_disjoint(map, map_i);
2833 isl_pw_aff_free(pwaff);
2835 return map;
2838 /* Construct a map with as domain the domain of pwaff and
2839 * one-dimensional range corresponding to the affine expressions.
2841 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2843 if (!pwaff)
2844 return NULL;
2845 if (isl_space_is_set(pwaff->dim))
2846 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2847 "space of input is not a map", goto error);
2848 return map_from_pw_aff(pwaff);
2849 error:
2850 isl_pw_aff_free(pwaff);
2851 return NULL;
2854 /* Construct a one-dimensional set with as parameter domain
2855 * the domain of pwaff and the single set dimension
2856 * corresponding to the affine expressions.
2858 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2860 if (!pwaff)
2861 return NULL;
2862 if (!isl_space_is_set(pwaff->dim))
2863 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2864 "space of input is not a set", goto error);
2865 return map_from_pw_aff(pwaff);
2866 error:
2867 isl_pw_aff_free(pwaff);
2868 return NULL;
2871 /* Return a set containing those elements in the domain
2872 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2873 * does not satisfy "fn" (if complement is 1).
2875 * The pieces with a NaN never belong to the result since
2876 * NaN does not satisfy any property.
2878 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2879 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2880 int complement)
2882 int i;
2883 isl_set *set;
2885 if (!pwaff)
2886 return NULL;
2888 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2890 for (i = 0; i < pwaff->n; ++i) {
2891 isl_basic_set *bset;
2892 isl_set *set_i, *locus;
2893 isl_bool rational;
2895 if (isl_aff_is_nan(pwaff->p[i].aff))
2896 continue;
2898 rational = isl_set_has_rational(pwaff->p[i].set);
2899 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2900 locus = isl_set_from_basic_set(bset);
2901 set_i = isl_set_copy(pwaff->p[i].set);
2902 if (complement)
2903 set_i = isl_set_subtract(set_i, locus);
2904 else
2905 set_i = isl_set_intersect(set_i, locus);
2906 set = isl_set_union_disjoint(set, set_i);
2909 isl_pw_aff_free(pwaff);
2911 return set;
2914 /* Return a set containing those elements in the domain
2915 * of "pa" where it is positive.
2917 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2919 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2922 /* Return a set containing those elements in the domain
2923 * of pwaff where it is non-negative.
2925 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2927 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2930 /* Return a set containing those elements in the domain
2931 * of pwaff where it is zero.
2933 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2935 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2938 /* Return a set containing those elements in the domain
2939 * of pwaff where it is not zero.
2941 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2943 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2946 /* Return a set containing those elements in the shared domain
2947 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2949 * We compute the difference on the shared domain and then construct
2950 * the set of values where this difference is non-negative.
2951 * If strict is set, we first subtract 1 from the difference.
2952 * If equal is set, we only return the elements where pwaff1 and pwaff2
2953 * are equal.
2955 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2956 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2958 isl_set *set1, *set2;
2960 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2961 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2962 set1 = isl_set_intersect(set1, set2);
2963 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2964 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2965 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2967 if (strict) {
2968 isl_space *dim = isl_set_get_space(set1);
2969 isl_aff *aff;
2970 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2971 aff = isl_aff_add_constant_si(aff, -1);
2972 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2973 } else
2974 isl_set_free(set1);
2976 if (equal)
2977 return isl_pw_aff_zero_set(pwaff1);
2978 return isl_pw_aff_nonneg_set(pwaff1);
2981 /* Return a set containing those elements in the shared domain
2982 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2984 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2985 __isl_take isl_pw_aff *pwaff2)
2987 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2990 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2991 __isl_take isl_pw_aff *pwaff2)
2993 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2996 /* Return a set containing those elements in the shared domain
2997 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2999 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3000 __isl_take isl_pw_aff *pwaff2)
3002 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3005 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3006 __isl_take isl_pw_aff *pwaff2)
3008 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3011 /* Return a set containing those elements in the shared domain
3012 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3014 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3015 __isl_take isl_pw_aff *pwaff2)
3017 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3020 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3021 __isl_take isl_pw_aff *pwaff2)
3023 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3026 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3027 __isl_take isl_pw_aff *pwaff2)
3029 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3032 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3033 __isl_take isl_pw_aff *pwaff2)
3035 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3038 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3039 * where the function values are ordered in the same way as "order",
3040 * which returns a set in the shared domain of its two arguments.
3041 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3043 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3044 * We first pull back the two functions such that they are defined on
3045 * the domain [A -> B]. Then we apply "order", resulting in a set
3046 * in the space [A -> B]. Finally, we unwrap this set to obtain
3047 * a map in the space A -> B.
3049 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3050 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3051 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3052 __isl_take isl_pw_aff *pa2))
3054 isl_space *space1, *space2;
3055 isl_multi_aff *ma;
3056 isl_set *set;
3058 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3059 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3060 space1 = isl_space_map_from_domain_and_range(space1, space2);
3061 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3062 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3063 ma = isl_multi_aff_range_map(space1);
3064 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3065 set = order(pa1, pa2);
3067 return isl_set_unwrap(set);
3070 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3071 * where the function values are equal.
3072 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3074 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3075 __isl_take isl_pw_aff *pa2)
3077 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3080 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3081 * where the function values are equal.
3083 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3084 __isl_take isl_pw_aff *pa2)
3086 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3089 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3090 * where the function value of "pa1" is less than the function value of "pa2".
3091 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3093 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3094 __isl_take isl_pw_aff *pa2)
3096 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3099 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3100 * where the function value of "pa1" is less than the function value of "pa2".
3102 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3103 __isl_take isl_pw_aff *pa2)
3105 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3108 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3109 * where the function value of "pa1" is greater than the function value
3110 * of "pa2".
3111 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3113 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3114 __isl_take isl_pw_aff *pa2)
3116 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3119 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3120 * where the function value of "pa1" is greater than the function value
3121 * of "pa2".
3123 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3124 __isl_take isl_pw_aff *pa2)
3126 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3129 /* Return a set containing those elements in the shared domain
3130 * of the elements of list1 and list2 where each element in list1
3131 * has the relation specified by "fn" with each element in list2.
3133 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3134 __isl_take isl_pw_aff_list *list2,
3135 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3136 __isl_take isl_pw_aff *pwaff2))
3138 int i, j;
3139 isl_ctx *ctx;
3140 isl_set *set;
3142 if (!list1 || !list2)
3143 goto error;
3145 ctx = isl_pw_aff_list_get_ctx(list1);
3146 if (list1->n < 1 || list2->n < 1)
3147 isl_die(ctx, isl_error_invalid,
3148 "list should contain at least one element", goto error);
3150 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3151 for (i = 0; i < list1->n; ++i)
3152 for (j = 0; j < list2->n; ++j) {
3153 isl_set *set_ij;
3155 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3156 isl_pw_aff_copy(list2->p[j]));
3157 set = isl_set_intersect(set, set_ij);
3160 isl_pw_aff_list_free(list1);
3161 isl_pw_aff_list_free(list2);
3162 return set;
3163 error:
3164 isl_pw_aff_list_free(list1);
3165 isl_pw_aff_list_free(list2);
3166 return NULL;
3169 /* Return a set containing those elements in the shared domain
3170 * of the elements of list1 and list2 where each element in list1
3171 * is equal to each element in list2.
3173 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3174 __isl_take isl_pw_aff_list *list2)
3176 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3179 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3180 __isl_take isl_pw_aff_list *list2)
3182 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3185 /* Return a set containing those elements in the shared domain
3186 * of the elements of list1 and list2 where each element in list1
3187 * is less than or equal to each element in list2.
3189 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3190 __isl_take isl_pw_aff_list *list2)
3192 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3195 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3196 __isl_take isl_pw_aff_list *list2)
3198 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3201 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3202 __isl_take isl_pw_aff_list *list2)
3204 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3207 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3208 __isl_take isl_pw_aff_list *list2)
3210 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3214 /* Return a set containing those elements in the shared domain
3215 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3217 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3218 __isl_take isl_pw_aff *pwaff2)
3220 isl_set *set_lt, *set_gt;
3222 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3223 isl_pw_aff_copy(pwaff2));
3224 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3225 return isl_set_union_disjoint(set_lt, set_gt);
3228 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3229 __isl_take isl_pw_aff *pwaff2)
3231 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3234 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3235 isl_int v)
3237 int i;
3239 if (isl_int_is_one(v))
3240 return pwaff;
3241 if (!isl_int_is_pos(v))
3242 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3243 "factor needs to be positive",
3244 return isl_pw_aff_free(pwaff));
3245 pwaff = isl_pw_aff_cow(pwaff);
3246 if (!pwaff)
3247 return NULL;
3248 if (pwaff->n == 0)
3249 return pwaff;
3251 for (i = 0; i < pwaff->n; ++i) {
3252 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3253 if (!pwaff->p[i].aff)
3254 return isl_pw_aff_free(pwaff);
3257 return pwaff;
3260 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3262 int i;
3264 pwaff = isl_pw_aff_cow(pwaff);
3265 if (!pwaff)
3266 return NULL;
3267 if (pwaff->n == 0)
3268 return pwaff;
3270 for (i = 0; i < pwaff->n; ++i) {
3271 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3272 if (!pwaff->p[i].aff)
3273 return isl_pw_aff_free(pwaff);
3276 return pwaff;
3279 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3281 int i;
3283 pwaff = isl_pw_aff_cow(pwaff);
3284 if (!pwaff)
3285 return NULL;
3286 if (pwaff->n == 0)
3287 return pwaff;
3289 for (i = 0; i < pwaff->n; ++i) {
3290 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3291 if (!pwaff->p[i].aff)
3292 return isl_pw_aff_free(pwaff);
3295 return pwaff;
3298 /* Assuming that "cond1" and "cond2" are disjoint,
3299 * return an affine expression that is equal to pwaff1 on cond1
3300 * and to pwaff2 on cond2.
3302 static __isl_give isl_pw_aff *isl_pw_aff_select(
3303 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3304 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3306 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3307 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3309 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3312 /* Return an affine expression that is equal to pwaff_true for elements
3313 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3314 * is zero.
3315 * That is, return cond ? pwaff_true : pwaff_false;
3317 * If "cond" involves and NaN, then we conservatively return a NaN
3318 * on its entire domain. In principle, we could consider the pieces
3319 * where it is NaN separately from those where it is not.
3321 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3322 * then only use the domain of "cond" to restrict the domain.
3324 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3325 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3327 isl_set *cond_true, *cond_false;
3328 isl_bool equal;
3330 if (!cond)
3331 goto error;
3332 if (isl_pw_aff_involves_nan(cond)) {
3333 isl_space *space = isl_pw_aff_get_domain_space(cond);
3334 isl_local_space *ls = isl_local_space_from_space(space);
3335 isl_pw_aff_free(cond);
3336 isl_pw_aff_free(pwaff_true);
3337 isl_pw_aff_free(pwaff_false);
3338 return isl_pw_aff_nan_on_domain(ls);
3341 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3342 isl_pw_aff_get_space(pwaff_false));
3343 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3344 isl_pw_aff_get_space(pwaff_true));
3345 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3346 if (equal < 0)
3347 goto error;
3348 if (equal) {
3349 isl_set *dom;
3351 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3352 isl_pw_aff_free(pwaff_false);
3353 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3356 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3357 cond_false = isl_pw_aff_zero_set(cond);
3358 return isl_pw_aff_select(cond_true, pwaff_true,
3359 cond_false, pwaff_false);
3360 error:
3361 isl_pw_aff_free(cond);
3362 isl_pw_aff_free(pwaff_true);
3363 isl_pw_aff_free(pwaff_false);
3364 return NULL;
3367 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3369 if (!aff)
3370 return isl_bool_error;
3372 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3375 /* Check whether pwaff is a piecewise constant.
3377 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3379 int i;
3381 if (!pwaff)
3382 return isl_bool_error;
3384 for (i = 0; i < pwaff->n; ++i) {
3385 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3386 if (is_cst < 0 || !is_cst)
3387 return is_cst;
3390 return isl_bool_true;
3393 /* Are all elements of "mpa" piecewise constants?
3395 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3397 int i;
3399 if (!mpa)
3400 return isl_bool_error;
3402 for (i = 0; i < mpa->n; ++i) {
3403 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3404 if (is_cst < 0 || !is_cst)
3405 return is_cst;
3408 return isl_bool_true;
3411 /* Return the product of "aff1" and "aff2".
3413 * If either of the two is NaN, then the result is NaN.
3415 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3417 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3418 __isl_take isl_aff *aff2)
3420 if (!aff1 || !aff2)
3421 goto error;
3423 if (isl_aff_is_nan(aff1)) {
3424 isl_aff_free(aff2);
3425 return aff1;
3427 if (isl_aff_is_nan(aff2)) {
3428 isl_aff_free(aff1);
3429 return aff2;
3432 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3433 return isl_aff_mul(aff2, aff1);
3435 if (!isl_aff_is_cst(aff2))
3436 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3437 "at least one affine expression should be constant",
3438 goto error);
3440 aff1 = isl_aff_cow(aff1);
3441 if (!aff1 || !aff2)
3442 goto error;
3444 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3445 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3447 isl_aff_free(aff2);
3448 return aff1;
3449 error:
3450 isl_aff_free(aff1);
3451 isl_aff_free(aff2);
3452 return NULL;
3455 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3457 * If either of the two is NaN, then the result is NaN.
3459 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3460 __isl_take isl_aff *aff2)
3462 int is_cst;
3463 int neg;
3465 if (!aff1 || !aff2)
3466 goto error;
3468 if (isl_aff_is_nan(aff1)) {
3469 isl_aff_free(aff2);
3470 return aff1;
3472 if (isl_aff_is_nan(aff2)) {
3473 isl_aff_free(aff1);
3474 return aff2;
3477 is_cst = isl_aff_is_cst(aff2);
3478 if (is_cst < 0)
3479 goto error;
3480 if (!is_cst)
3481 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3482 "second argument should be a constant", goto error);
3484 if (!aff2)
3485 goto error;
3487 neg = isl_int_is_neg(aff2->v->el[1]);
3488 if (neg) {
3489 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3490 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3493 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3494 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3496 if (neg) {
3497 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3498 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3501 isl_aff_free(aff2);
3502 return aff1;
3503 error:
3504 isl_aff_free(aff1);
3505 isl_aff_free(aff2);
3506 return NULL;
3509 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3510 __isl_take isl_pw_aff *pwaff2)
3512 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3515 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3516 __isl_take isl_pw_aff *pwaff2)
3518 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3521 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3522 __isl_take isl_pw_aff *pwaff2)
3524 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3527 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3528 __isl_take isl_pw_aff *pwaff2)
3530 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3533 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3534 __isl_take isl_pw_aff *pwaff2)
3536 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3539 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3540 __isl_take isl_pw_aff *pa2)
3542 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3545 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3547 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3548 __isl_take isl_pw_aff *pa2)
3550 int is_cst;
3552 is_cst = isl_pw_aff_is_cst(pa2);
3553 if (is_cst < 0)
3554 goto error;
3555 if (!is_cst)
3556 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3557 "second argument should be a piecewise constant",
3558 goto error);
3559 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3560 error:
3561 isl_pw_aff_free(pa1);
3562 isl_pw_aff_free(pa2);
3563 return NULL;
3566 /* Compute the quotient of the integer division of "pa1" by "pa2"
3567 * with rounding towards zero.
3568 * "pa2" is assumed to be a piecewise constant.
3570 * In particular, return
3572 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3575 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3576 __isl_take isl_pw_aff *pa2)
3578 int is_cst;
3579 isl_set *cond;
3580 isl_pw_aff *f, *c;
3582 is_cst = isl_pw_aff_is_cst(pa2);
3583 if (is_cst < 0)
3584 goto error;
3585 if (!is_cst)
3586 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3587 "second argument should be a piecewise constant",
3588 goto error);
3590 pa1 = isl_pw_aff_div(pa1, pa2);
3592 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3593 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3594 c = isl_pw_aff_ceil(pa1);
3595 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3596 error:
3597 isl_pw_aff_free(pa1);
3598 isl_pw_aff_free(pa2);
3599 return NULL;
3602 /* Compute the remainder of the integer division of "pa1" by "pa2"
3603 * with rounding towards zero.
3604 * "pa2" is assumed to be a piecewise constant.
3606 * In particular, return
3608 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3611 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3612 __isl_take isl_pw_aff *pa2)
3614 int is_cst;
3615 isl_pw_aff *res;
3617 is_cst = isl_pw_aff_is_cst(pa2);
3618 if (is_cst < 0)
3619 goto error;
3620 if (!is_cst)
3621 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3622 "second argument should be a piecewise constant",
3623 goto error);
3624 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3625 res = isl_pw_aff_mul(pa2, res);
3626 res = isl_pw_aff_sub(pa1, res);
3627 return res;
3628 error:
3629 isl_pw_aff_free(pa1);
3630 isl_pw_aff_free(pa2);
3631 return NULL;
3634 /* Does either of "pa1" or "pa2" involve any NaN2?
3636 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3637 __isl_keep isl_pw_aff *pa2)
3639 isl_bool has_nan;
3641 has_nan = isl_pw_aff_involves_nan(pa1);
3642 if (has_nan < 0 || has_nan)
3643 return has_nan;
3644 return isl_pw_aff_involves_nan(pa2);
3647 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3648 * by a NaN on their shared domain.
3650 * In principle, the result could be refined to only being NaN
3651 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3653 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3654 __isl_take isl_pw_aff *pa2)
3656 isl_local_space *ls;
3657 isl_set *dom;
3658 isl_pw_aff *pa;
3660 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3661 ls = isl_local_space_from_space(isl_set_get_space(dom));
3662 pa = isl_pw_aff_nan_on_domain(ls);
3663 pa = isl_pw_aff_intersect_domain(pa, dom);
3665 return pa;
3668 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3669 __isl_take isl_pw_aff *pwaff2)
3671 isl_set *le;
3672 isl_set *dom;
3674 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3675 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3676 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3677 isl_pw_aff_copy(pwaff2));
3678 dom = isl_set_subtract(dom, isl_set_copy(le));
3679 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3682 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3683 __isl_take isl_pw_aff *pwaff2)
3685 isl_set *ge;
3686 isl_set *dom;
3688 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3689 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3690 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3691 isl_pw_aff_copy(pwaff2));
3692 dom = isl_set_subtract(dom, isl_set_copy(ge));
3693 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3696 /* Return an expression for the minimum (if "max" is not set) or
3697 * the maximum (if "max" is set) of "pa1" and "pa2".
3698 * If either expression involves any NaN, then return a NaN
3699 * on the shared domain as result.
3701 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3702 __isl_take isl_pw_aff *pa2, int max)
3704 isl_bool has_nan;
3706 has_nan = either_involves_nan(pa1, pa2);
3707 if (has_nan < 0)
3708 pa1 = isl_pw_aff_free(pa1);
3709 else if (has_nan)
3710 return replace_by_nan(pa1, pa2);
3712 if (max)
3713 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3714 else
3715 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3718 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3720 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3721 __isl_take isl_pw_aff *pwaff2)
3723 return pw_aff_min_max(pwaff1, pwaff2, 0);
3726 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3728 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3729 __isl_take isl_pw_aff *pwaff2)
3731 return pw_aff_min_max(pwaff1, pwaff2, 1);
3734 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3735 __isl_take isl_pw_aff_list *list,
3736 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3737 __isl_take isl_pw_aff *pwaff2))
3739 int i;
3740 isl_ctx *ctx;
3741 isl_pw_aff *res;
3743 if (!list)
3744 return NULL;
3746 ctx = isl_pw_aff_list_get_ctx(list);
3747 if (list->n < 1)
3748 isl_die(ctx, isl_error_invalid,
3749 "list should contain at least one element", goto error);
3751 res = isl_pw_aff_copy(list->p[0]);
3752 for (i = 1; i < list->n; ++i)
3753 res = fn(res, isl_pw_aff_copy(list->p[i]));
3755 isl_pw_aff_list_free(list);
3756 return res;
3757 error:
3758 isl_pw_aff_list_free(list);
3759 return NULL;
3762 /* Return an isl_pw_aff that maps each element in the intersection of the
3763 * domains of the elements of list to the minimal corresponding affine
3764 * expression.
3766 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3768 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3771 /* Return an isl_pw_aff that maps each element in the intersection of the
3772 * domains of the elements of list to the maximal corresponding affine
3773 * expression.
3775 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3777 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3780 /* Mark the domains of "pwaff" as rational.
3782 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3784 int i;
3786 pwaff = isl_pw_aff_cow(pwaff);
3787 if (!pwaff)
3788 return NULL;
3789 if (pwaff->n == 0)
3790 return pwaff;
3792 for (i = 0; i < pwaff->n; ++i) {
3793 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3794 if (!pwaff->p[i].set)
3795 return isl_pw_aff_free(pwaff);
3798 return pwaff;
3801 /* Mark the domains of the elements of "list" as rational.
3803 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3804 __isl_take isl_pw_aff_list *list)
3806 int i, n;
3808 if (!list)
3809 return NULL;
3810 if (list->n == 0)
3811 return list;
3813 n = list->n;
3814 for (i = 0; i < n; ++i) {
3815 isl_pw_aff *pa;
3817 pa = isl_pw_aff_list_get_pw_aff(list, i);
3818 pa = isl_pw_aff_set_rational(pa);
3819 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3822 return list;
3825 /* Do the parameters of "aff" match those of "space"?
3827 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3828 __isl_keep isl_space *space)
3830 isl_space *aff_space;
3831 isl_bool match;
3833 if (!aff || !space)
3834 return isl_bool_error;
3836 aff_space = isl_aff_get_domain_space(aff);
3838 match = isl_space_has_equal_params(space, aff_space);
3840 isl_space_free(aff_space);
3841 return match;
3844 /* Check that the domain space of "aff" matches "space".
3846 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3847 __isl_keep isl_space *space)
3849 isl_space *aff_space;
3850 isl_bool match;
3852 if (!aff || !space)
3853 return isl_stat_error;
3855 aff_space = isl_aff_get_domain_space(aff);
3857 match = isl_space_has_equal_params(space, aff_space);
3858 if (match < 0)
3859 goto error;
3860 if (!match)
3861 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3862 "parameters don't match", goto error);
3863 match = isl_space_tuple_is_equal(space, isl_dim_in,
3864 aff_space, isl_dim_set);
3865 if (match < 0)
3866 goto error;
3867 if (!match)
3868 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3869 "domains don't match", goto error);
3870 isl_space_free(aff_space);
3871 return isl_stat_ok;
3872 error:
3873 isl_space_free(aff_space);
3874 return isl_stat_error;
3877 #undef BASE
3878 #define BASE aff
3879 #undef DOMBASE
3880 #define DOMBASE set
3881 #define NO_DOMAIN
3883 #include <isl_multi_no_explicit_domain.c>
3884 #include <isl_multi_templ.c>
3885 #include <isl_multi_apply_set.c>
3886 #include <isl_multi_cmp.c>
3887 #include <isl_multi_dims.c>
3888 #include <isl_multi_floor.c>
3889 #include <isl_multi_gist.c>
3891 #undef NO_DOMAIN
3893 /* Construct an isl_multi_aff living in "space" that corresponds
3894 * to the affine transformation matrix "mat".
3896 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3897 __isl_take isl_space *space, __isl_take isl_mat *mat)
3899 isl_ctx *ctx;
3900 isl_local_space *ls = NULL;
3901 isl_multi_aff *ma = NULL;
3902 int n_row, n_col, n_out, total;
3903 int i;
3905 if (!space || !mat)
3906 goto error;
3908 ctx = isl_mat_get_ctx(mat);
3910 n_row = isl_mat_rows(mat);
3911 n_col = isl_mat_cols(mat);
3912 if (n_row < 1)
3913 isl_die(ctx, isl_error_invalid,
3914 "insufficient number of rows", goto error);
3915 if (n_col < 1)
3916 isl_die(ctx, isl_error_invalid,
3917 "insufficient number of columns", goto error);
3918 n_out = isl_space_dim(space, isl_dim_out);
3919 total = isl_space_dim(space, isl_dim_all);
3920 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3921 isl_die(ctx, isl_error_invalid,
3922 "dimension mismatch", goto error);
3924 ma = isl_multi_aff_zero(isl_space_copy(space));
3925 ls = isl_local_space_from_space(isl_space_domain(space));
3927 for (i = 0; i < n_row - 1; ++i) {
3928 isl_vec *v;
3929 isl_aff *aff;
3931 v = isl_vec_alloc(ctx, 1 + n_col);
3932 if (!v)
3933 goto error;
3934 isl_int_set(v->el[0], mat->row[0][0]);
3935 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3936 v = isl_vec_normalize(v);
3937 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3938 ma = isl_multi_aff_set_aff(ma, i, aff);
3941 isl_local_space_free(ls);
3942 isl_mat_free(mat);
3943 return ma;
3944 error:
3945 isl_local_space_free(ls);
3946 isl_mat_free(mat);
3947 isl_multi_aff_free(ma);
3948 return NULL;
3951 /* Remove any internal structure of the domain of "ma".
3952 * If there is any such internal structure in the input,
3953 * then the name of the corresponding space is also removed.
3955 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3956 __isl_take isl_multi_aff *ma)
3958 isl_space *space;
3960 if (!ma)
3961 return NULL;
3963 if (!ma->space->nested[0])
3964 return ma;
3966 space = isl_multi_aff_get_space(ma);
3967 space = isl_space_flatten_domain(space);
3968 ma = isl_multi_aff_reset_space(ma, space);
3970 return ma;
3973 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3974 * of the space to its domain.
3976 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3978 int i, n_in;
3979 isl_local_space *ls;
3980 isl_multi_aff *ma;
3982 if (!space)
3983 return NULL;
3984 if (!isl_space_is_map(space))
3985 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3986 "not a map space", goto error);
3988 n_in = isl_space_dim(space, isl_dim_in);
3989 space = isl_space_domain_map(space);
3991 ma = isl_multi_aff_alloc(isl_space_copy(space));
3992 if (n_in == 0) {
3993 isl_space_free(space);
3994 return ma;
3997 space = isl_space_domain(space);
3998 ls = isl_local_space_from_space(space);
3999 for (i = 0; i < n_in; ++i) {
4000 isl_aff *aff;
4002 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4003 isl_dim_set, i);
4004 ma = isl_multi_aff_set_aff(ma, i, aff);
4006 isl_local_space_free(ls);
4007 return ma;
4008 error:
4009 isl_space_free(space);
4010 return NULL;
4013 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4014 * of the space to its range.
4016 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4018 int i, n_in, n_out;
4019 isl_local_space *ls;
4020 isl_multi_aff *ma;
4022 if (!space)
4023 return NULL;
4024 if (!isl_space_is_map(space))
4025 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4026 "not a map space", goto error);
4028 n_in = isl_space_dim(space, isl_dim_in);
4029 n_out = isl_space_dim(space, isl_dim_out);
4030 space = isl_space_range_map(space);
4032 ma = isl_multi_aff_alloc(isl_space_copy(space));
4033 if (n_out == 0) {
4034 isl_space_free(space);
4035 return ma;
4038 space = isl_space_domain(space);
4039 ls = isl_local_space_from_space(space);
4040 for (i = 0; i < n_out; ++i) {
4041 isl_aff *aff;
4043 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4044 isl_dim_set, n_in + i);
4045 ma = isl_multi_aff_set_aff(ma, i, aff);
4047 isl_local_space_free(ls);
4048 return ma;
4049 error:
4050 isl_space_free(space);
4051 return NULL;
4054 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4055 * of the space to its range.
4057 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4058 __isl_take isl_space *space)
4060 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4063 /* Given the space of a set and a range of set dimensions,
4064 * construct an isl_multi_aff that projects out those dimensions.
4066 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4067 __isl_take isl_space *space, enum isl_dim_type type,
4068 unsigned first, unsigned n)
4070 int i, dim;
4071 isl_local_space *ls;
4072 isl_multi_aff *ma;
4074 if (!space)
4075 return NULL;
4076 if (!isl_space_is_set(space))
4077 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4078 "expecting set space", goto error);
4079 if (type != isl_dim_set)
4080 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4081 "only set dimensions can be projected out", goto error);
4083 dim = isl_space_dim(space, isl_dim_set);
4084 if (first + n > dim)
4085 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4086 "range out of bounds", goto error);
4088 space = isl_space_from_domain(space);
4089 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4091 if (dim == n)
4092 return isl_multi_aff_alloc(space);
4094 ma = isl_multi_aff_alloc(isl_space_copy(space));
4095 space = isl_space_domain(space);
4096 ls = isl_local_space_from_space(space);
4098 for (i = 0; i < first; ++i) {
4099 isl_aff *aff;
4101 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4102 isl_dim_set, i);
4103 ma = isl_multi_aff_set_aff(ma, i, aff);
4106 for (i = 0; i < dim - (first + n); ++i) {
4107 isl_aff *aff;
4109 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4110 isl_dim_set, first + n + i);
4111 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4114 isl_local_space_free(ls);
4115 return ma;
4116 error:
4117 isl_space_free(space);
4118 return NULL;
4121 /* Given the space of a set and a range of set dimensions,
4122 * construct an isl_pw_multi_aff that projects out those dimensions.
4124 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4125 __isl_take isl_space *space, enum isl_dim_type type,
4126 unsigned first, unsigned n)
4128 isl_multi_aff *ma;
4130 ma = isl_multi_aff_project_out_map(space, type, first, n);
4131 return isl_pw_multi_aff_from_multi_aff(ma);
4134 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4135 * domain.
4137 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4138 __isl_take isl_multi_aff *ma)
4140 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4141 return isl_pw_multi_aff_alloc(dom, ma);
4144 /* Create a piecewise multi-affine expression in the given space that maps each
4145 * input dimension to the corresponding output dimension.
4147 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4148 __isl_take isl_space *space)
4150 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4153 /* Exploit the equalities in "eq" to simplify the affine expressions.
4155 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4156 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4158 int i;
4160 maff = isl_multi_aff_cow(maff);
4161 if (!maff || !eq)
4162 goto error;
4164 for (i = 0; i < maff->n; ++i) {
4165 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4166 isl_basic_set_copy(eq));
4167 if (!maff->u.p[i])
4168 goto error;
4171 isl_basic_set_free(eq);
4172 return maff;
4173 error:
4174 isl_basic_set_free(eq);
4175 isl_multi_aff_free(maff);
4176 return NULL;
4179 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4180 isl_int f)
4182 int i;
4184 maff = isl_multi_aff_cow(maff);
4185 if (!maff)
4186 return NULL;
4188 for (i = 0; i < maff->n; ++i) {
4189 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4190 if (!maff->u.p[i])
4191 return isl_multi_aff_free(maff);
4194 return maff;
4197 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4198 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4200 maff1 = isl_multi_aff_add(maff1, maff2);
4201 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4202 return maff1;
4205 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4207 if (!maff)
4208 return -1;
4210 return 0;
4213 /* Return the set of domain elements where "ma1" is lexicographically
4214 * smaller than or equal to "ma2".
4216 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4217 __isl_take isl_multi_aff *ma2)
4219 return isl_multi_aff_lex_ge_set(ma2, ma1);
4222 /* Return the set of domain elements where "ma1" is lexicographically
4223 * smaller than "ma2".
4225 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4226 __isl_take isl_multi_aff *ma2)
4228 return isl_multi_aff_lex_gt_set(ma2, ma1);
4231 /* Return the set of domain elements where "ma1" and "ma2"
4232 * satisfy "order".
4234 static __isl_give isl_set *isl_multi_aff_order_set(
4235 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4236 __isl_give isl_map *order(__isl_take isl_space *set_space))
4238 isl_space *space;
4239 isl_map *map1, *map2;
4240 isl_map *map, *ge;
4242 map1 = isl_map_from_multi_aff(ma1);
4243 map2 = isl_map_from_multi_aff(ma2);
4244 map = isl_map_range_product(map1, map2);
4245 space = isl_space_range(isl_map_get_space(map));
4246 space = isl_space_domain(isl_space_unwrap(space));
4247 ge = order(space);
4248 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4250 return isl_map_domain(map);
4253 /* Return the set of domain elements where "ma1" is lexicographically
4254 * greater than or equal to "ma2".
4256 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4257 __isl_take isl_multi_aff *ma2)
4259 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4262 /* Return the set of domain elements where "ma1" is lexicographically
4263 * greater than "ma2".
4265 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4266 __isl_take isl_multi_aff *ma2)
4268 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4271 #undef PW
4272 #define PW isl_pw_multi_aff
4273 #undef EL
4274 #define EL isl_multi_aff
4275 #undef EL_IS_ZERO
4276 #define EL_IS_ZERO is_empty
4277 #undef ZERO
4278 #define ZERO empty
4279 #undef IS_ZERO
4280 #define IS_ZERO is_empty
4281 #undef FIELD
4282 #define FIELD maff
4283 #undef DEFAULT_IS_ZERO
4284 #define DEFAULT_IS_ZERO 0
4286 #define NO_SUB
4287 #define NO_OPT
4288 #define NO_INSERT_DIMS
4289 #define NO_LIFT
4290 #define NO_MORPH
4292 #include <isl_pw_templ.c>
4293 #include <isl_pw_union_opt.c>
4295 #undef NO_SUB
4297 #undef UNION
4298 #define UNION isl_union_pw_multi_aff
4299 #undef PART
4300 #define PART isl_pw_multi_aff
4301 #undef PARTS
4302 #define PARTS pw_multi_aff
4304 #include <isl_union_multi.c>
4305 #include <isl_union_neg.c>
4307 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4308 __isl_take isl_pw_multi_aff *pma1,
4309 __isl_take isl_pw_multi_aff *pma2)
4311 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4312 &isl_multi_aff_lex_ge_set);
4315 /* Given two piecewise multi affine expressions, return a piecewise
4316 * multi-affine expression defined on the union of the definition domains
4317 * of the inputs that is equal to the lexicographic maximum of the two
4318 * inputs on each cell. If only one of the two inputs is defined on
4319 * a given cell, then it is considered to be the maximum.
4321 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4322 __isl_take isl_pw_multi_aff *pma1,
4323 __isl_take isl_pw_multi_aff *pma2)
4325 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4326 &pw_multi_aff_union_lexmax);
4329 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4330 __isl_take isl_pw_multi_aff *pma1,
4331 __isl_take isl_pw_multi_aff *pma2)
4333 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4334 &isl_multi_aff_lex_le_set);
4337 /* Given two piecewise multi affine expressions, return a piecewise
4338 * multi-affine expression defined on the union of the definition domains
4339 * of the inputs that is equal to the lexicographic minimum of the two
4340 * inputs on each cell. If only one of the two inputs is defined on
4341 * a given cell, then it is considered to be the minimum.
4343 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4344 __isl_take isl_pw_multi_aff *pma1,
4345 __isl_take isl_pw_multi_aff *pma2)
4347 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4348 &pw_multi_aff_union_lexmin);
4351 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4352 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4354 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4355 &isl_multi_aff_add);
4358 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4359 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4361 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4362 &pw_multi_aff_add);
4365 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4366 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4368 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4369 &isl_multi_aff_sub);
4372 /* Subtract "pma2" from "pma1" and return the result.
4374 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4375 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4377 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4378 &pw_multi_aff_sub);
4381 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4382 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4384 return isl_pw_multi_aff_union_add_(pma1, pma2);
4387 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4388 * with the actual sum on the shared domain and
4389 * the defined expression on the symmetric difference of the domains.
4391 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4392 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4394 return isl_union_pw_aff_union_add_(upa1, upa2);
4397 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4398 * with the actual sum on the shared domain and
4399 * the defined expression on the symmetric difference of the domains.
4401 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4402 __isl_take isl_union_pw_multi_aff *upma1,
4403 __isl_take isl_union_pw_multi_aff *upma2)
4405 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4408 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4409 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4411 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4412 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4414 int i, j, n;
4415 isl_space *space;
4416 isl_pw_multi_aff *res;
4418 if (!pma1 || !pma2)
4419 goto error;
4421 n = pma1->n * pma2->n;
4422 space = isl_space_product(isl_space_copy(pma1->dim),
4423 isl_space_copy(pma2->dim));
4424 res = isl_pw_multi_aff_alloc_size(space, n);
4426 for (i = 0; i < pma1->n; ++i) {
4427 for (j = 0; j < pma2->n; ++j) {
4428 isl_set *domain;
4429 isl_multi_aff *ma;
4431 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4432 isl_set_copy(pma2->p[j].set));
4433 ma = isl_multi_aff_product(
4434 isl_multi_aff_copy(pma1->p[i].maff),
4435 isl_multi_aff_copy(pma2->p[j].maff));
4436 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4440 isl_pw_multi_aff_free(pma1);
4441 isl_pw_multi_aff_free(pma2);
4442 return res;
4443 error:
4444 isl_pw_multi_aff_free(pma1);
4445 isl_pw_multi_aff_free(pma2);
4446 return NULL;
4449 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4450 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4452 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4453 &pw_multi_aff_product);
4456 /* Construct a map mapping the domain of the piecewise multi-affine expression
4457 * to its range, with each dimension in the range equated to the
4458 * corresponding affine expression on its cell.
4460 * If the domain of "pma" is rational, then so is the constructed "map".
4462 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4464 int i;
4465 isl_map *map;
4467 if (!pma)
4468 return NULL;
4470 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4472 for (i = 0; i < pma->n; ++i) {
4473 isl_bool rational;
4474 isl_multi_aff *maff;
4475 isl_basic_map *bmap;
4476 isl_map *map_i;
4478 rational = isl_set_is_rational(pma->p[i].set);
4479 if (rational < 0)
4480 map = isl_map_free(map);
4481 maff = isl_multi_aff_copy(pma->p[i].maff);
4482 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4483 map_i = isl_map_from_basic_map(bmap);
4484 map_i = isl_map_intersect_domain(map_i,
4485 isl_set_copy(pma->p[i].set));
4486 map = isl_map_union_disjoint(map, map_i);
4489 isl_pw_multi_aff_free(pma);
4490 return map;
4493 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4495 if (!pma)
4496 return NULL;
4498 if (!isl_space_is_set(pma->dim))
4499 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4500 "isl_pw_multi_aff cannot be converted into an isl_set",
4501 goto error);
4503 return isl_map_from_pw_multi_aff(pma);
4504 error:
4505 isl_pw_multi_aff_free(pma);
4506 return NULL;
4509 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4510 * denominator "denom".
4511 * "denom" is allowed to be negative, in which case the actual denominator
4512 * is -denom and the expressions are added instead.
4514 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4515 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4517 int i, first;
4518 int sign;
4519 isl_int d;
4521 first = isl_seq_first_non_zero(c, n);
4522 if (first == -1)
4523 return aff;
4525 sign = isl_int_sgn(denom);
4526 isl_int_init(d);
4527 isl_int_abs(d, denom);
4528 for (i = first; i < n; ++i) {
4529 isl_aff *aff_i;
4531 if (isl_int_is_zero(c[i]))
4532 continue;
4533 aff_i = isl_multi_aff_get_aff(ma, i);
4534 aff_i = isl_aff_scale(aff_i, c[i]);
4535 aff_i = isl_aff_scale_down(aff_i, d);
4536 if (sign >= 0)
4537 aff = isl_aff_sub(aff, aff_i);
4538 else
4539 aff = isl_aff_add(aff, aff_i);
4541 isl_int_clear(d);
4543 return aff;
4546 /* Extract an affine expression that expresses the output dimension "pos"
4547 * of "bmap" in terms of the parameters and input dimensions from
4548 * equality "eq".
4549 * Note that this expression may involve integer divisions defined
4550 * in terms of parameters and input dimensions.
4551 * The equality may also involve references to earlier (but not later)
4552 * output dimensions. These are replaced by the corresponding elements
4553 * in "ma".
4555 * If the equality is of the form
4557 * f(i) + h(j) + a x + g(i) = 0,
4559 * with f(i) a linear combinations of the parameters and input dimensions,
4560 * g(i) a linear combination of integer divisions defined in terms of the same
4561 * and h(j) a linear combinations of earlier output dimensions,
4562 * then the affine expression is
4564 * (-f(i) - g(i))/a - h(j)/a
4566 * If the equality is of the form
4568 * f(i) + h(j) - a x + g(i) = 0,
4570 * then the affine expression is
4572 * (f(i) + g(i))/a - h(j)/(-a)
4575 * If "div" refers to an integer division (i.e., it is smaller than
4576 * the number of integer divisions), then the equality constraint
4577 * does involve an integer division (the one at position "div") that
4578 * is defined in terms of output dimensions. However, this integer
4579 * division can be eliminated by exploiting a pair of constraints
4580 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4581 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4582 * -l + x >= 0.
4583 * In particular, let
4585 * x = e(i) + m floor(...)
4587 * with e(i) the expression derived above and floor(...) the integer
4588 * division involving output dimensions.
4589 * From
4591 * l <= x <= l + n,
4593 * we have
4595 * 0 <= x - l <= n
4597 * This means
4599 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4600 * = (e(i) - l) mod m
4602 * Therefore,
4604 * x - l = (e(i) - l) mod m
4606 * or
4608 * x = ((e(i) - l) mod m) + l
4610 * The variable "shift" below contains the expression -l, which may
4611 * also involve a linear combination of earlier output dimensions.
4613 static __isl_give isl_aff *extract_aff_from_equality(
4614 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4615 __isl_keep isl_multi_aff *ma)
4617 unsigned o_out;
4618 unsigned n_div, n_out;
4619 isl_ctx *ctx;
4620 isl_local_space *ls;
4621 isl_aff *aff, *shift;
4622 isl_val *mod;
4624 ctx = isl_basic_map_get_ctx(bmap);
4625 ls = isl_basic_map_get_local_space(bmap);
4626 ls = isl_local_space_domain(ls);
4627 aff = isl_aff_alloc(isl_local_space_copy(ls));
4628 if (!aff)
4629 goto error;
4630 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4631 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4632 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4633 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4634 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4635 isl_seq_cpy(aff->v->el + 1 + o_out,
4636 bmap->eq[eq] + o_out + n_out, n_div);
4637 } else {
4638 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4639 isl_seq_neg(aff->v->el + 1 + o_out,
4640 bmap->eq[eq] + o_out + n_out, n_div);
4642 if (div < n_div)
4643 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4644 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4645 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4646 bmap->eq[eq][o_out + pos]);
4647 if (div < n_div) {
4648 shift = isl_aff_alloc(isl_local_space_copy(ls));
4649 if (!shift)
4650 goto error;
4651 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4652 isl_seq_cpy(shift->v->el + 1 + o_out,
4653 bmap->ineq[ineq] + o_out + n_out, n_div);
4654 isl_int_set_si(shift->v->el[0], 1);
4655 shift = subtract_initial(shift, ma, pos,
4656 bmap->ineq[ineq] + o_out, ctx->negone);
4657 aff = isl_aff_add(aff, isl_aff_copy(shift));
4658 mod = isl_val_int_from_isl_int(ctx,
4659 bmap->eq[eq][o_out + n_out + div]);
4660 mod = isl_val_abs(mod);
4661 aff = isl_aff_mod_val(aff, mod);
4662 aff = isl_aff_sub(aff, shift);
4665 isl_local_space_free(ls);
4666 return aff;
4667 error:
4668 isl_local_space_free(ls);
4669 isl_aff_free(aff);
4670 return NULL;
4673 /* Given a basic map with output dimensions defined
4674 * in terms of the parameters input dimensions and earlier
4675 * output dimensions using an equality (and possibly a pair on inequalities),
4676 * extract an isl_aff that expresses output dimension "pos" in terms
4677 * of the parameters and input dimensions.
4678 * Note that this expression may involve integer divisions defined
4679 * in terms of parameters and input dimensions.
4680 * "ma" contains the expressions corresponding to earlier output dimensions.
4682 * This function shares some similarities with
4683 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4685 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4686 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4688 int eq, div, ineq;
4689 isl_aff *aff;
4691 if (!bmap)
4692 return NULL;
4693 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4694 if (eq >= bmap->n_eq)
4695 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4696 "unable to find suitable equality", return NULL);
4697 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4699 aff = isl_aff_remove_unused_divs(aff);
4700 return aff;
4703 /* Given a basic map where each output dimension is defined
4704 * in terms of the parameters and input dimensions using an equality,
4705 * extract an isl_multi_aff that expresses the output dimensions in terms
4706 * of the parameters and input dimensions.
4708 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4709 __isl_take isl_basic_map *bmap)
4711 int i;
4712 unsigned n_out;
4713 isl_multi_aff *ma;
4715 if (!bmap)
4716 return NULL;
4718 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4719 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4721 for (i = 0; i < n_out; ++i) {
4722 isl_aff *aff;
4724 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4725 ma = isl_multi_aff_set_aff(ma, i, aff);
4728 isl_basic_map_free(bmap);
4730 return ma;
4733 /* Given a basic set where each set dimension is defined
4734 * in terms of the parameters using an equality,
4735 * extract an isl_multi_aff that expresses the set dimensions in terms
4736 * of the parameters.
4738 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4739 __isl_take isl_basic_set *bset)
4741 return extract_isl_multi_aff_from_basic_map(bset);
4744 /* Create an isl_pw_multi_aff that is equivalent to
4745 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4746 * The given basic map is such that each output dimension is defined
4747 * in terms of the parameters and input dimensions using an equality.
4749 * Since some applications expect the result of isl_pw_multi_aff_from_map
4750 * to only contain integer affine expressions, we compute the floor
4751 * of the expression before returning.
4753 * Remove all constraints involving local variables without
4754 * an explicit representation (resulting in the removal of those
4755 * local variables) prior to the actual extraction to ensure
4756 * that the local spaces in which the resulting affine expressions
4757 * are created do not contain any unknown local variables.
4758 * Removing such constraints is safe because constraints involving
4759 * unknown local variables are not used to determine whether
4760 * a basic map is obviously single-valued.
4762 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4763 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4765 isl_multi_aff *ma;
4767 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4768 ma = extract_isl_multi_aff_from_basic_map(bmap);
4769 ma = isl_multi_aff_floor(ma);
4770 return isl_pw_multi_aff_alloc(domain, ma);
4773 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4774 * This obviously only works if the input "map" is single-valued.
4775 * If so, we compute the lexicographic minimum of the image in the form
4776 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4777 * to its lexicographic minimum.
4778 * If the input is not single-valued, we produce an error.
4780 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4781 __isl_take isl_map *map)
4783 int i;
4784 int sv;
4785 isl_pw_multi_aff *pma;
4787 sv = isl_map_is_single_valued(map);
4788 if (sv < 0)
4789 goto error;
4790 if (!sv)
4791 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4792 "map is not single-valued", goto error);
4793 map = isl_map_make_disjoint(map);
4794 if (!map)
4795 return NULL;
4797 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4799 for (i = 0; i < map->n; ++i) {
4800 isl_pw_multi_aff *pma_i;
4801 isl_basic_map *bmap;
4802 bmap = isl_basic_map_copy(map->p[i]);
4803 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4804 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4807 isl_map_free(map);
4808 return pma;
4809 error:
4810 isl_map_free(map);
4811 return NULL;
4814 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4815 * taking into account that the output dimension at position "d"
4816 * can be represented as
4818 * x = floor((e(...) + c1) / m)
4820 * given that constraint "i" is of the form
4822 * e(...) + c1 - m x >= 0
4825 * Let "map" be of the form
4827 * A -> B
4829 * We construct a mapping
4831 * A -> [A -> x = floor(...)]
4833 * apply that to the map, obtaining
4835 * [A -> x = floor(...)] -> B
4837 * and equate dimension "d" to x.
4838 * We then compute a isl_pw_multi_aff representation of the resulting map
4839 * and plug in the mapping above.
4841 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4842 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4844 isl_ctx *ctx;
4845 isl_space *space;
4846 isl_local_space *ls;
4847 isl_multi_aff *ma;
4848 isl_aff *aff;
4849 isl_vec *v;
4850 isl_map *insert;
4851 int offset;
4852 int n;
4853 int n_in;
4854 isl_pw_multi_aff *pma;
4855 isl_bool is_set;
4857 is_set = isl_map_is_set(map);
4858 if (is_set < 0)
4859 goto error;
4861 offset = isl_basic_map_offset(hull, isl_dim_out);
4862 ctx = isl_map_get_ctx(map);
4863 space = isl_space_domain(isl_map_get_space(map));
4864 n_in = isl_space_dim(space, isl_dim_set);
4865 n = isl_space_dim(space, isl_dim_all);
4867 v = isl_vec_alloc(ctx, 1 + 1 + n);
4868 if (v) {
4869 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4870 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4872 isl_basic_map_free(hull);
4874 ls = isl_local_space_from_space(isl_space_copy(space));
4875 aff = isl_aff_alloc_vec(ls, v);
4876 aff = isl_aff_floor(aff);
4877 if (is_set) {
4878 isl_space_free(space);
4879 ma = isl_multi_aff_from_aff(aff);
4880 } else {
4881 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4882 ma = isl_multi_aff_range_product(ma,
4883 isl_multi_aff_from_aff(aff));
4886 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4887 map = isl_map_apply_domain(map, insert);
4888 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4889 pma = isl_pw_multi_aff_from_map(map);
4890 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4892 return pma;
4893 error:
4894 isl_map_free(map);
4895 isl_basic_map_free(hull);
4896 return NULL;
4899 /* Is constraint "c" of the form
4901 * e(...) + c1 - m x >= 0
4903 * or
4905 * -e(...) + c2 + m x >= 0
4907 * where m > 1 and e only depends on parameters and input dimemnsions?
4909 * "offset" is the offset of the output dimensions
4910 * "pos" is the position of output dimension x.
4912 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4914 if (isl_int_is_zero(c[offset + d]))
4915 return 0;
4916 if (isl_int_is_one(c[offset + d]))
4917 return 0;
4918 if (isl_int_is_negone(c[offset + d]))
4919 return 0;
4920 if (isl_seq_first_non_zero(c + offset, d) != -1)
4921 return 0;
4922 if (isl_seq_first_non_zero(c + offset + d + 1,
4923 total - (offset + d + 1)) != -1)
4924 return 0;
4925 return 1;
4928 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4930 * As a special case, we first check if there is any pair of constraints,
4931 * shared by all the basic maps in "map" that force a given dimension
4932 * to be equal to the floor of some affine combination of the input dimensions.
4934 * In particular, if we can find two constraints
4936 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4938 * and
4940 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4942 * where m > 1 and e only depends on parameters and input dimemnsions,
4943 * and such that
4945 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4947 * then we know that we can take
4949 * x = floor((e(...) + c1) / m)
4951 * without having to perform any computation.
4953 * Note that we know that
4955 * c1 + c2 >= 1
4957 * If c1 + c2 were 0, then we would have detected an equality during
4958 * simplification. If c1 + c2 were negative, then we would have detected
4959 * a contradiction.
4961 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4962 __isl_take isl_map *map)
4964 int d, dim;
4965 int i, j, n;
4966 int offset, total;
4967 isl_int sum;
4968 isl_basic_map *hull;
4970 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4971 if (!hull)
4972 goto error;
4974 isl_int_init(sum);
4975 dim = isl_map_dim(map, isl_dim_out);
4976 offset = isl_basic_map_offset(hull, isl_dim_out);
4977 total = 1 + isl_basic_map_total_dim(hull);
4978 n = hull->n_ineq;
4979 for (d = 0; d < dim; ++d) {
4980 for (i = 0; i < n; ++i) {
4981 if (!is_potential_div_constraint(hull->ineq[i],
4982 offset, d, total))
4983 continue;
4984 for (j = i + 1; j < n; ++j) {
4985 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4986 hull->ineq[j] + 1, total - 1))
4987 continue;
4988 isl_int_add(sum, hull->ineq[i][0],
4989 hull->ineq[j][0]);
4990 if (isl_int_abs_lt(sum,
4991 hull->ineq[i][offset + d]))
4992 break;
4995 if (j >= n)
4996 continue;
4997 isl_int_clear(sum);
4998 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4999 j = i;
5000 return pw_multi_aff_from_map_div(map, hull, d, j);
5003 isl_int_clear(sum);
5004 isl_basic_map_free(hull);
5005 return pw_multi_aff_from_map_base(map);
5006 error:
5007 isl_map_free(map);
5008 isl_basic_map_free(hull);
5009 return NULL;
5012 /* Given an affine expression
5014 * [A -> B] -> f(A,B)
5016 * construct an isl_multi_aff
5018 * [A -> B] -> B'
5020 * such that dimension "d" in B' is set to "aff" and the remaining
5021 * dimensions are set equal to the corresponding dimensions in B.
5022 * "n_in" is the dimension of the space A.
5023 * "n_out" is the dimension of the space B.
5025 * If "is_set" is set, then the affine expression is of the form
5027 * [B] -> f(B)
5029 * and we construct an isl_multi_aff
5031 * B -> B'
5033 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5034 unsigned n_in, unsigned n_out, int is_set)
5036 int i;
5037 isl_multi_aff *ma;
5038 isl_space *space, *space2;
5039 isl_local_space *ls;
5041 space = isl_aff_get_domain_space(aff);
5042 ls = isl_local_space_from_space(isl_space_copy(space));
5043 space2 = isl_space_copy(space);
5044 if (!is_set)
5045 space2 = isl_space_range(isl_space_unwrap(space2));
5046 space = isl_space_map_from_domain_and_range(space, space2);
5047 ma = isl_multi_aff_alloc(space);
5048 ma = isl_multi_aff_set_aff(ma, d, aff);
5050 for (i = 0; i < n_out; ++i) {
5051 if (i == d)
5052 continue;
5053 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5054 isl_dim_set, n_in + i);
5055 ma = isl_multi_aff_set_aff(ma, i, aff);
5058 isl_local_space_free(ls);
5060 return ma;
5063 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5064 * taking into account that the dimension at position "d" can be written as
5066 * x = m a + f(..) (1)
5068 * where m is equal to "gcd".
5069 * "i" is the index of the equality in "hull" that defines f(..).
5070 * In particular, the equality is of the form
5072 * f(..) - x + m g(existentials) = 0
5074 * or
5076 * -f(..) + x + m g(existentials) = 0
5078 * We basically plug (1) into "map", resulting in a map with "a"
5079 * in the range instead of "x". The corresponding isl_pw_multi_aff
5080 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5082 * Specifically, given the input map
5084 * A -> B
5086 * We first wrap it into a set
5088 * [A -> B]
5090 * and define (1) on top of the corresponding space, resulting in "aff".
5091 * We use this to create an isl_multi_aff that maps the output position "d"
5092 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5093 * We plug this into the wrapped map, unwrap the result and compute the
5094 * corresponding isl_pw_multi_aff.
5095 * The result is an expression
5097 * A -> T(A)
5099 * We adjust that to
5101 * A -> [A -> T(A)]
5103 * so that we can plug that into "aff", after extending the latter to
5104 * a mapping
5106 * [A -> B] -> B'
5109 * If "map" is actually a set, then there is no "A" space, meaning
5110 * that we do not need to perform any wrapping, and that the result
5111 * of the recursive call is of the form
5113 * [T]
5115 * which is plugged into a mapping of the form
5117 * B -> B'
5119 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5120 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5121 isl_int gcd)
5123 isl_set *set;
5124 isl_space *space;
5125 isl_local_space *ls;
5126 isl_aff *aff;
5127 isl_multi_aff *ma;
5128 isl_pw_multi_aff *pma, *id;
5129 unsigned n_in;
5130 unsigned o_out;
5131 unsigned n_out;
5132 isl_bool is_set;
5134 is_set = isl_map_is_set(map);
5135 if (is_set < 0)
5136 goto error;
5138 n_in = isl_basic_map_dim(hull, isl_dim_in);
5139 n_out = isl_basic_map_dim(hull, isl_dim_out);
5140 o_out = isl_basic_map_offset(hull, isl_dim_out);
5142 if (is_set)
5143 set = map;
5144 else
5145 set = isl_map_wrap(map);
5146 space = isl_space_map_from_set(isl_set_get_space(set));
5147 ma = isl_multi_aff_identity(space);
5148 ls = isl_local_space_from_space(isl_set_get_space(set));
5149 aff = isl_aff_alloc(ls);
5150 if (aff) {
5151 isl_int_set_si(aff->v->el[0], 1);
5152 if (isl_int_is_one(hull->eq[i][o_out + d]))
5153 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5154 aff->v->size - 1);
5155 else
5156 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5157 aff->v->size - 1);
5158 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5160 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5161 set = isl_set_preimage_multi_aff(set, ma);
5163 ma = range_map(aff, d, n_in, n_out, is_set);
5165 if (is_set)
5166 map = set;
5167 else
5168 map = isl_set_unwrap(set);
5169 pma = isl_pw_multi_aff_from_map(map);
5171 if (!is_set) {
5172 space = isl_pw_multi_aff_get_domain_space(pma);
5173 space = isl_space_map_from_set(space);
5174 id = isl_pw_multi_aff_identity(space);
5175 pma = isl_pw_multi_aff_range_product(id, pma);
5177 id = isl_pw_multi_aff_from_multi_aff(ma);
5178 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5180 isl_basic_map_free(hull);
5181 return pma;
5182 error:
5183 isl_map_free(map);
5184 isl_basic_map_free(hull);
5185 return NULL;
5188 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5189 * "hull" contains the equalities valid for "map".
5191 * Check if any of the output dimensions is "strided".
5192 * That is, we check if it can be written as
5194 * x = m a + f(..)
5196 * with m greater than 1, a some combination of existentially quantified
5197 * variables and f an expression in the parameters and input dimensions.
5198 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5200 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5201 * special case.
5203 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5204 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5206 int i, j;
5207 unsigned n_out;
5208 unsigned o_out;
5209 unsigned n_div;
5210 unsigned o_div;
5211 isl_int gcd;
5213 n_div = isl_basic_map_dim(hull, isl_dim_div);
5214 o_div = isl_basic_map_offset(hull, isl_dim_div);
5216 if (n_div == 0) {
5217 isl_basic_map_free(hull);
5218 return pw_multi_aff_from_map_check_div(map);
5221 isl_int_init(gcd);
5223 n_out = isl_basic_map_dim(hull, isl_dim_out);
5224 o_out = isl_basic_map_offset(hull, isl_dim_out);
5226 for (i = 0; i < n_out; ++i) {
5227 for (j = 0; j < hull->n_eq; ++j) {
5228 isl_int *eq = hull->eq[j];
5229 isl_pw_multi_aff *res;
5231 if (!isl_int_is_one(eq[o_out + i]) &&
5232 !isl_int_is_negone(eq[o_out + i]))
5233 continue;
5234 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5235 continue;
5236 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5237 n_out - (i + 1)) != -1)
5238 continue;
5239 isl_seq_gcd(eq + o_div, n_div, &gcd);
5240 if (isl_int_is_zero(gcd))
5241 continue;
5242 if (isl_int_is_one(gcd))
5243 continue;
5245 res = pw_multi_aff_from_map_stride(map, hull,
5246 i, j, gcd);
5247 isl_int_clear(gcd);
5248 return res;
5252 isl_int_clear(gcd);
5253 isl_basic_map_free(hull);
5254 return pw_multi_aff_from_map_check_div(map);
5257 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5259 * As a special case, we first check if all output dimensions are uniquely
5260 * defined in terms of the parameters and input dimensions over the entire
5261 * domain. If so, we extract the desired isl_pw_multi_aff directly
5262 * from the affine hull of "map" and its domain.
5264 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5265 * special cases.
5267 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5269 isl_bool sv;
5270 isl_basic_map *hull;
5272 if (!map)
5273 return NULL;
5275 if (isl_map_n_basic_map(map) == 1) {
5276 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5277 hull = isl_basic_map_plain_affine_hull(hull);
5278 sv = isl_basic_map_plain_is_single_valued(hull);
5279 if (sv >= 0 && sv)
5280 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5281 hull);
5282 isl_basic_map_free(hull);
5284 map = isl_map_detect_equalities(map);
5285 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5286 sv = isl_basic_map_plain_is_single_valued(hull);
5287 if (sv >= 0 && sv)
5288 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5289 if (sv >= 0)
5290 return pw_multi_aff_from_map_check_strides(map, hull);
5291 isl_basic_map_free(hull);
5292 isl_map_free(map);
5293 return NULL;
5296 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5298 return isl_pw_multi_aff_from_map(set);
5301 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5302 * add it to *user.
5304 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5306 isl_union_pw_multi_aff **upma = user;
5307 isl_pw_multi_aff *pma;
5309 pma = isl_pw_multi_aff_from_map(map);
5310 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5312 return *upma ? isl_stat_ok : isl_stat_error;
5315 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5316 * domain.
5318 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5319 __isl_take isl_aff *aff)
5321 isl_multi_aff *ma;
5322 isl_pw_multi_aff *pma;
5324 ma = isl_multi_aff_from_aff(aff);
5325 pma = isl_pw_multi_aff_from_multi_aff(ma);
5326 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5329 /* Try and create an isl_union_pw_multi_aff that is equivalent
5330 * to the given isl_union_map.
5331 * The isl_union_map is required to be single-valued in each space.
5332 * Otherwise, an error is produced.
5334 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5335 __isl_take isl_union_map *umap)
5337 isl_space *space;
5338 isl_union_pw_multi_aff *upma;
5340 space = isl_union_map_get_space(umap);
5341 upma = isl_union_pw_multi_aff_empty(space);
5342 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5343 upma = isl_union_pw_multi_aff_free(upma);
5344 isl_union_map_free(umap);
5346 return upma;
5349 /* Try and create an isl_union_pw_multi_aff that is equivalent
5350 * to the given isl_union_set.
5351 * The isl_union_set is required to be a singleton in each space.
5352 * Otherwise, an error is produced.
5354 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5355 __isl_take isl_union_set *uset)
5357 return isl_union_pw_multi_aff_from_union_map(uset);
5360 /* Return the piecewise affine expression "set ? 1 : 0".
5362 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5364 isl_pw_aff *pa;
5365 isl_space *space = isl_set_get_space(set);
5366 isl_local_space *ls = isl_local_space_from_space(space);
5367 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5368 isl_aff *one = isl_aff_zero_on_domain(ls);
5370 one = isl_aff_add_constant_si(one, 1);
5371 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5372 set = isl_set_complement(set);
5373 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5375 return pa;
5378 /* Plug in "subs" for dimension "type", "pos" of "aff".
5380 * Let i be the dimension to replace and let "subs" be of the form
5382 * f/d
5384 * and "aff" of the form
5386 * (a i + g)/m
5388 * The result is
5390 * (a f + d g')/(m d)
5392 * where g' is the result of plugging in "subs" in each of the integer
5393 * divisions in g.
5395 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5396 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5398 isl_ctx *ctx;
5399 isl_int v;
5401 aff = isl_aff_cow(aff);
5402 if (!aff || !subs)
5403 return isl_aff_free(aff);
5405 ctx = isl_aff_get_ctx(aff);
5406 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5407 isl_die(ctx, isl_error_invalid,
5408 "spaces don't match", return isl_aff_free(aff));
5409 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5410 isl_die(ctx, isl_error_unsupported,
5411 "cannot handle divs yet", return isl_aff_free(aff));
5413 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5414 if (!aff->ls)
5415 return isl_aff_free(aff);
5417 aff->v = isl_vec_cow(aff->v);
5418 if (!aff->v)
5419 return isl_aff_free(aff);
5421 pos += isl_local_space_offset(aff->ls, type);
5423 isl_int_init(v);
5424 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5425 aff->v->size, subs->v->size, v);
5426 isl_int_clear(v);
5428 return aff;
5431 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5432 * expressions in "maff".
5434 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5435 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5436 __isl_keep isl_aff *subs)
5438 int i;
5440 maff = isl_multi_aff_cow(maff);
5441 if (!maff || !subs)
5442 return isl_multi_aff_free(maff);
5444 if (type == isl_dim_in)
5445 type = isl_dim_set;
5447 for (i = 0; i < maff->n; ++i) {
5448 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5449 type, pos, subs);
5450 if (!maff->u.p[i])
5451 return isl_multi_aff_free(maff);
5454 return maff;
5457 /* Plug in "subs" for dimension "type", "pos" of "pma".
5459 * pma is of the form
5461 * A_i(v) -> M_i(v)
5463 * while subs is of the form
5465 * v' = B_j(v) -> S_j
5467 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5468 * has a contribution in the result, in particular
5470 * C_ij(S_j) -> M_i(S_j)
5472 * Note that plugging in S_j in C_ij may also result in an empty set
5473 * and this contribution should simply be discarded.
5475 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5476 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5477 __isl_keep isl_pw_aff *subs)
5479 int i, j, n;
5480 isl_pw_multi_aff *res;
5482 if (!pma || !subs)
5483 return isl_pw_multi_aff_free(pma);
5485 n = pma->n * subs->n;
5486 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5488 for (i = 0; i < pma->n; ++i) {
5489 for (j = 0; j < subs->n; ++j) {
5490 isl_set *common;
5491 isl_multi_aff *res_ij;
5492 int empty;
5494 common = isl_set_intersect(
5495 isl_set_copy(pma->p[i].set),
5496 isl_set_copy(subs->p[j].set));
5497 common = isl_set_substitute(common,
5498 type, pos, subs->p[j].aff);
5499 empty = isl_set_plain_is_empty(common);
5500 if (empty < 0 || empty) {
5501 isl_set_free(common);
5502 if (empty < 0)
5503 goto error;
5504 continue;
5507 res_ij = isl_multi_aff_substitute(
5508 isl_multi_aff_copy(pma->p[i].maff),
5509 type, pos, subs->p[j].aff);
5511 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5515 isl_pw_multi_aff_free(pma);
5516 return res;
5517 error:
5518 isl_pw_multi_aff_free(pma);
5519 isl_pw_multi_aff_free(res);
5520 return NULL;
5523 /* Compute the preimage of a range of dimensions in the affine expression "src"
5524 * under "ma" and put the result in "dst". The number of dimensions in "src"
5525 * that precede the range is given by "n_before". The number of dimensions
5526 * in the range is given by the number of output dimensions of "ma".
5527 * The number of dimensions that follow the range is given by "n_after".
5528 * If "has_denom" is set (to one),
5529 * then "src" and "dst" have an extra initial denominator.
5530 * "n_div_ma" is the number of existentials in "ma"
5531 * "n_div_bset" is the number of existentials in "src"
5532 * The resulting "dst" (which is assumed to have been allocated by
5533 * the caller) contains coefficients for both sets of existentials,
5534 * first those in "ma" and then those in "src".
5535 * f, c1, c2 and g are temporary objects that have been initialized
5536 * by the caller.
5538 * Let src represent the expression
5540 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5542 * and let ma represent the expressions
5544 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5546 * We start out with the following expression for dst:
5548 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5550 * with the multiplication factor f initially equal to 1
5551 * and f \sum_i b_i v_i kept separately.
5552 * For each x_i that we substitute, we multiply the numerator
5553 * (and denominator) of dst by c_1 = m_i and add the numerator
5554 * of the x_i expression multiplied by c_2 = f b_i,
5555 * after removing the common factors of c_1 and c_2.
5556 * The multiplication factor f also needs to be multiplied by c_1
5557 * for the next x_j, j > i.
5559 void isl_seq_preimage(isl_int *dst, isl_int *src,
5560 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5561 int n_div_ma, int n_div_bmap,
5562 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5564 int i;
5565 int n_param, n_in, n_out;
5566 int o_dst, o_src;
5568 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5569 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5570 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5572 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5573 o_dst = o_src = has_denom + 1 + n_param + n_before;
5574 isl_seq_clr(dst + o_dst, n_in);
5575 o_dst += n_in;
5576 o_src += n_out;
5577 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5578 o_dst += n_after;
5579 o_src += n_after;
5580 isl_seq_clr(dst + o_dst, n_div_ma);
5581 o_dst += n_div_ma;
5582 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5584 isl_int_set_si(f, 1);
5586 for (i = 0; i < n_out; ++i) {
5587 int offset = has_denom + 1 + n_param + n_before + i;
5589 if (isl_int_is_zero(src[offset]))
5590 continue;
5591 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5592 isl_int_mul(c2, f, src[offset]);
5593 isl_int_gcd(g, c1, c2);
5594 isl_int_divexact(c1, c1, g);
5595 isl_int_divexact(c2, c2, g);
5597 isl_int_mul(f, f, c1);
5598 o_dst = has_denom;
5599 o_src = 1;
5600 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5601 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5602 o_dst += 1 + n_param;
5603 o_src += 1 + n_param;
5604 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5605 o_dst += n_before;
5606 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5607 c2, ma->u.p[i]->v->el + o_src, n_in);
5608 o_dst += n_in;
5609 o_src += n_in;
5610 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5611 o_dst += n_after;
5612 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5613 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5614 o_dst += n_div_ma;
5615 o_src += n_div_ma;
5616 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5617 if (has_denom)
5618 isl_int_mul(dst[0], dst[0], c1);
5622 /* Compute the pullback of "aff" by the function represented by "ma".
5623 * In other words, plug in "ma" in "aff". The result is an affine expression
5624 * defined over the domain space of "ma".
5626 * If "aff" is represented by
5628 * (a(p) + b x + c(divs))/d
5630 * and ma is represented by
5632 * x = D(p) + F(y) + G(divs')
5634 * then the result is
5636 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5638 * The divs in the local space of the input are similarly adjusted
5639 * through a call to isl_local_space_preimage_multi_aff.
5641 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5642 __isl_take isl_multi_aff *ma)
5644 isl_aff *res = NULL;
5645 isl_local_space *ls;
5646 int n_div_aff, n_div_ma;
5647 isl_int f, c1, c2, g;
5649 ma = isl_multi_aff_align_divs(ma);
5650 if (!aff || !ma)
5651 goto error;
5653 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5654 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5656 ls = isl_aff_get_domain_local_space(aff);
5657 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5658 res = isl_aff_alloc(ls);
5659 if (!res)
5660 goto error;
5662 isl_int_init(f);
5663 isl_int_init(c1);
5664 isl_int_init(c2);
5665 isl_int_init(g);
5667 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5668 f, c1, c2, g, 1);
5670 isl_int_clear(f);
5671 isl_int_clear(c1);
5672 isl_int_clear(c2);
5673 isl_int_clear(g);
5675 isl_aff_free(aff);
5676 isl_multi_aff_free(ma);
5677 res = isl_aff_normalize(res);
5678 return res;
5679 error:
5680 isl_aff_free(aff);
5681 isl_multi_aff_free(ma);
5682 isl_aff_free(res);
5683 return NULL;
5686 /* Compute the pullback of "aff1" by the function represented by "aff2".
5687 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5688 * defined over the domain space of "aff1".
5690 * The domain of "aff1" should match the range of "aff2", which means
5691 * that it should be single-dimensional.
5693 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5694 __isl_take isl_aff *aff2)
5696 isl_multi_aff *ma;
5698 ma = isl_multi_aff_from_aff(aff2);
5699 return isl_aff_pullback_multi_aff(aff1, ma);
5702 /* Compute the pullback of "ma1" by the function represented by "ma2".
5703 * In other words, plug in "ma2" in "ma1".
5705 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5707 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5708 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5710 int i;
5711 isl_space *space = NULL;
5713 ma2 = isl_multi_aff_align_divs(ma2);
5714 ma1 = isl_multi_aff_cow(ma1);
5715 if (!ma1 || !ma2)
5716 goto error;
5718 space = isl_space_join(isl_multi_aff_get_space(ma2),
5719 isl_multi_aff_get_space(ma1));
5721 for (i = 0; i < ma1->n; ++i) {
5722 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5723 isl_multi_aff_copy(ma2));
5724 if (!ma1->u.p[i])
5725 goto error;
5728 ma1 = isl_multi_aff_reset_space(ma1, space);
5729 isl_multi_aff_free(ma2);
5730 return ma1;
5731 error:
5732 isl_space_free(space);
5733 isl_multi_aff_free(ma2);
5734 isl_multi_aff_free(ma1);
5735 return NULL;
5738 /* Compute the pullback of "ma1" by the function represented by "ma2".
5739 * In other words, plug in "ma2" in "ma1".
5741 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5742 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5744 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5745 &isl_multi_aff_pullback_multi_aff_aligned);
5748 /* Extend the local space of "dst" to include the divs
5749 * in the local space of "src".
5751 * If "src" does not have any divs or if the local spaces of "dst" and
5752 * "src" are the same, then no extension is required.
5754 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5755 __isl_keep isl_aff *src)
5757 isl_ctx *ctx;
5758 int src_n_div, dst_n_div;
5759 int *exp1 = NULL;
5760 int *exp2 = NULL;
5761 isl_bool equal;
5762 isl_mat *div;
5764 if (!src || !dst)
5765 return isl_aff_free(dst);
5767 ctx = isl_aff_get_ctx(src);
5768 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5769 if (equal < 0)
5770 return isl_aff_free(dst);
5771 if (!equal)
5772 isl_die(ctx, isl_error_invalid,
5773 "spaces don't match", goto error);
5775 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5776 if (src_n_div == 0)
5777 return dst;
5778 equal = isl_local_space_is_equal(src->ls, dst->ls);
5779 if (equal < 0)
5780 return isl_aff_free(dst);
5781 if (equal)
5782 return dst;
5784 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5785 exp1 = isl_alloc_array(ctx, int, src_n_div);
5786 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5787 if (!exp1 || (dst_n_div && !exp2))
5788 goto error;
5790 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5791 dst = isl_aff_expand_divs(dst, div, exp2);
5792 free(exp1);
5793 free(exp2);
5795 return dst;
5796 error:
5797 free(exp1);
5798 free(exp2);
5799 return isl_aff_free(dst);
5802 /* Adjust the local spaces of the affine expressions in "maff"
5803 * such that they all have the save divs.
5805 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5806 __isl_take isl_multi_aff *maff)
5808 int i;
5810 if (!maff)
5811 return NULL;
5812 if (maff->n == 0)
5813 return maff;
5814 maff = isl_multi_aff_cow(maff);
5815 if (!maff)
5816 return NULL;
5818 for (i = 1; i < maff->n; ++i)
5819 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5820 for (i = 1; i < maff->n; ++i) {
5821 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5822 if (!maff->u.p[i])
5823 return isl_multi_aff_free(maff);
5826 return maff;
5829 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5831 aff = isl_aff_cow(aff);
5832 if (!aff)
5833 return NULL;
5835 aff->ls = isl_local_space_lift(aff->ls);
5836 if (!aff->ls)
5837 return isl_aff_free(aff);
5839 return aff;
5842 /* Lift "maff" to a space with extra dimensions such that the result
5843 * has no more existentially quantified variables.
5844 * If "ls" is not NULL, then *ls is assigned the local space that lies
5845 * at the basis of the lifting applied to "maff".
5847 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5848 __isl_give isl_local_space **ls)
5850 int i;
5851 isl_space *space;
5852 unsigned n_div;
5854 if (ls)
5855 *ls = NULL;
5857 if (!maff)
5858 return NULL;
5860 if (maff->n == 0) {
5861 if (ls) {
5862 isl_space *space = isl_multi_aff_get_domain_space(maff);
5863 *ls = isl_local_space_from_space(space);
5864 if (!*ls)
5865 return isl_multi_aff_free(maff);
5867 return maff;
5870 maff = isl_multi_aff_cow(maff);
5871 maff = isl_multi_aff_align_divs(maff);
5872 if (!maff)
5873 return NULL;
5875 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5876 space = isl_multi_aff_get_space(maff);
5877 space = isl_space_lift(isl_space_domain(space), n_div);
5878 space = isl_space_extend_domain_with_range(space,
5879 isl_multi_aff_get_space(maff));
5880 if (!space)
5881 return isl_multi_aff_free(maff);
5882 isl_space_free(maff->space);
5883 maff->space = space;
5885 if (ls) {
5886 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5887 if (!*ls)
5888 return isl_multi_aff_free(maff);
5891 for (i = 0; i < maff->n; ++i) {
5892 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5893 if (!maff->u.p[i])
5894 goto error;
5897 return maff;
5898 error:
5899 if (ls)
5900 isl_local_space_free(*ls);
5901 return isl_multi_aff_free(maff);
5905 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5907 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5908 __isl_keep isl_pw_multi_aff *pma, int pos)
5910 int i;
5911 int n_out;
5912 isl_space *space;
5913 isl_pw_aff *pa;
5915 if (!pma)
5916 return NULL;
5918 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5919 if (pos < 0 || pos >= n_out)
5920 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5921 "index out of bounds", return NULL);
5923 space = isl_pw_multi_aff_get_space(pma);
5924 space = isl_space_drop_dims(space, isl_dim_out,
5925 pos + 1, n_out - pos - 1);
5926 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5928 pa = isl_pw_aff_alloc_size(space, pma->n);
5929 for (i = 0; i < pma->n; ++i) {
5930 isl_aff *aff;
5931 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5932 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5935 return pa;
5938 /* Return an isl_pw_multi_aff with the given "set" as domain and
5939 * an unnamed zero-dimensional range.
5941 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5942 __isl_take isl_set *set)
5944 isl_multi_aff *ma;
5945 isl_space *space;
5947 space = isl_set_get_space(set);
5948 space = isl_space_from_domain(space);
5949 ma = isl_multi_aff_zero(space);
5950 return isl_pw_multi_aff_alloc(set, ma);
5953 /* Add an isl_pw_multi_aff with the given "set" as domain and
5954 * an unnamed zero-dimensional range to *user.
5956 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5957 void *user)
5959 isl_union_pw_multi_aff **upma = user;
5960 isl_pw_multi_aff *pma;
5962 pma = isl_pw_multi_aff_from_domain(set);
5963 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5965 return isl_stat_ok;
5968 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5969 * an unnamed zero-dimensional range.
5971 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5972 __isl_take isl_union_set *uset)
5974 isl_space *space;
5975 isl_union_pw_multi_aff *upma;
5977 if (!uset)
5978 return NULL;
5980 space = isl_union_set_get_space(uset);
5981 upma = isl_union_pw_multi_aff_empty(space);
5983 if (isl_union_set_foreach_set(uset,
5984 &add_pw_multi_aff_from_domain, &upma) < 0)
5985 goto error;
5987 isl_union_set_free(uset);
5988 return upma;
5989 error:
5990 isl_union_set_free(uset);
5991 isl_union_pw_multi_aff_free(upma);
5992 return NULL;
5995 /* Convert "pma" to an isl_map and add it to *umap.
5997 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5998 void *user)
6000 isl_union_map **umap = user;
6001 isl_map *map;
6003 map = isl_map_from_pw_multi_aff(pma);
6004 *umap = isl_union_map_add_map(*umap, map);
6006 return isl_stat_ok;
6009 /* Construct a union map mapping the domain of the union
6010 * piecewise multi-affine expression to its range, with each dimension
6011 * in the range equated to the corresponding affine expression on its cell.
6013 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
6014 __isl_take isl_union_pw_multi_aff *upma)
6016 isl_space *space;
6017 isl_union_map *umap;
6019 if (!upma)
6020 return NULL;
6022 space = isl_union_pw_multi_aff_get_space(upma);
6023 umap = isl_union_map_empty(space);
6025 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
6026 &map_from_pw_multi_aff, &umap) < 0)
6027 goto error;
6029 isl_union_pw_multi_aff_free(upma);
6030 return umap;
6031 error:
6032 isl_union_pw_multi_aff_free(upma);
6033 isl_union_map_free(umap);
6034 return NULL;
6037 /* Local data for bin_entry and the callback "fn".
6039 struct isl_union_pw_multi_aff_bin_data {
6040 isl_union_pw_multi_aff *upma2;
6041 isl_union_pw_multi_aff *res;
6042 isl_pw_multi_aff *pma;
6043 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6046 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6047 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6049 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6051 struct isl_union_pw_multi_aff_bin_data *data = user;
6052 isl_stat r;
6054 data->pma = pma;
6055 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6056 data->fn, data);
6057 isl_pw_multi_aff_free(pma);
6059 return r;
6062 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6063 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6064 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6065 * as *entry. The callback should adjust data->res if desired.
6067 static __isl_give isl_union_pw_multi_aff *bin_op(
6068 __isl_take isl_union_pw_multi_aff *upma1,
6069 __isl_take isl_union_pw_multi_aff *upma2,
6070 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6072 isl_space *space;
6073 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6075 space = isl_union_pw_multi_aff_get_space(upma2);
6076 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6077 space = isl_union_pw_multi_aff_get_space(upma1);
6078 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6080 if (!upma1 || !upma2)
6081 goto error;
6083 data.upma2 = upma2;
6084 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6085 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6086 &bin_entry, &data) < 0)
6087 goto error;
6089 isl_union_pw_multi_aff_free(upma1);
6090 isl_union_pw_multi_aff_free(upma2);
6091 return data.res;
6092 error:
6093 isl_union_pw_multi_aff_free(upma1);
6094 isl_union_pw_multi_aff_free(upma2);
6095 isl_union_pw_multi_aff_free(data.res);
6096 return NULL;
6099 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6100 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6102 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6103 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6105 isl_space *space;
6107 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6108 isl_pw_multi_aff_get_space(pma2));
6109 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6110 &isl_multi_aff_range_product);
6113 /* Given two isl_pw_multi_affs A -> B and C -> D,
6114 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6116 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6117 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6119 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6120 &pw_multi_aff_range_product);
6123 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6124 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6126 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6127 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6129 isl_space *space;
6131 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6132 isl_pw_multi_aff_get_space(pma2));
6133 space = isl_space_flatten_range(space);
6134 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6135 &isl_multi_aff_flat_range_product);
6138 /* Given two isl_pw_multi_affs A -> B and C -> D,
6139 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6141 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6142 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6144 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6145 &pw_multi_aff_flat_range_product);
6148 /* If data->pma and "pma2" have the same domain space, then compute
6149 * their flat range product and the result to data->res.
6151 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6152 void *user)
6154 struct isl_union_pw_multi_aff_bin_data *data = user;
6156 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6157 pma2->dim, isl_dim_in)) {
6158 isl_pw_multi_aff_free(pma2);
6159 return isl_stat_ok;
6162 pma2 = isl_pw_multi_aff_flat_range_product(
6163 isl_pw_multi_aff_copy(data->pma), pma2);
6165 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6167 return isl_stat_ok;
6170 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6171 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6173 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6174 __isl_take isl_union_pw_multi_aff *upma1,
6175 __isl_take isl_union_pw_multi_aff *upma2)
6177 return bin_op(upma1, upma2, &flat_range_product_entry);
6180 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6181 * The parameters are assumed to have been aligned.
6183 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6184 * except that it works on two different isl_pw_* types.
6186 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6187 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6188 __isl_take isl_pw_aff *pa)
6190 int i, j, n;
6191 isl_pw_multi_aff *res = NULL;
6193 if (!pma || !pa)
6194 goto error;
6196 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6197 pa->dim, isl_dim_in))
6198 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6199 "domains don't match", goto error);
6200 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6201 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6202 "index out of bounds", goto error);
6204 n = pma->n * pa->n;
6205 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6207 for (i = 0; i < pma->n; ++i) {
6208 for (j = 0; j < pa->n; ++j) {
6209 isl_set *common;
6210 isl_multi_aff *res_ij;
6211 int empty;
6213 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6214 isl_set_copy(pa->p[j].set));
6215 empty = isl_set_plain_is_empty(common);
6216 if (empty < 0 || empty) {
6217 isl_set_free(common);
6218 if (empty < 0)
6219 goto error;
6220 continue;
6223 res_ij = isl_multi_aff_set_aff(
6224 isl_multi_aff_copy(pma->p[i].maff), pos,
6225 isl_aff_copy(pa->p[j].aff));
6226 res_ij = isl_multi_aff_gist(res_ij,
6227 isl_set_copy(common));
6229 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6233 isl_pw_multi_aff_free(pma);
6234 isl_pw_aff_free(pa);
6235 return res;
6236 error:
6237 isl_pw_multi_aff_free(pma);
6238 isl_pw_aff_free(pa);
6239 return isl_pw_multi_aff_free(res);
6242 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6244 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6245 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6246 __isl_take isl_pw_aff *pa)
6248 isl_bool equal_params;
6250 if (!pma || !pa)
6251 goto error;
6252 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6253 if (equal_params < 0)
6254 goto error;
6255 if (equal_params)
6256 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6257 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6258 isl_pw_aff_check_named_params(pa) < 0)
6259 goto error;
6260 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6261 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6262 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6263 error:
6264 isl_pw_multi_aff_free(pma);
6265 isl_pw_aff_free(pa);
6266 return NULL;
6269 /* Do the parameters of "pa" match those of "space"?
6271 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6272 __isl_keep isl_space *space)
6274 isl_space *pa_space;
6275 isl_bool match;
6277 if (!pa || !space)
6278 return isl_bool_error;
6280 pa_space = isl_pw_aff_get_space(pa);
6282 match = isl_space_has_equal_params(space, pa_space);
6284 isl_space_free(pa_space);
6285 return match;
6288 /* Check that the domain space of "pa" matches "space".
6290 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6291 __isl_keep isl_space *space)
6293 isl_space *pa_space;
6294 isl_bool match;
6296 if (!pa || !space)
6297 return isl_stat_error;
6299 pa_space = isl_pw_aff_get_space(pa);
6301 match = isl_space_has_equal_params(space, pa_space);
6302 if (match < 0)
6303 goto error;
6304 if (!match)
6305 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6306 "parameters don't match", goto error);
6307 match = isl_space_tuple_is_equal(space, isl_dim_in,
6308 pa_space, isl_dim_in);
6309 if (match < 0)
6310 goto error;
6311 if (!match)
6312 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6313 "domains don't match", goto error);
6314 isl_space_free(pa_space);
6315 return isl_stat_ok;
6316 error:
6317 isl_space_free(pa_space);
6318 return isl_stat_error;
6321 #undef BASE
6322 #define BASE pw_aff
6323 #undef DOMBASE
6324 #define DOMBASE set
6326 #include <isl_multi_explicit_domain.c>
6327 #include <isl_multi_pw_aff_explicit_domain.c>
6328 #include <isl_multi_templ.c>
6329 #include <isl_multi_apply_set.c>
6330 #include <isl_multi_coalesce.c>
6331 #include <isl_multi_dims.c>
6332 #include <isl_multi_gist.c>
6333 #include <isl_multi_hash.c>
6334 #include <isl_multi_align_set.c>
6335 #include <isl_multi_intersect.c>
6337 /* Does "mpa" have a non-trivial explicit domain?
6339 * The explicit domain, if present, is trivial if it represents
6340 * an (obviously) universe set.
6342 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6343 __isl_keep isl_multi_pw_aff *mpa)
6345 if (!mpa)
6346 return isl_bool_error;
6347 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6348 return isl_bool_false;
6349 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6352 /* Scale the elements of "pma" by the corresponding elements of "mv".
6354 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6355 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6357 int i;
6358 isl_bool equal_params;
6360 pma = isl_pw_multi_aff_cow(pma);
6361 if (!pma || !mv)
6362 goto error;
6363 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6364 mv->space, isl_dim_set))
6365 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6366 "spaces don't match", goto error);
6367 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6368 if (equal_params < 0)
6369 goto error;
6370 if (!equal_params) {
6371 pma = isl_pw_multi_aff_align_params(pma,
6372 isl_multi_val_get_space(mv));
6373 mv = isl_multi_val_align_params(mv,
6374 isl_pw_multi_aff_get_space(pma));
6375 if (!pma || !mv)
6376 goto error;
6379 for (i = 0; i < pma->n; ++i) {
6380 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6381 isl_multi_val_copy(mv));
6382 if (!pma->p[i].maff)
6383 goto error;
6386 isl_multi_val_free(mv);
6387 return pma;
6388 error:
6389 isl_multi_val_free(mv);
6390 isl_pw_multi_aff_free(pma);
6391 return NULL;
6394 /* This function is called for each entry of an isl_union_pw_multi_aff.
6395 * If the space of the entry matches that of data->mv,
6396 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6397 * Otherwise, return an empty isl_pw_multi_aff.
6399 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6400 __isl_take isl_pw_multi_aff *pma, void *user)
6402 isl_multi_val *mv = user;
6404 if (!pma)
6405 return NULL;
6406 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6407 mv->space, isl_dim_set)) {
6408 isl_space *space = isl_pw_multi_aff_get_space(pma);
6409 isl_pw_multi_aff_free(pma);
6410 return isl_pw_multi_aff_empty(space);
6413 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6416 /* Scale the elements of "upma" by the corresponding elements of "mv",
6417 * for those entries that match the space of "mv".
6419 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6420 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6422 upma = isl_union_pw_multi_aff_align_params(upma,
6423 isl_multi_val_get_space(mv));
6424 mv = isl_multi_val_align_params(mv,
6425 isl_union_pw_multi_aff_get_space(upma));
6426 if (!upma || !mv)
6427 goto error;
6429 return isl_union_pw_multi_aff_transform(upma,
6430 &union_pw_multi_aff_scale_multi_val_entry, mv);
6432 isl_multi_val_free(mv);
6433 return upma;
6434 error:
6435 isl_multi_val_free(mv);
6436 isl_union_pw_multi_aff_free(upma);
6437 return NULL;
6440 /* Construct and return a piecewise multi affine expression
6441 * in the given space with value zero in each of the output dimensions and
6442 * a universe domain.
6444 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6446 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6449 /* Construct and return a piecewise multi affine expression
6450 * that is equal to the given piecewise affine expression.
6452 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6453 __isl_take isl_pw_aff *pa)
6455 int i;
6456 isl_space *space;
6457 isl_pw_multi_aff *pma;
6459 if (!pa)
6460 return NULL;
6462 space = isl_pw_aff_get_space(pa);
6463 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6465 for (i = 0; i < pa->n; ++i) {
6466 isl_set *set;
6467 isl_multi_aff *ma;
6469 set = isl_set_copy(pa->p[i].set);
6470 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6471 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6474 isl_pw_aff_free(pa);
6475 return pma;
6478 /* Construct a set or map mapping the shared (parameter) domain
6479 * of the piecewise affine expressions to the range of "mpa"
6480 * with each dimension in the range equated to the
6481 * corresponding piecewise affine expression.
6483 static __isl_give isl_map *map_from_multi_pw_aff(
6484 __isl_take isl_multi_pw_aff *mpa)
6486 int i;
6487 isl_space *space;
6488 isl_map *map;
6490 if (!mpa)
6491 return NULL;
6493 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6494 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6495 "invalid space", goto error);
6497 space = isl_multi_pw_aff_get_domain_space(mpa);
6498 map = isl_map_universe(isl_space_from_domain(space));
6500 for (i = 0; i < mpa->n; ++i) {
6501 isl_pw_aff *pa;
6502 isl_map *map_i;
6504 pa = isl_pw_aff_copy(mpa->u.p[i]);
6505 map_i = map_from_pw_aff(pa);
6507 map = isl_map_flat_range_product(map, map_i);
6510 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6512 isl_multi_pw_aff_free(mpa);
6513 return map;
6514 error:
6515 isl_multi_pw_aff_free(mpa);
6516 return NULL;
6519 /* Construct a map mapping the shared domain
6520 * of the piecewise affine expressions to the range of "mpa"
6521 * with each dimension in the range equated to the
6522 * corresponding piecewise affine expression.
6524 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6526 if (!mpa)
6527 return NULL;
6528 if (isl_space_is_set(mpa->space))
6529 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6530 "space of input is not a map", goto error);
6532 return map_from_multi_pw_aff(mpa);
6533 error:
6534 isl_multi_pw_aff_free(mpa);
6535 return NULL;
6538 /* Construct a set mapping the shared parameter domain
6539 * of the piecewise affine expressions to the space of "mpa"
6540 * with each dimension in the range equated to the
6541 * corresponding piecewise affine expression.
6543 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6545 if (!mpa)
6546 return NULL;
6547 if (!isl_space_is_set(mpa->space))
6548 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6549 "space of input is not a set", goto error);
6551 return map_from_multi_pw_aff(mpa);
6552 error:
6553 isl_multi_pw_aff_free(mpa);
6554 return NULL;
6557 /* Construct and return a piecewise multi affine expression
6558 * that is equal to the given multi piecewise affine expression
6559 * on the shared domain of the piecewise affine expressions,
6560 * in the special case of a 0D multi piecewise affine expression.
6562 * Create a piecewise multi affine expression with the explicit domain of
6563 * the 0D multi piecewise affine expression as domain.
6565 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6566 __isl_take isl_multi_pw_aff *mpa)
6568 isl_space *space;
6569 isl_set *dom;
6570 isl_multi_aff *ma;
6572 space = isl_multi_pw_aff_get_space(mpa);
6573 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6574 isl_multi_pw_aff_free(mpa);
6576 ma = isl_multi_aff_zero(space);
6577 return isl_pw_multi_aff_alloc(dom, ma);
6580 /* Construct and return a piecewise multi affine expression
6581 * that is equal to the given multi piecewise affine expression
6582 * on the shared domain of the piecewise affine expressions.
6584 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6585 __isl_take isl_multi_pw_aff *mpa)
6587 int i;
6588 isl_space *space;
6589 isl_pw_aff *pa;
6590 isl_pw_multi_aff *pma;
6592 if (!mpa)
6593 return NULL;
6595 if (mpa->n == 0)
6596 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6598 space = isl_multi_pw_aff_get_space(mpa);
6599 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6600 pma = isl_pw_multi_aff_from_pw_aff(pa);
6602 for (i = 1; i < mpa->n; ++i) {
6603 isl_pw_multi_aff *pma_i;
6605 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6606 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6607 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6610 pma = isl_pw_multi_aff_reset_space(pma, space);
6612 isl_multi_pw_aff_free(mpa);
6613 return pma;
6616 /* Construct and return a multi piecewise affine expression
6617 * that is equal to the given multi affine expression.
6619 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6620 __isl_take isl_multi_aff *ma)
6622 int i, n;
6623 isl_multi_pw_aff *mpa;
6625 if (!ma)
6626 return NULL;
6628 n = isl_multi_aff_dim(ma, isl_dim_out);
6629 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6631 for (i = 0; i < n; ++i) {
6632 isl_pw_aff *pa;
6634 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6635 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6638 isl_multi_aff_free(ma);
6639 return mpa;
6642 /* Construct and return a multi piecewise affine expression
6643 * that is equal to the given piecewise multi affine expression.
6645 * If the resulting multi piecewise affine expression has
6646 * an explicit domain, then assign it the domain of the input.
6647 * In other cases, the domain is stored in the individual elements.
6649 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6650 __isl_take isl_pw_multi_aff *pma)
6652 int i, n;
6653 isl_space *space;
6654 isl_multi_pw_aff *mpa;
6656 if (!pma)
6657 return NULL;
6659 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6660 space = isl_pw_multi_aff_get_space(pma);
6661 mpa = isl_multi_pw_aff_alloc(space);
6663 for (i = 0; i < n; ++i) {
6664 isl_pw_aff *pa;
6666 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6667 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6669 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6670 isl_set *dom;
6672 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6673 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6676 isl_pw_multi_aff_free(pma);
6677 return mpa;
6680 /* Do "pa1" and "pa2" represent the same function?
6682 * We first check if they are obviously equal.
6683 * If not, we convert them to maps and check if those are equal.
6685 * If "pa1" or "pa2" contain any NaNs, then they are considered
6686 * not to be the same. A NaN is not equal to anything, not even
6687 * to another NaN.
6689 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6690 __isl_keep isl_pw_aff *pa2)
6692 isl_bool equal;
6693 isl_bool has_nan;
6694 isl_map *map1, *map2;
6696 if (!pa1 || !pa2)
6697 return isl_bool_error;
6699 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6700 if (equal < 0 || equal)
6701 return equal;
6702 has_nan = either_involves_nan(pa1, pa2);
6703 if (has_nan < 0)
6704 return isl_bool_error;
6705 if (has_nan)
6706 return isl_bool_false;
6708 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6709 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6710 equal = isl_map_is_equal(map1, map2);
6711 isl_map_free(map1);
6712 isl_map_free(map2);
6714 return equal;
6717 /* Do "mpa1" and "mpa2" represent the same function?
6719 * Note that we cannot convert the entire isl_multi_pw_aff
6720 * to a map because the domains of the piecewise affine expressions
6721 * may not be the same.
6723 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6724 __isl_keep isl_multi_pw_aff *mpa2)
6726 int i;
6727 isl_bool equal, equal_params;
6729 if (!mpa1 || !mpa2)
6730 return isl_bool_error;
6732 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6733 if (equal_params < 0)
6734 return isl_bool_error;
6735 if (!equal_params) {
6736 if (!isl_space_has_named_params(mpa1->space))
6737 return isl_bool_false;
6738 if (!isl_space_has_named_params(mpa2->space))
6739 return isl_bool_false;
6740 mpa1 = isl_multi_pw_aff_copy(mpa1);
6741 mpa2 = isl_multi_pw_aff_copy(mpa2);
6742 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6743 isl_multi_pw_aff_get_space(mpa2));
6744 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6745 isl_multi_pw_aff_get_space(mpa1));
6746 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6747 isl_multi_pw_aff_free(mpa1);
6748 isl_multi_pw_aff_free(mpa2);
6749 return equal;
6752 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6753 if (equal < 0 || !equal)
6754 return equal;
6756 for (i = 0; i < mpa1->n; ++i) {
6757 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6758 if (equal < 0 || !equal)
6759 return equal;
6762 return isl_bool_true;
6765 /* Do "pma1" and "pma2" represent the same function?
6767 * First check if they are obviously equal.
6768 * If not, then convert them to maps and check if those are equal.
6770 * If "pa1" or "pa2" contain any NaNs, then they are considered
6771 * not to be the same. A NaN is not equal to anything, not even
6772 * to another NaN.
6774 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6775 __isl_keep isl_pw_multi_aff *pma2)
6777 isl_bool equal;
6778 isl_bool has_nan;
6779 isl_map *map1, *map2;
6781 if (!pma1 || !pma2)
6782 return isl_bool_error;
6784 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6785 if (equal < 0 || equal)
6786 return equal;
6787 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6788 if (has_nan >= 0 && !has_nan)
6789 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6790 if (has_nan < 0 || has_nan)
6791 return isl_bool_not(has_nan);
6793 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6794 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6795 equal = isl_map_is_equal(map1, map2);
6796 isl_map_free(map1);
6797 isl_map_free(map2);
6799 return equal;
6802 /* Compute the pullback of "mpa" by the function represented by "ma".
6803 * In other words, plug in "ma" in "mpa".
6805 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6807 * If "mpa" has an explicit domain, then it is this domain
6808 * that needs to undergo a pullback, i.e., a preimage.
6810 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6811 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6813 int i;
6814 isl_space *space = NULL;
6816 mpa = isl_multi_pw_aff_cow(mpa);
6817 if (!mpa || !ma)
6818 goto error;
6820 space = isl_space_join(isl_multi_aff_get_space(ma),
6821 isl_multi_pw_aff_get_space(mpa));
6822 if (!space)
6823 goto error;
6825 for (i = 0; i < mpa->n; ++i) {
6826 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6827 isl_multi_aff_copy(ma));
6828 if (!mpa->u.p[i])
6829 goto error;
6831 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6832 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6833 isl_multi_aff_copy(ma));
6834 if (!mpa->u.dom)
6835 goto error;
6838 isl_multi_aff_free(ma);
6839 isl_space_free(mpa->space);
6840 mpa->space = space;
6841 return mpa;
6842 error:
6843 isl_space_free(space);
6844 isl_multi_pw_aff_free(mpa);
6845 isl_multi_aff_free(ma);
6846 return NULL;
6849 /* Compute the pullback of "mpa" by the function represented by "ma".
6850 * In other words, plug in "ma" in "mpa".
6852 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6853 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6855 isl_bool equal_params;
6857 if (!mpa || !ma)
6858 goto error;
6859 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6860 if (equal_params < 0)
6861 goto error;
6862 if (equal_params)
6863 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6864 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6865 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6866 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6867 error:
6868 isl_multi_pw_aff_free(mpa);
6869 isl_multi_aff_free(ma);
6870 return NULL;
6873 /* Compute the pullback of "mpa" by the function represented by "pma".
6874 * In other words, plug in "pma" in "mpa".
6876 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6878 * If "mpa" has an explicit domain, then it is this domain
6879 * that needs to undergo a pullback, i.e., a preimage.
6881 static __isl_give isl_multi_pw_aff *
6882 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6883 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6885 int i;
6886 isl_space *space = NULL;
6888 mpa = isl_multi_pw_aff_cow(mpa);
6889 if (!mpa || !pma)
6890 goto error;
6892 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6893 isl_multi_pw_aff_get_space(mpa));
6895 for (i = 0; i < mpa->n; ++i) {
6896 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6897 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6898 if (!mpa->u.p[i])
6899 goto error;
6901 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6902 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6903 isl_pw_multi_aff_copy(pma));
6904 if (!mpa->u.dom)
6905 goto error;
6908 isl_pw_multi_aff_free(pma);
6909 isl_space_free(mpa->space);
6910 mpa->space = space;
6911 return mpa;
6912 error:
6913 isl_space_free(space);
6914 isl_multi_pw_aff_free(mpa);
6915 isl_pw_multi_aff_free(pma);
6916 return NULL;
6919 /* Compute the pullback of "mpa" by the function represented by "pma".
6920 * In other words, plug in "pma" in "mpa".
6922 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6923 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6925 isl_bool equal_params;
6927 if (!mpa || !pma)
6928 goto error;
6929 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6930 if (equal_params < 0)
6931 goto error;
6932 if (equal_params)
6933 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6934 mpa = isl_multi_pw_aff_align_params(mpa,
6935 isl_pw_multi_aff_get_space(pma));
6936 pma = isl_pw_multi_aff_align_params(pma,
6937 isl_multi_pw_aff_get_space(mpa));
6938 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6939 error:
6940 isl_multi_pw_aff_free(mpa);
6941 isl_pw_multi_aff_free(pma);
6942 return NULL;
6945 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6946 * with the domain of "aff". The domain of the result is the same
6947 * as that of "mpa".
6948 * "mpa" and "aff" are assumed to have been aligned.
6950 * We first extract the parametric constant from "aff", defined
6951 * over the correct domain.
6952 * Then we add the appropriate combinations of the members of "mpa".
6953 * Finally, we add the integer divisions through recursive calls.
6955 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6956 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6958 int i, n_in, n_div;
6959 isl_space *space;
6960 isl_val *v;
6961 isl_pw_aff *pa;
6962 isl_aff *tmp;
6964 n_in = isl_aff_dim(aff, isl_dim_in);
6965 n_div = isl_aff_dim(aff, isl_dim_div);
6967 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6968 tmp = isl_aff_copy(aff);
6969 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6970 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6971 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6972 isl_space_dim(space, isl_dim_set));
6973 tmp = isl_aff_reset_domain_space(tmp, space);
6974 pa = isl_pw_aff_from_aff(tmp);
6976 for (i = 0; i < n_in; ++i) {
6977 isl_pw_aff *pa_i;
6979 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6980 continue;
6981 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6982 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6983 pa_i = isl_pw_aff_scale_val(pa_i, v);
6984 pa = isl_pw_aff_add(pa, pa_i);
6987 for (i = 0; i < n_div; ++i) {
6988 isl_aff *div;
6989 isl_pw_aff *pa_i;
6991 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6992 continue;
6993 div = isl_aff_get_div(aff, i);
6994 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6995 isl_multi_pw_aff_copy(mpa), div);
6996 pa_i = isl_pw_aff_floor(pa_i);
6997 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6998 pa_i = isl_pw_aff_scale_val(pa_i, v);
6999 pa = isl_pw_aff_add(pa, pa_i);
7002 isl_multi_pw_aff_free(mpa);
7003 isl_aff_free(aff);
7005 return pa;
7008 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7009 * with the domain of "aff". The domain of the result is the same
7010 * as that of "mpa".
7012 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7013 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7015 isl_bool equal_params;
7017 if (!aff || !mpa)
7018 goto error;
7019 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7020 if (equal_params < 0)
7021 goto error;
7022 if (equal_params)
7023 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7025 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7026 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7028 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7029 error:
7030 isl_aff_free(aff);
7031 isl_multi_pw_aff_free(mpa);
7032 return NULL;
7035 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7036 * with the domain of "pa". The domain of the result is the same
7037 * as that of "mpa".
7038 * "mpa" and "pa" are assumed to have been aligned.
7040 * We consider each piece in turn. Note that the domains of the
7041 * pieces are assumed to be disjoint and they remain disjoint
7042 * after taking the preimage (over the same function).
7044 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7045 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7047 isl_space *space;
7048 isl_pw_aff *res;
7049 int i;
7051 if (!mpa || !pa)
7052 goto error;
7054 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7055 isl_pw_aff_get_space(pa));
7056 res = isl_pw_aff_empty(space);
7058 for (i = 0; i < pa->n; ++i) {
7059 isl_pw_aff *pa_i;
7060 isl_set *domain;
7062 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7063 isl_multi_pw_aff_copy(mpa),
7064 isl_aff_copy(pa->p[i].aff));
7065 domain = isl_set_copy(pa->p[i].set);
7066 domain = isl_set_preimage_multi_pw_aff(domain,
7067 isl_multi_pw_aff_copy(mpa));
7068 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7069 res = isl_pw_aff_add_disjoint(res, pa_i);
7072 isl_pw_aff_free(pa);
7073 isl_multi_pw_aff_free(mpa);
7074 return res;
7075 error:
7076 isl_pw_aff_free(pa);
7077 isl_multi_pw_aff_free(mpa);
7078 return NULL;
7081 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7082 * with the domain of "pa". The domain of the result is the same
7083 * as that of "mpa".
7085 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7086 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7088 isl_bool equal_params;
7090 if (!pa || !mpa)
7091 goto error;
7092 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7093 if (equal_params < 0)
7094 goto error;
7095 if (equal_params)
7096 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7098 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7099 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7101 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7102 error:
7103 isl_pw_aff_free(pa);
7104 isl_multi_pw_aff_free(mpa);
7105 return NULL;
7108 /* Compute the pullback of "pa" by the function represented by "mpa".
7109 * In other words, plug in "mpa" in "pa".
7110 * "pa" and "mpa" are assumed to have been aligned.
7112 * The pullback is computed by applying "pa" to "mpa".
7114 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7115 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7117 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7120 /* Compute the pullback of "pa" by the function represented by "mpa".
7121 * In other words, plug in "mpa" in "pa".
7123 * The pullback is computed by applying "pa" to "mpa".
7125 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7126 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7128 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7131 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7132 * In other words, plug in "mpa2" in "mpa1".
7134 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7136 * We pullback each member of "mpa1" in turn.
7138 * If "mpa1" has an explicit domain, then it is this domain
7139 * that needs to undergo a pullback instead, i.e., a preimage.
7141 static __isl_give isl_multi_pw_aff *
7142 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7143 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7145 int i;
7146 isl_space *space = NULL;
7148 mpa1 = isl_multi_pw_aff_cow(mpa1);
7149 if (!mpa1 || !mpa2)
7150 goto error;
7152 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7153 isl_multi_pw_aff_get_space(mpa1));
7155 for (i = 0; i < mpa1->n; ++i) {
7156 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7157 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7158 if (!mpa1->u.p[i])
7159 goto error;
7162 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7163 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7164 isl_multi_pw_aff_copy(mpa2));
7165 if (!mpa1->u.dom)
7166 goto error;
7168 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7170 isl_multi_pw_aff_free(mpa2);
7171 return mpa1;
7172 error:
7173 isl_space_free(space);
7174 isl_multi_pw_aff_free(mpa1);
7175 isl_multi_pw_aff_free(mpa2);
7176 return NULL;
7179 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7180 * In other words, plug in "mpa2" in "mpa1".
7182 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7183 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7185 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7186 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7189 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7190 * of "mpa1" and "mpa2" live in the same space, construct map space
7191 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7192 * with this map space as extract argument.
7194 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7195 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7196 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7197 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7199 int match;
7200 isl_space *space1, *space2;
7201 isl_map *res;
7203 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7204 isl_multi_pw_aff_get_space(mpa2));
7205 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7206 isl_multi_pw_aff_get_space(mpa1));
7207 if (!mpa1 || !mpa2)
7208 goto error;
7209 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7210 mpa2->space, isl_dim_out);
7211 if (match < 0)
7212 goto error;
7213 if (!match)
7214 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7215 "range spaces don't match", goto error);
7216 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7217 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7218 space1 = isl_space_map_from_domain_and_range(space1, space2);
7220 res = order(mpa1, mpa2, space1);
7221 isl_multi_pw_aff_free(mpa1);
7222 isl_multi_pw_aff_free(mpa2);
7223 return res;
7224 error:
7225 isl_multi_pw_aff_free(mpa1);
7226 isl_multi_pw_aff_free(mpa2);
7227 return NULL;
7230 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7231 * where the function values are equal. "space" is the space of the result.
7232 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7234 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7235 * in the sequences are equal.
7237 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7238 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7239 __isl_take isl_space *space)
7241 int i, n;
7242 isl_map *res;
7244 res = isl_map_universe(space);
7246 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7247 for (i = 0; i < n; ++i) {
7248 isl_pw_aff *pa1, *pa2;
7249 isl_map *map;
7251 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7252 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7253 map = isl_pw_aff_eq_map(pa1, pa2);
7254 res = isl_map_intersect(res, map);
7257 return res;
7260 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7261 * where the function values are equal.
7263 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7264 __isl_take isl_multi_pw_aff *mpa2)
7266 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7267 &isl_multi_pw_aff_eq_map_on_space);
7270 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7271 * where the function values of "mpa1" is lexicographically satisfies "base"
7272 * compared to that of "mpa2". "space" is the space of the result.
7273 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7275 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7276 * if its i-th element satisfies "base" when compared to
7277 * the i-th element of "mpa2" while all previous elements are
7278 * pairwise equal.
7280 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7281 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7282 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7283 __isl_take isl_pw_aff *pa2),
7284 __isl_take isl_space *space)
7286 int i, n;
7287 isl_map *res, *rest;
7289 res = isl_map_empty(isl_space_copy(space));
7290 rest = isl_map_universe(space);
7292 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7293 for (i = 0; i < n; ++i) {
7294 isl_pw_aff *pa1, *pa2;
7295 isl_map *map;
7297 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7298 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7299 map = base(pa1, pa2);
7300 map = isl_map_intersect(map, isl_map_copy(rest));
7301 res = isl_map_union(res, map);
7303 if (i == n - 1)
7304 continue;
7306 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7307 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7308 map = isl_pw_aff_eq_map(pa1, pa2);
7309 rest = isl_map_intersect(rest, map);
7312 isl_map_free(rest);
7313 return res;
7316 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7317 * where the function value of "mpa1" is lexicographically less than that
7318 * of "mpa2". "space" is the space of the result.
7319 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7321 * "mpa1" is less than "mpa2" if its i-th element is smaller
7322 * than the i-th element of "mpa2" while all previous elements are
7323 * pairwise equal.
7325 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7326 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7327 __isl_take isl_space *space)
7329 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7330 &isl_pw_aff_lt_map, space);
7333 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7334 * where the function value of "mpa1" is lexicographically less than that
7335 * of "mpa2".
7337 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7338 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7340 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7341 &isl_multi_pw_aff_lex_lt_map_on_space);
7344 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7345 * where the function value of "mpa1" is lexicographically greater than that
7346 * of "mpa2". "space" is the space of the result.
7347 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7349 * "mpa1" is greater than "mpa2" if its i-th element is greater
7350 * than the i-th element of "mpa2" while all previous elements are
7351 * pairwise equal.
7353 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7354 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7355 __isl_take isl_space *space)
7357 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7358 &isl_pw_aff_gt_map, space);
7361 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7362 * where the function value of "mpa1" is lexicographically greater than that
7363 * of "mpa2".
7365 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7366 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7368 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7369 &isl_multi_pw_aff_lex_gt_map_on_space);
7372 /* Compare two isl_affs.
7374 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7375 * than "aff2" and 0 if they are equal.
7377 * The order is fairly arbitrary. We do consider expressions that only involve
7378 * earlier dimensions as "smaller".
7380 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7382 int cmp;
7383 int last1, last2;
7385 if (aff1 == aff2)
7386 return 0;
7388 if (!aff1)
7389 return -1;
7390 if (!aff2)
7391 return 1;
7393 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7394 if (cmp != 0)
7395 return cmp;
7397 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7398 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7399 if (last1 != last2)
7400 return last1 - last2;
7402 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7405 /* Compare two isl_pw_affs.
7407 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7408 * than "pa2" and 0 if they are equal.
7410 * The order is fairly arbitrary. We do consider expressions that only involve
7411 * earlier dimensions as "smaller".
7413 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7414 __isl_keep isl_pw_aff *pa2)
7416 int i;
7417 int cmp;
7419 if (pa1 == pa2)
7420 return 0;
7422 if (!pa1)
7423 return -1;
7424 if (!pa2)
7425 return 1;
7427 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7428 if (cmp != 0)
7429 return cmp;
7431 if (pa1->n != pa2->n)
7432 return pa1->n - pa2->n;
7434 for (i = 0; i < pa1->n; ++i) {
7435 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7436 if (cmp != 0)
7437 return cmp;
7438 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7439 if (cmp != 0)
7440 return cmp;
7443 return 0;
7446 /* Return a piecewise affine expression that is equal to "v" on "domain".
7448 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7449 __isl_take isl_val *v)
7451 isl_space *space;
7452 isl_local_space *ls;
7453 isl_aff *aff;
7455 space = isl_set_get_space(domain);
7456 ls = isl_local_space_from_space(space);
7457 aff = isl_aff_val_on_domain(ls, v);
7459 return isl_pw_aff_alloc(domain, aff);
7462 /* Return a multi affine expression that is equal to "mv" on domain
7463 * space "space".
7465 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7466 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7468 int i, n;
7469 isl_space *space2;
7470 isl_local_space *ls;
7471 isl_multi_aff *ma;
7473 if (!space || !mv)
7474 goto error;
7476 n = isl_multi_val_dim(mv, isl_dim_set);
7477 space2 = isl_multi_val_get_space(mv);
7478 space2 = isl_space_align_params(space2, isl_space_copy(space));
7479 space = isl_space_align_params(space, isl_space_copy(space2));
7480 space = isl_space_map_from_domain_and_range(space, space2);
7481 ma = isl_multi_aff_alloc(isl_space_copy(space));
7482 ls = isl_local_space_from_space(isl_space_domain(space));
7483 for (i = 0; i < n; ++i) {
7484 isl_val *v;
7485 isl_aff *aff;
7487 v = isl_multi_val_get_val(mv, i);
7488 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7489 ma = isl_multi_aff_set_aff(ma, i, aff);
7491 isl_local_space_free(ls);
7493 isl_multi_val_free(mv);
7494 return ma;
7495 error:
7496 isl_space_free(space);
7497 isl_multi_val_free(mv);
7498 return NULL;
7501 /* Return a piecewise multi-affine expression
7502 * that is equal to "mv" on "domain".
7504 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7505 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7507 isl_space *space;
7508 isl_multi_aff *ma;
7510 space = isl_set_get_space(domain);
7511 ma = isl_multi_aff_multi_val_on_space(space, mv);
7513 return isl_pw_multi_aff_alloc(domain, ma);
7516 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7517 * mv is the value that should be attained on each domain set
7518 * res collects the results
7520 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7521 isl_multi_val *mv;
7522 isl_union_pw_multi_aff *res;
7525 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7526 * and add it to data->res.
7528 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7529 void *user)
7531 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7532 isl_pw_multi_aff *pma;
7533 isl_multi_val *mv;
7535 mv = isl_multi_val_copy(data->mv);
7536 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7537 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7539 return data->res ? isl_stat_ok : isl_stat_error;
7542 /* Return a union piecewise multi-affine expression
7543 * that is equal to "mv" on "domain".
7545 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7546 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7548 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7549 isl_space *space;
7551 space = isl_union_set_get_space(domain);
7552 data.res = isl_union_pw_multi_aff_empty(space);
7553 data.mv = mv;
7554 if (isl_union_set_foreach_set(domain,
7555 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7556 data.res = isl_union_pw_multi_aff_free(data.res);
7557 isl_union_set_free(domain);
7558 isl_multi_val_free(mv);
7559 return data.res;
7562 /* Compute the pullback of data->pma by the function represented by "pma2",
7563 * provided the spaces match, and add the results to data->res.
7565 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7567 struct isl_union_pw_multi_aff_bin_data *data = user;
7569 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7570 pma2->dim, isl_dim_out)) {
7571 isl_pw_multi_aff_free(pma2);
7572 return isl_stat_ok;
7575 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7576 isl_pw_multi_aff_copy(data->pma), pma2);
7578 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7579 if (!data->res)
7580 return isl_stat_error;
7582 return isl_stat_ok;
7585 /* Compute the pullback of "upma1" by the function represented by "upma2".
7587 __isl_give isl_union_pw_multi_aff *
7588 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7589 __isl_take isl_union_pw_multi_aff *upma1,
7590 __isl_take isl_union_pw_multi_aff *upma2)
7592 return bin_op(upma1, upma2, &pullback_entry);
7595 /* Check that the domain space of "upa" matches "space".
7597 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7598 * can in principle never fail since the space "space" is that
7599 * of the isl_multi_union_pw_aff and is a set space such that
7600 * there is no domain space to match.
7602 * We check the parameters and double-check that "space" is
7603 * indeed that of a set.
7605 static isl_stat isl_union_pw_aff_check_match_domain_space(
7606 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7608 isl_space *upa_space;
7609 isl_bool match;
7611 if (!upa || !space)
7612 return isl_stat_error;
7614 match = isl_space_is_set(space);
7615 if (match < 0)
7616 return isl_stat_error;
7617 if (!match)
7618 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7619 "expecting set space", return -1);
7621 upa_space = isl_union_pw_aff_get_space(upa);
7622 match = isl_space_has_equal_params(space, upa_space);
7623 if (match < 0)
7624 goto error;
7625 if (!match)
7626 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7627 "parameters don't match", goto error);
7629 isl_space_free(upa_space);
7630 return isl_stat_ok;
7631 error:
7632 isl_space_free(upa_space);
7633 return isl_stat_error;
7636 /* Do the parameters of "upa" match those of "space"?
7638 static isl_bool isl_union_pw_aff_matching_params(
7639 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7641 isl_space *upa_space;
7642 isl_bool match;
7644 if (!upa || !space)
7645 return isl_bool_error;
7647 upa_space = isl_union_pw_aff_get_space(upa);
7649 match = isl_space_has_equal_params(space, upa_space);
7651 isl_space_free(upa_space);
7652 return match;
7655 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7656 * space represents the new parameters.
7657 * res collects the results.
7659 struct isl_union_pw_aff_reset_params_data {
7660 isl_space *space;
7661 isl_union_pw_aff *res;
7664 /* Replace the parameters of "pa" by data->space and
7665 * add the result to data->res.
7667 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7669 struct isl_union_pw_aff_reset_params_data *data = user;
7670 isl_space *space;
7672 space = isl_pw_aff_get_space(pa);
7673 space = isl_space_replace_params(space, data->space);
7674 pa = isl_pw_aff_reset_space(pa, space);
7675 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7677 return data->res ? isl_stat_ok : isl_stat_error;
7680 /* Replace the domain space of "upa" by "space".
7681 * Since a union expression does not have a (single) domain space,
7682 * "space" is necessarily a parameter space.
7684 * Since the order and the names of the parameters determine
7685 * the hash value, we need to create a new hash table.
7687 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7688 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7690 struct isl_union_pw_aff_reset_params_data data = { space };
7691 isl_bool match;
7693 match = isl_union_pw_aff_matching_params(upa, space);
7694 if (match < 0)
7695 upa = isl_union_pw_aff_free(upa);
7696 else if (match) {
7697 isl_space_free(space);
7698 return upa;
7701 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7702 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7703 data.res = isl_union_pw_aff_free(data.res);
7705 isl_union_pw_aff_free(upa);
7706 isl_space_free(space);
7707 return data.res;
7710 /* Return the floor of "pa".
7712 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7714 return isl_pw_aff_floor(pa);
7717 /* Given f, return floor(f).
7719 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7720 __isl_take isl_union_pw_aff *upa)
7722 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7725 /* Compute
7727 * upa mod m = upa - m * floor(upa/m)
7729 * with m an integer value.
7731 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7732 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7734 isl_union_pw_aff *res;
7736 if (!upa || !m)
7737 goto error;
7739 if (!isl_val_is_int(m))
7740 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7741 "expecting integer modulo", goto error);
7742 if (!isl_val_is_pos(m))
7743 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7744 "expecting positive modulo", goto error);
7746 res = isl_union_pw_aff_copy(upa);
7747 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7748 upa = isl_union_pw_aff_floor(upa);
7749 upa = isl_union_pw_aff_scale_val(upa, m);
7750 res = isl_union_pw_aff_sub(res, upa);
7752 return res;
7753 error:
7754 isl_val_free(m);
7755 isl_union_pw_aff_free(upa);
7756 return NULL;
7759 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7760 * pos is the output position that needs to be extracted.
7761 * res collects the results.
7763 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7764 int pos;
7765 isl_union_pw_aff *res;
7768 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7769 * (assuming it has such a dimension) and add it to data->res.
7771 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7773 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7774 int n_out;
7775 isl_pw_aff *pa;
7777 if (!pma)
7778 return isl_stat_error;
7780 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7781 if (data->pos >= n_out) {
7782 isl_pw_multi_aff_free(pma);
7783 return isl_stat_ok;
7786 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7787 isl_pw_multi_aff_free(pma);
7789 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7791 return data->res ? isl_stat_ok : isl_stat_error;
7794 /* Extract an isl_union_pw_aff corresponding to
7795 * output dimension "pos" of "upma".
7797 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7798 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7800 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7801 isl_space *space;
7803 if (!upma)
7804 return NULL;
7806 if (pos < 0)
7807 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7808 "cannot extract at negative position", return NULL);
7810 space = isl_union_pw_multi_aff_get_space(upma);
7811 data.res = isl_union_pw_aff_empty(space);
7812 data.pos = pos;
7813 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7814 &get_union_pw_aff, &data) < 0)
7815 data.res = isl_union_pw_aff_free(data.res);
7817 return data.res;
7820 /* Return a union piecewise affine expression
7821 * that is equal to "aff" on "domain".
7823 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7824 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7826 isl_pw_aff *pa;
7828 pa = isl_pw_aff_from_aff(aff);
7829 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7832 /* Return a union piecewise affine expression
7833 * that is equal to the parameter identified by "id" on "domain".
7835 * Make sure the parameter appears in the space passed to
7836 * isl_aff_param_on_domain_space_id.
7838 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7839 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7841 isl_space *space;
7842 isl_aff *aff;
7844 space = isl_union_set_get_space(domain);
7845 space = isl_space_add_param_id(space, isl_id_copy(id));
7846 aff = isl_aff_param_on_domain_space_id(space, id);
7847 return isl_union_pw_aff_aff_on_domain(domain, aff);
7850 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7851 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7852 * needs to attain.
7853 * "res" collects the results.
7855 struct isl_union_pw_aff_pw_aff_on_domain_data {
7856 isl_pw_aff *pa;
7857 isl_union_pw_aff *res;
7860 /* Construct a piecewise affine expression that is equal to data->pa
7861 * on "domain" and add the result to data->res.
7863 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7865 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7866 isl_pw_aff *pa;
7867 int dim;
7869 pa = isl_pw_aff_copy(data->pa);
7870 dim = isl_set_dim(domain, isl_dim_set);
7871 pa = isl_pw_aff_from_range(pa);
7872 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7873 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7874 pa = isl_pw_aff_intersect_domain(pa, domain);
7875 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7877 return data->res ? isl_stat_ok : isl_stat_error;
7880 /* Return a union piecewise affine expression
7881 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7882 * have been aligned.
7884 * Construct an isl_pw_aff on each of the sets in "domain" and
7885 * collect the results.
7887 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7888 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7890 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7891 isl_space *space;
7893 space = isl_union_set_get_space(domain);
7894 data.res = isl_union_pw_aff_empty(space);
7895 data.pa = pa;
7896 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7897 data.res = isl_union_pw_aff_free(data.res);
7898 isl_union_set_free(domain);
7899 isl_pw_aff_free(pa);
7900 return data.res;
7903 /* Return a union piecewise affine expression
7904 * that is equal to "pa" on "domain".
7906 * Check that "pa" is a parametric expression,
7907 * align the parameters if needed and call
7908 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7910 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7911 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7913 isl_bool is_set;
7914 isl_bool equal_params;
7915 isl_space *domain_space, *pa_space;
7917 pa_space = isl_pw_aff_peek_space(pa);
7918 is_set = isl_space_is_set(pa_space);
7919 if (is_set < 0)
7920 goto error;
7921 if (!is_set)
7922 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7923 "expecting parametric expression", goto error);
7925 domain_space = isl_union_set_get_space(domain);
7926 pa_space = isl_pw_aff_get_space(pa);
7927 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7928 if (equal_params >= 0 && !equal_params) {
7929 isl_space *space;
7931 space = isl_space_align_params(domain_space, pa_space);
7932 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7933 domain = isl_union_set_align_params(domain, space);
7934 } else {
7935 isl_space_free(domain_space);
7936 isl_space_free(pa_space);
7939 if (equal_params < 0)
7940 goto error;
7941 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7942 error:
7943 isl_union_set_free(domain);
7944 isl_pw_aff_free(pa);
7945 return NULL;
7948 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7949 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7950 * "res" collects the results.
7952 struct isl_union_pw_aff_val_on_domain_data {
7953 isl_val *v;
7954 isl_union_pw_aff *res;
7957 /* Construct a piecewise affine expression that is equal to data->v
7958 * on "domain" and add the result to data->res.
7960 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7962 struct isl_union_pw_aff_val_on_domain_data *data = user;
7963 isl_pw_aff *pa;
7964 isl_val *v;
7966 v = isl_val_copy(data->v);
7967 pa = isl_pw_aff_val_on_domain(domain, v);
7968 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7970 return data->res ? isl_stat_ok : isl_stat_error;
7973 /* Return a union piecewise affine expression
7974 * that is equal to "v" on "domain".
7976 * Construct an isl_pw_aff on each of the sets in "domain" and
7977 * collect the results.
7979 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7980 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7982 struct isl_union_pw_aff_val_on_domain_data data;
7983 isl_space *space;
7985 space = isl_union_set_get_space(domain);
7986 data.res = isl_union_pw_aff_empty(space);
7987 data.v = v;
7988 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7989 data.res = isl_union_pw_aff_free(data.res);
7990 isl_union_set_free(domain);
7991 isl_val_free(v);
7992 return data.res;
7995 /* Construct a piecewise multi affine expression
7996 * that is equal to "pa" and add it to upma.
7998 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7999 void *user)
8001 isl_union_pw_multi_aff **upma = user;
8002 isl_pw_multi_aff *pma;
8004 pma = isl_pw_multi_aff_from_pw_aff(pa);
8005 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8007 return *upma ? isl_stat_ok : isl_stat_error;
8010 /* Construct and return a union piecewise multi affine expression
8011 * that is equal to the given union piecewise affine expression.
8013 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8014 __isl_take isl_union_pw_aff *upa)
8016 isl_space *space;
8017 isl_union_pw_multi_aff *upma;
8019 if (!upa)
8020 return NULL;
8022 space = isl_union_pw_aff_get_space(upa);
8023 upma = isl_union_pw_multi_aff_empty(space);
8025 if (isl_union_pw_aff_foreach_pw_aff(upa,
8026 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8027 upma = isl_union_pw_multi_aff_free(upma);
8029 isl_union_pw_aff_free(upa);
8030 return upma;
8033 /* Compute the set of elements in the domain of "pa" where it is zero and
8034 * add this set to "uset".
8036 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8038 isl_union_set **uset = (isl_union_set **)user;
8040 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8042 return *uset ? isl_stat_ok : isl_stat_error;
8045 /* Return a union set containing those elements in the domain
8046 * of "upa" where it is zero.
8048 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8049 __isl_take isl_union_pw_aff *upa)
8051 isl_union_set *zero;
8053 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8054 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8055 zero = isl_union_set_free(zero);
8057 isl_union_pw_aff_free(upa);
8058 return zero;
8061 /* Convert "pa" to an isl_map and add it to *umap.
8063 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8065 isl_union_map **umap = user;
8066 isl_map *map;
8068 map = isl_map_from_pw_aff(pa);
8069 *umap = isl_union_map_add_map(*umap, map);
8071 return *umap ? isl_stat_ok : isl_stat_error;
8074 /* Construct a union map mapping the domain of the union
8075 * piecewise affine expression to its range, with the single output dimension
8076 * equated to the corresponding affine expressions on their cells.
8078 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8079 __isl_take isl_union_pw_aff *upa)
8081 isl_space *space;
8082 isl_union_map *umap;
8084 if (!upa)
8085 return NULL;
8087 space = isl_union_pw_aff_get_space(upa);
8088 umap = isl_union_map_empty(space);
8090 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8091 &umap) < 0)
8092 umap = isl_union_map_free(umap);
8094 isl_union_pw_aff_free(upa);
8095 return umap;
8098 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8099 * upma is the function that is plugged in.
8100 * pa is the current part of the function in which upma is plugged in.
8101 * res collects the results.
8103 struct isl_union_pw_aff_pullback_upma_data {
8104 isl_union_pw_multi_aff *upma;
8105 isl_pw_aff *pa;
8106 isl_union_pw_aff *res;
8109 /* Check if "pma" can be plugged into data->pa.
8110 * If so, perform the pullback and add the result to data->res.
8112 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8114 struct isl_union_pw_aff_pullback_upma_data *data = user;
8115 isl_pw_aff *pa;
8117 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8118 pma->dim, isl_dim_out)) {
8119 isl_pw_multi_aff_free(pma);
8120 return isl_stat_ok;
8123 pa = isl_pw_aff_copy(data->pa);
8124 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8126 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8128 return data->res ? isl_stat_ok : isl_stat_error;
8131 /* Check if any of the elements of data->upma can be plugged into pa,
8132 * add if so add the result to data->res.
8134 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8136 struct isl_union_pw_aff_pullback_upma_data *data = user;
8137 isl_stat r;
8139 data->pa = pa;
8140 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8141 &pa_pb_pma, data);
8142 isl_pw_aff_free(pa);
8144 return r;
8147 /* Compute the pullback of "upa" by the function represented by "upma".
8148 * In other words, plug in "upma" in "upa". The result contains
8149 * expressions defined over the domain space of "upma".
8151 * Run over all pairs of elements in "upa" and "upma", perform
8152 * the pullback when appropriate and collect the results.
8153 * If the hash value were based on the domain space rather than
8154 * the function space, then we could run through all elements
8155 * of "upma" and directly pick out the corresponding element of "upa".
8157 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8158 __isl_take isl_union_pw_aff *upa,
8159 __isl_take isl_union_pw_multi_aff *upma)
8161 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8162 isl_space *space;
8164 space = isl_union_pw_multi_aff_get_space(upma);
8165 upa = isl_union_pw_aff_align_params(upa, space);
8166 space = isl_union_pw_aff_get_space(upa);
8167 upma = isl_union_pw_multi_aff_align_params(upma, space);
8169 if (!upa || !upma)
8170 goto error;
8172 data.upma = upma;
8173 data.res = isl_union_pw_aff_alloc_same_size(upa);
8174 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8175 data.res = isl_union_pw_aff_free(data.res);
8177 isl_union_pw_aff_free(upa);
8178 isl_union_pw_multi_aff_free(upma);
8179 return data.res;
8180 error:
8181 isl_union_pw_aff_free(upa);
8182 isl_union_pw_multi_aff_free(upma);
8183 return NULL;
8186 #undef BASE
8187 #define BASE union_pw_aff
8188 #undef DOMBASE
8189 #define DOMBASE union_set
8191 #define NO_MOVE_DIMS
8192 #define NO_DOMAIN
8193 #define NO_PRODUCT
8194 #define NO_SPLICE
8195 #define NO_ZERO
8196 #define NO_IDENTITY
8197 #define NO_GIST
8199 #include <isl_multi_explicit_domain.c>
8200 #include <isl_multi_union_pw_aff_explicit_domain.c>
8201 #include <isl_multi_templ.c>
8202 #include <isl_multi_apply_set.c>
8203 #include <isl_multi_apply_union_set.c>
8204 #include <isl_multi_coalesce.c>
8205 #include <isl_multi_floor.c>
8206 #include <isl_multi_gist.c>
8207 #include <isl_multi_align_set.c>
8208 #include <isl_multi_align_union_set.c>
8209 #include <isl_multi_intersect.c>
8211 /* Does "mupa" have a non-trivial explicit domain?
8213 * The explicit domain, if present, is trivial if it represents
8214 * an (obviously) universe parameter set.
8216 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8217 __isl_keep isl_multi_union_pw_aff *mupa)
8219 isl_bool is_params, trivial;
8220 isl_set *set;
8222 if (!mupa)
8223 return isl_bool_error;
8224 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8225 return isl_bool_false;
8226 is_params = isl_union_set_is_params(mupa->u.dom);
8227 if (is_params < 0 || !is_params)
8228 return isl_bool_not(is_params);
8229 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8230 trivial = isl_set_plain_is_universe(set);
8231 isl_set_free(set);
8232 return isl_bool_not(trivial);
8235 /* Construct a multiple union piecewise affine expression
8236 * in the given space with value zero in each of the output dimensions.
8238 * Since there is no canonical zero value for
8239 * a union piecewise affine expression, we can only construct
8240 * a zero-dimensional "zero" value.
8242 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8243 __isl_take isl_space *space)
8245 isl_bool params;
8247 if (!space)
8248 return NULL;
8250 params = isl_space_is_params(space);
8251 if (params < 0)
8252 goto error;
8253 if (params)
8254 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8255 "expecting proper set space", goto error);
8256 if (!isl_space_is_set(space))
8257 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8258 "expecting set space", goto error);
8259 if (isl_space_dim(space , isl_dim_out) != 0)
8260 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8261 "expecting 0D space", goto error);
8263 return isl_multi_union_pw_aff_alloc(space);
8264 error:
8265 isl_space_free(space);
8266 return NULL;
8269 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8270 * with the actual sum on the shared domain and
8271 * the defined expression on the symmetric difference of the domains.
8273 * We simply iterate over the elements in both arguments and
8274 * call isl_union_pw_aff_union_add on each of them, if there is
8275 * at least one element.
8277 * Otherwise, the two expressions have an explicit domain and
8278 * the union of these explicit domains is computed.
8279 * This assumes that the explicit domains are either both in terms
8280 * of specific domains elements or both in terms of parameters.
8281 * However, if one of the expressions does not have any constraints
8282 * on its explicit domain, then this is allowed as well and the result
8283 * is the expression with no constraints on its explicit domain.
8285 static __isl_give isl_multi_union_pw_aff *
8286 isl_multi_union_pw_aff_union_add_aligned(
8287 __isl_take isl_multi_union_pw_aff *mupa1,
8288 __isl_take isl_multi_union_pw_aff *mupa2)
8290 isl_bool has_domain, is_params1, is_params2;
8292 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8293 goto error;
8294 if (mupa1->n > 0)
8295 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8296 &isl_union_pw_aff_union_add);
8297 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8298 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8299 goto error;
8301 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8302 if (has_domain < 0)
8303 goto error;
8304 if (!has_domain) {
8305 isl_multi_union_pw_aff_free(mupa2);
8306 return mupa1;
8308 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8309 if (has_domain < 0)
8310 goto error;
8311 if (!has_domain) {
8312 isl_multi_union_pw_aff_free(mupa1);
8313 return mupa2;
8316 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8317 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8318 if (is_params1 < 0 || is_params2 < 0)
8319 goto error;
8320 if (is_params1 != is_params2)
8321 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8322 isl_error_invalid,
8323 "cannot compute union of concrete domain and "
8324 "parameter constraints", goto error);
8325 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8326 if (!mupa1)
8327 goto error;
8328 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8329 isl_union_set_copy(mupa2->u.dom));
8330 if (!mupa1->u.dom)
8331 goto error;
8332 isl_multi_union_pw_aff_free(mupa2);
8333 return mupa1;
8334 error:
8335 isl_multi_union_pw_aff_free(mupa1);
8336 isl_multi_union_pw_aff_free(mupa2);
8337 return NULL;
8340 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8341 * with the actual sum on the shared domain and
8342 * the defined expression on the symmetric difference of the domains.
8344 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8345 __isl_take isl_multi_union_pw_aff *mupa1,
8346 __isl_take isl_multi_union_pw_aff *mupa2)
8348 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8349 &isl_multi_union_pw_aff_union_add_aligned);
8352 /* Construct and return a multi union piecewise affine expression
8353 * that is equal to the given multi affine expression.
8355 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8356 __isl_take isl_multi_aff *ma)
8358 isl_multi_pw_aff *mpa;
8360 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8361 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8364 /* Construct and return a multi union piecewise affine expression
8365 * that is equal to the given multi piecewise affine expression.
8367 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8368 __isl_take isl_multi_pw_aff *mpa)
8370 int i, n;
8371 isl_space *space;
8372 isl_multi_union_pw_aff *mupa;
8374 if (!mpa)
8375 return NULL;
8377 space = isl_multi_pw_aff_get_space(mpa);
8378 space = isl_space_range(space);
8379 mupa = isl_multi_union_pw_aff_alloc(space);
8381 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8382 for (i = 0; i < n; ++i) {
8383 isl_pw_aff *pa;
8384 isl_union_pw_aff *upa;
8386 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8387 upa = isl_union_pw_aff_from_pw_aff(pa);
8388 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8391 isl_multi_pw_aff_free(mpa);
8393 return mupa;
8396 /* Extract the range space of "pma" and assign it to *space.
8397 * If *space has already been set (through a previous call to this function),
8398 * then check that the range space is the same.
8400 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8402 isl_space **space = user;
8403 isl_space *pma_space;
8404 isl_bool equal;
8406 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8407 isl_pw_multi_aff_free(pma);
8409 if (!pma_space)
8410 return isl_stat_error;
8411 if (!*space) {
8412 *space = pma_space;
8413 return isl_stat_ok;
8416 equal = isl_space_is_equal(pma_space, *space);
8417 isl_space_free(pma_space);
8419 if (equal < 0)
8420 return isl_stat_error;
8421 if (!equal)
8422 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8423 "range spaces not the same", return isl_stat_error);
8424 return isl_stat_ok;
8427 /* Construct and return a multi union piecewise affine expression
8428 * that is equal to the given union piecewise multi affine expression.
8430 * In order to be able to perform the conversion, the input
8431 * needs to be non-empty and may only involve a single range space.
8433 * If the resulting multi union piecewise affine expression has
8434 * an explicit domain, then assign it the domain of the input.
8435 * In other cases, the domain is stored in the individual elements.
8437 __isl_give isl_multi_union_pw_aff *
8438 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8439 __isl_take isl_union_pw_multi_aff *upma)
8441 isl_space *space = NULL;
8442 isl_multi_union_pw_aff *mupa;
8443 int i, n;
8445 if (!upma)
8446 return NULL;
8447 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8448 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8449 "cannot extract range space from empty input",
8450 goto error);
8451 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8452 &space) < 0)
8453 goto error;
8455 if (!space)
8456 goto error;
8458 n = isl_space_dim(space, isl_dim_set);
8459 mupa = isl_multi_union_pw_aff_alloc(space);
8461 for (i = 0; i < n; ++i) {
8462 isl_union_pw_aff *upa;
8464 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8465 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8467 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8468 isl_union_set *dom;
8469 isl_union_pw_multi_aff *copy;
8471 copy = isl_union_pw_multi_aff_copy(upma);
8472 dom = isl_union_pw_multi_aff_domain(copy);
8473 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8476 isl_union_pw_multi_aff_free(upma);
8477 return mupa;
8478 error:
8479 isl_space_free(space);
8480 isl_union_pw_multi_aff_free(upma);
8481 return NULL;
8484 /* Try and create an isl_multi_union_pw_aff that is equivalent
8485 * to the given isl_union_map.
8486 * The isl_union_map is required to be single-valued in each space.
8487 * Moreover, it cannot be empty and all range spaces need to be the same.
8488 * Otherwise, an error is produced.
8490 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8491 __isl_take isl_union_map *umap)
8493 isl_union_pw_multi_aff *upma;
8495 upma = isl_union_pw_multi_aff_from_union_map(umap);
8496 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8499 /* Return a multiple union piecewise affine expression
8500 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8501 * have been aligned.
8503 static __isl_give isl_multi_union_pw_aff *
8504 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8505 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8507 int i, n;
8508 isl_space *space;
8509 isl_multi_union_pw_aff *mupa;
8511 if (!domain || !mv)
8512 goto error;
8514 n = isl_multi_val_dim(mv, isl_dim_set);
8515 space = isl_multi_val_get_space(mv);
8516 mupa = isl_multi_union_pw_aff_alloc(space);
8517 for (i = 0; i < n; ++i) {
8518 isl_val *v;
8519 isl_union_pw_aff *upa;
8521 v = isl_multi_val_get_val(mv, i);
8522 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8524 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8527 isl_union_set_free(domain);
8528 isl_multi_val_free(mv);
8529 return mupa;
8530 error:
8531 isl_union_set_free(domain);
8532 isl_multi_val_free(mv);
8533 return NULL;
8536 /* Return a multiple union piecewise affine expression
8537 * that is equal to "mv" on "domain".
8539 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8540 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8542 isl_bool equal_params;
8544 if (!domain || !mv)
8545 goto error;
8546 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8547 if (equal_params < 0)
8548 goto error;
8549 if (equal_params)
8550 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8551 domain, mv);
8552 domain = isl_union_set_align_params(domain,
8553 isl_multi_val_get_space(mv));
8554 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8555 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8556 error:
8557 isl_union_set_free(domain);
8558 isl_multi_val_free(mv);
8559 return NULL;
8562 /* Return a multiple union piecewise affine expression
8563 * that is equal to "ma" on "domain".
8565 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8566 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8568 isl_pw_multi_aff *pma;
8570 pma = isl_pw_multi_aff_from_multi_aff(ma);
8571 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8574 /* Return a multiple union piecewise affine expression
8575 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8576 * have been aligned.
8578 * If the resulting multi union piecewise affine expression has
8579 * an explicit domain, then assign it the input domain.
8580 * In other cases, the domain is stored in the individual elements.
8582 static __isl_give isl_multi_union_pw_aff *
8583 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8584 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8586 int i, n;
8587 isl_space *space;
8588 isl_multi_union_pw_aff *mupa;
8590 if (!domain || !pma)
8591 goto error;
8593 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8594 space = isl_pw_multi_aff_get_space(pma);
8595 mupa = isl_multi_union_pw_aff_alloc(space);
8596 for (i = 0; i < n; ++i) {
8597 isl_pw_aff *pa;
8598 isl_union_pw_aff *upa;
8600 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8601 upa = isl_union_pw_aff_pw_aff_on_domain(
8602 isl_union_set_copy(domain), pa);
8603 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8605 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8606 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8607 isl_union_set_copy(domain));
8609 isl_union_set_free(domain);
8610 isl_pw_multi_aff_free(pma);
8611 return mupa;
8612 error:
8613 isl_union_set_free(domain);
8614 isl_pw_multi_aff_free(pma);
8615 return NULL;
8618 /* Return a multiple union piecewise affine expression
8619 * that is equal to "pma" on "domain".
8621 __isl_give isl_multi_union_pw_aff *
8622 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8623 __isl_take isl_pw_multi_aff *pma)
8625 isl_bool equal_params;
8626 isl_space *space;
8628 space = isl_pw_multi_aff_peek_space(pma);
8629 equal_params = isl_union_set_space_has_equal_params(domain, space);
8630 if (equal_params < 0)
8631 goto error;
8632 if (equal_params)
8633 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8634 domain, pma);
8635 domain = isl_union_set_align_params(domain,
8636 isl_pw_multi_aff_get_space(pma));
8637 pma = isl_pw_multi_aff_align_params(pma,
8638 isl_union_set_get_space(domain));
8639 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8640 pma);
8641 error:
8642 isl_union_set_free(domain);
8643 isl_pw_multi_aff_free(pma);
8644 return NULL;
8647 /* Return a union set containing those elements in the domains
8648 * of the elements of "mupa" where they are all zero.
8650 * If there are no elements, then simply return the entire domain.
8652 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8653 __isl_take isl_multi_union_pw_aff *mupa)
8655 int i, n;
8656 isl_union_pw_aff *upa;
8657 isl_union_set *zero;
8659 if (!mupa)
8660 return NULL;
8662 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8663 if (n == 0)
8664 return isl_multi_union_pw_aff_domain(mupa);
8666 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8667 zero = isl_union_pw_aff_zero_union_set(upa);
8669 for (i = 1; i < n; ++i) {
8670 isl_union_set *zero_i;
8672 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8673 zero_i = isl_union_pw_aff_zero_union_set(upa);
8675 zero = isl_union_set_intersect(zero, zero_i);
8678 isl_multi_union_pw_aff_free(mupa);
8679 return zero;
8682 /* Construct a union map mapping the shared domain
8683 * of the union piecewise affine expressions to the range of "mupa"
8684 * in the special case of a 0D multi union piecewise affine expression.
8686 * Construct a map between the explicit domain of "mupa" and
8687 * the range space.
8688 * Note that this assumes that the domain consists of explicit elements.
8690 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8691 __isl_take isl_multi_union_pw_aff *mupa)
8693 isl_bool is_params;
8694 isl_space *space;
8695 isl_union_set *dom, *ran;
8697 space = isl_multi_union_pw_aff_get_space(mupa);
8698 dom = isl_multi_union_pw_aff_domain(mupa);
8699 ran = isl_union_set_from_set(isl_set_universe(space));
8701 is_params = isl_union_set_is_params(dom);
8702 if (is_params < 0)
8703 dom = isl_union_set_free(dom);
8704 else if (is_params)
8705 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8706 "cannot create union map from expression without "
8707 "explicit domain elements",
8708 dom = isl_union_set_free(dom));
8710 return isl_union_map_from_domain_and_range(dom, ran);
8713 /* Construct a union map mapping the shared domain
8714 * of the union piecewise affine expressions to the range of "mupa"
8715 * with each dimension in the range equated to the
8716 * corresponding union piecewise affine expression.
8718 * If the input is zero-dimensional, then construct a mapping
8719 * from its explicit domain.
8721 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8722 __isl_take isl_multi_union_pw_aff *mupa)
8724 int i, n;
8725 isl_space *space;
8726 isl_union_map *umap;
8727 isl_union_pw_aff *upa;
8729 if (!mupa)
8730 return NULL;
8732 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8733 if (n == 0)
8734 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8736 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8737 umap = isl_union_map_from_union_pw_aff(upa);
8739 for (i = 1; i < n; ++i) {
8740 isl_union_map *umap_i;
8742 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8743 umap_i = isl_union_map_from_union_pw_aff(upa);
8744 umap = isl_union_map_flat_range_product(umap, umap_i);
8747 space = isl_multi_union_pw_aff_get_space(mupa);
8748 umap = isl_union_map_reset_range_space(umap, space);
8750 isl_multi_union_pw_aff_free(mupa);
8751 return umap;
8754 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8755 * "range" is the space from which to set the range space.
8756 * "res" collects the results.
8758 struct isl_union_pw_multi_aff_reset_range_space_data {
8759 isl_space *range;
8760 isl_union_pw_multi_aff *res;
8763 /* Replace the range space of "pma" by the range space of data->range and
8764 * add the result to data->res.
8766 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8768 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8769 isl_space *space;
8771 space = isl_pw_multi_aff_get_space(pma);
8772 space = isl_space_domain(space);
8773 space = isl_space_extend_domain_with_range(space,
8774 isl_space_copy(data->range));
8775 pma = isl_pw_multi_aff_reset_space(pma, space);
8776 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8778 return data->res ? isl_stat_ok : isl_stat_error;
8781 /* Replace the range space of all the piecewise affine expressions in "upma" by
8782 * the range space of "space".
8784 * This assumes that all these expressions have the same output dimension.
8786 * Since the spaces of the expressions change, so do their hash values.
8787 * We therefore need to create a new isl_union_pw_multi_aff.
8788 * Note that the hash value is currently computed based on the entire
8789 * space even though there can only be a single expression with a given
8790 * domain space.
8792 static __isl_give isl_union_pw_multi_aff *
8793 isl_union_pw_multi_aff_reset_range_space(
8794 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8796 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8797 isl_space *space_upma;
8799 space_upma = isl_union_pw_multi_aff_get_space(upma);
8800 data.res = isl_union_pw_multi_aff_empty(space_upma);
8801 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8802 &reset_range_space, &data) < 0)
8803 data.res = isl_union_pw_multi_aff_free(data.res);
8805 isl_space_free(space);
8806 isl_union_pw_multi_aff_free(upma);
8807 return data.res;
8810 /* Construct and return a union piecewise multi affine expression
8811 * that is equal to the given multi union piecewise affine expression,
8812 * in the special case of a 0D multi union piecewise affine expression.
8814 * Construct a union piecewise multi affine expression
8815 * on top of the explicit domain of the input.
8817 __isl_give isl_union_pw_multi_aff *
8818 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8819 __isl_take isl_multi_union_pw_aff *mupa)
8821 isl_space *space;
8822 isl_multi_val *mv;
8823 isl_union_set *domain;
8825 space = isl_multi_union_pw_aff_get_space(mupa);
8826 mv = isl_multi_val_zero(space);
8827 domain = isl_multi_union_pw_aff_domain(mupa);
8828 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8831 /* Construct and return a union piecewise multi affine expression
8832 * that is equal to the given multi union piecewise affine expression.
8834 * If the input is zero-dimensional, then
8835 * construct a union piecewise multi affine expression
8836 * on top of the explicit domain of the input.
8838 __isl_give isl_union_pw_multi_aff *
8839 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8840 __isl_take isl_multi_union_pw_aff *mupa)
8842 int i, n;
8843 isl_space *space;
8844 isl_union_pw_multi_aff *upma;
8845 isl_union_pw_aff *upa;
8847 if (!mupa)
8848 return NULL;
8850 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8851 if (n == 0)
8852 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8854 space = isl_multi_union_pw_aff_get_space(mupa);
8855 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8856 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8858 for (i = 1; i < n; ++i) {
8859 isl_union_pw_multi_aff *upma_i;
8861 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8862 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8863 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8866 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8868 isl_multi_union_pw_aff_free(mupa);
8869 return upma;
8872 /* Intersect the range of "mupa" with "range",
8873 * in the special case where "mupa" is 0D.
8875 * Intersect the domain of "mupa" with the constraints on the parameters
8876 * of "range".
8878 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8879 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8881 range = isl_set_params(range);
8882 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8883 return mupa;
8886 /* Intersect the range of "mupa" with "range".
8887 * That is, keep only those domain elements that have a function value
8888 * in "range".
8890 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8891 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8893 isl_union_pw_multi_aff *upma;
8894 isl_union_set *domain;
8895 isl_space *space;
8896 int n;
8897 int match;
8899 if (!mupa || !range)
8900 goto error;
8902 space = isl_set_get_space(range);
8903 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8904 space, isl_dim_set);
8905 isl_space_free(space);
8906 if (match < 0)
8907 goto error;
8908 if (!match)
8909 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8910 "space don't match", goto error);
8911 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8912 if (n == 0)
8913 return mupa_intersect_range_0D(mupa, range);
8915 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8916 isl_multi_union_pw_aff_copy(mupa));
8917 domain = isl_union_set_from_set(range);
8918 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8919 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8921 return mupa;
8922 error:
8923 isl_multi_union_pw_aff_free(mupa);
8924 isl_set_free(range);
8925 return NULL;
8928 /* Return the shared domain of the elements of "mupa",
8929 * in the special case where "mupa" is zero-dimensional.
8931 * Return the explicit domain of "mupa".
8932 * Note that this domain may be a parameter set, either
8933 * because "mupa" is meant to live in a set space or
8934 * because no explicit domain has been set.
8936 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8937 __isl_take isl_multi_union_pw_aff *mupa)
8939 isl_union_set *dom;
8941 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8942 isl_multi_union_pw_aff_free(mupa);
8944 return dom;
8947 /* Return the shared domain of the elements of "mupa".
8949 * If "mupa" is zero-dimensional, then return its explicit domain.
8951 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8952 __isl_take isl_multi_union_pw_aff *mupa)
8954 int i, n;
8955 isl_union_pw_aff *upa;
8956 isl_union_set *dom;
8958 if (!mupa)
8959 return NULL;
8961 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8962 if (n == 0)
8963 return isl_multi_union_pw_aff_domain_0D(mupa);
8965 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8966 dom = isl_union_pw_aff_domain(upa);
8967 for (i = 1; i < n; ++i) {
8968 isl_union_set *dom_i;
8970 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8971 dom_i = isl_union_pw_aff_domain(upa);
8972 dom = isl_union_set_intersect(dom, dom_i);
8975 isl_multi_union_pw_aff_free(mupa);
8976 return dom;
8979 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8980 * In particular, the spaces have been aligned.
8981 * The result is defined over the shared domain of the elements of "mupa"
8983 * We first extract the parametric constant part of "aff" and
8984 * define that over the shared domain.
8985 * Then we iterate over all input dimensions of "aff" and add the corresponding
8986 * multiples of the elements of "mupa".
8987 * Finally, we consider the integer divisions, calling the function
8988 * recursively to obtain an isl_union_pw_aff corresponding to the
8989 * integer division argument.
8991 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8992 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8994 int i, n_in, n_div;
8995 isl_union_pw_aff *upa;
8996 isl_union_set *uset;
8997 isl_val *v;
8998 isl_aff *cst;
9000 n_in = isl_aff_dim(aff, isl_dim_in);
9001 n_div = isl_aff_dim(aff, isl_dim_div);
9003 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9004 cst = isl_aff_copy(aff);
9005 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9006 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9007 cst = isl_aff_project_domain_on_params(cst);
9008 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9010 for (i = 0; i < n_in; ++i) {
9011 isl_union_pw_aff *upa_i;
9013 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9014 continue;
9015 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9016 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9017 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9018 upa = isl_union_pw_aff_add(upa, upa_i);
9021 for (i = 0; i < n_div; ++i) {
9022 isl_aff *div;
9023 isl_union_pw_aff *upa_i;
9025 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9026 continue;
9027 div = isl_aff_get_div(aff, i);
9028 upa_i = multi_union_pw_aff_apply_aff(
9029 isl_multi_union_pw_aff_copy(mupa), div);
9030 upa_i = isl_union_pw_aff_floor(upa_i);
9031 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9032 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9033 upa = isl_union_pw_aff_add(upa, upa_i);
9036 isl_multi_union_pw_aff_free(mupa);
9037 isl_aff_free(aff);
9039 return upa;
9042 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9043 * with the domain of "aff".
9044 * Furthermore, the dimension of this space needs to be greater than zero.
9045 * The result is defined over the shared domain of the elements of "mupa"
9047 * We perform these checks and then hand over control to
9048 * multi_union_pw_aff_apply_aff.
9050 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9051 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9053 isl_space *space1, *space2;
9054 int equal;
9056 mupa = isl_multi_union_pw_aff_align_params(mupa,
9057 isl_aff_get_space(aff));
9058 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9059 if (!mupa || !aff)
9060 goto error;
9062 space1 = isl_multi_union_pw_aff_get_space(mupa);
9063 space2 = isl_aff_get_domain_space(aff);
9064 equal = isl_space_is_equal(space1, space2);
9065 isl_space_free(space1);
9066 isl_space_free(space2);
9067 if (equal < 0)
9068 goto error;
9069 if (!equal)
9070 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9071 "spaces don't match", goto error);
9072 if (isl_aff_dim(aff, isl_dim_in) == 0)
9073 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9074 "cannot determine domains", goto error);
9076 return multi_union_pw_aff_apply_aff(mupa, aff);
9077 error:
9078 isl_multi_union_pw_aff_free(mupa);
9079 isl_aff_free(aff);
9080 return NULL;
9083 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9084 * The space of "mupa" is known to be compatible with the domain of "ma".
9086 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9087 * on the domain of "mupa".
9089 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9090 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9092 isl_union_set *dom;
9094 dom = isl_multi_union_pw_aff_domain(mupa);
9095 ma = isl_multi_aff_project_domain_on_params(ma);
9097 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9100 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9101 * with the domain of "ma".
9102 * The result is defined over the shared domain of the elements of "mupa"
9104 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9105 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9107 isl_space *space1, *space2;
9108 isl_multi_union_pw_aff *res;
9109 int equal;
9110 int i, n_out;
9112 mupa = isl_multi_union_pw_aff_align_params(mupa,
9113 isl_multi_aff_get_space(ma));
9114 ma = isl_multi_aff_align_params(ma,
9115 isl_multi_union_pw_aff_get_space(mupa));
9116 if (!mupa || !ma)
9117 goto error;
9119 space1 = isl_multi_union_pw_aff_get_space(mupa);
9120 space2 = isl_multi_aff_get_domain_space(ma);
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_multi_aff_get_ctx(ma), isl_error_invalid,
9128 "spaces don't match", goto error);
9129 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9130 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9131 return mupa_apply_multi_aff_0D(mupa, ma);
9133 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9134 res = isl_multi_union_pw_aff_alloc(space1);
9136 for (i = 0; i < n_out; ++i) {
9137 isl_aff *aff;
9138 isl_union_pw_aff *upa;
9140 aff = isl_multi_aff_get_aff(ma, i);
9141 upa = multi_union_pw_aff_apply_aff(
9142 isl_multi_union_pw_aff_copy(mupa), aff);
9143 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9146 isl_multi_aff_free(ma);
9147 isl_multi_union_pw_aff_free(mupa);
9148 return res;
9149 error:
9150 isl_multi_union_pw_aff_free(mupa);
9151 isl_multi_aff_free(ma);
9152 return NULL;
9155 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9156 * The space of "mupa" is known to be compatible with the domain of "pa".
9158 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9159 * on the domain of "mupa".
9161 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9162 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9164 isl_union_set *dom;
9166 dom = isl_multi_union_pw_aff_domain(mupa);
9167 pa = isl_pw_aff_project_domain_on_params(pa);
9169 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9172 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9173 * with the domain of "pa".
9174 * Furthermore, the dimension of this space needs to be greater than zero.
9175 * The result is defined over the shared domain of the elements of "mupa"
9177 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9178 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9180 int i;
9181 int equal;
9182 isl_space *space, *space2;
9183 isl_union_pw_aff *upa;
9185 mupa = isl_multi_union_pw_aff_align_params(mupa,
9186 isl_pw_aff_get_space(pa));
9187 pa = isl_pw_aff_align_params(pa,
9188 isl_multi_union_pw_aff_get_space(mupa));
9189 if (!mupa || !pa)
9190 goto error;
9192 space = isl_multi_union_pw_aff_get_space(mupa);
9193 space2 = isl_pw_aff_get_domain_space(pa);
9194 equal = isl_space_is_equal(space, space2);
9195 isl_space_free(space);
9196 isl_space_free(space2);
9197 if (equal < 0)
9198 goto error;
9199 if (!equal)
9200 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9201 "spaces don't match", goto error);
9202 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9203 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9205 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9206 upa = isl_union_pw_aff_empty(space);
9208 for (i = 0; i < pa->n; ++i) {
9209 isl_aff *aff;
9210 isl_set *domain;
9211 isl_multi_union_pw_aff *mupa_i;
9212 isl_union_pw_aff *upa_i;
9214 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9215 domain = isl_set_copy(pa->p[i].set);
9216 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9217 aff = isl_aff_copy(pa->p[i].aff);
9218 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9219 upa = isl_union_pw_aff_union_add(upa, upa_i);
9222 isl_multi_union_pw_aff_free(mupa);
9223 isl_pw_aff_free(pa);
9224 return upa;
9225 error:
9226 isl_multi_union_pw_aff_free(mupa);
9227 isl_pw_aff_free(pa);
9228 return NULL;
9231 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9232 * The space of "mupa" is known to be compatible with the domain of "pma".
9234 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9235 * on the domain of "mupa".
9237 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9238 __isl_take isl_multi_union_pw_aff *mupa,
9239 __isl_take isl_pw_multi_aff *pma)
9241 isl_union_set *dom;
9243 dom = isl_multi_union_pw_aff_domain(mupa);
9244 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9246 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9249 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9250 * with the domain of "pma".
9251 * The result is defined over the shared domain of the elements of "mupa"
9253 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9254 __isl_take isl_multi_union_pw_aff *mupa,
9255 __isl_take isl_pw_multi_aff *pma)
9257 isl_space *space1, *space2;
9258 isl_multi_union_pw_aff *res;
9259 int equal;
9260 int i, n_out;
9262 mupa = isl_multi_union_pw_aff_align_params(mupa,
9263 isl_pw_multi_aff_get_space(pma));
9264 pma = isl_pw_multi_aff_align_params(pma,
9265 isl_multi_union_pw_aff_get_space(mupa));
9266 if (!mupa || !pma)
9267 goto error;
9269 space1 = isl_multi_union_pw_aff_get_space(mupa);
9270 space2 = isl_pw_multi_aff_get_domain_space(pma);
9271 equal = isl_space_is_equal(space1, space2);
9272 isl_space_free(space1);
9273 isl_space_free(space2);
9274 if (equal < 0)
9275 goto error;
9276 if (!equal)
9277 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9278 "spaces don't match", goto error);
9279 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9280 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9281 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9283 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9284 res = isl_multi_union_pw_aff_alloc(space1);
9286 for (i = 0; i < n_out; ++i) {
9287 isl_pw_aff *pa;
9288 isl_union_pw_aff *upa;
9290 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9291 upa = isl_multi_union_pw_aff_apply_pw_aff(
9292 isl_multi_union_pw_aff_copy(mupa), pa);
9293 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9296 isl_pw_multi_aff_free(pma);
9297 isl_multi_union_pw_aff_free(mupa);
9298 return res;
9299 error:
9300 isl_multi_union_pw_aff_free(mupa);
9301 isl_pw_multi_aff_free(pma);
9302 return NULL;
9305 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9306 * If the explicit domain only keeps track of constraints on the parameters,
9307 * then only update those constraints.
9309 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9310 __isl_take isl_multi_union_pw_aff *mupa,
9311 __isl_keep isl_union_pw_multi_aff *upma)
9313 isl_bool is_params;
9315 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9316 return isl_multi_union_pw_aff_free(mupa);
9318 mupa = isl_multi_union_pw_aff_cow(mupa);
9319 if (!mupa)
9320 return NULL;
9322 is_params = isl_union_set_is_params(mupa->u.dom);
9323 if (is_params < 0)
9324 return isl_multi_union_pw_aff_free(mupa);
9326 upma = isl_union_pw_multi_aff_copy(upma);
9327 if (is_params)
9328 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9329 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9330 else
9331 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9332 mupa->u.dom, upma);
9333 if (!mupa->u.dom)
9334 return isl_multi_union_pw_aff_free(mupa);
9335 return mupa;
9338 /* Compute the pullback of "mupa" by the function represented by "upma".
9339 * In other words, plug in "upma" in "mupa". The result contains
9340 * expressions defined over the domain space of "upma".
9342 * Run over all elements of "mupa" and plug in "upma" in each of them.
9344 * If "mupa" has an explicit domain, then it is this domain
9345 * that needs to undergo a pullback instead, i.e., a preimage.
9347 __isl_give isl_multi_union_pw_aff *
9348 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9349 __isl_take isl_multi_union_pw_aff *mupa,
9350 __isl_take isl_union_pw_multi_aff *upma)
9352 int i, n;
9354 mupa = isl_multi_union_pw_aff_align_params(mupa,
9355 isl_union_pw_multi_aff_get_space(upma));
9356 upma = isl_union_pw_multi_aff_align_params(upma,
9357 isl_multi_union_pw_aff_get_space(mupa));
9358 mupa = isl_multi_union_pw_aff_cow(mupa);
9359 if (!mupa || !upma)
9360 goto error;
9362 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9363 for (i = 0; i < n; ++i) {
9364 isl_union_pw_aff *upa;
9366 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9367 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9368 isl_union_pw_multi_aff_copy(upma));
9369 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9372 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9373 mupa = preimage_explicit_domain(mupa, upma);
9375 isl_union_pw_multi_aff_free(upma);
9376 return mupa;
9377 error:
9378 isl_multi_union_pw_aff_free(mupa);
9379 isl_union_pw_multi_aff_free(upma);
9380 return NULL;
9383 /* Extract the sequence of elements in "mupa" with domain space "space"
9384 * (ignoring parameters).
9386 * For the elements of "mupa" that are not defined on the specified space,
9387 * the corresponding element in the result is empty.
9389 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9390 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9392 int i, n;
9393 isl_space *space_mpa;
9394 isl_multi_pw_aff *mpa;
9396 if (!mupa || !space)
9397 goto error;
9399 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9400 space = isl_space_replace_params(space, space_mpa);
9401 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9402 space_mpa);
9403 mpa = isl_multi_pw_aff_alloc(space_mpa);
9405 space = isl_space_from_domain(space);
9406 space = isl_space_add_dims(space, isl_dim_out, 1);
9407 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9408 for (i = 0; i < n; ++i) {
9409 isl_union_pw_aff *upa;
9410 isl_pw_aff *pa;
9412 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9413 pa = isl_union_pw_aff_extract_pw_aff(upa,
9414 isl_space_copy(space));
9415 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9416 isl_union_pw_aff_free(upa);
9419 isl_space_free(space);
9420 return mpa;
9421 error:
9422 isl_space_free(space);
9423 return NULL;
9426 /* Evaluate the affine function "aff" in the void point "pnt".
9427 * In particular, return the value NaN.
9429 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9430 __isl_take isl_point *pnt)
9432 isl_ctx *ctx;
9434 ctx = isl_point_get_ctx(pnt);
9435 isl_aff_free(aff);
9436 isl_point_free(pnt);
9437 return isl_val_nan(ctx);
9440 /* Evaluate the affine expression "aff"
9441 * in the coordinates (with denominator) "pnt".
9443 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9444 __isl_keep isl_vec *pnt)
9446 isl_int n, d;
9447 isl_ctx *ctx;
9448 isl_val *v;
9450 if (!aff || !pnt)
9451 return NULL;
9453 ctx = isl_vec_get_ctx(aff);
9454 isl_int_init(n);
9455 isl_int_init(d);
9456 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9457 isl_int_mul(d, aff->el[0], pnt->el[0]);
9458 v = isl_val_rat_from_isl_int(ctx, n, d);
9459 v = isl_val_normalize(v);
9460 isl_int_clear(n);
9461 isl_int_clear(d);
9463 return v;
9466 /* Check that the domain space of "aff" is equal to "space".
9468 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9469 __isl_keep isl_space *space)
9471 isl_bool ok;
9473 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9474 if (ok < 0)
9475 return isl_stat_error;
9476 if (!ok)
9477 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9478 "incompatible spaces", return isl_stat_error);
9479 return isl_stat_ok;
9482 /* Evaluate the affine function "aff" in "pnt".
9484 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9485 __isl_take isl_point *pnt)
9487 isl_bool is_void;
9488 isl_val *v;
9489 isl_local_space *ls;
9491 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9492 goto error;
9493 is_void = isl_point_is_void(pnt);
9494 if (is_void < 0)
9495 goto error;
9496 if (is_void)
9497 return eval_void(aff, pnt);
9499 ls = isl_aff_get_domain_local_space(aff);
9500 pnt = isl_local_space_lift_point(ls, pnt);
9502 v = eval(aff->v, isl_point_peek_vec(pnt));
9504 isl_aff_free(aff);
9505 isl_point_free(pnt);
9507 return v;
9508 error:
9509 isl_aff_free(aff);
9510 isl_point_free(pnt);
9511 return NULL;