add some basic tests for binary operations on isl_aff objects
[isl.git] / isl_aff.c
bloba0abd093646c58753f2193dc84f67ce4932d7318
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #define ISL_DIM_H
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/constraint.h>
24 #include <isl_seq.h>
25 #include <isl/set.h>
26 #include <isl_val_private.h>
27 #include <isl/deprecated/aff_int.h>
28 #include <isl_config.h>
30 #undef BASE
31 #define BASE aff
33 #include <isl_list_templ.c>
35 #undef BASE
36 #define BASE pw_aff
38 #include <isl_list_templ.c>
40 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
41 __isl_take isl_vec *v)
43 isl_aff *aff;
45 if (!ls || !v)
46 goto error;
48 aff = isl_calloc_type(v->ctx, struct isl_aff);
49 if (!aff)
50 goto error;
52 aff->ref = 1;
53 aff->ls = ls;
54 aff->v = v;
56 return aff;
57 error:
58 isl_local_space_free(ls);
59 isl_vec_free(v);
60 return NULL;
63 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
65 isl_ctx *ctx;
66 isl_vec *v;
67 unsigned total;
69 if (!ls)
70 return NULL;
72 ctx = isl_local_space_get_ctx(ls);
73 if (!isl_local_space_divs_known(ls))
74 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
75 goto error);
76 if (!isl_local_space_is_set(ls))
77 isl_die(ctx, isl_error_invalid,
78 "domain of affine expression should be a set",
79 goto error);
81 total = isl_local_space_dim(ls, isl_dim_all);
82 v = isl_vec_alloc(ctx, 1 + 1 + total);
83 return isl_aff_alloc_vec(ls, v);
84 error:
85 isl_local_space_free(ls);
86 return NULL;
89 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
91 isl_aff *aff;
93 aff = isl_aff_alloc(ls);
94 if (!aff)
95 return NULL;
97 isl_int_set_si(aff->v->el[0], 1);
98 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
100 return aff;
103 /* Return a piecewise affine expression defined on the specified domain
104 * that is equal to zero.
106 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
108 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
111 /* Return an affine expression that is equal to "val" on
112 * domain local space "ls".
114 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
115 __isl_take isl_val *val)
117 isl_aff *aff;
119 if (!ls || !val)
120 goto error;
121 if (!isl_val_is_rat(val))
122 isl_die(isl_val_get_ctx(val), isl_error_invalid,
123 "expecting rational value", goto error);
125 aff = isl_aff_alloc(isl_local_space_copy(ls));
126 if (!aff)
127 goto error;
129 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
130 isl_int_set(aff->v->el[1], val->n);
131 isl_int_set(aff->v->el[0], val->d);
133 isl_local_space_free(ls);
134 isl_val_free(val);
135 return aff;
136 error:
137 isl_local_space_free(ls);
138 isl_val_free(val);
139 return NULL;
142 /* Return an affine expression that is equal to the specified dimension
143 * in "ls".
145 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
146 enum isl_dim_type type, unsigned pos)
148 isl_space *space;
149 isl_aff *aff;
151 if (!ls)
152 return NULL;
154 space = isl_local_space_get_space(ls);
155 if (!space)
156 goto error;
157 if (isl_space_is_map(space))
158 isl_die(isl_space_get_ctx(space), isl_error_invalid,
159 "expecting (parameter) set space", goto error);
160 if (pos >= isl_local_space_dim(ls, type))
161 isl_die(isl_space_get_ctx(space), isl_error_invalid,
162 "position out of bounds", goto error);
164 isl_space_free(space);
165 aff = isl_aff_alloc(ls);
166 if (!aff)
167 return NULL;
169 pos += isl_local_space_offset(aff->ls, type);
171 isl_int_set_si(aff->v->el[0], 1);
172 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
173 isl_int_set_si(aff->v->el[1 + pos], 1);
175 return aff;
176 error:
177 isl_local_space_free(ls);
178 isl_space_free(space);
179 return NULL;
182 /* Return a piecewise affine expression that is equal to
183 * the specified dimension in "ls".
185 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
186 enum isl_dim_type type, unsigned pos)
188 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
191 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
193 if (!aff)
194 return NULL;
196 aff->ref++;
197 return aff;
200 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
202 if (!aff)
203 return NULL;
205 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
206 isl_vec_copy(aff->v));
209 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
211 if (!aff)
212 return NULL;
214 if (aff->ref == 1)
215 return aff;
216 aff->ref--;
217 return isl_aff_dup(aff);
220 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
222 if (!aff)
223 return NULL;
225 if (--aff->ref > 0)
226 return NULL;
228 isl_local_space_free(aff->ls);
229 isl_vec_free(aff->v);
231 free(aff);
233 return NULL;
236 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
238 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
241 /* Externally, an isl_aff has a map space, but internally, the
242 * ls field corresponds to the domain of that space.
244 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
246 if (!aff)
247 return 0;
248 if (type == isl_dim_out)
249 return 1;
250 if (type == isl_dim_in)
251 type = isl_dim_set;
252 return isl_local_space_dim(aff->ls, type);
255 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
257 return aff ? isl_local_space_get_space(aff->ls) : NULL;
260 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
262 isl_space *space;
263 if (!aff)
264 return NULL;
265 space = isl_local_space_get_space(aff->ls);
266 space = isl_space_from_domain(space);
267 space = isl_space_add_dims(space, isl_dim_out, 1);
268 return space;
271 __isl_give isl_local_space *isl_aff_get_domain_local_space(
272 __isl_keep isl_aff *aff)
274 return aff ? isl_local_space_copy(aff->ls) : NULL;
277 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
279 isl_local_space *ls;
280 if (!aff)
281 return NULL;
282 ls = isl_local_space_copy(aff->ls);
283 ls = isl_local_space_from_domain(ls);
284 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
285 return ls;
288 /* Externally, an isl_aff has a map space, but internally, the
289 * ls field corresponds to the domain of that space.
291 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
292 enum isl_dim_type type, unsigned pos)
294 if (!aff)
295 return NULL;
296 if (type == isl_dim_out)
297 return NULL;
298 if (type == isl_dim_in)
299 type = isl_dim_set;
300 return isl_local_space_get_dim_name(aff->ls, type, pos);
303 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
304 __isl_take isl_space *dim)
306 aff = isl_aff_cow(aff);
307 if (!aff || !dim)
308 goto error;
310 aff->ls = isl_local_space_reset_space(aff->ls, dim);
311 if (!aff->ls)
312 return isl_aff_free(aff);
314 return aff;
315 error:
316 isl_aff_free(aff);
317 isl_space_free(dim);
318 return NULL;
321 /* Reset the space of "aff". This function is called from isl_pw_templ.c
322 * and doesn't know if the space of an element object is represented
323 * directly or through its domain. It therefore passes along both.
325 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
326 __isl_take isl_space *space, __isl_take isl_space *domain)
328 isl_space_free(space);
329 return isl_aff_reset_domain_space(aff, domain);
332 /* Reorder the coefficients of the affine expression based
333 * on the given reodering.
334 * The reordering r is assumed to have been extended with the local
335 * variables.
337 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
338 __isl_take isl_reordering *r, int n_div)
340 isl_vec *res;
341 int i;
343 if (!vec || !r)
344 goto error;
346 res = isl_vec_alloc(vec->ctx,
347 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
348 isl_seq_cpy(res->el, vec->el, 2);
349 isl_seq_clr(res->el + 2, res->size - 2);
350 for (i = 0; i < r->len; ++i)
351 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
353 isl_reordering_free(r);
354 isl_vec_free(vec);
355 return res;
356 error:
357 isl_vec_free(vec);
358 isl_reordering_free(r);
359 return NULL;
362 /* Reorder the dimensions of the domain of "aff" according
363 * to the given reordering.
365 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
366 __isl_take isl_reordering *r)
368 aff = isl_aff_cow(aff);
369 if (!aff)
370 goto error;
372 r = isl_reordering_extend(r, aff->ls->div->n_row);
373 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
374 aff->ls->div->n_row);
375 aff->ls = isl_local_space_realign(aff->ls, r);
377 if (!aff->v || !aff->ls)
378 return isl_aff_free(aff);
380 return aff;
381 error:
382 isl_aff_free(aff);
383 isl_reordering_free(r);
384 return NULL;
387 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
388 __isl_take isl_space *model)
390 if (!aff || !model)
391 goto error;
393 if (!isl_space_match(aff->ls->dim, isl_dim_param,
394 model, isl_dim_param)) {
395 isl_reordering *exp;
397 model = isl_space_drop_dims(model, isl_dim_in,
398 0, isl_space_dim(model, isl_dim_in));
399 model = isl_space_drop_dims(model, isl_dim_out,
400 0, isl_space_dim(model, isl_dim_out));
401 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
402 exp = isl_reordering_extend_space(exp,
403 isl_aff_get_domain_space(aff));
404 aff = isl_aff_realign_domain(aff, exp);
407 isl_space_free(model);
408 return aff;
409 error:
410 isl_space_free(model);
411 isl_aff_free(aff);
412 return NULL;
415 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
417 if (!aff)
418 return -1;
420 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
423 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
425 int equal;
427 if (!aff1 || !aff2)
428 return -1;
430 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
431 if (equal < 0 || !equal)
432 return equal;
434 return isl_vec_is_equal(aff1->v, aff2->v);
437 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
439 if (!aff)
440 return -1;
441 isl_int_set(*v, aff->v->el[0]);
442 return 0;
445 /* Return the common denominator of "aff".
447 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
449 isl_ctx *ctx;
451 if (!aff)
452 return NULL;
454 ctx = isl_aff_get_ctx(aff);
455 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
458 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
460 if (!aff)
461 return -1;
462 isl_int_set(*v, aff->v->el[1]);
463 return 0;
466 /* Return the constant term of "aff".
468 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
470 isl_ctx *ctx;
471 isl_val *v;
473 if (!aff)
474 return NULL;
476 ctx = isl_aff_get_ctx(aff);
477 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
478 return isl_val_normalize(v);
481 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
482 enum isl_dim_type type, int pos, isl_int *v)
484 if (!aff)
485 return -1;
487 if (type == isl_dim_out)
488 isl_die(aff->v->ctx, isl_error_invalid,
489 "output/set dimension does not have a coefficient",
490 return -1);
491 if (type == isl_dim_in)
492 type = isl_dim_set;
494 if (pos >= isl_local_space_dim(aff->ls, type))
495 isl_die(aff->v->ctx, isl_error_invalid,
496 "position out of bounds", return -1);
498 pos += isl_local_space_offset(aff->ls, type);
499 isl_int_set(*v, aff->v->el[1 + pos]);
501 return 0;
504 /* Return the coefficient of the variable of type "type" at position "pos"
505 * of "aff".
507 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
508 enum isl_dim_type type, int pos)
510 isl_ctx *ctx;
511 isl_val *v;
513 if (!aff)
514 return NULL;
516 ctx = isl_aff_get_ctx(aff);
517 if (type == isl_dim_out)
518 isl_die(ctx, isl_error_invalid,
519 "output/set dimension does not have a coefficient",
520 return NULL);
521 if (type == isl_dim_in)
522 type = isl_dim_set;
524 if (pos >= isl_local_space_dim(aff->ls, type))
525 isl_die(ctx, isl_error_invalid,
526 "position out of bounds", return NULL);
528 pos += isl_local_space_offset(aff->ls, type);
529 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
530 return isl_val_normalize(v);
533 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
535 aff = isl_aff_cow(aff);
536 if (!aff)
537 return NULL;
539 aff->v = isl_vec_cow(aff->v);
540 if (!aff->v)
541 return isl_aff_free(aff);
543 isl_int_set(aff->v->el[0], v);
545 return aff;
548 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
550 aff = isl_aff_cow(aff);
551 if (!aff)
552 return NULL;
554 aff->v = isl_vec_cow(aff->v);
555 if (!aff->v)
556 return isl_aff_free(aff);
558 isl_int_set(aff->v->el[1], v);
560 return aff;
563 /* Replace the constant term of "aff" by "v".
565 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
566 __isl_take isl_val *v)
568 if (!aff || !v)
569 goto error;
571 if (!isl_val_is_rat(v))
572 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
573 "expecting rational value", goto error);
575 if (isl_int_eq(aff->v->el[1], v->n) &&
576 isl_int_eq(aff->v->el[0], v->d)) {
577 isl_val_free(v);
578 return aff;
581 aff = isl_aff_cow(aff);
582 if (!aff)
583 goto error;
584 aff->v = isl_vec_cow(aff->v);
585 if (!aff->v)
586 goto error;
588 if (isl_int_eq(aff->v->el[0], v->d)) {
589 isl_int_set(aff->v->el[1], v->n);
590 } else if (isl_int_is_one(v->d)) {
591 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
592 } else {
593 isl_seq_scale(aff->v->el + 1,
594 aff->v->el + 1, v->d, aff->v->size - 1);
595 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
596 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
597 aff->v = isl_vec_normalize(aff->v);
598 if (!aff->v)
599 goto error;
602 isl_val_free(v);
603 return aff;
604 error:
605 isl_aff_free(aff);
606 isl_val_free(v);
607 return NULL;
610 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
612 if (isl_int_is_zero(v))
613 return aff;
615 aff = isl_aff_cow(aff);
616 if (!aff)
617 return NULL;
619 aff->v = isl_vec_cow(aff->v);
620 if (!aff->v)
621 return isl_aff_free(aff);
623 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
625 return aff;
628 /* Add "v" to the constant term of "aff".
630 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
631 __isl_take isl_val *v)
633 if (!aff || !v)
634 goto error;
636 if (isl_val_is_zero(v)) {
637 isl_val_free(v);
638 return aff;
641 if (!isl_val_is_rat(v))
642 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
643 "expecting rational value", goto error);
645 aff = isl_aff_cow(aff);
646 if (!aff)
647 goto error;
649 aff->v = isl_vec_cow(aff->v);
650 if (!aff->v)
651 goto error;
653 if (isl_int_is_one(v->d)) {
654 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
655 } else if (isl_int_eq(aff->v->el[0], v->d)) {
656 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
657 aff->v = isl_vec_normalize(aff->v);
658 if (!aff->v)
659 goto error;
660 } else {
661 isl_seq_scale(aff->v->el + 1,
662 aff->v->el + 1, v->d, aff->v->size - 1);
663 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
664 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
665 aff->v = isl_vec_normalize(aff->v);
666 if (!aff->v)
667 goto error;
670 isl_val_free(v);
671 return aff;
672 error:
673 isl_aff_free(aff);
674 isl_val_free(v);
675 return NULL;
678 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
680 isl_int t;
682 isl_int_init(t);
683 isl_int_set_si(t, v);
684 aff = isl_aff_add_constant(aff, t);
685 isl_int_clear(t);
687 return aff;
690 /* Add "v" to the numerator of the constant term of "aff".
692 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
694 if (isl_int_is_zero(v))
695 return aff;
697 aff = isl_aff_cow(aff);
698 if (!aff)
699 return NULL;
701 aff->v = isl_vec_cow(aff->v);
702 if (!aff->v)
703 return isl_aff_free(aff);
705 isl_int_add(aff->v->el[1], aff->v->el[1], v);
707 return aff;
710 /* Add "v" to the numerator of the constant term of "aff".
712 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
714 isl_int t;
716 if (v == 0)
717 return aff;
719 isl_int_init(t);
720 isl_int_set_si(t, v);
721 aff = isl_aff_add_constant_num(aff, t);
722 isl_int_clear(t);
724 return aff;
727 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
729 aff = isl_aff_cow(aff);
730 if (!aff)
731 return NULL;
733 aff->v = isl_vec_cow(aff->v);
734 if (!aff->v)
735 return isl_aff_free(aff);
737 isl_int_set_si(aff->v->el[1], v);
739 return aff;
742 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
743 enum isl_dim_type type, int pos, isl_int v)
745 if (!aff)
746 return NULL;
748 if (type == isl_dim_out)
749 isl_die(aff->v->ctx, isl_error_invalid,
750 "output/set dimension does not have a coefficient",
751 return isl_aff_free(aff));
752 if (type == isl_dim_in)
753 type = isl_dim_set;
755 if (pos >= isl_local_space_dim(aff->ls, type))
756 isl_die(aff->v->ctx, isl_error_invalid,
757 "position out of bounds", return isl_aff_free(aff));
759 aff = isl_aff_cow(aff);
760 if (!aff)
761 return NULL;
763 aff->v = isl_vec_cow(aff->v);
764 if (!aff->v)
765 return isl_aff_free(aff);
767 pos += isl_local_space_offset(aff->ls, type);
768 isl_int_set(aff->v->el[1 + pos], v);
770 return aff;
773 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
774 enum isl_dim_type type, int pos, int v)
776 if (!aff)
777 return NULL;
779 if (type == isl_dim_out)
780 isl_die(aff->v->ctx, isl_error_invalid,
781 "output/set dimension does not have a coefficient",
782 return isl_aff_free(aff));
783 if (type == isl_dim_in)
784 type = isl_dim_set;
786 if (pos >= isl_local_space_dim(aff->ls, type))
787 isl_die(aff->v->ctx, isl_error_invalid,
788 "position out of bounds", return isl_aff_free(aff));
790 pos += isl_local_space_offset(aff->ls, type);
791 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
792 return aff;
794 aff = isl_aff_cow(aff);
795 if (!aff)
796 return NULL;
798 aff->v = isl_vec_cow(aff->v);
799 if (!aff->v)
800 return isl_aff_free(aff);
802 isl_int_set_si(aff->v->el[1 + pos], v);
804 return aff;
807 /* Replace the coefficient of the variable of type "type" at position "pos"
808 * of "aff" by "v".
810 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
811 enum isl_dim_type type, int pos, __isl_take isl_val *v)
813 if (!aff || !v)
814 goto error;
816 if (type == isl_dim_out)
817 isl_die(aff->v->ctx, isl_error_invalid,
818 "output/set dimension does not have a coefficient",
819 goto error);
820 if (type == isl_dim_in)
821 type = isl_dim_set;
823 if (pos >= isl_local_space_dim(aff->ls, type))
824 isl_die(aff->v->ctx, isl_error_invalid,
825 "position out of bounds", goto error);
827 if (!isl_val_is_rat(v))
828 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
829 "expecting rational value", goto error);
831 pos += isl_local_space_offset(aff->ls, type);
832 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
833 isl_int_eq(aff->v->el[0], v->d)) {
834 isl_val_free(v);
835 return aff;
838 aff = isl_aff_cow(aff);
839 if (!aff)
840 goto error;
841 aff->v = isl_vec_cow(aff->v);
842 if (!aff->v)
843 goto error;
845 if (isl_int_eq(aff->v->el[0], v->d)) {
846 isl_int_set(aff->v->el[1 + pos], v->n);
847 } else if (isl_int_is_one(v->d)) {
848 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
849 } else {
850 isl_seq_scale(aff->v->el + 1,
851 aff->v->el + 1, v->d, aff->v->size - 1);
852 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
853 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
854 aff->v = isl_vec_normalize(aff->v);
855 if (!aff->v)
856 goto error;
859 isl_val_free(v);
860 return aff;
861 error:
862 isl_aff_free(aff);
863 isl_val_free(v);
864 return NULL;
867 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
868 enum isl_dim_type type, int pos, isl_int v)
870 if (!aff)
871 return NULL;
873 if (type == isl_dim_out)
874 isl_die(aff->v->ctx, isl_error_invalid,
875 "output/set dimension does not have a coefficient",
876 return isl_aff_free(aff));
877 if (type == isl_dim_in)
878 type = isl_dim_set;
880 if (pos >= isl_local_space_dim(aff->ls, type))
881 isl_die(aff->v->ctx, isl_error_invalid,
882 "position out of bounds", return isl_aff_free(aff));
884 aff = isl_aff_cow(aff);
885 if (!aff)
886 return NULL;
888 aff->v = isl_vec_cow(aff->v);
889 if (!aff->v)
890 return isl_aff_free(aff);
892 pos += isl_local_space_offset(aff->ls, type);
893 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
895 return aff;
898 /* Add "v" to the coefficient of the variable of type "type"
899 * at position "pos" of "aff".
901 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
902 enum isl_dim_type type, int pos, __isl_take isl_val *v)
904 if (!aff || !v)
905 goto error;
907 if (isl_val_is_zero(v)) {
908 isl_val_free(v);
909 return aff;
912 if (type == isl_dim_out)
913 isl_die(aff->v->ctx, isl_error_invalid,
914 "output/set dimension does not have a coefficient",
915 goto error);
916 if (type == isl_dim_in)
917 type = isl_dim_set;
919 if (pos >= isl_local_space_dim(aff->ls, type))
920 isl_die(aff->v->ctx, isl_error_invalid,
921 "position out of bounds", goto error);
923 if (!isl_val_is_rat(v))
924 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
925 "expecting rational value", goto error);
927 aff = isl_aff_cow(aff);
928 if (!aff)
929 goto error;
931 aff->v = isl_vec_cow(aff->v);
932 if (!aff->v)
933 goto error;
935 pos += isl_local_space_offset(aff->ls, type);
936 if (isl_int_is_one(v->d)) {
937 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
938 } else if (isl_int_eq(aff->v->el[0], v->d)) {
939 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
940 aff->v = isl_vec_normalize(aff->v);
941 if (!aff->v)
942 goto error;
943 } else {
944 isl_seq_scale(aff->v->el + 1,
945 aff->v->el + 1, v->d, aff->v->size - 1);
946 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
947 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
948 aff->v = isl_vec_normalize(aff->v);
949 if (!aff->v)
950 goto error;
953 isl_val_free(v);
954 return aff;
955 error:
956 isl_aff_free(aff);
957 isl_val_free(v);
958 return NULL;
961 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
962 enum isl_dim_type type, int pos, int v)
964 isl_int t;
966 isl_int_init(t);
967 isl_int_set_si(t, v);
968 aff = isl_aff_add_coefficient(aff, type, pos, t);
969 isl_int_clear(t);
971 return aff;
974 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
976 if (!aff)
977 return NULL;
979 return isl_local_space_get_div(aff->ls, pos);
982 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
984 aff = isl_aff_cow(aff);
985 if (!aff)
986 return NULL;
987 aff->v = isl_vec_cow(aff->v);
988 if (!aff->v)
989 return isl_aff_free(aff);
991 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
993 return aff;
996 /* Remove divs from the local space that do not appear in the affine
997 * expression.
998 * We currently only remove divs at the end.
999 * Some intermediate divs may also not appear directly in the affine
1000 * expression, but we would also need to check that no other divs are
1001 * defined in terms of them.
1003 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1005 int pos;
1006 int off;
1007 int n;
1009 if (!aff)
1010 return NULL;
1012 n = isl_local_space_dim(aff->ls, isl_dim_div);
1013 off = isl_local_space_offset(aff->ls, isl_dim_div);
1015 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1016 if (pos == n)
1017 return aff;
1019 aff = isl_aff_cow(aff);
1020 if (!aff)
1021 return NULL;
1023 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1024 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1025 if (!aff->ls || !aff->v)
1026 return isl_aff_free(aff);
1028 return aff;
1031 /* Given two affine expressions "p" of length p_len (including the
1032 * denominator and the constant term) and "subs" of length subs_len,
1033 * plug in "subs" for the variable at position "pos".
1034 * The variables of "subs" and "p" are assumed to match up to subs_len,
1035 * but "p" may have additional variables.
1036 * "v" is an initialized isl_int that can be used internally.
1038 * In particular, if "p" represents the expression
1040 * (a i + g)/m
1042 * with i the variable at position "pos" and "subs" represents the expression
1044 * f/d
1046 * then the result represents the expression
1048 * (a f + d g)/(m d)
1051 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1052 int p_len, int subs_len, isl_int v)
1054 isl_int_set(v, p[1 + pos]);
1055 isl_int_set_si(p[1 + pos], 0);
1056 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1057 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1058 isl_int_mul(p[0], p[0], subs[0]);
1061 /* Look for any divs in the aff->ls with a denominator equal to one
1062 * and plug them into the affine expression and any subsequent divs
1063 * that may reference the div.
1065 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1067 int i, n;
1068 int len;
1069 isl_int v;
1070 isl_vec *vec;
1071 isl_local_space *ls;
1072 unsigned pos;
1074 if (!aff)
1075 return NULL;
1077 n = isl_local_space_dim(aff->ls, isl_dim_div);
1078 len = aff->v->size;
1079 for (i = 0; i < n; ++i) {
1080 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1081 continue;
1082 ls = isl_local_space_copy(aff->ls);
1083 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1084 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1085 vec = isl_vec_copy(aff->v);
1086 vec = isl_vec_cow(vec);
1087 if (!ls || !vec)
1088 goto error;
1090 isl_int_init(v);
1092 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1093 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1094 len, len, v);
1096 isl_int_clear(v);
1098 isl_vec_free(aff->v);
1099 aff->v = vec;
1100 isl_local_space_free(aff->ls);
1101 aff->ls = ls;
1104 return aff;
1105 error:
1106 isl_vec_free(vec);
1107 isl_local_space_free(ls);
1108 return isl_aff_free(aff);
1111 /* Look for any divs j that appear with a unit coefficient inside
1112 * the definitions of other divs i and plug them into the definitions
1113 * of the divs i.
1115 * In particular, an expression of the form
1117 * floor((f(..) + floor(g(..)/n))/m)
1119 * is simplified to
1121 * floor((n * f(..) + g(..))/(n * m))
1123 * This simplification is correct because we can move the expression
1124 * f(..) into the inner floor in the original expression to obtain
1126 * floor(floor((n * f(..) + g(..))/n)/m)
1128 * from which we can derive the simplified expression.
1130 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1132 int i, j, n;
1133 int off;
1135 if (!aff)
1136 return NULL;
1138 n = isl_local_space_dim(aff->ls, isl_dim_div);
1139 off = isl_local_space_offset(aff->ls, isl_dim_div);
1140 for (i = 1; i < n; ++i) {
1141 for (j = 0; j < i; ++j) {
1142 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1143 continue;
1144 aff->ls = isl_local_space_substitute_seq(aff->ls,
1145 isl_dim_div, j, aff->ls->div->row[j],
1146 aff->v->size, i, 1);
1147 if (!aff->ls)
1148 return isl_aff_free(aff);
1152 return aff;
1155 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1157 * Even though this function is only called on isl_affs with a single
1158 * reference, we are careful to only change aff->v and aff->ls together.
1160 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1162 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1163 isl_local_space *ls;
1164 isl_vec *v;
1166 ls = isl_local_space_copy(aff->ls);
1167 ls = isl_local_space_swap_div(ls, a, b);
1168 v = isl_vec_copy(aff->v);
1169 v = isl_vec_cow(v);
1170 if (!ls || !v)
1171 goto error;
1173 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1174 isl_vec_free(aff->v);
1175 aff->v = v;
1176 isl_local_space_free(aff->ls);
1177 aff->ls = ls;
1179 return aff;
1180 error:
1181 isl_vec_free(v);
1182 isl_local_space_free(ls);
1183 return isl_aff_free(aff);
1186 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1188 * We currently do not actually remove div "b", but simply add its
1189 * coefficient to that of "a" and then zero it out.
1191 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1193 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1195 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1196 return aff;
1198 aff->v = isl_vec_cow(aff->v);
1199 if (!aff->v)
1200 return isl_aff_free(aff);
1202 isl_int_add(aff->v->el[1 + off + a],
1203 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1204 isl_int_set_si(aff->v->el[1 + off + b], 0);
1206 return aff;
1209 /* Sort the divs in the local space of "aff" according to
1210 * the comparison function "cmp_row" in isl_local_space.c,
1211 * combining the coefficients of identical divs.
1213 * Reordering divs does not change the semantics of "aff",
1214 * so there is no need to call isl_aff_cow.
1215 * Moreover, this function is currently only called on isl_affs
1216 * with a single reference.
1218 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1220 int i, j, n;
1221 unsigned off;
1223 if (!aff)
1224 return NULL;
1226 off = isl_local_space_offset(aff->ls, isl_dim_div);
1227 n = isl_aff_dim(aff, isl_dim_div);
1228 for (i = 1; i < n; ++i) {
1229 for (j = i - 1; j >= 0; --j) {
1230 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1231 if (cmp < 0)
1232 break;
1233 if (cmp == 0)
1234 aff = merge_divs(aff, j, j + 1);
1235 else
1236 aff = swap_div(aff, j, j + 1);
1237 if (!aff)
1238 return NULL;
1242 return aff;
1245 /* Normalize the representation of "aff".
1247 * This function should only be called of "new" isl_affs, i.e.,
1248 * with only a single reference. We therefore do not need to
1249 * worry about affecting other instances.
1251 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1253 if (!aff)
1254 return NULL;
1255 aff->v = isl_vec_normalize(aff->v);
1256 if (!aff->v)
1257 return isl_aff_free(aff);
1258 aff = plug_in_integral_divs(aff);
1259 aff = plug_in_unit_divs(aff);
1260 aff = sort_divs(aff);
1261 aff = isl_aff_remove_unused_divs(aff);
1262 return aff;
1265 /* Given f, return floor(f).
1266 * If f is an integer expression, then just return f.
1267 * If f is a constant, then return the constant floor(f).
1268 * Otherwise, if f = g/m, write g = q m + r,
1269 * create a new div d = [r/m] and return the expression q + d.
1270 * The coefficients in r are taken to lie between -m/2 and m/2.
1272 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1274 int i;
1275 int size;
1276 isl_ctx *ctx;
1277 isl_vec *div;
1279 if (!aff)
1280 return NULL;
1282 if (isl_int_is_one(aff->v->el[0]))
1283 return aff;
1285 aff = isl_aff_cow(aff);
1286 if (!aff)
1287 return NULL;
1289 aff->v = isl_vec_cow(aff->v);
1290 if (!aff->v)
1291 return isl_aff_free(aff);
1293 if (isl_aff_is_cst(aff)) {
1294 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1295 isl_int_set_si(aff->v->el[0], 1);
1296 return aff;
1299 div = isl_vec_copy(aff->v);
1300 div = isl_vec_cow(div);
1301 if (!div)
1302 return isl_aff_free(aff);
1304 ctx = isl_aff_get_ctx(aff);
1305 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1306 for (i = 1; i < aff->v->size; ++i) {
1307 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1308 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1309 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1310 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1311 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1315 aff->ls = isl_local_space_add_div(aff->ls, div);
1316 if (!aff->ls)
1317 return isl_aff_free(aff);
1319 size = aff->v->size;
1320 aff->v = isl_vec_extend(aff->v, size + 1);
1321 if (!aff->v)
1322 return isl_aff_free(aff);
1323 isl_int_set_si(aff->v->el[0], 1);
1324 isl_int_set_si(aff->v->el[size], 1);
1326 aff = isl_aff_normalize(aff);
1328 return aff;
1331 /* Compute
1333 * aff mod m = aff - m * floor(aff/m)
1335 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1337 isl_aff *res;
1339 res = isl_aff_copy(aff);
1340 aff = isl_aff_scale_down(aff, m);
1341 aff = isl_aff_floor(aff);
1342 aff = isl_aff_scale(aff, m);
1343 res = isl_aff_sub(res, aff);
1345 return res;
1348 /* Compute
1350 * aff mod m = aff - m * floor(aff/m)
1352 * with m an integer value.
1354 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1355 __isl_take isl_val *m)
1357 isl_aff *res;
1359 if (!aff || !m)
1360 goto error;
1362 if (!isl_val_is_int(m))
1363 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1364 "expecting integer modulo", goto error);
1366 res = isl_aff_copy(aff);
1367 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1368 aff = isl_aff_floor(aff);
1369 aff = isl_aff_scale_val(aff, m);
1370 res = isl_aff_sub(res, aff);
1372 return res;
1373 error:
1374 isl_aff_free(aff);
1375 isl_val_free(m);
1376 return NULL;
1379 /* Compute
1381 * pwaff mod m = pwaff - m * floor(pwaff/m)
1383 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1385 isl_pw_aff *res;
1387 res = isl_pw_aff_copy(pwaff);
1388 pwaff = isl_pw_aff_scale_down(pwaff, m);
1389 pwaff = isl_pw_aff_floor(pwaff);
1390 pwaff = isl_pw_aff_scale(pwaff, m);
1391 res = isl_pw_aff_sub(res, pwaff);
1393 return res;
1396 /* Compute
1398 * pa mod m = pa - m * floor(pa/m)
1400 * with m an integer value.
1402 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1403 __isl_take isl_val *m)
1405 if (!pa || !m)
1406 goto error;
1407 if (!isl_val_is_int(m))
1408 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1409 "expecting integer modulo", goto error);
1410 pa = isl_pw_aff_mod(pa, m->n);
1411 isl_val_free(m);
1412 return pa;
1413 error:
1414 isl_pw_aff_free(pa);
1415 isl_val_free(m);
1416 return NULL;
1419 /* Given f, return ceil(f).
1420 * If f is an integer expression, then just return f.
1421 * Otherwise, let f be the expression
1423 * e/m
1425 * then return
1427 * floor((e + m - 1)/m)
1429 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1431 if (!aff)
1432 return NULL;
1434 if (isl_int_is_one(aff->v->el[0]))
1435 return aff;
1437 aff = isl_aff_cow(aff);
1438 if (!aff)
1439 return NULL;
1440 aff->v = isl_vec_cow(aff->v);
1441 if (!aff->v)
1442 return isl_aff_free(aff);
1444 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1445 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1446 aff = isl_aff_floor(aff);
1448 return aff;
1451 /* Apply the expansion computed by isl_merge_divs.
1452 * The expansion itself is given by "exp" while the resulting
1453 * list of divs is given by "div".
1455 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1456 __isl_take isl_mat *div, int *exp)
1458 int i, j;
1459 int old_n_div;
1460 int new_n_div;
1461 int offset;
1463 aff = isl_aff_cow(aff);
1464 if (!aff || !div)
1465 goto error;
1467 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1468 new_n_div = isl_mat_rows(div);
1469 if (new_n_div < old_n_div)
1470 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1471 "not an expansion", goto error);
1473 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1474 if (!aff->v)
1475 goto error;
1477 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1478 j = old_n_div - 1;
1479 for (i = new_n_div - 1; i >= 0; --i) {
1480 if (j >= 0 && exp[j] == i) {
1481 if (i != j)
1482 isl_int_swap(aff->v->el[offset + i],
1483 aff->v->el[offset + j]);
1484 j--;
1485 } else
1486 isl_int_set_si(aff->v->el[offset + i], 0);
1489 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1490 if (!aff->ls)
1491 goto error;
1492 isl_mat_free(div);
1493 return aff;
1494 error:
1495 isl_aff_free(aff);
1496 isl_mat_free(div);
1497 return NULL;
1500 /* Add two affine expressions that live in the same local space.
1502 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1503 __isl_take isl_aff *aff2)
1505 isl_int gcd, f;
1507 aff1 = isl_aff_cow(aff1);
1508 if (!aff1 || !aff2)
1509 goto error;
1511 aff1->v = isl_vec_cow(aff1->v);
1512 if (!aff1->v)
1513 goto error;
1515 isl_int_init(gcd);
1516 isl_int_init(f);
1517 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1518 isl_int_divexact(f, aff2->v->el[0], gcd);
1519 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1520 isl_int_divexact(f, aff1->v->el[0], gcd);
1521 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1522 isl_int_divexact(f, aff2->v->el[0], gcd);
1523 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1524 isl_int_clear(f);
1525 isl_int_clear(gcd);
1527 isl_aff_free(aff2);
1528 return aff1;
1529 error:
1530 isl_aff_free(aff1);
1531 isl_aff_free(aff2);
1532 return NULL;
1535 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1536 __isl_take isl_aff *aff2)
1538 isl_ctx *ctx;
1539 int *exp1 = NULL;
1540 int *exp2 = NULL;
1541 isl_mat *div;
1542 int n_div1, n_div2;
1544 if (!aff1 || !aff2)
1545 goto error;
1547 ctx = isl_aff_get_ctx(aff1);
1548 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1549 isl_die(ctx, isl_error_invalid,
1550 "spaces don't match", goto error);
1552 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1553 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1554 if (n_div1 == 0 && n_div2 == 0)
1555 return add_expanded(aff1, aff2);
1557 exp1 = isl_alloc_array(ctx, int, n_div1);
1558 exp2 = isl_alloc_array(ctx, int, n_div2);
1559 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1560 goto error;
1562 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1563 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1564 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1565 free(exp1);
1566 free(exp2);
1568 return add_expanded(aff1, aff2);
1569 error:
1570 free(exp1);
1571 free(exp2);
1572 isl_aff_free(aff1);
1573 isl_aff_free(aff2);
1574 return NULL;
1577 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1578 __isl_take isl_aff *aff2)
1580 return isl_aff_add(aff1, isl_aff_neg(aff2));
1583 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1585 isl_int gcd;
1587 if (isl_int_is_one(f))
1588 return aff;
1590 aff = isl_aff_cow(aff);
1591 if (!aff)
1592 return NULL;
1593 aff->v = isl_vec_cow(aff->v);
1594 if (!aff->v)
1595 return isl_aff_free(aff);
1597 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1598 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1599 return aff;
1602 isl_int_init(gcd);
1603 isl_int_gcd(gcd, aff->v->el[0], f);
1604 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1605 isl_int_divexact(gcd, f, gcd);
1606 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1607 isl_int_clear(gcd);
1609 return aff;
1612 /* Multiple "aff" by "v".
1614 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1615 __isl_take isl_val *v)
1617 if (!aff || !v)
1618 goto error;
1620 if (isl_val_is_one(v)) {
1621 isl_val_free(v);
1622 return aff;
1625 if (!isl_val_is_rat(v))
1626 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1627 "expecting rational factor", goto error);
1629 aff = isl_aff_scale(aff, v->n);
1630 aff = isl_aff_scale_down(aff, v->d);
1632 isl_val_free(v);
1633 return aff;
1634 error:
1635 isl_aff_free(aff);
1636 isl_val_free(v);
1637 return NULL;
1640 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1642 isl_int gcd;
1644 if (isl_int_is_one(f))
1645 return aff;
1647 aff = isl_aff_cow(aff);
1648 if (!aff)
1649 return NULL;
1651 if (isl_int_is_zero(f))
1652 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1653 "cannot scale down by zero", return isl_aff_free(aff));
1655 aff->v = isl_vec_cow(aff->v);
1656 if (!aff->v)
1657 return isl_aff_free(aff);
1659 isl_int_init(gcd);
1660 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1661 isl_int_gcd(gcd, gcd, f);
1662 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1663 isl_int_divexact(gcd, f, gcd);
1664 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1665 isl_int_clear(gcd);
1667 return aff;
1670 /* Divide "aff" by "v".
1672 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1673 __isl_take isl_val *v)
1675 if (!aff || !v)
1676 goto error;
1678 if (isl_val_is_one(v)) {
1679 isl_val_free(v);
1680 return aff;
1683 if (!isl_val_is_rat(v))
1684 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1685 "expecting rational factor", goto error);
1686 if (!isl_val_is_pos(v))
1687 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1688 "factor needs to be positive", goto error);
1690 aff = isl_aff_scale(aff, v->d);
1691 aff = isl_aff_scale_down(aff, v->n);
1693 isl_val_free(v);
1694 return aff;
1695 error:
1696 isl_aff_free(aff);
1697 isl_val_free(v);
1698 return NULL;
1701 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1703 isl_int v;
1705 if (f == 1)
1706 return aff;
1708 isl_int_init(v);
1709 isl_int_set_ui(v, f);
1710 aff = isl_aff_scale_down(aff, v);
1711 isl_int_clear(v);
1713 return aff;
1716 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1717 enum isl_dim_type type, unsigned pos, const char *s)
1719 aff = isl_aff_cow(aff);
1720 if (!aff)
1721 return NULL;
1722 if (type == isl_dim_out)
1723 isl_die(aff->v->ctx, isl_error_invalid,
1724 "cannot set name of output/set dimension",
1725 return isl_aff_free(aff));
1726 if (type == isl_dim_in)
1727 type = isl_dim_set;
1728 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1729 if (!aff->ls)
1730 return isl_aff_free(aff);
1732 return aff;
1735 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1736 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1738 aff = isl_aff_cow(aff);
1739 if (!aff)
1740 goto error;
1741 if (type == isl_dim_out)
1742 isl_die(aff->v->ctx, isl_error_invalid,
1743 "cannot set name of output/set dimension",
1744 goto error);
1745 if (type == isl_dim_in)
1746 type = isl_dim_set;
1747 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1748 if (!aff->ls)
1749 return isl_aff_free(aff);
1751 return aff;
1752 error:
1753 isl_id_free(id);
1754 isl_aff_free(aff);
1755 return NULL;
1758 /* Replace the identifier of the input tuple of "aff" by "id".
1759 * type is currently required to be equal to isl_dim_in
1761 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1762 enum isl_dim_type type, __isl_take isl_id *id)
1764 aff = isl_aff_cow(aff);
1765 if (!aff)
1766 goto error;
1767 if (type != isl_dim_out)
1768 isl_die(aff->v->ctx, isl_error_invalid,
1769 "cannot only set id of input tuple", goto error);
1770 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
1771 if (!aff->ls)
1772 return isl_aff_free(aff);
1774 return aff;
1775 error:
1776 isl_id_free(id);
1777 isl_aff_free(aff);
1778 return NULL;
1781 /* Exploit the equalities in "eq" to simplify the affine expression
1782 * and the expressions of the integer divisions in the local space.
1783 * The integer divisions in this local space are assumed to appear
1784 * as regular dimensions in "eq".
1786 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1787 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1789 int i, j;
1790 unsigned total;
1791 unsigned n_div;
1793 if (!eq)
1794 goto error;
1795 if (eq->n_eq == 0) {
1796 isl_basic_set_free(eq);
1797 return aff;
1800 aff = isl_aff_cow(aff);
1801 if (!aff)
1802 goto error;
1804 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1805 isl_basic_set_copy(eq));
1806 aff->v = isl_vec_cow(aff->v);
1807 if (!aff->ls || !aff->v)
1808 goto error;
1810 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1811 n_div = eq->n_div;
1812 for (i = 0; i < eq->n_eq; ++i) {
1813 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1814 if (j < 0 || j == 0 || j >= total)
1815 continue;
1817 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1818 &aff->v->el[0]);
1821 isl_basic_set_free(eq);
1822 aff = isl_aff_normalize(aff);
1823 return aff;
1824 error:
1825 isl_basic_set_free(eq);
1826 isl_aff_free(aff);
1827 return NULL;
1830 /* Exploit the equalities in "eq" to simplify the affine expression
1831 * and the expressions of the integer divisions in the local space.
1833 static __isl_give isl_aff *isl_aff_substitute_equalities(
1834 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1836 int n_div;
1838 if (!aff || !eq)
1839 goto error;
1840 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1841 if (n_div > 0)
1842 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1843 return isl_aff_substitute_equalities_lifted(aff, eq);
1844 error:
1845 isl_basic_set_free(eq);
1846 isl_aff_free(aff);
1847 return NULL;
1850 /* Look for equalities among the variables shared by context and aff
1851 * and the integer divisions of aff, if any.
1852 * The equalities are then used to eliminate coefficients and/or integer
1853 * divisions from aff.
1855 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1856 __isl_take isl_set *context)
1858 isl_basic_set *hull;
1859 int n_div;
1861 if (!aff)
1862 goto error;
1863 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1864 if (n_div > 0) {
1865 isl_basic_set *bset;
1866 isl_local_space *ls;
1867 context = isl_set_add_dims(context, isl_dim_set, n_div);
1868 ls = isl_aff_get_domain_local_space(aff);
1869 bset = isl_basic_set_from_local_space(ls);
1870 bset = isl_basic_set_lift(bset);
1871 bset = isl_basic_set_flatten(bset);
1872 context = isl_set_intersect(context,
1873 isl_set_from_basic_set(bset));
1876 hull = isl_set_affine_hull(context);
1877 return isl_aff_substitute_equalities_lifted(aff, hull);
1878 error:
1879 isl_aff_free(aff);
1880 isl_set_free(context);
1881 return NULL;
1884 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1885 __isl_take isl_set *context)
1887 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1888 dom_context = isl_set_intersect_params(dom_context, context);
1889 return isl_aff_gist(aff, dom_context);
1892 /* Return a basic set containing those elements in the space
1893 * of aff where it is non-negative.
1894 * If "rational" is set, then return a rational basic set.
1896 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1897 __isl_take isl_aff *aff, int rational)
1899 isl_constraint *ineq;
1900 isl_basic_set *bset;
1902 ineq = isl_inequality_from_aff(aff);
1904 bset = isl_basic_set_from_constraint(ineq);
1905 if (rational)
1906 bset = isl_basic_set_set_rational(bset);
1907 bset = isl_basic_set_simplify(bset);
1908 return bset;
1911 /* Return a basic set containing those elements in the space
1912 * of aff where it is non-negative.
1914 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1916 return aff_nonneg_basic_set(aff, 0);
1919 /* Return a basic set containing those elements in the domain space
1920 * of aff where it is negative.
1922 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1924 aff = isl_aff_neg(aff);
1925 aff = isl_aff_add_constant_num_si(aff, -1);
1926 return isl_aff_nonneg_basic_set(aff);
1929 /* Return a basic set containing those elements in the space
1930 * of aff where it is zero.
1931 * If "rational" is set, then return a rational basic set.
1933 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1934 int rational)
1936 isl_constraint *ineq;
1937 isl_basic_set *bset;
1939 ineq = isl_equality_from_aff(aff);
1941 bset = isl_basic_set_from_constraint(ineq);
1942 if (rational)
1943 bset = isl_basic_set_set_rational(bset);
1944 bset = isl_basic_set_simplify(bset);
1945 return bset;
1948 /* Return a basic set containing those elements in the space
1949 * of aff where it is zero.
1951 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1953 return aff_zero_basic_set(aff, 0);
1956 /* Return a basic set containing those elements in the shared space
1957 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1959 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1960 __isl_take isl_aff *aff2)
1962 aff1 = isl_aff_sub(aff1, aff2);
1964 return isl_aff_nonneg_basic_set(aff1);
1967 /* Return a basic set containing those elements in the shared space
1968 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1970 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1971 __isl_take isl_aff *aff2)
1973 return isl_aff_ge_basic_set(aff2, aff1);
1976 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1977 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1979 aff1 = isl_aff_add(aff1, aff2);
1980 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1981 return aff1;
1984 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1986 if (!aff)
1987 return -1;
1989 return 0;
1992 /* Check whether the given affine expression has non-zero coefficient
1993 * for any dimension in the given range or if any of these dimensions
1994 * appear with non-zero coefficients in any of the integer divisions
1995 * involved in the affine expression.
1997 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1998 enum isl_dim_type type, unsigned first, unsigned n)
2000 int i;
2001 isl_ctx *ctx;
2002 int *active = NULL;
2003 int involves = 0;
2005 if (!aff)
2006 return -1;
2007 if (n == 0)
2008 return 0;
2010 ctx = isl_aff_get_ctx(aff);
2011 if (first + n > isl_aff_dim(aff, type))
2012 isl_die(ctx, isl_error_invalid,
2013 "range out of bounds", return -1);
2015 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2016 if (!active)
2017 goto error;
2019 first += isl_local_space_offset(aff->ls, type) - 1;
2020 for (i = 0; i < n; ++i)
2021 if (active[first + i]) {
2022 involves = 1;
2023 break;
2026 free(active);
2028 return involves;
2029 error:
2030 free(active);
2031 return -1;
2034 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2035 enum isl_dim_type type, unsigned first, unsigned n)
2037 isl_ctx *ctx;
2039 if (!aff)
2040 return NULL;
2041 if (type == isl_dim_out)
2042 isl_die(aff->v->ctx, isl_error_invalid,
2043 "cannot drop output/set dimension",
2044 return isl_aff_free(aff));
2045 if (type == isl_dim_in)
2046 type = isl_dim_set;
2047 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2048 return aff;
2050 ctx = isl_aff_get_ctx(aff);
2051 if (first + n > isl_local_space_dim(aff->ls, type))
2052 isl_die(ctx, isl_error_invalid, "range out of bounds",
2053 return isl_aff_free(aff));
2055 aff = isl_aff_cow(aff);
2056 if (!aff)
2057 return NULL;
2059 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2060 if (!aff->ls)
2061 return isl_aff_free(aff);
2063 first += 1 + isl_local_space_offset(aff->ls, type);
2064 aff->v = isl_vec_drop_els(aff->v, first, n);
2065 if (!aff->v)
2066 return isl_aff_free(aff);
2068 return aff;
2071 /* Project the domain of the affine expression onto its parameter space.
2072 * The affine expression may not involve any of the domain dimensions.
2074 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2076 isl_space *space;
2077 unsigned n;
2078 int involves;
2080 n = isl_aff_dim(aff, isl_dim_in);
2081 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2082 if (involves < 0)
2083 return isl_aff_free(aff);
2084 if (involves)
2085 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2086 "affine expression involves some of the domain dimensions",
2087 return isl_aff_free(aff));
2088 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2089 space = isl_aff_get_domain_space(aff);
2090 space = isl_space_params(space);
2091 aff = isl_aff_reset_domain_space(aff, space);
2092 return aff;
2095 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2096 enum isl_dim_type type, unsigned first, unsigned n)
2098 isl_ctx *ctx;
2100 if (!aff)
2101 return NULL;
2102 if (type == isl_dim_out)
2103 isl_die(aff->v->ctx, isl_error_invalid,
2104 "cannot insert output/set dimensions",
2105 return isl_aff_free(aff));
2106 if (type == isl_dim_in)
2107 type = isl_dim_set;
2108 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2109 return aff;
2111 ctx = isl_aff_get_ctx(aff);
2112 if (first > isl_local_space_dim(aff->ls, type))
2113 isl_die(ctx, isl_error_invalid, "position out of bounds",
2114 return isl_aff_free(aff));
2116 aff = isl_aff_cow(aff);
2117 if (!aff)
2118 return NULL;
2120 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2121 if (!aff->ls)
2122 return isl_aff_free(aff);
2124 first += 1 + isl_local_space_offset(aff->ls, type);
2125 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2126 if (!aff->v)
2127 return isl_aff_free(aff);
2129 return aff;
2132 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2133 enum isl_dim_type type, unsigned n)
2135 unsigned pos;
2137 pos = isl_aff_dim(aff, type);
2139 return isl_aff_insert_dims(aff, type, pos, n);
2142 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2143 enum isl_dim_type type, unsigned n)
2145 unsigned pos;
2147 pos = isl_pw_aff_dim(pwaff, type);
2149 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2152 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2153 * to dimensions of "dst_type" at "dst_pos".
2155 * We only support moving input dimensions to parameters and vice versa.
2157 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2158 enum isl_dim_type dst_type, unsigned dst_pos,
2159 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2161 unsigned g_dst_pos;
2162 unsigned g_src_pos;
2164 if (!aff)
2165 return NULL;
2166 if (n == 0 &&
2167 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2168 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2169 return aff;
2171 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2172 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2173 "cannot move output/set dimension", isl_aff_free(aff));
2174 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2175 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2176 "cannot move divs", isl_aff_free(aff));
2177 if (dst_type == isl_dim_in)
2178 dst_type = isl_dim_set;
2179 if (src_type == isl_dim_in)
2180 src_type = isl_dim_set;
2182 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2183 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2184 "range out of bounds", isl_aff_free(aff));
2185 if (dst_type == src_type)
2186 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2187 "moving dims within the same type not supported",
2188 isl_aff_free(aff));
2190 aff = isl_aff_cow(aff);
2191 if (!aff)
2192 return NULL;
2194 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2195 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2196 if (dst_type > src_type)
2197 g_dst_pos -= n;
2199 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2200 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2201 src_type, src_pos, n);
2202 if (!aff->v || !aff->ls)
2203 return isl_aff_free(aff);
2205 aff = sort_divs(aff);
2207 return aff;
2210 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2212 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2213 return isl_pw_aff_alloc(dom, aff);
2216 #undef PW
2217 #define PW isl_pw_aff
2218 #undef EL
2219 #define EL isl_aff
2220 #undef EL_IS_ZERO
2221 #define EL_IS_ZERO is_empty
2222 #undef ZERO
2223 #define ZERO empty
2224 #undef IS_ZERO
2225 #define IS_ZERO is_empty
2226 #undef FIELD
2227 #define FIELD aff
2228 #undef DEFAULT_IS_ZERO
2229 #define DEFAULT_IS_ZERO 0
2231 #define NO_EVAL
2232 #define NO_OPT
2233 #define NO_LIFT
2234 #define NO_MORPH
2236 #include <isl_pw_templ.c>
2238 static __isl_give isl_set *align_params_pw_pw_set_and(
2239 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2240 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2241 __isl_take isl_pw_aff *pwaff2))
2243 if (!pwaff1 || !pwaff2)
2244 goto error;
2245 if (isl_space_match(pwaff1->dim, isl_dim_param,
2246 pwaff2->dim, isl_dim_param))
2247 return fn(pwaff1, pwaff2);
2248 if (!isl_space_has_named_params(pwaff1->dim) ||
2249 !isl_space_has_named_params(pwaff2->dim))
2250 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2251 "unaligned unnamed parameters", goto error);
2252 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2253 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2254 return fn(pwaff1, pwaff2);
2255 error:
2256 isl_pw_aff_free(pwaff1);
2257 isl_pw_aff_free(pwaff2);
2258 return NULL;
2261 /* Compute a piecewise quasi-affine expression with a domain that
2262 * is the union of those of pwaff1 and pwaff2 and such that on each
2263 * cell, the quasi-affine expression is the better (according to cmp)
2264 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2265 * is defined on a given cell, then the associated expression
2266 * is the defined one.
2268 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2269 __isl_take isl_pw_aff *pwaff2,
2270 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2271 __isl_take isl_aff *aff2))
2273 int i, j, n;
2274 isl_pw_aff *res;
2275 isl_ctx *ctx;
2276 isl_set *set;
2278 if (!pwaff1 || !pwaff2)
2279 goto error;
2281 ctx = isl_space_get_ctx(pwaff1->dim);
2282 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2283 isl_die(ctx, isl_error_invalid,
2284 "arguments should live in same space", goto error);
2286 if (isl_pw_aff_is_empty(pwaff1)) {
2287 isl_pw_aff_free(pwaff1);
2288 return pwaff2;
2291 if (isl_pw_aff_is_empty(pwaff2)) {
2292 isl_pw_aff_free(pwaff2);
2293 return pwaff1;
2296 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2297 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2299 for (i = 0; i < pwaff1->n; ++i) {
2300 set = isl_set_copy(pwaff1->p[i].set);
2301 for (j = 0; j < pwaff2->n; ++j) {
2302 struct isl_set *common;
2303 isl_set *better;
2305 common = isl_set_intersect(
2306 isl_set_copy(pwaff1->p[i].set),
2307 isl_set_copy(pwaff2->p[j].set));
2308 better = isl_set_from_basic_set(cmp(
2309 isl_aff_copy(pwaff2->p[j].aff),
2310 isl_aff_copy(pwaff1->p[i].aff)));
2311 better = isl_set_intersect(common, better);
2312 if (isl_set_plain_is_empty(better)) {
2313 isl_set_free(better);
2314 continue;
2316 set = isl_set_subtract(set, isl_set_copy(better));
2318 res = isl_pw_aff_add_piece(res, better,
2319 isl_aff_copy(pwaff2->p[j].aff));
2321 res = isl_pw_aff_add_piece(res, set,
2322 isl_aff_copy(pwaff1->p[i].aff));
2325 for (j = 0; j < pwaff2->n; ++j) {
2326 set = isl_set_copy(pwaff2->p[j].set);
2327 for (i = 0; i < pwaff1->n; ++i)
2328 set = isl_set_subtract(set,
2329 isl_set_copy(pwaff1->p[i].set));
2330 res = isl_pw_aff_add_piece(res, set,
2331 isl_aff_copy(pwaff2->p[j].aff));
2334 isl_pw_aff_free(pwaff1);
2335 isl_pw_aff_free(pwaff2);
2337 return res;
2338 error:
2339 isl_pw_aff_free(pwaff1);
2340 isl_pw_aff_free(pwaff2);
2341 return NULL;
2344 /* Compute a piecewise quasi-affine expression with a domain that
2345 * is the union of those of pwaff1 and pwaff2 and such that on each
2346 * cell, the quasi-affine expression is the maximum of those of pwaff1
2347 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2348 * cell, then the associated expression is the defined one.
2350 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2351 __isl_take isl_pw_aff *pwaff2)
2353 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2356 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2357 __isl_take isl_pw_aff *pwaff2)
2359 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2360 &pw_aff_union_max);
2363 /* Compute a piecewise quasi-affine expression with a domain that
2364 * is the union of those of pwaff1 and pwaff2 and such that on each
2365 * cell, the quasi-affine expression is the minimum of those of pwaff1
2366 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2367 * cell, then the associated expression is the defined one.
2369 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2370 __isl_take isl_pw_aff *pwaff2)
2372 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2375 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2376 __isl_take isl_pw_aff *pwaff2)
2378 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2379 &pw_aff_union_min);
2382 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2383 __isl_take isl_pw_aff *pwaff2, int max)
2385 if (max)
2386 return isl_pw_aff_union_max(pwaff1, pwaff2);
2387 else
2388 return isl_pw_aff_union_min(pwaff1, pwaff2);
2391 /* Construct a map with as domain the domain of pwaff and
2392 * one-dimensional range corresponding to the affine expressions.
2394 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2396 int i;
2397 isl_space *dim;
2398 isl_map *map;
2400 if (!pwaff)
2401 return NULL;
2403 dim = isl_pw_aff_get_space(pwaff);
2404 map = isl_map_empty(dim);
2406 for (i = 0; i < pwaff->n; ++i) {
2407 isl_basic_map *bmap;
2408 isl_map *map_i;
2410 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2411 map_i = isl_map_from_basic_map(bmap);
2412 map_i = isl_map_intersect_domain(map_i,
2413 isl_set_copy(pwaff->p[i].set));
2414 map = isl_map_union_disjoint(map, map_i);
2417 isl_pw_aff_free(pwaff);
2419 return map;
2422 /* Construct a map with as domain the domain of pwaff and
2423 * one-dimensional range corresponding to the affine expressions.
2425 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2427 if (!pwaff)
2428 return NULL;
2429 if (isl_space_is_set(pwaff->dim))
2430 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2431 "space of input is not a map", goto error);
2432 return map_from_pw_aff(pwaff);
2433 error:
2434 isl_pw_aff_free(pwaff);
2435 return NULL;
2438 /* Construct a one-dimensional set with as parameter domain
2439 * the domain of pwaff and the single set dimension
2440 * corresponding to the affine expressions.
2442 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2444 if (!pwaff)
2445 return NULL;
2446 if (!isl_space_is_set(pwaff->dim))
2447 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2448 "space of input is not a set", goto error);
2449 return map_from_pw_aff(pwaff);
2450 error:
2451 isl_pw_aff_free(pwaff);
2452 return NULL;
2455 /* Return a set containing those elements in the domain
2456 * of pwaff where it is non-negative.
2458 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2460 int i;
2461 isl_set *set;
2463 if (!pwaff)
2464 return NULL;
2466 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2468 for (i = 0; i < pwaff->n; ++i) {
2469 isl_basic_set *bset;
2470 isl_set *set_i;
2471 int rational;
2473 rational = isl_set_has_rational(pwaff->p[i].set);
2474 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2475 rational);
2476 set_i = isl_set_from_basic_set(bset);
2477 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2478 set = isl_set_union_disjoint(set, set_i);
2481 isl_pw_aff_free(pwaff);
2483 return set;
2486 /* Return a set containing those elements in the domain
2487 * of pwaff where it is zero (if complement is 0) or not zero
2488 * (if complement is 1).
2490 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2491 int complement)
2493 int i;
2494 isl_set *set;
2496 if (!pwaff)
2497 return NULL;
2499 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2501 for (i = 0; i < pwaff->n; ++i) {
2502 isl_basic_set *bset;
2503 isl_set *set_i, *zero;
2504 int rational;
2506 rational = isl_set_has_rational(pwaff->p[i].set);
2507 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2508 rational);
2509 zero = isl_set_from_basic_set(bset);
2510 set_i = isl_set_copy(pwaff->p[i].set);
2511 if (complement)
2512 set_i = isl_set_subtract(set_i, zero);
2513 else
2514 set_i = isl_set_intersect(set_i, zero);
2515 set = isl_set_union_disjoint(set, set_i);
2518 isl_pw_aff_free(pwaff);
2520 return set;
2523 /* Return a set containing those elements in the domain
2524 * of pwaff where it is zero.
2526 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2528 return pw_aff_zero_set(pwaff, 0);
2531 /* Return a set containing those elements in the domain
2532 * of pwaff where it is not zero.
2534 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2536 return pw_aff_zero_set(pwaff, 1);
2539 /* Return a set containing those elements in the shared domain
2540 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2542 * We compute the difference on the shared domain and then construct
2543 * the set of values where this difference is non-negative.
2544 * If strict is set, we first subtract 1 from the difference.
2545 * If equal is set, we only return the elements where pwaff1 and pwaff2
2546 * are equal.
2548 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2549 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2551 isl_set *set1, *set2;
2553 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2554 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2555 set1 = isl_set_intersect(set1, set2);
2556 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2557 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2558 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2560 if (strict) {
2561 isl_space *dim = isl_set_get_space(set1);
2562 isl_aff *aff;
2563 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2564 aff = isl_aff_add_constant_si(aff, -1);
2565 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2566 } else
2567 isl_set_free(set1);
2569 if (equal)
2570 return isl_pw_aff_zero_set(pwaff1);
2571 return isl_pw_aff_nonneg_set(pwaff1);
2574 /* Return a set containing those elements in the shared domain
2575 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2577 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2578 __isl_take isl_pw_aff *pwaff2)
2580 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2583 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2584 __isl_take isl_pw_aff *pwaff2)
2586 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2589 /* Return a set containing those elements in the shared domain
2590 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2592 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2593 __isl_take isl_pw_aff *pwaff2)
2595 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2598 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2599 __isl_take isl_pw_aff *pwaff2)
2601 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2604 /* Return a set containing those elements in the shared domain
2605 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2607 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2608 __isl_take isl_pw_aff *pwaff2)
2610 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2613 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2614 __isl_take isl_pw_aff *pwaff2)
2616 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2619 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2620 __isl_take isl_pw_aff *pwaff2)
2622 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2625 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2626 __isl_take isl_pw_aff *pwaff2)
2628 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2631 /* Return a set containing those elements in the shared domain
2632 * of the elements of list1 and list2 where each element in list1
2633 * has the relation specified by "fn" with each element in list2.
2635 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2636 __isl_take isl_pw_aff_list *list2,
2637 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2638 __isl_take isl_pw_aff *pwaff2))
2640 int i, j;
2641 isl_ctx *ctx;
2642 isl_set *set;
2644 if (!list1 || !list2)
2645 goto error;
2647 ctx = isl_pw_aff_list_get_ctx(list1);
2648 if (list1->n < 1 || list2->n < 1)
2649 isl_die(ctx, isl_error_invalid,
2650 "list should contain at least one element", goto error);
2652 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2653 for (i = 0; i < list1->n; ++i)
2654 for (j = 0; j < list2->n; ++j) {
2655 isl_set *set_ij;
2657 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2658 isl_pw_aff_copy(list2->p[j]));
2659 set = isl_set_intersect(set, set_ij);
2662 isl_pw_aff_list_free(list1);
2663 isl_pw_aff_list_free(list2);
2664 return set;
2665 error:
2666 isl_pw_aff_list_free(list1);
2667 isl_pw_aff_list_free(list2);
2668 return NULL;
2671 /* Return a set containing those elements in the shared domain
2672 * of the elements of list1 and list2 where each element in list1
2673 * is equal to each element in list2.
2675 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2676 __isl_take isl_pw_aff_list *list2)
2678 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2681 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2682 __isl_take isl_pw_aff_list *list2)
2684 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2687 /* Return a set containing those elements in the shared domain
2688 * of the elements of list1 and list2 where each element in list1
2689 * is less than or equal to each element in list2.
2691 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2692 __isl_take isl_pw_aff_list *list2)
2694 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2697 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2698 __isl_take isl_pw_aff_list *list2)
2700 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2703 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2704 __isl_take isl_pw_aff_list *list2)
2706 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2709 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2710 __isl_take isl_pw_aff_list *list2)
2712 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2716 /* Return a set containing those elements in the shared domain
2717 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2719 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2720 __isl_take isl_pw_aff *pwaff2)
2722 isl_set *set_lt, *set_gt;
2724 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2725 isl_pw_aff_copy(pwaff2));
2726 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2727 return isl_set_union_disjoint(set_lt, set_gt);
2730 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2731 __isl_take isl_pw_aff *pwaff2)
2733 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2736 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2737 isl_int v)
2739 int i;
2741 if (isl_int_is_one(v))
2742 return pwaff;
2743 if (!isl_int_is_pos(v))
2744 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2745 "factor needs to be positive",
2746 return isl_pw_aff_free(pwaff));
2747 pwaff = isl_pw_aff_cow(pwaff);
2748 if (!pwaff)
2749 return NULL;
2750 if (pwaff->n == 0)
2751 return pwaff;
2753 for (i = 0; i < pwaff->n; ++i) {
2754 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2755 if (!pwaff->p[i].aff)
2756 return isl_pw_aff_free(pwaff);
2759 return pwaff;
2762 /* Divide "pa" by "f".
2764 __isl_give isl_pw_aff *isl_pw_aff_scale_down_val(__isl_take isl_pw_aff *pa,
2765 __isl_take isl_val *f)
2767 int i;
2769 if (!pa || !f)
2770 goto error;
2772 if (isl_val_is_one(f)) {
2773 isl_val_free(f);
2774 return pa;
2777 if (!isl_val_is_rat(f))
2778 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2779 "expecting rational factor", goto error);
2780 if (!isl_val_is_pos(f))
2781 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2782 "factor needs to be positive", goto error);
2784 pa = isl_pw_aff_cow(pa);
2785 if (!pa)
2786 return NULL;
2787 if (pa->n == 0)
2788 return pa;
2790 for (i = 0; i < pa->n; ++i) {
2791 pa->p[i].aff = isl_aff_scale_down_val(pa->p[i].aff,
2792 isl_val_copy(f));
2793 if (!pa->p[i].aff)
2794 goto error;
2797 isl_val_free(f);
2798 return pa;
2799 error:
2800 isl_pw_aff_free(pa);
2801 isl_val_free(f);
2802 return NULL;
2805 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2807 int i;
2809 pwaff = isl_pw_aff_cow(pwaff);
2810 if (!pwaff)
2811 return NULL;
2812 if (pwaff->n == 0)
2813 return pwaff;
2815 for (i = 0; i < pwaff->n; ++i) {
2816 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2817 if (!pwaff->p[i].aff)
2818 return isl_pw_aff_free(pwaff);
2821 return pwaff;
2824 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2826 int i;
2828 pwaff = isl_pw_aff_cow(pwaff);
2829 if (!pwaff)
2830 return NULL;
2831 if (pwaff->n == 0)
2832 return pwaff;
2834 for (i = 0; i < pwaff->n; ++i) {
2835 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2836 if (!pwaff->p[i].aff)
2837 return isl_pw_aff_free(pwaff);
2840 return pwaff;
2843 /* Assuming that "cond1" and "cond2" are disjoint,
2844 * return an affine expression that is equal to pwaff1 on cond1
2845 * and to pwaff2 on cond2.
2847 static __isl_give isl_pw_aff *isl_pw_aff_select(
2848 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2849 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2851 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2852 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2854 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2857 /* Return an affine expression that is equal to pwaff_true for elements
2858 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2859 * is zero.
2860 * That is, return cond ? pwaff_true : pwaff_false;
2862 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2863 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2865 isl_set *cond_true, *cond_false;
2867 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2868 cond_false = isl_pw_aff_zero_set(cond);
2869 return isl_pw_aff_select(cond_true, pwaff_true,
2870 cond_false, pwaff_false);
2873 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2875 if (!aff)
2876 return -1;
2878 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2881 /* Check whether pwaff is a piecewise constant.
2883 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2885 int i;
2887 if (!pwaff)
2888 return -1;
2890 for (i = 0; i < pwaff->n; ++i) {
2891 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2892 if (is_cst < 0 || !is_cst)
2893 return is_cst;
2896 return 1;
2899 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2900 __isl_take isl_aff *aff2)
2902 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2903 return isl_aff_mul(aff2, aff1);
2905 if (!isl_aff_is_cst(aff2))
2906 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2907 "at least one affine expression should be constant",
2908 goto error);
2910 aff1 = isl_aff_cow(aff1);
2911 if (!aff1 || !aff2)
2912 goto error;
2914 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2915 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2917 isl_aff_free(aff2);
2918 return aff1;
2919 error:
2920 isl_aff_free(aff1);
2921 isl_aff_free(aff2);
2922 return NULL;
2925 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
2927 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2928 __isl_take isl_aff *aff2)
2930 int is_cst;
2931 int neg;
2933 is_cst = isl_aff_is_cst(aff2);
2934 if (is_cst < 0)
2935 goto error;
2936 if (!is_cst)
2937 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2938 "second argument should be a constant", goto error);
2940 if (!aff2)
2941 goto error;
2943 neg = isl_int_is_neg(aff2->v->el[1]);
2944 if (neg) {
2945 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2946 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2949 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2950 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2952 if (neg) {
2953 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2954 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2957 isl_aff_free(aff2);
2958 return aff1;
2959 error:
2960 isl_aff_free(aff1);
2961 isl_aff_free(aff2);
2962 return NULL;
2965 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2966 __isl_take isl_pw_aff *pwaff2)
2968 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2971 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2972 __isl_take isl_pw_aff *pwaff2)
2974 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2977 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2978 __isl_take isl_pw_aff *pwaff2)
2980 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2983 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2984 __isl_take isl_pw_aff *pwaff2)
2986 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2989 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2990 __isl_take isl_pw_aff *pwaff2)
2992 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2995 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2996 __isl_take isl_pw_aff *pa2)
2998 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3001 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3003 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3004 __isl_take isl_pw_aff *pa2)
3006 int is_cst;
3008 is_cst = isl_pw_aff_is_cst(pa2);
3009 if (is_cst < 0)
3010 goto error;
3011 if (!is_cst)
3012 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3013 "second argument should be a piecewise constant",
3014 goto error);
3015 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3016 error:
3017 isl_pw_aff_free(pa1);
3018 isl_pw_aff_free(pa2);
3019 return NULL;
3022 /* Compute the quotient of the integer division of "pa1" by "pa2"
3023 * with rounding towards zero.
3024 * "pa2" is assumed to be a piecewise constant.
3026 * In particular, return
3028 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3031 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3032 __isl_take isl_pw_aff *pa2)
3034 int is_cst;
3035 isl_set *cond;
3036 isl_pw_aff *f, *c;
3038 is_cst = isl_pw_aff_is_cst(pa2);
3039 if (is_cst < 0)
3040 goto error;
3041 if (!is_cst)
3042 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3043 "second argument should be a piecewise constant",
3044 goto error);
3046 pa1 = isl_pw_aff_div(pa1, pa2);
3048 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3049 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3050 c = isl_pw_aff_ceil(pa1);
3051 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3052 error:
3053 isl_pw_aff_free(pa1);
3054 isl_pw_aff_free(pa2);
3055 return NULL;
3058 /* Compute the remainder of the integer division of "pa1" by "pa2"
3059 * with rounding towards zero.
3060 * "pa2" is assumed to be a piecewise constant.
3062 * In particular, return
3064 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3067 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3068 __isl_take isl_pw_aff *pa2)
3070 int is_cst;
3071 isl_pw_aff *res;
3073 is_cst = isl_pw_aff_is_cst(pa2);
3074 if (is_cst < 0)
3075 goto error;
3076 if (!is_cst)
3077 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3078 "second argument should be a piecewise constant",
3079 goto error);
3080 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3081 res = isl_pw_aff_mul(pa2, res);
3082 res = isl_pw_aff_sub(pa1, res);
3083 return res;
3084 error:
3085 isl_pw_aff_free(pa1);
3086 isl_pw_aff_free(pa2);
3087 return NULL;
3090 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3091 __isl_take isl_pw_aff *pwaff2)
3093 isl_set *le;
3094 isl_set *dom;
3096 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3097 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3098 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3099 isl_pw_aff_copy(pwaff2));
3100 dom = isl_set_subtract(dom, isl_set_copy(le));
3101 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3104 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3105 __isl_take isl_pw_aff *pwaff2)
3107 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3110 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3111 __isl_take isl_pw_aff *pwaff2)
3113 isl_set *ge;
3114 isl_set *dom;
3116 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3117 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3118 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3119 isl_pw_aff_copy(pwaff2));
3120 dom = isl_set_subtract(dom, isl_set_copy(ge));
3121 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3124 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3125 __isl_take isl_pw_aff *pwaff2)
3127 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3130 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3131 __isl_take isl_pw_aff_list *list,
3132 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3133 __isl_take isl_pw_aff *pwaff2))
3135 int i;
3136 isl_ctx *ctx;
3137 isl_pw_aff *res;
3139 if (!list)
3140 return NULL;
3142 ctx = isl_pw_aff_list_get_ctx(list);
3143 if (list->n < 1)
3144 isl_die(ctx, isl_error_invalid,
3145 "list should contain at least one element", goto error);
3147 res = isl_pw_aff_copy(list->p[0]);
3148 for (i = 1; i < list->n; ++i)
3149 res = fn(res, isl_pw_aff_copy(list->p[i]));
3151 isl_pw_aff_list_free(list);
3152 return res;
3153 error:
3154 isl_pw_aff_list_free(list);
3155 return NULL;
3158 /* Return an isl_pw_aff that maps each element in the intersection of the
3159 * domains of the elements of list to the minimal corresponding affine
3160 * expression.
3162 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3164 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3167 /* Return an isl_pw_aff that maps each element in the intersection of the
3168 * domains of the elements of list to the maximal corresponding affine
3169 * expression.
3171 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3173 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3176 /* Mark the domains of "pwaff" as rational.
3178 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3180 int i;
3182 pwaff = isl_pw_aff_cow(pwaff);
3183 if (!pwaff)
3184 return NULL;
3185 if (pwaff->n == 0)
3186 return pwaff;
3188 for (i = 0; i < pwaff->n; ++i) {
3189 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3190 if (!pwaff->p[i].set)
3191 return isl_pw_aff_free(pwaff);
3194 return pwaff;
3197 /* Mark the domains of the elements of "list" as rational.
3199 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3200 __isl_take isl_pw_aff_list *list)
3202 int i, n;
3204 if (!list)
3205 return NULL;
3206 if (list->n == 0)
3207 return list;
3209 n = list->n;
3210 for (i = 0; i < n; ++i) {
3211 isl_pw_aff *pa;
3213 pa = isl_pw_aff_list_get_pw_aff(list, i);
3214 pa = isl_pw_aff_set_rational(pa);
3215 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3218 return list;
3221 /* Do the parameters of "aff" match those of "space"?
3223 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3224 __isl_keep isl_space *space)
3226 isl_space *aff_space;
3227 int match;
3229 if (!aff || !space)
3230 return -1;
3232 aff_space = isl_aff_get_domain_space(aff);
3234 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3236 isl_space_free(aff_space);
3237 return match;
3240 /* Check that the domain space of "aff" matches "space".
3242 * Return 0 on success and -1 on error.
3244 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3245 __isl_keep isl_space *space)
3247 isl_space *aff_space;
3248 int match;
3250 if (!aff || !space)
3251 return -1;
3253 aff_space = isl_aff_get_domain_space(aff);
3255 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3256 if (match < 0)
3257 goto error;
3258 if (!match)
3259 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3260 "parameters don't match", goto error);
3261 match = isl_space_tuple_match(space, isl_dim_in,
3262 aff_space, isl_dim_set);
3263 if (match < 0)
3264 goto error;
3265 if (!match)
3266 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3267 "domains don't match", goto error);
3268 isl_space_free(aff_space);
3269 return 0;
3270 error:
3271 isl_space_free(aff_space);
3272 return -1;
3275 #undef BASE
3276 #define BASE aff
3277 #define NO_INTERSECT_DOMAIN
3278 #define NO_DOMAIN
3280 #include <isl_multi_templ.c>
3282 #undef NO_DOMAIN
3283 #undef NO_INTERSECT_DOMAIN
3285 /* Remove any internal structure of the domain of "ma".
3286 * If there is any such internal structure in the input,
3287 * then the name of the corresponding space is also removed.
3289 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3290 __isl_take isl_multi_aff *ma)
3292 isl_space *space;
3294 if (!ma)
3295 return NULL;
3297 if (!ma->space->nested[0])
3298 return ma;
3300 space = isl_multi_aff_get_space(ma);
3301 space = isl_space_flatten_domain(space);
3302 ma = isl_multi_aff_reset_space(ma, space);
3304 return ma;
3307 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3308 * of the space to its domain.
3310 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3312 int i, n_in;
3313 isl_local_space *ls;
3314 isl_multi_aff *ma;
3316 if (!space)
3317 return NULL;
3318 if (!isl_space_is_map(space))
3319 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3320 "not a map space", goto error);
3322 n_in = isl_space_dim(space, isl_dim_in);
3323 space = isl_space_domain_map(space);
3325 ma = isl_multi_aff_alloc(isl_space_copy(space));
3326 if (n_in == 0) {
3327 isl_space_free(space);
3328 return ma;
3331 space = isl_space_domain(space);
3332 ls = isl_local_space_from_space(space);
3333 for (i = 0; i < n_in; ++i) {
3334 isl_aff *aff;
3336 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3337 isl_dim_set, i);
3338 ma = isl_multi_aff_set_aff(ma, i, aff);
3340 isl_local_space_free(ls);
3341 return ma;
3342 error:
3343 isl_space_free(space);
3344 return NULL;
3347 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3348 * of the space to its range.
3350 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3352 int i, n_in, n_out;
3353 isl_local_space *ls;
3354 isl_multi_aff *ma;
3356 if (!space)
3357 return NULL;
3358 if (!isl_space_is_map(space))
3359 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3360 "not a map space", goto error);
3362 n_in = isl_space_dim(space, isl_dim_in);
3363 n_out = isl_space_dim(space, isl_dim_out);
3364 space = isl_space_range_map(space);
3366 ma = isl_multi_aff_alloc(isl_space_copy(space));
3367 if (n_out == 0) {
3368 isl_space_free(space);
3369 return ma;
3372 space = isl_space_domain(space);
3373 ls = isl_local_space_from_space(space);
3374 for (i = 0; i < n_out; ++i) {
3375 isl_aff *aff;
3377 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3378 isl_dim_set, n_in + i);
3379 ma = isl_multi_aff_set_aff(ma, i, aff);
3381 isl_local_space_free(ls);
3382 return ma;
3383 error:
3384 isl_space_free(space);
3385 return NULL;
3388 /* Given the space of a set and a range of set dimensions,
3389 * construct an isl_multi_aff that projects out those dimensions.
3391 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3392 __isl_take isl_space *space, enum isl_dim_type type,
3393 unsigned first, unsigned n)
3395 int i, dim;
3396 isl_local_space *ls;
3397 isl_multi_aff *ma;
3399 if (!space)
3400 return NULL;
3401 if (!isl_space_is_set(space))
3402 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3403 "expecting set space", goto error);
3404 if (type != isl_dim_set)
3405 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3406 "only set dimensions can be projected out", goto error);
3408 dim = isl_space_dim(space, isl_dim_set);
3409 if (first + n > dim)
3410 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3411 "range out of bounds", goto error);
3413 space = isl_space_from_domain(space);
3414 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3416 if (dim == n)
3417 return isl_multi_aff_alloc(space);
3419 ma = isl_multi_aff_alloc(isl_space_copy(space));
3420 space = isl_space_domain(space);
3421 ls = isl_local_space_from_space(space);
3423 for (i = 0; i < first; ++i) {
3424 isl_aff *aff;
3426 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3427 isl_dim_set, i);
3428 ma = isl_multi_aff_set_aff(ma, i, aff);
3431 for (i = 0; i < dim - (first + n); ++i) {
3432 isl_aff *aff;
3434 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3435 isl_dim_set, first + n + i);
3436 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3439 isl_local_space_free(ls);
3440 return ma;
3441 error:
3442 isl_space_free(space);
3443 return NULL;
3446 /* Given the space of a set and a range of set dimensions,
3447 * construct an isl_pw_multi_aff that projects out those dimensions.
3449 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3450 __isl_take isl_space *space, enum isl_dim_type type,
3451 unsigned first, unsigned n)
3453 isl_multi_aff *ma;
3455 ma = isl_multi_aff_project_out_map(space, type, first, n);
3456 return isl_pw_multi_aff_from_multi_aff(ma);
3459 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3460 * domain.
3462 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3463 __isl_take isl_multi_aff *ma)
3465 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3466 return isl_pw_multi_aff_alloc(dom, ma);
3469 /* Create a piecewise multi-affine expression in the given space that maps each
3470 * input dimension to the corresponding output dimension.
3472 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3473 __isl_take isl_space *space)
3475 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3478 /* Add "ma2" to "ma1" and return the result.
3480 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3482 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3483 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3485 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3488 /* Add "ma2" to "ma1" and return the result.
3490 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3491 __isl_take isl_multi_aff *ma2)
3493 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3494 &isl_multi_aff_add_aligned);
3497 /* Subtract "ma2" from "ma1" and return the result.
3499 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3501 static __isl_give isl_multi_aff *isl_multi_aff_sub_aligned(
3502 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3504 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3507 /* Subtract "ma2" from "ma1" and return the result.
3509 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3510 __isl_take isl_multi_aff *ma2)
3512 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3513 &isl_multi_aff_sub_aligned);
3516 /* Exploit the equalities in "eq" to simplify the affine expressions.
3518 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3519 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3521 int i;
3523 maff = isl_multi_aff_cow(maff);
3524 if (!maff || !eq)
3525 goto error;
3527 for (i = 0; i < maff->n; ++i) {
3528 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3529 isl_basic_set_copy(eq));
3530 if (!maff->p[i])
3531 goto error;
3534 isl_basic_set_free(eq);
3535 return maff;
3536 error:
3537 isl_basic_set_free(eq);
3538 isl_multi_aff_free(maff);
3539 return NULL;
3542 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3543 isl_int f)
3545 int i;
3547 maff = isl_multi_aff_cow(maff);
3548 if (!maff)
3549 return NULL;
3551 for (i = 0; i < maff->n; ++i) {
3552 maff->p[i] = isl_aff_scale(maff->p[i], f);
3553 if (!maff->p[i])
3554 return isl_multi_aff_free(maff);
3557 return maff;
3560 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3561 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3563 maff1 = isl_multi_aff_add(maff1, maff2);
3564 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3565 return maff1;
3568 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3570 if (!maff)
3571 return -1;
3573 return 0;
3576 /* Return the set of domain elements where "ma1" is lexicographically
3577 * smaller than or equal to "ma2".
3579 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3580 __isl_take isl_multi_aff *ma2)
3582 return isl_multi_aff_lex_ge_set(ma2, ma1);
3585 /* Return the set of domain elements where "ma1" is lexicographically
3586 * greater than or equal to "ma2".
3588 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3589 __isl_take isl_multi_aff *ma2)
3591 isl_space *space;
3592 isl_map *map1, *map2;
3593 isl_map *map, *ge;
3595 map1 = isl_map_from_multi_aff(ma1);
3596 map2 = isl_map_from_multi_aff(ma2);
3597 map = isl_map_range_product(map1, map2);
3598 space = isl_space_range(isl_map_get_space(map));
3599 space = isl_space_domain(isl_space_unwrap(space));
3600 ge = isl_map_lex_ge(space);
3601 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3603 return isl_map_domain(map);
3606 #undef PW
3607 #define PW isl_pw_multi_aff
3608 #undef EL
3609 #define EL isl_multi_aff
3610 #undef EL_IS_ZERO
3611 #define EL_IS_ZERO is_empty
3612 #undef ZERO
3613 #define ZERO empty
3614 #undef IS_ZERO
3615 #define IS_ZERO is_empty
3616 #undef FIELD
3617 #define FIELD maff
3618 #undef DEFAULT_IS_ZERO
3619 #define DEFAULT_IS_ZERO 0
3621 #define NO_NEG
3622 #define NO_EVAL
3623 #define NO_OPT
3624 #define NO_INVOLVES_DIMS
3625 #define NO_INSERT_DIMS
3626 #define NO_LIFT
3627 #define NO_MORPH
3629 #include <isl_pw_templ.c>
3631 #undef UNION
3632 #define UNION isl_union_pw_multi_aff
3633 #undef PART
3634 #define PART isl_pw_multi_aff
3635 #undef PARTS
3636 #define PARTS pw_multi_aff
3637 #define ALIGN_DOMAIN
3639 #define NO_EVAL
3641 #include <isl_union_templ.c>
3643 /* Given a function "cmp" that returns the set of elements where
3644 * "ma1" is "better" than "ma2", return the intersection of this
3645 * set with "dom1" and "dom2".
3647 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3648 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3649 __isl_keep isl_multi_aff *ma2,
3650 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3651 __isl_take isl_multi_aff *ma2))
3653 isl_set *common;
3654 isl_set *better;
3655 int is_empty;
3657 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3658 is_empty = isl_set_plain_is_empty(common);
3659 if (is_empty >= 0 && is_empty)
3660 return common;
3661 if (is_empty < 0)
3662 return isl_set_free(common);
3663 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3664 better = isl_set_intersect(common, better);
3666 return better;
3669 /* Given a function "cmp" that returns the set of elements where
3670 * "ma1" is "better" than "ma2", return a piecewise multi affine
3671 * expression defined on the union of the definition domains
3672 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3673 * "pma2" on each cell. If only one of the two input functions
3674 * is defined on a given cell, then it is considered the best.
3676 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3677 __isl_take isl_pw_multi_aff *pma1,
3678 __isl_take isl_pw_multi_aff *pma2,
3679 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3680 __isl_take isl_multi_aff *ma2))
3682 int i, j, n;
3683 isl_pw_multi_aff *res = NULL;
3684 isl_ctx *ctx;
3685 isl_set *set = NULL;
3687 if (!pma1 || !pma2)
3688 goto error;
3690 ctx = isl_space_get_ctx(pma1->dim);
3691 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3692 isl_die(ctx, isl_error_invalid,
3693 "arguments should live in the same space", goto error);
3695 if (isl_pw_multi_aff_is_empty(pma1)) {
3696 isl_pw_multi_aff_free(pma1);
3697 return pma2;
3700 if (isl_pw_multi_aff_is_empty(pma2)) {
3701 isl_pw_multi_aff_free(pma2);
3702 return pma1;
3705 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3706 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3708 for (i = 0; i < pma1->n; ++i) {
3709 set = isl_set_copy(pma1->p[i].set);
3710 for (j = 0; j < pma2->n; ++j) {
3711 isl_set *better;
3712 int is_empty;
3714 better = shared_and_better(pma2->p[j].set,
3715 pma1->p[i].set, pma2->p[j].maff,
3716 pma1->p[i].maff, cmp);
3717 is_empty = isl_set_plain_is_empty(better);
3718 if (is_empty < 0 || is_empty) {
3719 isl_set_free(better);
3720 if (is_empty < 0)
3721 goto error;
3722 continue;
3724 set = isl_set_subtract(set, isl_set_copy(better));
3726 res = isl_pw_multi_aff_add_piece(res, better,
3727 isl_multi_aff_copy(pma2->p[j].maff));
3729 res = isl_pw_multi_aff_add_piece(res, set,
3730 isl_multi_aff_copy(pma1->p[i].maff));
3733 for (j = 0; j < pma2->n; ++j) {
3734 set = isl_set_copy(pma2->p[j].set);
3735 for (i = 0; i < pma1->n; ++i)
3736 set = isl_set_subtract(set,
3737 isl_set_copy(pma1->p[i].set));
3738 res = isl_pw_multi_aff_add_piece(res, set,
3739 isl_multi_aff_copy(pma2->p[j].maff));
3742 isl_pw_multi_aff_free(pma1);
3743 isl_pw_multi_aff_free(pma2);
3745 return res;
3746 error:
3747 isl_pw_multi_aff_free(pma1);
3748 isl_pw_multi_aff_free(pma2);
3749 isl_set_free(set);
3750 return isl_pw_multi_aff_free(res);
3753 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3754 __isl_take isl_pw_multi_aff *pma1,
3755 __isl_take isl_pw_multi_aff *pma2)
3757 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3760 /* Given two piecewise multi affine expressions, return a piecewise
3761 * multi-affine expression defined on the union of the definition domains
3762 * of the inputs that is equal to the lexicographic maximum of the two
3763 * inputs on each cell. If only one of the two inputs is defined on
3764 * a given cell, then it is considered to be the maximum.
3766 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3767 __isl_take isl_pw_multi_aff *pma1,
3768 __isl_take isl_pw_multi_aff *pma2)
3770 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3771 &pw_multi_aff_union_lexmax);
3774 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3775 __isl_take isl_pw_multi_aff *pma1,
3776 __isl_take isl_pw_multi_aff *pma2)
3778 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3781 /* Given two piecewise multi affine expressions, return a piecewise
3782 * multi-affine expression defined on the union of the definition domains
3783 * of the inputs that is equal to the lexicographic minimum of the two
3784 * inputs on each cell. If only one of the two inputs is defined on
3785 * a given cell, then it is considered to be the minimum.
3787 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3788 __isl_take isl_pw_multi_aff *pma1,
3789 __isl_take isl_pw_multi_aff *pma2)
3791 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3792 &pw_multi_aff_union_lexmin);
3795 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3796 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3798 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3799 &isl_multi_aff_add);
3802 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3803 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3805 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3806 &pw_multi_aff_add);
3809 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3810 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3812 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3813 &isl_multi_aff_sub);
3816 /* Subtract "pma2" from "pma1" and return the result.
3818 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3819 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3821 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3822 &pw_multi_aff_sub);
3825 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3826 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3828 return isl_pw_multi_aff_union_add_(pma1, pma2);
3831 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3832 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3834 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3835 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3837 int i, j, n;
3838 isl_space *space;
3839 isl_pw_multi_aff *res;
3841 if (!pma1 || !pma2)
3842 goto error;
3844 n = pma1->n * pma2->n;
3845 space = isl_space_product(isl_space_copy(pma1->dim),
3846 isl_space_copy(pma2->dim));
3847 res = isl_pw_multi_aff_alloc_size(space, n);
3849 for (i = 0; i < pma1->n; ++i) {
3850 for (j = 0; j < pma2->n; ++j) {
3851 isl_set *domain;
3852 isl_multi_aff *ma;
3854 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3855 isl_set_copy(pma2->p[j].set));
3856 ma = isl_multi_aff_product(
3857 isl_multi_aff_copy(pma1->p[i].maff),
3858 isl_multi_aff_copy(pma2->p[j].maff));
3859 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3863 isl_pw_multi_aff_free(pma1);
3864 isl_pw_multi_aff_free(pma2);
3865 return res;
3866 error:
3867 isl_pw_multi_aff_free(pma1);
3868 isl_pw_multi_aff_free(pma2);
3869 return NULL;
3872 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3873 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3875 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3876 &pw_multi_aff_product);
3879 /* Construct a map mapping the domain of the piecewise multi-affine expression
3880 * to its range, with each dimension in the range equated to the
3881 * corresponding affine expression on its cell.
3883 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3885 int i;
3886 isl_map *map;
3888 if (!pma)
3889 return NULL;
3891 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3893 for (i = 0; i < pma->n; ++i) {
3894 isl_multi_aff *maff;
3895 isl_basic_map *bmap;
3896 isl_map *map_i;
3898 maff = isl_multi_aff_copy(pma->p[i].maff);
3899 bmap = isl_basic_map_from_multi_aff(maff);
3900 map_i = isl_map_from_basic_map(bmap);
3901 map_i = isl_map_intersect_domain(map_i,
3902 isl_set_copy(pma->p[i].set));
3903 map = isl_map_union_disjoint(map, map_i);
3906 isl_pw_multi_aff_free(pma);
3907 return map;
3910 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3912 if (!pma)
3913 return NULL;
3915 if (!isl_space_is_set(pma->dim))
3916 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3917 "isl_pw_multi_aff cannot be converted into an isl_set",
3918 goto error);
3920 return isl_map_from_pw_multi_aff(pma);
3921 error:
3922 isl_pw_multi_aff_free(pma);
3923 return NULL;
3926 /* Given a basic map with a single output dimension that is defined
3927 * in terms of the parameters and input dimensions using an equality,
3928 * extract an isl_aff that expresses the output dimension in terms
3929 * of the parameters and input dimensions.
3931 * Since some applications expect the result of isl_pw_multi_aff_from_map
3932 * to only contain integer affine expressions, we compute the floor
3933 * of the expression before returning.
3935 * This function shares some similarities with
3936 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3938 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3939 __isl_take isl_basic_map *bmap)
3941 int i;
3942 unsigned offset;
3943 unsigned total;
3944 isl_local_space *ls;
3945 isl_aff *aff;
3947 if (!bmap)
3948 return NULL;
3949 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3950 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3951 "basic map should have a single output dimension",
3952 goto error);
3953 offset = isl_basic_map_offset(bmap, isl_dim_out);
3954 total = isl_basic_map_total_dim(bmap);
3955 for (i = 0; i < bmap->n_eq; ++i) {
3956 if (isl_int_is_zero(bmap->eq[i][offset]))
3957 continue;
3958 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3959 1 + total - (offset + 1)) != -1)
3960 continue;
3961 break;
3963 if (i >= bmap->n_eq)
3964 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3965 "unable to find suitable equality", goto error);
3966 ls = isl_basic_map_get_local_space(bmap);
3967 aff = isl_aff_alloc(isl_local_space_domain(ls));
3968 if (!aff)
3969 goto error;
3970 if (isl_int_is_neg(bmap->eq[i][offset]))
3971 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3972 else
3973 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3974 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3975 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3976 isl_basic_map_free(bmap);
3978 aff = isl_aff_remove_unused_divs(aff);
3979 aff = isl_aff_floor(aff);
3980 return aff;
3981 error:
3982 isl_basic_map_free(bmap);
3983 return NULL;
3986 /* Given a basic map where each output dimension is defined
3987 * in terms of the parameters and input dimensions using an equality,
3988 * extract an isl_multi_aff that expresses the output dimensions in terms
3989 * of the parameters and input dimensions.
3991 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3992 __isl_take isl_basic_map *bmap)
3994 int i;
3995 unsigned n_out;
3996 isl_multi_aff *ma;
3998 if (!bmap)
3999 return NULL;
4001 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4002 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4004 for (i = 0; i < n_out; ++i) {
4005 isl_basic_map *bmap_i;
4006 isl_aff *aff;
4008 bmap_i = isl_basic_map_copy(bmap);
4009 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4010 i + 1, n_out - (1 + i));
4011 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4012 aff = extract_isl_aff_from_basic_map(bmap_i);
4013 ma = isl_multi_aff_set_aff(ma, i, aff);
4016 isl_basic_map_free(bmap);
4018 return ma;
4021 /* Create an isl_pw_multi_aff that is equivalent to
4022 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4023 * The given basic map is such that each output dimension is defined
4024 * in terms of the parameters and input dimensions using an equality.
4026 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4027 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4029 isl_multi_aff *ma;
4031 ma = extract_isl_multi_aff_from_basic_map(bmap);
4032 return isl_pw_multi_aff_alloc(domain, ma);
4035 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4036 * This obviously only works if the input "map" is single-valued.
4037 * If so, we compute the lexicographic minimum of the image in the form
4038 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4039 * to its lexicographic minimum.
4040 * If the input is not single-valued, we produce an error.
4042 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4043 __isl_take isl_map *map)
4045 int i;
4046 int sv;
4047 isl_pw_multi_aff *pma;
4049 sv = isl_map_is_single_valued(map);
4050 if (sv < 0)
4051 goto error;
4052 if (!sv)
4053 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4054 "map is not single-valued", goto error);
4055 map = isl_map_make_disjoint(map);
4056 if (!map)
4057 return NULL;
4059 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4061 for (i = 0; i < map->n; ++i) {
4062 isl_pw_multi_aff *pma_i;
4063 isl_basic_map *bmap;
4064 bmap = isl_basic_map_copy(map->p[i]);
4065 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4066 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4069 isl_map_free(map);
4070 return pma;
4071 error:
4072 isl_map_free(map);
4073 return NULL;
4076 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4077 * taking into account that the output dimension at position "d"
4078 * can be represented as
4080 * x = floor((e(...) + c1) / m)
4082 * given that constraint "i" is of the form
4084 * e(...) + c1 - m x >= 0
4087 * Let "map" be of the form
4089 * A -> B
4091 * We construct a mapping
4093 * A -> [A -> x = floor(...)]
4095 * apply that to the map, obtaining
4097 * [A -> x = floor(...)] -> B
4099 * and equate dimension "d" to x.
4100 * We then compute a isl_pw_multi_aff representation of the resulting map
4101 * and plug in the mapping above.
4103 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4104 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4106 isl_ctx *ctx;
4107 isl_space *space;
4108 isl_local_space *ls;
4109 isl_multi_aff *ma;
4110 isl_aff *aff;
4111 isl_vec *v;
4112 isl_map *insert;
4113 int offset;
4114 int n;
4115 int n_in;
4116 isl_pw_multi_aff *pma;
4117 int is_set;
4119 is_set = isl_map_is_set(map);
4121 offset = isl_basic_map_offset(hull, isl_dim_out);
4122 ctx = isl_map_get_ctx(map);
4123 space = isl_space_domain(isl_map_get_space(map));
4124 n_in = isl_space_dim(space, isl_dim_set);
4125 n = isl_space_dim(space, isl_dim_all);
4127 v = isl_vec_alloc(ctx, 1 + 1 + n);
4128 if (v) {
4129 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4130 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4132 isl_basic_map_free(hull);
4134 ls = isl_local_space_from_space(isl_space_copy(space));
4135 aff = isl_aff_alloc_vec(ls, v);
4136 aff = isl_aff_floor(aff);
4137 if (is_set) {
4138 isl_space_free(space);
4139 ma = isl_multi_aff_from_aff(aff);
4140 } else {
4141 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4142 ma = isl_multi_aff_range_product(ma,
4143 isl_multi_aff_from_aff(aff));
4146 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4147 map = isl_map_apply_domain(map, insert);
4148 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4149 pma = isl_pw_multi_aff_from_map(map);
4150 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4152 return pma;
4155 /* Is constraint "c" of the form
4157 * e(...) + c1 - m x >= 0
4159 * or
4161 * -e(...) + c2 + m x >= 0
4163 * where m > 1 and e only depends on parameters and input dimemnsions?
4165 * "offset" is the offset of the output dimensions
4166 * "pos" is the position of output dimension x.
4168 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4170 if (isl_int_is_zero(c[offset + d]))
4171 return 0;
4172 if (isl_int_is_one(c[offset + d]))
4173 return 0;
4174 if (isl_int_is_negone(c[offset + d]))
4175 return 0;
4176 if (isl_seq_first_non_zero(c + offset, d) != -1)
4177 return 0;
4178 if (isl_seq_first_non_zero(c + offset + d + 1,
4179 total - (offset + d + 1)) != -1)
4180 return 0;
4181 return 1;
4184 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4186 * As a special case, we first check if there is any pair of constraints,
4187 * shared by all the basic maps in "map" that force a given dimension
4188 * to be equal to the floor of some affine combination of the input dimensions.
4190 * In particular, if we can find two constraints
4192 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4194 * and
4196 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4198 * where m > 1 and e only depends on parameters and input dimemnsions,
4199 * and such that
4201 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4203 * then we know that we can take
4205 * x = floor((e(...) + c1) / m)
4207 * without having to perform any computation.
4209 * Note that we know that
4211 * c1 + c2 >= 1
4213 * If c1 + c2 were 0, then we would have detected an equality during
4214 * simplification. If c1 + c2 were negative, then we would have detected
4215 * a contradiction.
4217 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4218 __isl_take isl_map *map)
4220 int d, dim;
4221 int i, j, n;
4222 int offset, total;
4223 isl_int sum;
4224 isl_basic_map *hull;
4226 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4227 if (!hull)
4228 goto error;
4230 isl_int_init(sum);
4231 dim = isl_map_dim(map, isl_dim_out);
4232 offset = isl_basic_map_offset(hull, isl_dim_out);
4233 total = 1 + isl_basic_map_total_dim(hull);
4234 n = hull->n_ineq;
4235 for (d = 0; d < dim; ++d) {
4236 for (i = 0; i < n; ++i) {
4237 if (!is_potential_div_constraint(hull->ineq[i],
4238 offset, d, total))
4239 continue;
4240 for (j = i + 1; j < n; ++j) {
4241 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4242 hull->ineq[j] + 1, total - 1))
4243 continue;
4244 isl_int_add(sum, hull->ineq[i][0],
4245 hull->ineq[j][0]);
4246 if (isl_int_abs_lt(sum,
4247 hull->ineq[i][offset + d]))
4248 break;
4251 if (j >= n)
4252 continue;
4253 isl_int_clear(sum);
4254 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4255 j = i;
4256 return pw_multi_aff_from_map_div(map, hull, d, j);
4259 isl_int_clear(sum);
4260 isl_basic_map_free(hull);
4261 return pw_multi_aff_from_map_base(map);
4262 error:
4263 isl_map_free(map);
4264 isl_basic_map_free(hull);
4265 return NULL;
4268 /* Given an affine expression
4270 * [A -> B] -> f(A,B)
4272 * construct an isl_multi_aff
4274 * [A -> B] -> B'
4276 * such that dimension "d" in B' is set to "aff" and the remaining
4277 * dimensions are set equal to the corresponding dimensions in B.
4278 * "n_in" is the dimension of the space A.
4279 * "n_out" is the dimension of the space B.
4281 * If "is_set" is set, then the affine expression is of the form
4283 * [B] -> f(B)
4285 * and we construct an isl_multi_aff
4287 * B -> B'
4289 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4290 unsigned n_in, unsigned n_out, int is_set)
4292 int i;
4293 isl_multi_aff *ma;
4294 isl_space *space, *space2;
4295 isl_local_space *ls;
4297 space = isl_aff_get_domain_space(aff);
4298 ls = isl_local_space_from_space(isl_space_copy(space));
4299 space2 = isl_space_copy(space);
4300 if (!is_set)
4301 space2 = isl_space_range(isl_space_unwrap(space2));
4302 space = isl_space_map_from_domain_and_range(space, space2);
4303 ma = isl_multi_aff_alloc(space);
4304 ma = isl_multi_aff_set_aff(ma, d, aff);
4306 for (i = 0; i < n_out; ++i) {
4307 if (i == d)
4308 continue;
4309 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4310 isl_dim_set, n_in + i);
4311 ma = isl_multi_aff_set_aff(ma, i, aff);
4314 isl_local_space_free(ls);
4316 return ma;
4319 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4320 * taking into account that the dimension at position "d" can be written as
4322 * x = m a + f(..) (1)
4324 * where m is equal to "gcd".
4325 * "i" is the index of the equality in "hull" that defines f(..).
4326 * In particular, the equality is of the form
4328 * f(..) - x + m g(existentials) = 0
4330 * or
4332 * -f(..) + x + m g(existentials) = 0
4334 * We basically plug (1) into "map", resulting in a map with "a"
4335 * in the range instead of "x". The corresponding isl_pw_multi_aff
4336 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4338 * Specifically, given the input map
4340 * A -> B
4342 * We first wrap it into a set
4344 * [A -> B]
4346 * and define (1) on top of the corresponding space, resulting in "aff".
4347 * We use this to create an isl_multi_aff that maps the output position "d"
4348 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4349 * We plug this into the wrapped map, unwrap the result and compute the
4350 * corresponding isl_pw_multi_aff.
4351 * The result is an expression
4353 * A -> T(A)
4355 * We adjust that to
4357 * A -> [A -> T(A)]
4359 * so that we can plug that into "aff", after extending the latter to
4360 * a mapping
4362 * [A -> B] -> B'
4365 * If "map" is actually a set, then there is no "A" space, meaning
4366 * that we do not need to perform any wrapping, and that the result
4367 * of the recursive call is of the form
4369 * [T]
4371 * which is plugged into a mapping of the form
4373 * B -> B'
4375 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4376 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4377 isl_int gcd)
4379 isl_set *set;
4380 isl_space *space;
4381 isl_local_space *ls;
4382 isl_aff *aff;
4383 isl_multi_aff *ma;
4384 isl_pw_multi_aff *pma, *id;
4385 unsigned n_in;
4386 unsigned o_out;
4387 unsigned n_out;
4388 int is_set;
4390 is_set = isl_map_is_set(map);
4392 n_in = isl_basic_map_dim(hull, isl_dim_in);
4393 n_out = isl_basic_map_dim(hull, isl_dim_out);
4394 o_out = isl_basic_map_offset(hull, isl_dim_out);
4396 if (is_set)
4397 set = map;
4398 else
4399 set = isl_map_wrap(map);
4400 space = isl_space_map_from_set(isl_set_get_space(set));
4401 ma = isl_multi_aff_identity(space);
4402 ls = isl_local_space_from_space(isl_set_get_space(set));
4403 aff = isl_aff_alloc(ls);
4404 if (aff) {
4405 isl_int_set_si(aff->v->el[0], 1);
4406 if (isl_int_is_one(hull->eq[i][o_out + d]))
4407 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4408 aff->v->size - 1);
4409 else
4410 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4411 aff->v->size - 1);
4412 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4414 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4415 set = isl_set_preimage_multi_aff(set, ma);
4417 ma = range_map(aff, d, n_in, n_out, is_set);
4419 if (is_set)
4420 map = set;
4421 else
4422 map = isl_set_unwrap(set);
4423 pma = isl_pw_multi_aff_from_map(set);
4425 if (!is_set) {
4426 space = isl_pw_multi_aff_get_domain_space(pma);
4427 space = isl_space_map_from_set(space);
4428 id = isl_pw_multi_aff_identity(space);
4429 pma = isl_pw_multi_aff_range_product(id, pma);
4431 id = isl_pw_multi_aff_from_multi_aff(ma);
4432 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4434 isl_basic_map_free(hull);
4435 return pma;
4438 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4440 * As a special case, we first check if all output dimensions are uniquely
4441 * defined in terms of the parameters and input dimensions over the entire
4442 * domain. If so, we extract the desired isl_pw_multi_aff directly
4443 * from the affine hull of "map" and its domain.
4445 * Otherwise, we check if any of the output dimensions is "strided".
4446 * That is, we check if can be written as
4448 * x = m a + f(..)
4450 * with m greater than 1, a some combination of existentiall quantified
4451 * variables and f and expression in the parameters and input dimensions.
4452 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4454 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4455 * special case.
4457 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4459 int i, j;
4460 int sv;
4461 isl_basic_map *hull;
4462 unsigned n_out;
4463 unsigned o_out;
4464 unsigned n_div;
4465 unsigned o_div;
4466 isl_int gcd;
4468 if (!map)
4469 return NULL;
4471 hull = isl_map_affine_hull(isl_map_copy(map));
4472 sv = isl_basic_map_plain_is_single_valued(hull);
4473 if (sv >= 0 && sv)
4474 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4475 if (sv < 0)
4476 hull = isl_basic_map_free(hull);
4477 if (!hull)
4478 goto error;
4480 n_div = isl_basic_map_dim(hull, isl_dim_div);
4481 o_div = isl_basic_map_offset(hull, isl_dim_div);
4483 if (n_div == 0) {
4484 isl_basic_map_free(hull);
4485 return pw_multi_aff_from_map_check_div(map);
4488 isl_int_init(gcd);
4490 n_out = isl_basic_map_dim(hull, isl_dim_out);
4491 o_out = isl_basic_map_offset(hull, isl_dim_out);
4493 for (i = 0; i < n_out; ++i) {
4494 for (j = 0; j < hull->n_eq; ++j) {
4495 isl_int *eq = hull->eq[j];
4496 isl_pw_multi_aff *res;
4498 if (!isl_int_is_one(eq[o_out + i]) &&
4499 !isl_int_is_negone(eq[o_out + i]))
4500 continue;
4501 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4502 continue;
4503 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4504 n_out - (i + 1)) != -1)
4505 continue;
4506 isl_seq_gcd(eq + o_div, n_div, &gcd);
4507 if (isl_int_is_zero(gcd))
4508 continue;
4509 if (isl_int_is_one(gcd))
4510 continue;
4512 res = pw_multi_aff_from_map_stride(map, hull,
4513 i, j, gcd);
4514 isl_int_clear(gcd);
4515 return res;
4519 isl_int_clear(gcd);
4520 isl_basic_map_free(hull);
4521 return pw_multi_aff_from_map_check_div(map);
4522 error:
4523 isl_map_free(map);
4524 return NULL;
4527 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4529 return isl_pw_multi_aff_from_map(set);
4532 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4533 * add it to *user.
4535 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4537 isl_union_pw_multi_aff **upma = user;
4538 isl_pw_multi_aff *pma;
4540 pma = isl_pw_multi_aff_from_map(map);
4541 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4543 return *upma ? 0 : -1;
4546 /* Try and create an isl_union_pw_multi_aff that is equivalent
4547 * to the given isl_union_map.
4548 * The isl_union_map is required to be single-valued in each space.
4549 * Otherwise, an error is produced.
4551 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4552 __isl_take isl_union_map *umap)
4554 isl_space *space;
4555 isl_union_pw_multi_aff *upma;
4557 space = isl_union_map_get_space(umap);
4558 upma = isl_union_pw_multi_aff_empty(space);
4559 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4560 upma = isl_union_pw_multi_aff_free(upma);
4561 isl_union_map_free(umap);
4563 return upma;
4566 /* Try and create an isl_union_pw_multi_aff that is equivalent
4567 * to the given isl_union_set.
4568 * The isl_union_set is required to be a singleton in each space.
4569 * Otherwise, an error is produced.
4571 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4572 __isl_take isl_union_set *uset)
4574 return isl_union_pw_multi_aff_from_union_map(uset);
4577 /* Return the piecewise affine expression "set ? 1 : 0".
4579 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4581 isl_pw_aff *pa;
4582 isl_space *space = isl_set_get_space(set);
4583 isl_local_space *ls = isl_local_space_from_space(space);
4584 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4585 isl_aff *one = isl_aff_zero_on_domain(ls);
4587 one = isl_aff_add_constant_si(one, 1);
4588 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4589 set = isl_set_complement(set);
4590 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4592 return pa;
4595 /* Plug in "subs" for dimension "type", "pos" of "aff".
4597 * Let i be the dimension to replace and let "subs" be of the form
4599 * f/d
4601 * and "aff" of the form
4603 * (a i + g)/m
4605 * The result is
4607 * (a f + d g')/(m d)
4609 * where g' is the result of plugging in "subs" in each of the integer
4610 * divisions in g.
4612 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4613 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4615 isl_ctx *ctx;
4616 isl_int v;
4618 aff = isl_aff_cow(aff);
4619 if (!aff || !subs)
4620 return isl_aff_free(aff);
4622 ctx = isl_aff_get_ctx(aff);
4623 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4624 isl_die(ctx, isl_error_invalid,
4625 "spaces don't match", return isl_aff_free(aff));
4626 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4627 isl_die(ctx, isl_error_unsupported,
4628 "cannot handle divs yet", return isl_aff_free(aff));
4630 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4631 if (!aff->ls)
4632 return isl_aff_free(aff);
4634 aff->v = isl_vec_cow(aff->v);
4635 if (!aff->v)
4636 return isl_aff_free(aff);
4638 pos += isl_local_space_offset(aff->ls, type);
4640 isl_int_init(v);
4641 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4642 aff->v->size, subs->v->size, v);
4643 isl_int_clear(v);
4645 return aff;
4648 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4649 * expressions in "maff".
4651 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4652 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4653 __isl_keep isl_aff *subs)
4655 int i;
4657 maff = isl_multi_aff_cow(maff);
4658 if (!maff || !subs)
4659 return isl_multi_aff_free(maff);
4661 if (type == isl_dim_in)
4662 type = isl_dim_set;
4664 for (i = 0; i < maff->n; ++i) {
4665 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4666 if (!maff->p[i])
4667 return isl_multi_aff_free(maff);
4670 return maff;
4673 /* Plug in "subs" for dimension "type", "pos" of "pma".
4675 * pma is of the form
4677 * A_i(v) -> M_i(v)
4679 * while subs is of the form
4681 * v' = B_j(v) -> S_j
4683 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4684 * has a contribution in the result, in particular
4686 * C_ij(S_j) -> M_i(S_j)
4688 * Note that plugging in S_j in C_ij may also result in an empty set
4689 * and this contribution should simply be discarded.
4691 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4692 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4693 __isl_keep isl_pw_aff *subs)
4695 int i, j, n;
4696 isl_pw_multi_aff *res;
4698 if (!pma || !subs)
4699 return isl_pw_multi_aff_free(pma);
4701 n = pma->n * subs->n;
4702 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4704 for (i = 0; i < pma->n; ++i) {
4705 for (j = 0; j < subs->n; ++j) {
4706 isl_set *common;
4707 isl_multi_aff *res_ij;
4708 int empty;
4710 common = isl_set_intersect(
4711 isl_set_copy(pma->p[i].set),
4712 isl_set_copy(subs->p[j].set));
4713 common = isl_set_substitute(common,
4714 type, pos, subs->p[j].aff);
4715 empty = isl_set_plain_is_empty(common);
4716 if (empty < 0 || empty) {
4717 isl_set_free(common);
4718 if (empty < 0)
4719 goto error;
4720 continue;
4723 res_ij = isl_multi_aff_substitute(
4724 isl_multi_aff_copy(pma->p[i].maff),
4725 type, pos, subs->p[j].aff);
4727 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4731 isl_pw_multi_aff_free(pma);
4732 return res;
4733 error:
4734 isl_pw_multi_aff_free(pma);
4735 isl_pw_multi_aff_free(res);
4736 return NULL;
4739 /* Compute the preimage of a range of dimensions in the affine expression "src"
4740 * under "ma" and put the result in "dst". The number of dimensions in "src"
4741 * that precede the range is given by "n_before". The number of dimensions
4742 * in the range is given by the number of output dimensions of "ma".
4743 * The number of dimensions that follow the range is given by "n_after".
4744 * If "has_denom" is set (to one),
4745 * then "src" and "dst" have an extra initial denominator.
4746 * "n_div_ma" is the number of existentials in "ma"
4747 * "n_div_bset" is the number of existentials in "src"
4748 * The resulting "dst" (which is assumed to have been allocated by
4749 * the caller) contains coefficients for both sets of existentials,
4750 * first those in "ma" and then those in "src".
4751 * f, c1, c2 and g are temporary objects that have been initialized
4752 * by the caller.
4754 * Let src represent the expression
4756 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4758 * and let ma represent the expressions
4760 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4762 * We start out with the following expression for dst:
4764 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4766 * with the multiplication factor f initially equal to 1
4767 * and f \sum_i b_i v_i kept separately.
4768 * For each x_i that we substitute, we multiply the numerator
4769 * (and denominator) of dst by c_1 = m_i and add the numerator
4770 * of the x_i expression multiplied by c_2 = f b_i,
4771 * after removing the common factors of c_1 and c_2.
4772 * The multiplication factor f also needs to be multiplied by c_1
4773 * for the next x_j, j > i.
4775 void isl_seq_preimage(isl_int *dst, isl_int *src,
4776 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4777 int n_div_ma, int n_div_bmap,
4778 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4780 int i;
4781 int n_param, n_in, n_out;
4782 int o_dst, o_src;
4784 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4785 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4786 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4788 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4789 o_dst = o_src = has_denom + 1 + n_param + n_before;
4790 isl_seq_clr(dst + o_dst, n_in);
4791 o_dst += n_in;
4792 o_src += n_out;
4793 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4794 o_dst += n_after;
4795 o_src += n_after;
4796 isl_seq_clr(dst + o_dst, n_div_ma);
4797 o_dst += n_div_ma;
4798 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4800 isl_int_set_si(f, 1);
4802 for (i = 0; i < n_out; ++i) {
4803 int offset = has_denom + 1 + n_param + n_before + i;
4805 if (isl_int_is_zero(src[offset]))
4806 continue;
4807 isl_int_set(c1, ma->p[i]->v->el[0]);
4808 isl_int_mul(c2, f, src[offset]);
4809 isl_int_gcd(g, c1, c2);
4810 isl_int_divexact(c1, c1, g);
4811 isl_int_divexact(c2, c2, g);
4813 isl_int_mul(f, f, c1);
4814 o_dst = has_denom;
4815 o_src = 1;
4816 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4817 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4818 o_dst += 1 + n_param;
4819 o_src += 1 + n_param;
4820 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4821 o_dst += n_before;
4822 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4823 c2, ma->p[i]->v->el + o_src, n_in);
4824 o_dst += n_in;
4825 o_src += n_in;
4826 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4827 o_dst += n_after;
4828 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4829 c2, ma->p[i]->v->el + o_src, n_div_ma);
4830 o_dst += n_div_ma;
4831 o_src += n_div_ma;
4832 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4833 if (has_denom)
4834 isl_int_mul(dst[0], dst[0], c1);
4838 /* Compute the pullback of "aff" by the function represented by "ma".
4839 * In other words, plug in "ma" in "aff". The result is an affine expression
4840 * defined over the domain space of "ma".
4842 * If "aff" is represented by
4844 * (a(p) + b x + c(divs))/d
4846 * and ma is represented by
4848 * x = D(p) + F(y) + G(divs')
4850 * then the result is
4852 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4854 * The divs in the local space of the input are similarly adjusted
4855 * through a call to isl_local_space_preimage_multi_aff.
4857 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4858 __isl_take isl_multi_aff *ma)
4860 isl_aff *res = NULL;
4861 isl_local_space *ls;
4862 int n_div_aff, n_div_ma;
4863 isl_int f, c1, c2, g;
4865 ma = isl_multi_aff_align_divs(ma);
4866 if (!aff || !ma)
4867 goto error;
4869 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4870 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4872 ls = isl_aff_get_domain_local_space(aff);
4873 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4874 res = isl_aff_alloc(ls);
4875 if (!res)
4876 goto error;
4878 isl_int_init(f);
4879 isl_int_init(c1);
4880 isl_int_init(c2);
4881 isl_int_init(g);
4883 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4884 f, c1, c2, g, 1);
4886 isl_int_clear(f);
4887 isl_int_clear(c1);
4888 isl_int_clear(c2);
4889 isl_int_clear(g);
4891 isl_aff_free(aff);
4892 isl_multi_aff_free(ma);
4893 res = isl_aff_normalize(res);
4894 return res;
4895 error:
4896 isl_aff_free(aff);
4897 isl_multi_aff_free(ma);
4898 isl_aff_free(res);
4899 return NULL;
4902 /* Compute the pullback of "aff1" by the function represented by "aff2".
4903 * In other words, plug in "aff2" in "aff1". The result is an affine expression
4904 * defined over the domain space of "aff1".
4906 * The domain of "aff1" should match the range of "aff2", which means
4907 * that it should be single-dimensional.
4909 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
4910 __isl_take isl_aff *aff2)
4912 isl_multi_aff *ma;
4914 ma = isl_multi_aff_from_aff(aff2);
4915 return isl_aff_pullback_multi_aff(aff1, ma);
4918 /* Compute the pullback of "ma1" by the function represented by "ma2".
4919 * In other words, plug in "ma2" in "ma1".
4921 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
4923 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
4924 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4926 int i;
4927 isl_space *space = NULL;
4929 ma2 = isl_multi_aff_align_divs(ma2);
4930 ma1 = isl_multi_aff_cow(ma1);
4931 if (!ma1 || !ma2)
4932 goto error;
4934 space = isl_space_join(isl_multi_aff_get_space(ma2),
4935 isl_multi_aff_get_space(ma1));
4937 for (i = 0; i < ma1->n; ++i) {
4938 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4939 isl_multi_aff_copy(ma2));
4940 if (!ma1->p[i])
4941 goto error;
4944 ma1 = isl_multi_aff_reset_space(ma1, space);
4945 isl_multi_aff_free(ma2);
4946 return ma1;
4947 error:
4948 isl_space_free(space);
4949 isl_multi_aff_free(ma2);
4950 isl_multi_aff_free(ma1);
4951 return NULL;
4954 /* Compute the pullback of "ma1" by the function represented by "ma2".
4955 * In other words, plug in "ma2" in "ma1".
4957 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4958 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4960 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
4961 &isl_multi_aff_pullback_multi_aff_aligned);
4964 /* Extend the local space of "dst" to include the divs
4965 * in the local space of "src".
4967 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4968 __isl_keep isl_aff *src)
4970 isl_ctx *ctx;
4971 int *exp1 = NULL;
4972 int *exp2 = NULL;
4973 isl_mat *div;
4975 if (!src || !dst)
4976 return isl_aff_free(dst);
4978 ctx = isl_aff_get_ctx(src);
4979 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4980 isl_die(ctx, isl_error_invalid,
4981 "spaces don't match", goto error);
4983 if (src->ls->div->n_row == 0)
4984 return dst;
4986 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4987 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4988 if (!exp1 || (dst->ls->div->n_row && !exp2))
4989 goto error;
4991 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4992 dst = isl_aff_expand_divs(dst, div, exp2);
4993 free(exp1);
4994 free(exp2);
4996 return dst;
4997 error:
4998 free(exp1);
4999 free(exp2);
5000 return isl_aff_free(dst);
5003 /* Adjust the local spaces of the affine expressions in "maff"
5004 * such that they all have the save divs.
5006 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5007 __isl_take isl_multi_aff *maff)
5009 int i;
5011 if (!maff)
5012 return NULL;
5013 if (maff->n == 0)
5014 return maff;
5015 maff = isl_multi_aff_cow(maff);
5016 if (!maff)
5017 return NULL;
5019 for (i = 1; i < maff->n; ++i)
5020 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5021 for (i = 1; i < maff->n; ++i) {
5022 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5023 if (!maff->p[i])
5024 return isl_multi_aff_free(maff);
5027 return maff;
5030 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5032 aff = isl_aff_cow(aff);
5033 if (!aff)
5034 return NULL;
5036 aff->ls = isl_local_space_lift(aff->ls);
5037 if (!aff->ls)
5038 return isl_aff_free(aff);
5040 return aff;
5043 /* Lift "maff" to a space with extra dimensions such that the result
5044 * has no more existentially quantified variables.
5045 * If "ls" is not NULL, then *ls is assigned the local space that lies
5046 * at the basis of the lifting applied to "maff".
5048 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5049 __isl_give isl_local_space **ls)
5051 int i;
5052 isl_space *space;
5053 unsigned n_div;
5055 if (ls)
5056 *ls = NULL;
5058 if (!maff)
5059 return NULL;
5061 if (maff->n == 0) {
5062 if (ls) {
5063 isl_space *space = isl_multi_aff_get_domain_space(maff);
5064 *ls = isl_local_space_from_space(space);
5065 if (!*ls)
5066 return isl_multi_aff_free(maff);
5068 return maff;
5071 maff = isl_multi_aff_cow(maff);
5072 maff = isl_multi_aff_align_divs(maff);
5073 if (!maff)
5074 return NULL;
5076 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5077 space = isl_multi_aff_get_space(maff);
5078 space = isl_space_lift(isl_space_domain(space), n_div);
5079 space = isl_space_extend_domain_with_range(space,
5080 isl_multi_aff_get_space(maff));
5081 if (!space)
5082 return isl_multi_aff_free(maff);
5083 isl_space_free(maff->space);
5084 maff->space = space;
5086 if (ls) {
5087 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5088 if (!*ls)
5089 return isl_multi_aff_free(maff);
5092 for (i = 0; i < maff->n; ++i) {
5093 maff->p[i] = isl_aff_lift(maff->p[i]);
5094 if (!maff->p[i])
5095 goto error;
5098 return maff;
5099 error:
5100 if (ls)
5101 isl_local_space_free(*ls);
5102 return isl_multi_aff_free(maff);
5106 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5108 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5109 __isl_keep isl_pw_multi_aff *pma, int pos)
5111 int i;
5112 int n_out;
5113 isl_space *space;
5114 isl_pw_aff *pa;
5116 if (!pma)
5117 return NULL;
5119 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5120 if (pos < 0 || pos >= n_out)
5121 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5122 "index out of bounds", return NULL);
5124 space = isl_pw_multi_aff_get_space(pma);
5125 space = isl_space_drop_dims(space, isl_dim_out,
5126 pos + 1, n_out - pos - 1);
5127 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5129 pa = isl_pw_aff_alloc_size(space, pma->n);
5130 for (i = 0; i < pma->n; ++i) {
5131 isl_aff *aff;
5132 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5133 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5136 return pa;
5139 /* Return an isl_pw_multi_aff with the given "set" as domain and
5140 * an unnamed zero-dimensional range.
5142 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5143 __isl_take isl_set *set)
5145 isl_multi_aff *ma;
5146 isl_space *space;
5148 space = isl_set_get_space(set);
5149 space = isl_space_from_domain(space);
5150 ma = isl_multi_aff_zero(space);
5151 return isl_pw_multi_aff_alloc(set, ma);
5154 /* Add an isl_pw_multi_aff with the given "set" as domain and
5155 * an unnamed zero-dimensional range to *user.
5157 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5159 isl_union_pw_multi_aff **upma = user;
5160 isl_pw_multi_aff *pma;
5162 pma = isl_pw_multi_aff_from_domain(set);
5163 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5165 return 0;
5168 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5169 * an unnamed zero-dimensional range.
5171 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5172 __isl_take isl_union_set *uset)
5174 isl_space *space;
5175 isl_union_pw_multi_aff *upma;
5177 if (!uset)
5178 return NULL;
5180 space = isl_union_set_get_space(uset);
5181 upma = isl_union_pw_multi_aff_empty(space);
5183 if (isl_union_set_foreach_set(uset,
5184 &add_pw_multi_aff_from_domain, &upma) < 0)
5185 goto error;
5187 isl_union_set_free(uset);
5188 return upma;
5189 error:
5190 isl_union_set_free(uset);
5191 isl_union_pw_multi_aff_free(upma);
5192 return NULL;
5195 /* Convert "pma" to an isl_map and add it to *umap.
5197 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5199 isl_union_map **umap = user;
5200 isl_map *map;
5202 map = isl_map_from_pw_multi_aff(pma);
5203 *umap = isl_union_map_add_map(*umap, map);
5205 return 0;
5208 /* Construct a union map mapping the domain of the union
5209 * piecewise multi-affine expression to its range, with each dimension
5210 * in the range equated to the corresponding affine expression on its cell.
5212 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5213 __isl_take isl_union_pw_multi_aff *upma)
5215 isl_space *space;
5216 isl_union_map *umap;
5218 if (!upma)
5219 return NULL;
5221 space = isl_union_pw_multi_aff_get_space(upma);
5222 umap = isl_union_map_empty(space);
5224 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5225 &map_from_pw_multi_aff, &umap) < 0)
5226 goto error;
5228 isl_union_pw_multi_aff_free(upma);
5229 return umap;
5230 error:
5231 isl_union_pw_multi_aff_free(upma);
5232 isl_union_map_free(umap);
5233 return NULL;
5236 /* Local data for bin_entry and the callback "fn".
5238 struct isl_union_pw_multi_aff_bin_data {
5239 isl_union_pw_multi_aff *upma2;
5240 isl_union_pw_multi_aff *res;
5241 isl_pw_multi_aff *pma;
5242 int (*fn)(void **entry, void *user);
5245 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5246 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5248 static int bin_entry(void **entry, void *user)
5250 struct isl_union_pw_multi_aff_bin_data *data = user;
5251 isl_pw_multi_aff *pma = *entry;
5253 data->pma = pma;
5254 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
5255 data->fn, data) < 0)
5256 return -1;
5258 return 0;
5261 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5262 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5263 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5264 * as *entry. The callback should adjust data->res if desired.
5266 static __isl_give isl_union_pw_multi_aff *bin_op(
5267 __isl_take isl_union_pw_multi_aff *upma1,
5268 __isl_take isl_union_pw_multi_aff *upma2,
5269 int (*fn)(void **entry, void *user))
5271 isl_space *space;
5272 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5274 space = isl_union_pw_multi_aff_get_space(upma2);
5275 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5276 space = isl_union_pw_multi_aff_get_space(upma1);
5277 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5279 if (!upma1 || !upma2)
5280 goto error;
5282 data.upma2 = upma2;
5283 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
5284 upma1->table.n);
5285 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
5286 &bin_entry, &data) < 0)
5287 goto error;
5289 isl_union_pw_multi_aff_free(upma1);
5290 isl_union_pw_multi_aff_free(upma2);
5291 return data.res;
5292 error:
5293 isl_union_pw_multi_aff_free(upma1);
5294 isl_union_pw_multi_aff_free(upma2);
5295 isl_union_pw_multi_aff_free(data.res);
5296 return NULL;
5299 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5300 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5302 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5303 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5305 isl_space *space;
5307 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5308 isl_pw_multi_aff_get_space(pma2));
5309 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5310 &isl_multi_aff_range_product);
5313 /* Given two isl_pw_multi_affs A -> B and C -> D,
5314 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5316 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5317 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5319 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5320 &pw_multi_aff_range_product);
5323 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5324 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5326 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5327 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5329 isl_space *space;
5331 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5332 isl_pw_multi_aff_get_space(pma2));
5333 space = isl_space_flatten_range(space);
5334 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5335 &isl_multi_aff_flat_range_product);
5338 /* Given two isl_pw_multi_affs A -> B and C -> D,
5339 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5341 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5342 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5344 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5345 &pw_multi_aff_flat_range_product);
5348 /* If data->pma and *entry have the same domain space, then compute
5349 * their flat range product and the result to data->res.
5351 static int flat_range_product_entry(void **entry, void *user)
5353 struct isl_union_pw_multi_aff_bin_data *data = user;
5354 isl_pw_multi_aff *pma2 = *entry;
5356 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
5357 pma2->dim, isl_dim_in))
5358 return 0;
5360 pma2 = isl_pw_multi_aff_flat_range_product(
5361 isl_pw_multi_aff_copy(data->pma),
5362 isl_pw_multi_aff_copy(pma2));
5364 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5366 return 0;
5369 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5370 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5372 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5373 __isl_take isl_union_pw_multi_aff *upma1,
5374 __isl_take isl_union_pw_multi_aff *upma2)
5376 return bin_op(upma1, upma2, &flat_range_product_entry);
5379 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5380 * The parameters are assumed to have been aligned.
5382 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5383 * except that it works on two different isl_pw_* types.
5385 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5386 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5387 __isl_take isl_pw_aff *pa)
5389 int i, j, n;
5390 isl_pw_multi_aff *res = NULL;
5392 if (!pma || !pa)
5393 goto error;
5395 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
5396 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5397 "domains don't match", goto error);
5398 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5399 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5400 "index out of bounds", goto error);
5402 n = pma->n * pa->n;
5403 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5405 for (i = 0; i < pma->n; ++i) {
5406 for (j = 0; j < pa->n; ++j) {
5407 isl_set *common;
5408 isl_multi_aff *res_ij;
5409 int empty;
5411 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5412 isl_set_copy(pa->p[j].set));
5413 empty = isl_set_plain_is_empty(common);
5414 if (empty < 0 || empty) {
5415 isl_set_free(common);
5416 if (empty < 0)
5417 goto error;
5418 continue;
5421 res_ij = isl_multi_aff_set_aff(
5422 isl_multi_aff_copy(pma->p[i].maff), pos,
5423 isl_aff_copy(pa->p[j].aff));
5424 res_ij = isl_multi_aff_gist(res_ij,
5425 isl_set_copy(common));
5427 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5431 isl_pw_multi_aff_free(pma);
5432 isl_pw_aff_free(pa);
5433 return res;
5434 error:
5435 isl_pw_multi_aff_free(pma);
5436 isl_pw_aff_free(pa);
5437 return isl_pw_multi_aff_free(res);
5440 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5442 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5443 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5444 __isl_take isl_pw_aff *pa)
5446 if (!pma || !pa)
5447 goto error;
5448 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5449 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5450 if (!isl_space_has_named_params(pma->dim) ||
5451 !isl_space_has_named_params(pa->dim))
5452 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5453 "unaligned unnamed parameters", goto error);
5454 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5455 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5456 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5457 error:
5458 isl_pw_multi_aff_free(pma);
5459 isl_pw_aff_free(pa);
5460 return NULL;
5463 /* Do the parameters of "pa" match those of "space"?
5465 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5466 __isl_keep isl_space *space)
5468 isl_space *pa_space;
5469 int match;
5471 if (!pa || !space)
5472 return -1;
5474 pa_space = isl_pw_aff_get_space(pa);
5476 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5478 isl_space_free(pa_space);
5479 return match;
5482 /* Check that the domain space of "pa" matches "space".
5484 * Return 0 on success and -1 on error.
5486 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5487 __isl_keep isl_space *space)
5489 isl_space *pa_space;
5490 int match;
5492 if (!pa || !space)
5493 return -1;
5495 pa_space = isl_pw_aff_get_space(pa);
5497 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5498 if (match < 0)
5499 goto error;
5500 if (!match)
5501 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5502 "parameters don't match", goto error);
5503 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
5504 if (match < 0)
5505 goto error;
5506 if (!match)
5507 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5508 "domains don't match", goto error);
5509 isl_space_free(pa_space);
5510 return 0;
5511 error:
5512 isl_space_free(pa_space);
5513 return -1;
5516 #undef BASE
5517 #define BASE pw_aff
5519 #include <isl_multi_templ.c>
5521 /* Scale the elements of "pma" by the corresponding elements of "mv".
5523 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5524 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5526 int i;
5528 pma = isl_pw_multi_aff_cow(pma);
5529 if (!pma || !mv)
5530 goto error;
5531 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5532 mv->space, isl_dim_set))
5533 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5534 "spaces don't match", goto error);
5535 if (!isl_space_match(pma->dim, isl_dim_param,
5536 mv->space, isl_dim_param)) {
5537 pma = isl_pw_multi_aff_align_params(pma,
5538 isl_multi_val_get_space(mv));
5539 mv = isl_multi_val_align_params(mv,
5540 isl_pw_multi_aff_get_space(pma));
5541 if (!pma || !mv)
5542 goto error;
5545 for (i = 0; i < pma->n; ++i) {
5546 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5547 isl_multi_val_copy(mv));
5548 if (!pma->p[i].maff)
5549 goto error;
5552 isl_multi_val_free(mv);
5553 return pma;
5554 error:
5555 isl_multi_val_free(mv);
5556 isl_pw_multi_aff_free(pma);
5557 return NULL;
5560 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5561 * mv contains the mv argument.
5562 * res collects the results.
5564 struct isl_union_pw_multi_aff_scale_multi_val_data {
5565 isl_multi_val *mv;
5566 isl_union_pw_multi_aff *res;
5569 /* This function is called for each entry of an isl_union_pw_multi_aff.
5570 * If the space of the entry matches that of data->mv,
5571 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5572 * to data->res.
5574 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5576 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5577 isl_pw_multi_aff *pma = *entry;
5579 if (!pma)
5580 return -1;
5581 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5582 data->mv->space, isl_dim_set))
5583 return 0;
5585 pma = isl_pw_multi_aff_copy(pma);
5586 pma = isl_pw_multi_aff_scale_multi_val(pma,
5587 isl_multi_val_copy(data->mv));
5588 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5589 if (!data->res)
5590 return -1;
5592 return 0;
5595 /* Scale the elements of "upma" by the corresponding elements of "mv",
5596 * for those entries that match the space of "mv".
5598 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5599 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5601 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5603 upma = isl_union_pw_multi_aff_align_params(upma,
5604 isl_multi_val_get_space(mv));
5605 mv = isl_multi_val_align_params(mv,
5606 isl_union_pw_multi_aff_get_space(upma));
5607 if (!upma || !mv)
5608 goto error;
5610 data.mv = mv;
5611 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->dim),
5612 upma->table.n);
5613 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5614 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5615 goto error;
5617 isl_multi_val_free(mv);
5618 isl_union_pw_multi_aff_free(upma);
5619 return data.res;
5620 error:
5621 isl_multi_val_free(mv);
5622 isl_union_pw_multi_aff_free(upma);
5623 return NULL;
5626 /* Construct and return a piecewise multi affine expression
5627 * in the given space with value zero in each of the output dimensions and
5628 * a universe domain.
5630 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
5632 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
5635 /* Construct and return a piecewise multi affine expression
5636 * that is equal to the given piecewise affine expression.
5638 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
5639 __isl_take isl_pw_aff *pa)
5641 int i;
5642 isl_space *space;
5643 isl_pw_multi_aff *pma;
5645 if (!pa)
5646 return NULL;
5648 space = isl_pw_aff_get_space(pa);
5649 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
5651 for (i = 0; i < pa->n; ++i) {
5652 isl_set *set;
5653 isl_multi_aff *ma;
5655 set = isl_set_copy(pa->p[i].set);
5656 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
5657 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
5660 isl_pw_aff_free(pa);
5661 return pma;
5664 /* Construct a set or map mapping the shared (parameter) domain
5665 * of the piecewise affine expressions to the range of "mpa"
5666 * with each dimension in the range equated to the
5667 * corresponding piecewise affine expression.
5669 static __isl_give isl_map *map_from_multi_pw_aff(
5670 __isl_take isl_multi_pw_aff *mpa)
5672 int i;
5673 isl_space *space;
5674 isl_map *map;
5676 if (!mpa)
5677 return NULL;
5679 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
5680 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5681 "invalid space", goto error);
5683 space = isl_multi_pw_aff_get_domain_space(mpa);
5684 map = isl_map_universe(isl_space_from_domain(space));
5686 for (i = 0; i < mpa->n; ++i) {
5687 isl_pw_aff *pa;
5688 isl_map *map_i;
5690 pa = isl_pw_aff_copy(mpa->p[i]);
5691 map_i = map_from_pw_aff(pa);
5693 map = isl_map_flat_range_product(map, map_i);
5696 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
5698 isl_multi_pw_aff_free(mpa);
5699 return map;
5700 error:
5701 isl_multi_pw_aff_free(mpa);
5702 return NULL;
5705 /* Construct a map mapping the shared domain
5706 * of the piecewise affine expressions to the range of "mpa"
5707 * with each dimension in the range equated to the
5708 * corresponding piecewise affine expression.
5710 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
5712 if (!mpa)
5713 return NULL;
5714 if (isl_space_is_set(mpa->space))
5715 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5716 "space of input is not a map", goto error);
5718 return map_from_multi_pw_aff(mpa);
5719 error:
5720 isl_multi_pw_aff_free(mpa);
5721 return NULL;
5724 /* Construct a set mapping the shared parameter domain
5725 * of the piecewise affine expressions to the space of "mpa"
5726 * with each dimension in the range equated to the
5727 * corresponding piecewise affine expression.
5729 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
5731 if (!mpa)
5732 return NULL;
5733 if (!isl_space_is_set(mpa->space))
5734 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5735 "space of input is not a set", goto error);
5737 return map_from_multi_pw_aff(mpa);
5738 error:
5739 isl_multi_pw_aff_free(mpa);
5740 return NULL;
5743 /* Construct and return a piecewise multi affine expression
5744 * that is equal to the given multi piecewise affine expression
5745 * on the shared domain of the piecewise affine expressions.
5747 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
5748 __isl_take isl_multi_pw_aff *mpa)
5750 int i;
5751 isl_space *space;
5752 isl_pw_aff *pa;
5753 isl_pw_multi_aff *pma;
5755 if (!mpa)
5756 return NULL;
5758 space = isl_multi_pw_aff_get_space(mpa);
5760 if (mpa->n == 0) {
5761 isl_multi_pw_aff_free(mpa);
5762 return isl_pw_multi_aff_zero(space);
5765 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
5766 pma = isl_pw_multi_aff_from_pw_aff(pa);
5768 for (i = 1; i < mpa->n; ++i) {
5769 isl_pw_multi_aff *pma_i;
5771 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
5772 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
5773 pma = isl_pw_multi_aff_range_product(pma, pma_i);
5776 pma = isl_pw_multi_aff_reset_space(pma, space);
5778 isl_multi_pw_aff_free(mpa);
5779 return pma;
5782 /* Construct and return a multi piecewise affine expression
5783 * that is equal to the given multi affine expression.
5785 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
5786 __isl_take isl_multi_aff *ma)
5788 int i, n;
5789 isl_multi_pw_aff *mpa;
5791 if (!ma)
5792 return NULL;
5794 n = isl_multi_aff_dim(ma, isl_dim_out);
5795 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
5797 for (i = 0; i < n; ++i) {
5798 isl_pw_aff *pa;
5800 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
5801 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
5804 isl_multi_aff_free(ma);
5805 return mpa;
5808 /* Construct and return a multi piecewise affine expression
5809 * that is equal to the given piecewise multi affine expression.
5811 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
5812 __isl_take isl_pw_multi_aff *pma)
5814 int i, n;
5815 isl_space *space;
5816 isl_multi_pw_aff *mpa;
5818 if (!pma)
5819 return NULL;
5821 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
5822 space = isl_pw_multi_aff_get_space(pma);
5823 mpa = isl_multi_pw_aff_alloc(space);
5825 for (i = 0; i < n; ++i) {
5826 isl_pw_aff *pa;
5828 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
5829 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
5832 isl_pw_multi_aff_free(pma);
5833 return mpa;
5836 /* Do "pa1" and "pa2" represent the same function?
5838 * We first check if they are obviously equal.
5839 * If not, we convert them to maps and check if those are equal.
5841 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
5843 int equal;
5844 isl_map *map1, *map2;
5846 if (!pa1 || !pa2)
5847 return -1;
5849 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
5850 if (equal < 0 || equal)
5851 return equal;
5853 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
5854 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
5855 equal = isl_map_is_equal(map1, map2);
5856 isl_map_free(map1);
5857 isl_map_free(map2);
5859 return equal;
5862 /* Do "mpa1" and "mpa2" represent the same function?
5864 * Note that we cannot convert the entire isl_multi_pw_aff
5865 * to a map because the domains of the piecewise affine expressions
5866 * may not be the same.
5868 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
5869 __isl_keep isl_multi_pw_aff *mpa2)
5871 int i;
5872 int equal;
5874 if (!mpa1 || !mpa2)
5875 return -1;
5877 if (!isl_space_match(mpa1->space, isl_dim_param,
5878 mpa2->space, isl_dim_param)) {
5879 if (!isl_space_has_named_params(mpa1->space))
5880 return 0;
5881 if (!isl_space_has_named_params(mpa2->space))
5882 return 0;
5883 mpa1 = isl_multi_pw_aff_copy(mpa1);
5884 mpa2 = isl_multi_pw_aff_copy(mpa2);
5885 mpa1 = isl_multi_pw_aff_align_params(mpa1,
5886 isl_multi_pw_aff_get_space(mpa2));
5887 mpa2 = isl_multi_pw_aff_align_params(mpa2,
5888 isl_multi_pw_aff_get_space(mpa1));
5889 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
5890 isl_multi_pw_aff_free(mpa1);
5891 isl_multi_pw_aff_free(mpa2);
5892 return equal;
5895 equal = isl_space_is_equal(mpa1->space, mpa2->space);
5896 if (equal < 0 || !equal)
5897 return equal;
5899 for (i = 0; i < mpa1->n; ++i) {
5900 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
5901 if (equal < 0 || !equal)
5902 return equal;
5905 return 1;
5908 /* Coalesce the elements of "mpa".
5910 * Note that such coalescing does not change the meaning of "mpa"
5911 * so there is no need to cow. We do need to be careful not to
5912 * destroy any other copies of "mpa" in case of failure.
5914 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
5915 __isl_take isl_multi_pw_aff *mpa)
5917 int i;
5919 if (!mpa)
5920 return NULL;
5922 for (i = 0; i < mpa->n; ++i) {
5923 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
5924 pa = isl_pw_aff_coalesce(pa);
5925 if (!pa)
5926 return isl_multi_pw_aff_free(mpa);
5927 isl_pw_aff_free(mpa->p[i]);
5928 mpa->p[i] = pa;
5931 return mpa;
5934 /* Compute the pullback of "mpa" by the function represented by "ma".
5935 * In other words, plug in "ma" in "mpa".
5937 * The parameters of "mpa" and "ma" are assumed to have been aligned.
5939 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
5940 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
5942 int i;
5943 isl_space *space = NULL;
5945 mpa = isl_multi_pw_aff_cow(mpa);
5946 if (!mpa || !ma)
5947 goto error;
5949 space = isl_space_join(isl_multi_aff_get_space(ma),
5950 isl_multi_pw_aff_get_space(mpa));
5951 if (!space)
5952 goto error;
5954 for (i = 0; i < mpa->n; ++i) {
5955 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
5956 isl_multi_aff_copy(ma));
5957 if (!mpa->p[i])
5958 goto error;
5961 isl_multi_aff_free(ma);
5962 isl_space_free(mpa->space);
5963 mpa->space = space;
5964 return mpa;
5965 error:
5966 isl_space_free(space);
5967 isl_multi_pw_aff_free(mpa);
5968 isl_multi_aff_free(ma);
5969 return NULL;
5972 /* Compute the pullback of "mpa" by the function represented by "ma".
5973 * In other words, plug in "ma" in "mpa".
5975 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
5976 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
5978 if (!mpa || !ma)
5979 goto error;
5980 if (isl_space_match(mpa->space, isl_dim_param,
5981 ma->space, isl_dim_param))
5982 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
5983 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
5984 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
5985 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
5986 error:
5987 isl_multi_pw_aff_free(mpa);
5988 isl_multi_aff_free(ma);
5989 return NULL;
5992 /* Compute the pullback of "mpa" by the function represented by "pma".
5993 * In other words, plug in "pma" in "mpa".
5995 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
5997 static __isl_give isl_multi_pw_aff *
5998 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
5999 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6001 int i;
6002 isl_space *space = NULL;
6004 mpa = isl_multi_pw_aff_cow(mpa);
6005 if (!mpa || !pma)
6006 goto error;
6008 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6009 isl_multi_pw_aff_get_space(mpa));
6011 for (i = 0; i < mpa->n; ++i) {
6012 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6013 isl_pw_multi_aff_copy(pma));
6014 if (!mpa->p[i])
6015 goto error;
6018 isl_pw_multi_aff_free(pma);
6019 isl_space_free(mpa->space);
6020 mpa->space = space;
6021 return mpa;
6022 error:
6023 isl_space_free(space);
6024 isl_multi_pw_aff_free(mpa);
6025 isl_pw_multi_aff_free(pma);
6026 return NULL;
6029 /* Compute the pullback of "mpa" by the function represented by "pma".
6030 * In other words, plug in "pma" in "mpa".
6032 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6033 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6035 if (!mpa || !pma)
6036 goto error;
6037 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6038 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6039 mpa = isl_multi_pw_aff_align_params(mpa,
6040 isl_pw_multi_aff_get_space(pma));
6041 pma = isl_pw_multi_aff_align_params(pma,
6042 isl_multi_pw_aff_get_space(mpa));
6043 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6044 error:
6045 isl_multi_pw_aff_free(mpa);
6046 isl_pw_multi_aff_free(pma);
6047 return NULL;
6050 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6051 * with the domain of "aff". The domain of the result is the same
6052 * as that of "mpa".
6053 * "mpa" and "aff" are assumed to have been aligned.
6055 * We first extract the parametric constant from "aff", defined
6056 * over the correct domain.
6057 * Then we add the appropriate combinations of the members of "mpa".
6058 * Finally, we add the integer divisions through recursive calls.
6060 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6061 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6063 int i, n_param, n_in, n_div;
6064 isl_space *space;
6065 isl_val *v;
6066 isl_pw_aff *pa;
6067 isl_aff *tmp;
6069 n_param = isl_aff_dim(aff, isl_dim_param);
6070 n_in = isl_aff_dim(aff, isl_dim_in);
6071 n_div = isl_aff_dim(aff, isl_dim_div);
6073 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6074 tmp = isl_aff_copy(aff);
6075 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6076 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6077 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6078 isl_space_dim(space, isl_dim_set));
6079 tmp = isl_aff_reset_domain_space(tmp, space);
6080 pa = isl_pw_aff_from_aff(tmp);
6082 for (i = 0; i < n_in; ++i) {
6083 isl_pw_aff *pa_i;
6085 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6086 continue;
6087 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6088 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6089 pa_i = isl_pw_aff_scale_val(pa_i, v);
6090 pa = isl_pw_aff_add(pa, pa_i);
6093 for (i = 0; i < n_div; ++i) {
6094 isl_aff *div;
6095 isl_pw_aff *pa_i;
6097 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6098 continue;
6099 div = isl_aff_get_div(aff, i);
6100 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6101 isl_multi_pw_aff_copy(mpa), div);
6102 pa_i = isl_pw_aff_floor(pa_i);
6103 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6104 pa_i = isl_pw_aff_scale_val(pa_i, v);
6105 pa = isl_pw_aff_add(pa, pa_i);
6108 isl_multi_pw_aff_free(mpa);
6109 isl_aff_free(aff);
6111 return pa;
6114 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6115 * with the domain of "aff". The domain of the result is the same
6116 * as that of "mpa".
6118 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6119 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6121 if (!aff || !mpa)
6122 goto error;
6123 if (isl_space_match(aff->ls->dim, isl_dim_param,
6124 mpa->space, isl_dim_param))
6125 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6127 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6128 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6130 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6131 error:
6132 isl_aff_free(aff);
6133 isl_multi_pw_aff_free(mpa);
6134 return NULL;
6137 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6138 * with the domain of "pa". The domain of the result is the same
6139 * as that of "mpa".
6140 * "mpa" and "pa" are assumed to have been aligned.
6142 * We consider each piece in turn. Note that the domains of the
6143 * pieces are assumed to be disjoint and they remain disjoint
6144 * after taking the preimage (over the same function).
6146 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6147 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6149 isl_space *space;
6150 isl_pw_aff *res;
6151 int i;
6153 if (!mpa || !pa)
6154 goto error;
6156 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6157 isl_pw_aff_get_space(pa));
6158 res = isl_pw_aff_empty(space);
6160 for (i = 0; i < pa->n; ++i) {
6161 isl_pw_aff *pa_i;
6162 isl_set *domain;
6164 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6165 isl_multi_pw_aff_copy(mpa),
6166 isl_aff_copy(pa->p[i].aff));
6167 domain = isl_set_copy(pa->p[i].set);
6168 domain = isl_set_preimage_multi_pw_aff(domain,
6169 isl_multi_pw_aff_copy(mpa));
6170 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6171 res = isl_pw_aff_add_disjoint(res, pa_i);
6174 isl_pw_aff_free(pa);
6175 isl_multi_pw_aff_free(mpa);
6176 return res;
6177 error:
6178 isl_pw_aff_free(pa);
6179 isl_multi_pw_aff_free(mpa);
6180 return NULL;
6183 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6184 * with the domain of "pa". The domain of the result is the same
6185 * as that of "mpa".
6187 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6188 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6190 if (!pa || !mpa)
6191 goto error;
6192 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6193 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6195 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6196 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6198 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6199 error:
6200 isl_pw_aff_free(pa);
6201 isl_multi_pw_aff_free(mpa);
6202 return NULL;
6205 /* Compute the pullback of "pa" by the function represented by "mpa".
6206 * In other words, plug in "mpa" in "pa".
6207 * "pa" and "mpa" are assumed to have been aligned.
6209 * The pullback is computed by applying "pa" to "mpa".
6211 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6212 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6214 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6217 /* Compute the pullback of "pa" by the function represented by "mpa".
6218 * In other words, plug in "mpa" in "pa".
6220 * The pullback is computed by applying "pa" to "mpa".
6222 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6223 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6225 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6228 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6229 * In other words, plug in "mpa2" in "mpa1".
6231 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6233 * We pullback each member of "mpa1" in turn.
6235 static __isl_give isl_multi_pw_aff *
6236 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6237 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6239 int i;
6240 isl_space *space = NULL;
6242 mpa1 = isl_multi_pw_aff_cow(mpa1);
6243 if (!mpa1 || !mpa2)
6244 goto error;
6246 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6247 isl_multi_pw_aff_get_space(mpa1));
6249 for (i = 0; i < mpa1->n; ++i) {
6250 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6251 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6252 if (!mpa1->p[i])
6253 goto error;
6256 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6258 isl_multi_pw_aff_free(mpa2);
6259 return mpa1;
6260 error:
6261 isl_space_free(space);
6262 isl_multi_pw_aff_free(mpa1);
6263 isl_multi_pw_aff_free(mpa2);
6264 return NULL;
6267 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6268 * In other words, plug in "mpa2" in "mpa1".
6270 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6271 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6273 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6274 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6277 /* Compare two isl_affs.
6279 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6280 * than "aff2" and 0 if they are equal.
6282 * The order is fairly arbitrary. We do consider expressions that only involve
6283 * earlier dimensions as "smaller".
6285 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6287 int cmp;
6288 int last1, last2;
6290 if (aff1 == aff2)
6291 return 0;
6293 if (!aff1)
6294 return -1;
6295 if (!aff2)
6296 return 1;
6298 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6299 if (cmp != 0)
6300 return cmp;
6302 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6303 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6304 if (last1 != last2)
6305 return last1 - last2;
6307 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6310 /* Compare two isl_pw_affs.
6312 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6313 * than "pa2" and 0 if they are equal.
6315 * The order is fairly arbitrary. We do consider expressions that only involve
6316 * earlier dimensions as "smaller".
6318 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
6319 __isl_keep isl_pw_aff *pa2)
6321 int i;
6322 int cmp;
6324 if (pa1 == pa2)
6325 return 0;
6327 if (!pa1)
6328 return -1;
6329 if (!pa2)
6330 return 1;
6332 cmp = isl_space_cmp(pa1->dim, pa2->dim);
6333 if (cmp != 0)
6334 return cmp;
6336 if (pa1->n != pa2->n)
6337 return pa1->n - pa2->n;
6339 for (i = 0; i < pa1->n; ++i) {
6340 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
6341 if (cmp != 0)
6342 return cmp;
6343 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
6344 if (cmp != 0)
6345 return cmp;
6348 return 0;