complain on memory allocation failure
[isl.git] / isl_aff.c
blob007d4e2dcadf81a1b516b72f6ecf397a15165438
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2013 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 the specified dimension
112 * in "ls".
114 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
115 enum isl_dim_type type, unsigned pos)
117 isl_space *space;
118 isl_aff *aff;
120 if (!ls)
121 return NULL;
123 space = isl_local_space_get_space(ls);
124 if (!space)
125 goto error;
126 if (isl_space_is_map(space))
127 isl_die(isl_space_get_ctx(space), isl_error_invalid,
128 "expecting (parameter) set space", goto error);
129 if (pos >= isl_local_space_dim(ls, type))
130 isl_die(isl_space_get_ctx(space), isl_error_invalid,
131 "position out of bounds", goto error);
133 isl_space_free(space);
134 aff = isl_aff_alloc(ls);
135 if (!aff)
136 return NULL;
138 pos += isl_local_space_offset(aff->ls, type);
140 isl_int_set_si(aff->v->el[0], 1);
141 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
142 isl_int_set_si(aff->v->el[1 + pos], 1);
144 return aff;
145 error:
146 isl_local_space_free(ls);
147 isl_space_free(space);
148 return NULL;
151 /* Return a piecewise affine expression that is equal to
152 * the specified dimension in "ls".
154 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
155 enum isl_dim_type type, unsigned pos)
157 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
160 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
162 if (!aff)
163 return NULL;
165 aff->ref++;
166 return aff;
169 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
171 if (!aff)
172 return NULL;
174 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
175 isl_vec_copy(aff->v));
178 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
180 if (!aff)
181 return NULL;
183 if (aff->ref == 1)
184 return aff;
185 aff->ref--;
186 return isl_aff_dup(aff);
189 void *isl_aff_free(__isl_take isl_aff *aff)
191 if (!aff)
192 return NULL;
194 if (--aff->ref > 0)
195 return NULL;
197 isl_local_space_free(aff->ls);
198 isl_vec_free(aff->v);
200 free(aff);
202 return NULL;
205 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
207 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
210 /* Externally, an isl_aff has a map space, but internally, the
211 * ls field corresponds to the domain of that space.
213 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
215 if (!aff)
216 return 0;
217 if (type == isl_dim_out)
218 return 1;
219 if (type == isl_dim_in)
220 type = isl_dim_set;
221 return isl_local_space_dim(aff->ls, type);
224 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
226 return aff ? isl_local_space_get_space(aff->ls) : NULL;
229 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
231 isl_space *space;
232 if (!aff)
233 return NULL;
234 space = isl_local_space_get_space(aff->ls);
235 space = isl_space_from_domain(space);
236 space = isl_space_add_dims(space, isl_dim_out, 1);
237 return space;
240 __isl_give isl_local_space *isl_aff_get_domain_local_space(
241 __isl_keep isl_aff *aff)
243 return aff ? isl_local_space_copy(aff->ls) : NULL;
246 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
248 isl_local_space *ls;
249 if (!aff)
250 return NULL;
251 ls = isl_local_space_copy(aff->ls);
252 ls = isl_local_space_from_domain(ls);
253 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
254 return ls;
257 /* Externally, an isl_aff has a map space, but internally, the
258 * ls field corresponds to the domain of that space.
260 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
261 enum isl_dim_type type, unsigned pos)
263 if (!aff)
264 return NULL;
265 if (type == isl_dim_out)
266 return NULL;
267 if (type == isl_dim_in)
268 type = isl_dim_set;
269 return isl_local_space_get_dim_name(aff->ls, type, pos);
272 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
273 __isl_take isl_space *dim)
275 aff = isl_aff_cow(aff);
276 if (!aff || !dim)
277 goto error;
279 aff->ls = isl_local_space_reset_space(aff->ls, dim);
280 if (!aff->ls)
281 return isl_aff_free(aff);
283 return aff;
284 error:
285 isl_aff_free(aff);
286 isl_space_free(dim);
287 return NULL;
290 /* Reset the space of "aff". This function is called from isl_pw_templ.c
291 * and doesn't know if the space of an element object is represented
292 * directly or through its domain. It therefore passes along both.
294 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
295 __isl_take isl_space *space, __isl_take isl_space *domain)
297 isl_space_free(space);
298 return isl_aff_reset_domain_space(aff, domain);
301 /* Reorder the coefficients of the affine expression based
302 * on the given reodering.
303 * The reordering r is assumed to have been extended with the local
304 * variables.
306 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
307 __isl_take isl_reordering *r, int n_div)
309 isl_vec *res;
310 int i;
312 if (!vec || !r)
313 goto error;
315 res = isl_vec_alloc(vec->ctx,
316 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
317 isl_seq_cpy(res->el, vec->el, 2);
318 isl_seq_clr(res->el + 2, res->size - 2);
319 for (i = 0; i < r->len; ++i)
320 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
322 isl_reordering_free(r);
323 isl_vec_free(vec);
324 return res;
325 error:
326 isl_vec_free(vec);
327 isl_reordering_free(r);
328 return NULL;
331 /* Reorder the dimensions of the domain of "aff" according
332 * to the given reordering.
334 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
335 __isl_take isl_reordering *r)
337 aff = isl_aff_cow(aff);
338 if (!aff)
339 goto error;
341 r = isl_reordering_extend(r, aff->ls->div->n_row);
342 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
343 aff->ls->div->n_row);
344 aff->ls = isl_local_space_realign(aff->ls, r);
346 if (!aff->v || !aff->ls)
347 return isl_aff_free(aff);
349 return aff;
350 error:
351 isl_aff_free(aff);
352 isl_reordering_free(r);
353 return NULL;
356 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
357 __isl_take isl_space *model)
359 if (!aff || !model)
360 goto error;
362 if (!isl_space_match(aff->ls->dim, isl_dim_param,
363 model, isl_dim_param)) {
364 isl_reordering *exp;
366 model = isl_space_drop_dims(model, isl_dim_in,
367 0, isl_space_dim(model, isl_dim_in));
368 model = isl_space_drop_dims(model, isl_dim_out,
369 0, isl_space_dim(model, isl_dim_out));
370 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
371 exp = isl_reordering_extend_space(exp,
372 isl_aff_get_domain_space(aff));
373 aff = isl_aff_realign_domain(aff, exp);
376 isl_space_free(model);
377 return aff;
378 error:
379 isl_space_free(model);
380 isl_aff_free(aff);
381 return NULL;
384 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
386 if (!aff)
387 return -1;
389 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
392 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
394 int equal;
396 if (!aff1 || !aff2)
397 return -1;
399 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
400 if (equal < 0 || !equal)
401 return equal;
403 return isl_vec_is_equal(aff1->v, aff2->v);
406 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
408 if (!aff)
409 return -1;
410 isl_int_set(*v, aff->v->el[0]);
411 return 0;
414 /* Return the common denominator of "aff".
416 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
418 isl_ctx *ctx;
420 if (!aff)
421 return NULL;
423 ctx = isl_aff_get_ctx(aff);
424 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
427 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
429 if (!aff)
430 return -1;
431 isl_int_set(*v, aff->v->el[1]);
432 return 0;
435 /* Return the constant term of "aff".
437 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
439 isl_ctx *ctx;
440 isl_val *v;
442 if (!aff)
443 return NULL;
445 ctx = isl_aff_get_ctx(aff);
446 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
447 return isl_val_normalize(v);
450 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
451 enum isl_dim_type type, int pos, isl_int *v)
453 if (!aff)
454 return -1;
456 if (type == isl_dim_out)
457 isl_die(aff->v->ctx, isl_error_invalid,
458 "output/set dimension does not have a coefficient",
459 return -1);
460 if (type == isl_dim_in)
461 type = isl_dim_set;
463 if (pos >= isl_local_space_dim(aff->ls, type))
464 isl_die(aff->v->ctx, isl_error_invalid,
465 "position out of bounds", return -1);
467 pos += isl_local_space_offset(aff->ls, type);
468 isl_int_set(*v, aff->v->el[1 + pos]);
470 return 0;
473 /* Return the coefficient of the variable of type "type" at position "pos"
474 * of "aff".
476 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
477 enum isl_dim_type type, int pos)
479 isl_ctx *ctx;
480 isl_val *v;
482 if (!aff)
483 return NULL;
485 ctx = isl_aff_get_ctx(aff);
486 if (type == isl_dim_out)
487 isl_die(ctx, isl_error_invalid,
488 "output/set dimension does not have a coefficient",
489 return NULL);
490 if (type == isl_dim_in)
491 type = isl_dim_set;
493 if (pos >= isl_local_space_dim(aff->ls, type))
494 isl_die(ctx, isl_error_invalid,
495 "position out of bounds", return NULL);
497 pos += isl_local_space_offset(aff->ls, type);
498 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
499 return isl_val_normalize(v);
502 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
504 aff = isl_aff_cow(aff);
505 if (!aff)
506 return NULL;
508 aff->v = isl_vec_cow(aff->v);
509 if (!aff->v)
510 return isl_aff_free(aff);
512 isl_int_set(aff->v->el[0], v);
514 return aff;
517 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
519 aff = isl_aff_cow(aff);
520 if (!aff)
521 return NULL;
523 aff->v = isl_vec_cow(aff->v);
524 if (!aff->v)
525 return isl_aff_free(aff);
527 isl_int_set(aff->v->el[1], v);
529 return aff;
532 /* Replace the constant term of "aff" by "v".
534 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
535 __isl_take isl_val *v)
537 if (!aff || !v)
538 goto error;
540 if (!isl_val_is_rat(v))
541 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
542 "expecting rational value", goto error);
544 if (isl_int_eq(aff->v->el[1], v->n) &&
545 isl_int_eq(aff->v->el[0], v->d)) {
546 isl_val_free(v);
547 return aff;
550 aff = isl_aff_cow(aff);
551 if (!aff)
552 goto error;
553 aff->v = isl_vec_cow(aff->v);
554 if (!aff->v)
555 goto error;
557 if (isl_int_eq(aff->v->el[0], v->d)) {
558 isl_int_set(aff->v->el[1], v->n);
559 } else if (isl_int_is_one(v->d)) {
560 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
561 } else {
562 isl_seq_scale(aff->v->el + 1,
563 aff->v->el + 1, v->d, aff->v->size - 1);
564 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
565 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
566 aff->v = isl_vec_normalize(aff->v);
567 if (!aff->v)
568 goto error;
571 isl_val_free(v);
572 return aff;
573 error:
574 isl_aff_free(aff);
575 isl_val_free(v);
576 return NULL;
579 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
581 if (isl_int_is_zero(v))
582 return aff;
584 aff = isl_aff_cow(aff);
585 if (!aff)
586 return NULL;
588 aff->v = isl_vec_cow(aff->v);
589 if (!aff->v)
590 return isl_aff_free(aff);
592 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
594 return aff;
597 /* Add "v" to the constant term of "aff".
599 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
600 __isl_take isl_val *v)
602 if (!aff || !v)
603 goto error;
605 if (isl_val_is_zero(v)) {
606 isl_val_free(v);
607 return aff;
610 if (!isl_val_is_rat(v))
611 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
612 "expecting rational value", goto error);
614 aff = isl_aff_cow(aff);
615 if (!aff)
616 goto error;
618 aff->v = isl_vec_cow(aff->v);
619 if (!aff->v)
620 goto error;
622 if (isl_int_is_one(v->d)) {
623 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
624 } else if (isl_int_eq(aff->v->el[0], v->d)) {
625 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
626 aff->v = isl_vec_normalize(aff->v);
627 if (!aff->v)
628 goto error;
629 } else {
630 isl_seq_scale(aff->v->el + 1,
631 aff->v->el + 1, v->d, aff->v->size - 1);
632 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
633 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
634 aff->v = isl_vec_normalize(aff->v);
635 if (!aff->v)
636 goto error;
639 isl_val_free(v);
640 return aff;
641 error:
642 isl_aff_free(aff);
643 isl_val_free(v);
644 return NULL;
647 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
649 isl_int t;
651 isl_int_init(t);
652 isl_int_set_si(t, v);
653 aff = isl_aff_add_constant(aff, t);
654 isl_int_clear(t);
656 return aff;
659 /* Add "v" to the numerator of the constant term of "aff".
661 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
663 if (isl_int_is_zero(v))
664 return aff;
666 aff = isl_aff_cow(aff);
667 if (!aff)
668 return NULL;
670 aff->v = isl_vec_cow(aff->v);
671 if (!aff->v)
672 return isl_aff_free(aff);
674 isl_int_add(aff->v->el[1], aff->v->el[1], v);
676 return aff;
679 /* Add "v" to the numerator of the constant term of "aff".
681 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
683 isl_int t;
685 if (v == 0)
686 return aff;
688 isl_int_init(t);
689 isl_int_set_si(t, v);
690 aff = isl_aff_add_constant_num(aff, t);
691 isl_int_clear(t);
693 return aff;
696 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
698 aff = isl_aff_cow(aff);
699 if (!aff)
700 return NULL;
702 aff->v = isl_vec_cow(aff->v);
703 if (!aff->v)
704 return isl_aff_free(aff);
706 isl_int_set_si(aff->v->el[1], v);
708 return aff;
711 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
712 enum isl_dim_type type, int pos, isl_int v)
714 if (!aff)
715 return NULL;
717 if (type == isl_dim_out)
718 isl_die(aff->v->ctx, isl_error_invalid,
719 "output/set dimension does not have a coefficient",
720 return isl_aff_free(aff));
721 if (type == isl_dim_in)
722 type = isl_dim_set;
724 if (pos >= isl_local_space_dim(aff->ls, type))
725 isl_die(aff->v->ctx, isl_error_invalid,
726 "position out of bounds", return isl_aff_free(aff));
728 aff = isl_aff_cow(aff);
729 if (!aff)
730 return NULL;
732 aff->v = isl_vec_cow(aff->v);
733 if (!aff->v)
734 return isl_aff_free(aff);
736 pos += isl_local_space_offset(aff->ls, type);
737 isl_int_set(aff->v->el[1 + pos], v);
739 return aff;
742 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
743 enum isl_dim_type type, int pos, 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_si(aff->v->el[1 + pos], v);
770 return aff;
773 /* Replace the coefficient of the variable of type "type" at position "pos"
774 * of "aff" by "v".
776 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
777 enum isl_dim_type type, int pos, __isl_take isl_val *v)
779 if (!aff || !v)
780 goto error;
782 if (type == isl_dim_out)
783 isl_die(aff->v->ctx, isl_error_invalid,
784 "output/set dimension does not have a coefficient",
785 goto error);
786 if (type == isl_dim_in)
787 type = isl_dim_set;
789 if (pos >= isl_local_space_dim(aff->ls, type))
790 isl_die(aff->v->ctx, isl_error_invalid,
791 "position out of bounds", goto error);
793 if (!isl_val_is_rat(v))
794 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
795 "expecting rational value", goto error);
797 pos += isl_local_space_offset(aff->ls, type);
798 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
799 isl_int_eq(aff->v->el[0], v->d)) {
800 isl_val_free(v);
801 return aff;
804 aff = isl_aff_cow(aff);
805 if (!aff)
806 goto error;
807 aff->v = isl_vec_cow(aff->v);
808 if (!aff->v)
809 goto error;
811 if (isl_int_eq(aff->v->el[0], v->d)) {
812 isl_int_set(aff->v->el[1 + pos], v->n);
813 } else if (isl_int_is_one(v->d)) {
814 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
815 } else {
816 isl_seq_scale(aff->v->el + 1,
817 aff->v->el + 1, v->d, aff->v->size - 1);
818 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
819 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
820 aff->v = isl_vec_normalize(aff->v);
821 if (!aff->v)
822 goto error;
825 isl_val_free(v);
826 return aff;
827 error:
828 isl_aff_free(aff);
829 isl_val_free(v);
830 return NULL;
833 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
834 enum isl_dim_type type, int pos, isl_int v)
836 if (!aff)
837 return NULL;
839 if (type == isl_dim_out)
840 isl_die(aff->v->ctx, isl_error_invalid,
841 "output/set dimension does not have a coefficient",
842 return isl_aff_free(aff));
843 if (type == isl_dim_in)
844 type = isl_dim_set;
846 if (pos >= isl_local_space_dim(aff->ls, type))
847 isl_die(aff->v->ctx, isl_error_invalid,
848 "position out of bounds", return isl_aff_free(aff));
850 aff = isl_aff_cow(aff);
851 if (!aff)
852 return NULL;
854 aff->v = isl_vec_cow(aff->v);
855 if (!aff->v)
856 return isl_aff_free(aff);
858 pos += isl_local_space_offset(aff->ls, type);
859 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
861 return aff;
864 /* Add "v" to the coefficient of the variable of type "type"
865 * at position "pos" of "aff".
867 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
868 enum isl_dim_type type, int pos, __isl_take isl_val *v)
870 if (!aff || !v)
871 goto error;
873 if (isl_val_is_zero(v)) {
874 isl_val_free(v);
875 return aff;
878 if (type == isl_dim_out)
879 isl_die(aff->v->ctx, isl_error_invalid,
880 "output/set dimension does not have a coefficient",
881 goto error);
882 if (type == isl_dim_in)
883 type = isl_dim_set;
885 if (pos >= isl_local_space_dim(aff->ls, type))
886 isl_die(aff->v->ctx, isl_error_invalid,
887 "position out of bounds", goto error);
889 if (!isl_val_is_rat(v))
890 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
891 "expecting rational value", goto error);
893 aff = isl_aff_cow(aff);
894 if (!aff)
895 goto error;
897 aff->v = isl_vec_cow(aff->v);
898 if (!aff->v)
899 goto error;
901 pos += isl_local_space_offset(aff->ls, type);
902 if (isl_int_is_one(v->d)) {
903 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
904 } else if (isl_int_eq(aff->v->el[0], v->d)) {
905 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
906 aff->v = isl_vec_normalize(aff->v);
907 if (!aff->v)
908 goto error;
909 } else {
910 isl_seq_scale(aff->v->el + 1,
911 aff->v->el + 1, v->d, aff->v->size - 1);
912 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
913 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
914 aff->v = isl_vec_normalize(aff->v);
915 if (!aff->v)
916 goto error;
919 isl_val_free(v);
920 return aff;
921 error:
922 isl_aff_free(aff);
923 isl_val_free(v);
924 return NULL;
927 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
928 enum isl_dim_type type, int pos, int v)
930 isl_int t;
932 isl_int_init(t);
933 isl_int_set_si(t, v);
934 aff = isl_aff_add_coefficient(aff, type, pos, t);
935 isl_int_clear(t);
937 return aff;
940 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
942 if (!aff)
943 return NULL;
945 return isl_local_space_get_div(aff->ls, pos);
948 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
950 aff = isl_aff_cow(aff);
951 if (!aff)
952 return NULL;
953 aff->v = isl_vec_cow(aff->v);
954 if (!aff->v)
955 return isl_aff_free(aff);
957 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
959 return aff;
962 /* Remove divs from the local space that do not appear in the affine
963 * expression.
964 * We currently only remove divs at the end.
965 * Some intermediate divs may also not appear directly in the affine
966 * expression, but we would also need to check that no other divs are
967 * defined in terms of them.
969 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
971 int pos;
972 int off;
973 int n;
975 if (!aff)
976 return NULL;
978 n = isl_local_space_dim(aff->ls, isl_dim_div);
979 off = isl_local_space_offset(aff->ls, isl_dim_div);
981 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
982 if (pos == n)
983 return aff;
985 aff = isl_aff_cow(aff);
986 if (!aff)
987 return NULL;
989 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
990 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
991 if (!aff->ls || !aff->v)
992 return isl_aff_free(aff);
994 return aff;
997 /* Given two affine expressions "p" of length p_len (including the
998 * denominator and the constant term) and "subs" of length subs_len,
999 * plug in "subs" for the variable at position "pos".
1000 * The variables of "subs" and "p" are assumed to match up to subs_len,
1001 * but "p" may have additional variables.
1002 * "v" is an initialized isl_int that can be used internally.
1004 * In particular, if "p" represents the expression
1006 * (a i + g)/m
1008 * with i the variable at position "pos" and "subs" represents the expression
1010 * f/d
1012 * then the result represents the expression
1014 * (a f + d g)/(m d)
1017 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1018 int p_len, int subs_len, isl_int v)
1020 isl_int_set(v, p[1 + pos]);
1021 isl_int_set_si(p[1 + pos], 0);
1022 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1023 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1024 isl_int_mul(p[0], p[0], subs[0]);
1027 /* Look for any divs in the aff->ls with a denominator equal to one
1028 * and plug them into the affine expression and any subsequent divs
1029 * that may reference the div.
1031 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1033 int i, n;
1034 int len;
1035 isl_int v;
1036 isl_vec *vec;
1037 isl_local_space *ls;
1038 unsigned pos;
1040 if (!aff)
1041 return NULL;
1043 n = isl_local_space_dim(aff->ls, isl_dim_div);
1044 len = aff->v->size;
1045 for (i = 0; i < n; ++i) {
1046 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1047 continue;
1048 ls = isl_local_space_copy(aff->ls);
1049 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1050 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1051 vec = isl_vec_copy(aff->v);
1052 vec = isl_vec_cow(vec);
1053 if (!ls || !vec)
1054 goto error;
1056 isl_int_init(v);
1058 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1059 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1060 len, len, v);
1062 isl_int_clear(v);
1064 isl_vec_free(aff->v);
1065 aff->v = vec;
1066 isl_local_space_free(aff->ls);
1067 aff->ls = ls;
1070 return aff;
1071 error:
1072 isl_vec_free(vec);
1073 isl_local_space_free(ls);
1074 return isl_aff_free(aff);
1077 /* Look for any divs j that appear with a unit coefficient inside
1078 * the definitions of other divs i and plug them into the definitions
1079 * of the divs i.
1081 * In particular, an expression of the form
1083 * floor((f(..) + floor(g(..)/n))/m)
1085 * is simplified to
1087 * floor((n * f(..) + g(..))/(n * m))
1089 * This simplification is correct because we can move the expression
1090 * f(..) into the inner floor in the original expression to obtain
1092 * floor(floor((n * f(..) + g(..))/n)/m)
1094 * from which we can derive the simplified expression.
1096 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1098 int i, j, n;
1099 int off;
1101 if (!aff)
1102 return NULL;
1104 n = isl_local_space_dim(aff->ls, isl_dim_div);
1105 off = isl_local_space_offset(aff->ls, isl_dim_div);
1106 for (i = 1; i < n; ++i) {
1107 for (j = 0; j < i; ++j) {
1108 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1109 continue;
1110 aff->ls = isl_local_space_substitute_seq(aff->ls,
1111 isl_dim_div, j, aff->ls->div->row[j],
1112 aff->v->size, i, 1);
1113 if (!aff->ls)
1114 return isl_aff_free(aff);
1118 return aff;
1121 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1123 * Even though this function is only called on isl_affs with a single
1124 * reference, we are careful to only change aff->v and aff->ls together.
1126 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1128 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1129 isl_local_space *ls;
1130 isl_vec *v;
1132 ls = isl_local_space_copy(aff->ls);
1133 ls = isl_local_space_swap_div(ls, a, b);
1134 v = isl_vec_copy(aff->v);
1135 v = isl_vec_cow(v);
1136 if (!ls || !v)
1137 goto error;
1139 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1140 isl_vec_free(aff->v);
1141 aff->v = v;
1142 isl_local_space_free(aff->ls);
1143 aff->ls = ls;
1145 return aff;
1146 error:
1147 isl_vec_free(v);
1148 isl_local_space_free(ls);
1149 return isl_aff_free(aff);
1152 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1154 * We currently do not actually remove div "b", but simply add its
1155 * coefficient to that of "a" and then zero it out.
1157 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1159 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1161 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1162 return aff;
1164 aff->v = isl_vec_cow(aff->v);
1165 if (!aff->v)
1166 return isl_aff_free(aff);
1168 isl_int_add(aff->v->el[1 + off + a],
1169 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1170 isl_int_set_si(aff->v->el[1 + off + b], 0);
1172 return aff;
1175 /* Sort the divs in the local space of "aff" according to
1176 * the comparison function "cmp_row" in isl_local_space.c,
1177 * combining the coefficients of identical divs.
1179 * Reordering divs does not change the semantics of "aff",
1180 * so there is no need to call isl_aff_cow.
1181 * Moreover, this function is currently only called on isl_affs
1182 * with a single reference.
1184 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1186 int i, j, n;
1187 unsigned off;
1189 if (!aff)
1190 return NULL;
1192 off = isl_local_space_offset(aff->ls, isl_dim_div);
1193 n = isl_aff_dim(aff, isl_dim_div);
1194 for (i = 1; i < n; ++i) {
1195 for (j = i - 1; j >= 0; --j) {
1196 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1197 if (cmp < 0)
1198 break;
1199 if (cmp == 0)
1200 aff = merge_divs(aff, j, j + 1);
1201 else
1202 aff = swap_div(aff, j, j + 1);
1203 if (!aff)
1204 return NULL;
1208 return aff;
1211 /* Normalize the representation of "aff".
1213 * This function should only be called of "new" isl_affs, i.e.,
1214 * with only a single reference. We therefore do not need to
1215 * worry about affecting other instances.
1217 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1219 if (!aff)
1220 return NULL;
1221 aff->v = isl_vec_normalize(aff->v);
1222 if (!aff->v)
1223 return isl_aff_free(aff);
1224 aff = plug_in_integral_divs(aff);
1225 aff = plug_in_unit_divs(aff);
1226 aff = sort_divs(aff);
1227 aff = isl_aff_remove_unused_divs(aff);
1228 return aff;
1231 /* Given f, return floor(f).
1232 * If f is an integer expression, then just return f.
1233 * If f is a constant, then return the constant floor(f).
1234 * Otherwise, if f = g/m, write g = q m + r,
1235 * create a new div d = [r/m] and return the expression q + d.
1236 * The coefficients in r are taken to lie between -m/2 and m/2.
1238 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1240 int i;
1241 int size;
1242 isl_ctx *ctx;
1243 isl_vec *div;
1245 if (!aff)
1246 return NULL;
1248 if (isl_int_is_one(aff->v->el[0]))
1249 return aff;
1251 aff = isl_aff_cow(aff);
1252 if (!aff)
1253 return NULL;
1255 aff->v = isl_vec_cow(aff->v);
1256 if (!aff->v)
1257 return isl_aff_free(aff);
1259 if (isl_aff_is_cst(aff)) {
1260 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1261 isl_int_set_si(aff->v->el[0], 1);
1262 return aff;
1265 div = isl_vec_copy(aff->v);
1266 div = isl_vec_cow(div);
1267 if (!div)
1268 return isl_aff_free(aff);
1270 ctx = isl_aff_get_ctx(aff);
1271 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1272 for (i = 1; i < aff->v->size; ++i) {
1273 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1274 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1275 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1276 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1277 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1281 aff->ls = isl_local_space_add_div(aff->ls, div);
1282 if (!aff->ls)
1283 return isl_aff_free(aff);
1285 size = aff->v->size;
1286 aff->v = isl_vec_extend(aff->v, size + 1);
1287 if (!aff->v)
1288 return isl_aff_free(aff);
1289 isl_int_set_si(aff->v->el[0], 1);
1290 isl_int_set_si(aff->v->el[size], 1);
1292 aff = isl_aff_normalize(aff);
1294 return aff;
1297 /* Compute
1299 * aff mod m = aff - m * floor(aff/m)
1301 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1303 isl_aff *res;
1305 res = isl_aff_copy(aff);
1306 aff = isl_aff_scale_down(aff, m);
1307 aff = isl_aff_floor(aff);
1308 aff = isl_aff_scale(aff, m);
1309 res = isl_aff_sub(res, aff);
1311 return res;
1314 /* Compute
1316 * aff mod m = aff - m * floor(aff/m)
1318 * with m an integer value.
1320 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1321 __isl_take isl_val *m)
1323 isl_aff *res;
1325 if (!aff || !m)
1326 goto error;
1328 if (!isl_val_is_int(m))
1329 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1330 "expecting integer modulo", goto error);
1332 res = isl_aff_copy(aff);
1333 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1334 aff = isl_aff_floor(aff);
1335 aff = isl_aff_scale_val(aff, m);
1336 res = isl_aff_sub(res, aff);
1338 return res;
1339 error:
1340 isl_aff_free(aff);
1341 isl_val_free(m);
1342 return NULL;
1345 /* Compute
1347 * pwaff mod m = pwaff - m * floor(pwaff/m)
1349 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1351 isl_pw_aff *res;
1353 res = isl_pw_aff_copy(pwaff);
1354 pwaff = isl_pw_aff_scale_down(pwaff, m);
1355 pwaff = isl_pw_aff_floor(pwaff);
1356 pwaff = isl_pw_aff_scale(pwaff, m);
1357 res = isl_pw_aff_sub(res, pwaff);
1359 return res;
1362 /* Compute
1364 * pa mod m = pa - m * floor(pa/m)
1366 * with m an integer value.
1368 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1369 __isl_take isl_val *m)
1371 if (!pa || !m)
1372 goto error;
1373 if (!isl_val_is_int(m))
1374 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1375 "expecting integer modulo", goto error);
1376 pa = isl_pw_aff_mod(pa, m->n);
1377 isl_val_free(m);
1378 return pa;
1379 error:
1380 isl_pw_aff_free(pa);
1381 isl_val_free(m);
1382 return NULL;
1385 /* Given f, return ceil(f).
1386 * If f is an integer expression, then just return f.
1387 * Otherwise, let f be the expression
1389 * e/m
1391 * then return
1393 * floor((e + m - 1)/m)
1395 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1397 if (!aff)
1398 return NULL;
1400 if (isl_int_is_one(aff->v->el[0]))
1401 return aff;
1403 aff = isl_aff_cow(aff);
1404 if (!aff)
1405 return NULL;
1406 aff->v = isl_vec_cow(aff->v);
1407 if (!aff->v)
1408 return isl_aff_free(aff);
1410 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1411 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1412 aff = isl_aff_floor(aff);
1414 return aff;
1417 /* Apply the expansion computed by isl_merge_divs.
1418 * The expansion itself is given by "exp" while the resulting
1419 * list of divs is given by "div".
1421 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1422 __isl_take isl_mat *div, int *exp)
1424 int i, j;
1425 int old_n_div;
1426 int new_n_div;
1427 int offset;
1429 aff = isl_aff_cow(aff);
1430 if (!aff || !div)
1431 goto error;
1433 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1434 new_n_div = isl_mat_rows(div);
1435 if (new_n_div < old_n_div)
1436 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1437 "not an expansion", goto error);
1439 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1440 if (!aff->v)
1441 goto error;
1443 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1444 j = old_n_div - 1;
1445 for (i = new_n_div - 1; i >= 0; --i) {
1446 if (j >= 0 && exp[j] == i) {
1447 if (i != j)
1448 isl_int_swap(aff->v->el[offset + i],
1449 aff->v->el[offset + j]);
1450 j--;
1451 } else
1452 isl_int_set_si(aff->v->el[offset + i], 0);
1455 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1456 if (!aff->ls)
1457 goto error;
1458 isl_mat_free(div);
1459 return aff;
1460 error:
1461 isl_aff_free(aff);
1462 isl_mat_free(div);
1463 return NULL;
1466 /* Add two affine expressions that live in the same local space.
1468 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1469 __isl_take isl_aff *aff2)
1471 isl_int gcd, f;
1473 aff1 = isl_aff_cow(aff1);
1474 if (!aff1 || !aff2)
1475 goto error;
1477 aff1->v = isl_vec_cow(aff1->v);
1478 if (!aff1->v)
1479 goto error;
1481 isl_int_init(gcd);
1482 isl_int_init(f);
1483 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1484 isl_int_divexact(f, aff2->v->el[0], gcd);
1485 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1486 isl_int_divexact(f, aff1->v->el[0], gcd);
1487 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1488 isl_int_divexact(f, aff2->v->el[0], gcd);
1489 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1490 isl_int_clear(f);
1491 isl_int_clear(gcd);
1493 isl_aff_free(aff2);
1494 return aff1;
1495 error:
1496 isl_aff_free(aff1);
1497 isl_aff_free(aff2);
1498 return NULL;
1501 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1502 __isl_take isl_aff *aff2)
1504 isl_ctx *ctx;
1505 int *exp1 = NULL;
1506 int *exp2 = NULL;
1507 isl_mat *div;
1508 int n_div1, n_div2;
1510 if (!aff1 || !aff2)
1511 goto error;
1513 ctx = isl_aff_get_ctx(aff1);
1514 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1515 isl_die(ctx, isl_error_invalid,
1516 "spaces don't match", goto error);
1518 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1519 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1520 if (n_div1 == 0 && n_div2 == 0)
1521 return add_expanded(aff1, aff2);
1523 exp1 = isl_alloc_array(ctx, int, n_div1);
1524 exp2 = isl_alloc_array(ctx, int, n_div2);
1525 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1526 goto error;
1528 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1529 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1530 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1531 free(exp1);
1532 free(exp2);
1534 return add_expanded(aff1, aff2);
1535 error:
1536 free(exp1);
1537 free(exp2);
1538 isl_aff_free(aff1);
1539 isl_aff_free(aff2);
1540 return NULL;
1543 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1544 __isl_take isl_aff *aff2)
1546 return isl_aff_add(aff1, isl_aff_neg(aff2));
1549 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1551 isl_int gcd;
1553 if (isl_int_is_one(f))
1554 return aff;
1556 aff = isl_aff_cow(aff);
1557 if (!aff)
1558 return NULL;
1559 aff->v = isl_vec_cow(aff->v);
1560 if (!aff->v)
1561 return isl_aff_free(aff);
1563 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1564 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1565 return aff;
1568 isl_int_init(gcd);
1569 isl_int_gcd(gcd, aff->v->el[0], f);
1570 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1571 isl_int_divexact(gcd, f, gcd);
1572 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1573 isl_int_clear(gcd);
1575 return aff;
1578 /* Multiple "aff" by "v".
1580 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1581 __isl_take isl_val *v)
1583 if (!aff || !v)
1584 goto error;
1586 if (isl_val_is_one(v)) {
1587 isl_val_free(v);
1588 return aff;
1591 if (!isl_val_is_rat(v))
1592 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1593 "expecting rational factor", goto error);
1595 aff = isl_aff_scale(aff, v->n);
1596 aff = isl_aff_scale_down(aff, v->d);
1598 isl_val_free(v);
1599 return aff;
1600 error:
1601 isl_aff_free(aff);
1602 isl_val_free(v);
1603 return NULL;
1606 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1608 isl_int gcd;
1610 if (isl_int_is_one(f))
1611 return aff;
1613 aff = isl_aff_cow(aff);
1614 if (!aff)
1615 return NULL;
1617 if (isl_int_is_zero(f))
1618 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1619 "cannot scale down by zero", return isl_aff_free(aff));
1621 aff->v = isl_vec_cow(aff->v);
1622 if (!aff->v)
1623 return isl_aff_free(aff);
1625 isl_int_init(gcd);
1626 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1627 isl_int_gcd(gcd, gcd, f);
1628 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1629 isl_int_divexact(gcd, f, gcd);
1630 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1631 isl_int_clear(gcd);
1633 return aff;
1636 /* Divide "aff" by "v".
1638 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1639 __isl_take isl_val *v)
1641 if (!aff || !v)
1642 goto error;
1644 if (isl_val_is_one(v)) {
1645 isl_val_free(v);
1646 return aff;
1649 if (!isl_val_is_rat(v))
1650 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1651 "expecting rational factor", goto error);
1652 if (!isl_val_is_pos(v))
1653 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1654 "factor needs to be positive", goto error);
1656 aff = isl_aff_scale(aff, v->d);
1657 aff = isl_aff_scale_down(aff, v->n);
1659 isl_val_free(v);
1660 return aff;
1661 error:
1662 isl_aff_free(aff);
1663 isl_val_free(v);
1664 return NULL;
1667 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1669 isl_int v;
1671 if (f == 1)
1672 return aff;
1674 isl_int_init(v);
1675 isl_int_set_ui(v, f);
1676 aff = isl_aff_scale_down(aff, v);
1677 isl_int_clear(v);
1679 return aff;
1682 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1683 enum isl_dim_type type, unsigned pos, const char *s)
1685 aff = isl_aff_cow(aff);
1686 if (!aff)
1687 return NULL;
1688 if (type == isl_dim_out)
1689 isl_die(aff->v->ctx, isl_error_invalid,
1690 "cannot set name of output/set dimension",
1691 return isl_aff_free(aff));
1692 if (type == isl_dim_in)
1693 type = isl_dim_set;
1694 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1695 if (!aff->ls)
1696 return isl_aff_free(aff);
1698 return aff;
1701 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1702 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1704 aff = isl_aff_cow(aff);
1705 if (!aff)
1706 return isl_id_free(id);
1707 if (type == isl_dim_out)
1708 isl_die(aff->v->ctx, isl_error_invalid,
1709 "cannot set name of output/set dimension",
1710 goto error);
1711 if (type == isl_dim_in)
1712 type = isl_dim_set;
1713 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1714 if (!aff->ls)
1715 return isl_aff_free(aff);
1717 return aff;
1718 error:
1719 isl_id_free(id);
1720 isl_aff_free(aff);
1721 return NULL;
1724 /* Exploit the equalities in "eq" to simplify the affine expression
1725 * and the expressions of the integer divisions in the local space.
1726 * The integer divisions in this local space are assumed to appear
1727 * as regular dimensions in "eq".
1729 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1730 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1732 int i, j;
1733 unsigned total;
1734 unsigned n_div;
1736 if (!eq)
1737 goto error;
1738 if (eq->n_eq == 0) {
1739 isl_basic_set_free(eq);
1740 return aff;
1743 aff = isl_aff_cow(aff);
1744 if (!aff)
1745 goto error;
1747 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1748 isl_basic_set_copy(eq));
1749 aff->v = isl_vec_cow(aff->v);
1750 if (!aff->ls || !aff->v)
1751 goto error;
1753 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1754 n_div = eq->n_div;
1755 for (i = 0; i < eq->n_eq; ++i) {
1756 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1757 if (j < 0 || j == 0 || j >= total)
1758 continue;
1760 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1761 &aff->v->el[0]);
1764 isl_basic_set_free(eq);
1765 aff = isl_aff_normalize(aff);
1766 return aff;
1767 error:
1768 isl_basic_set_free(eq);
1769 isl_aff_free(aff);
1770 return NULL;
1773 /* Exploit the equalities in "eq" to simplify the affine expression
1774 * and the expressions of the integer divisions in the local space.
1776 static __isl_give isl_aff *isl_aff_substitute_equalities(
1777 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1779 int n_div;
1781 if (!aff || !eq)
1782 goto error;
1783 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1784 if (n_div > 0)
1785 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1786 return isl_aff_substitute_equalities_lifted(aff, eq);
1787 error:
1788 isl_basic_set_free(eq);
1789 isl_aff_free(aff);
1790 return NULL;
1793 /* Look for equalities among the variables shared by context and aff
1794 * and the integer divisions of aff, if any.
1795 * The equalities are then used to eliminate coefficients and/or integer
1796 * divisions from aff.
1798 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1799 __isl_take isl_set *context)
1801 isl_basic_set *hull;
1802 int n_div;
1804 if (!aff)
1805 goto error;
1806 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1807 if (n_div > 0) {
1808 isl_basic_set *bset;
1809 isl_local_space *ls;
1810 context = isl_set_add_dims(context, isl_dim_set, n_div);
1811 ls = isl_aff_get_domain_local_space(aff);
1812 bset = isl_basic_set_from_local_space(ls);
1813 bset = isl_basic_set_lift(bset);
1814 bset = isl_basic_set_flatten(bset);
1815 context = isl_set_intersect(context,
1816 isl_set_from_basic_set(bset));
1819 hull = isl_set_affine_hull(context);
1820 return isl_aff_substitute_equalities_lifted(aff, hull);
1821 error:
1822 isl_aff_free(aff);
1823 isl_set_free(context);
1824 return NULL;
1827 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1828 __isl_take isl_set *context)
1830 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1831 dom_context = isl_set_intersect_params(dom_context, context);
1832 return isl_aff_gist(aff, dom_context);
1835 /* Return a basic set containing those elements in the space
1836 * of aff where it is non-negative.
1837 * If "rational" is set, then return a rational basic set.
1839 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1840 __isl_take isl_aff *aff, int rational)
1842 isl_constraint *ineq;
1843 isl_basic_set *bset;
1845 ineq = isl_inequality_from_aff(aff);
1847 bset = isl_basic_set_from_constraint(ineq);
1848 if (rational)
1849 bset = isl_basic_set_set_rational(bset);
1850 bset = isl_basic_set_simplify(bset);
1851 return bset;
1854 /* Return a basic set containing those elements in the space
1855 * of aff where it is non-negative.
1857 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1859 return aff_nonneg_basic_set(aff, 0);
1862 /* Return a basic set containing those elements in the domain space
1863 * of aff where it is negative.
1865 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1867 aff = isl_aff_neg(aff);
1868 aff = isl_aff_add_constant_num_si(aff, -1);
1869 return isl_aff_nonneg_basic_set(aff);
1872 /* Return a basic set containing those elements in the space
1873 * of aff where it is zero.
1874 * If "rational" is set, then return a rational basic set.
1876 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1877 int rational)
1879 isl_constraint *ineq;
1880 isl_basic_set *bset;
1882 ineq = isl_equality_from_aff(aff);
1884 bset = isl_basic_set_from_constraint(ineq);
1885 if (rational)
1886 bset = isl_basic_set_set_rational(bset);
1887 bset = isl_basic_set_simplify(bset);
1888 return bset;
1891 /* Return a basic set containing those elements in the space
1892 * of aff where it is zero.
1894 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1896 return aff_zero_basic_set(aff, 0);
1899 /* Return a basic set containing those elements in the shared space
1900 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1902 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1903 __isl_take isl_aff *aff2)
1905 aff1 = isl_aff_sub(aff1, aff2);
1907 return isl_aff_nonneg_basic_set(aff1);
1910 /* Return a basic set containing those elements in the shared space
1911 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1913 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1914 __isl_take isl_aff *aff2)
1916 return isl_aff_ge_basic_set(aff2, aff1);
1919 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1920 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1922 aff1 = isl_aff_add(aff1, aff2);
1923 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1924 return aff1;
1927 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1929 if (!aff)
1930 return -1;
1932 return 0;
1935 /* Check whether the given affine expression has non-zero coefficient
1936 * for any dimension in the given range or if any of these dimensions
1937 * appear with non-zero coefficients in any of the integer divisions
1938 * involved in the affine expression.
1940 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1941 enum isl_dim_type type, unsigned first, unsigned n)
1943 int i;
1944 isl_ctx *ctx;
1945 int *active = NULL;
1946 int involves = 0;
1948 if (!aff)
1949 return -1;
1950 if (n == 0)
1951 return 0;
1953 ctx = isl_aff_get_ctx(aff);
1954 if (first + n > isl_aff_dim(aff, type))
1955 isl_die(ctx, isl_error_invalid,
1956 "range out of bounds", return -1);
1958 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1959 if (!active)
1960 goto error;
1962 first += isl_local_space_offset(aff->ls, type) - 1;
1963 for (i = 0; i < n; ++i)
1964 if (active[first + i]) {
1965 involves = 1;
1966 break;
1969 free(active);
1971 return involves;
1972 error:
1973 free(active);
1974 return -1;
1977 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1978 enum isl_dim_type type, unsigned first, unsigned n)
1980 isl_ctx *ctx;
1982 if (!aff)
1983 return NULL;
1984 if (type == isl_dim_out)
1985 isl_die(aff->v->ctx, isl_error_invalid,
1986 "cannot drop output/set dimension",
1987 return isl_aff_free(aff));
1988 if (type == isl_dim_in)
1989 type = isl_dim_set;
1990 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1991 return aff;
1993 ctx = isl_aff_get_ctx(aff);
1994 if (first + n > isl_local_space_dim(aff->ls, type))
1995 isl_die(ctx, isl_error_invalid, "range out of bounds",
1996 return isl_aff_free(aff));
1998 aff = isl_aff_cow(aff);
1999 if (!aff)
2000 return NULL;
2002 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2003 if (!aff->ls)
2004 return isl_aff_free(aff);
2006 first += 1 + isl_local_space_offset(aff->ls, type);
2007 aff->v = isl_vec_drop_els(aff->v, first, n);
2008 if (!aff->v)
2009 return isl_aff_free(aff);
2011 return aff;
2014 /* Project the domain of the affine expression onto its parameter space.
2015 * The affine expression may not involve any of the domain dimensions.
2017 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2019 isl_space *space;
2020 unsigned n;
2021 int involves;
2023 n = isl_aff_dim(aff, isl_dim_in);
2024 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2025 if (involves < 0)
2026 return isl_aff_free(aff);
2027 if (involves)
2028 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2029 "affine expression involves some of the domain dimensions",
2030 return isl_aff_free(aff));
2031 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2032 space = isl_aff_get_domain_space(aff);
2033 space = isl_space_params(space);
2034 aff = isl_aff_reset_domain_space(aff, space);
2035 return aff;
2038 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2039 enum isl_dim_type type, unsigned first, unsigned n)
2041 isl_ctx *ctx;
2043 if (!aff)
2044 return NULL;
2045 if (type == isl_dim_out)
2046 isl_die(aff->v->ctx, isl_error_invalid,
2047 "cannot insert output/set dimensions",
2048 return isl_aff_free(aff));
2049 if (type == isl_dim_in)
2050 type = isl_dim_set;
2051 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2052 return aff;
2054 ctx = isl_aff_get_ctx(aff);
2055 if (first > isl_local_space_dim(aff->ls, type))
2056 isl_die(ctx, isl_error_invalid, "position out of bounds",
2057 return isl_aff_free(aff));
2059 aff = isl_aff_cow(aff);
2060 if (!aff)
2061 return NULL;
2063 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2064 if (!aff->ls)
2065 return isl_aff_free(aff);
2067 first += 1 + isl_local_space_offset(aff->ls, type);
2068 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2069 if (!aff->v)
2070 return isl_aff_free(aff);
2072 return aff;
2075 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2076 enum isl_dim_type type, unsigned n)
2078 unsigned pos;
2080 pos = isl_aff_dim(aff, type);
2082 return isl_aff_insert_dims(aff, type, pos, n);
2085 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2086 enum isl_dim_type type, unsigned n)
2088 unsigned pos;
2090 pos = isl_pw_aff_dim(pwaff, type);
2092 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2095 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2097 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2098 return isl_pw_aff_alloc(dom, aff);
2101 #undef PW
2102 #define PW isl_pw_aff
2103 #undef EL
2104 #define EL isl_aff
2105 #undef EL_IS_ZERO
2106 #define EL_IS_ZERO is_empty
2107 #undef ZERO
2108 #define ZERO empty
2109 #undef IS_ZERO
2110 #define IS_ZERO is_empty
2111 #undef FIELD
2112 #define FIELD aff
2113 #undef DEFAULT_IS_ZERO
2114 #define DEFAULT_IS_ZERO 0
2116 #define NO_EVAL
2117 #define NO_OPT
2118 #define NO_MOVE_DIMS
2119 #define NO_LIFT
2120 #define NO_MORPH
2122 #include <isl_pw_templ.c>
2124 static __isl_give isl_set *align_params_pw_pw_set_and(
2125 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2126 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2127 __isl_take isl_pw_aff *pwaff2))
2129 if (!pwaff1 || !pwaff2)
2130 goto error;
2131 if (isl_space_match(pwaff1->dim, isl_dim_param,
2132 pwaff2->dim, isl_dim_param))
2133 return fn(pwaff1, pwaff2);
2134 if (!isl_space_has_named_params(pwaff1->dim) ||
2135 !isl_space_has_named_params(pwaff2->dim))
2136 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2137 "unaligned unnamed parameters", goto error);
2138 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2139 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2140 return fn(pwaff1, pwaff2);
2141 error:
2142 isl_pw_aff_free(pwaff1);
2143 isl_pw_aff_free(pwaff2);
2144 return NULL;
2147 /* Compute a piecewise quasi-affine expression with a domain that
2148 * is the union of those of pwaff1 and pwaff2 and such that on each
2149 * cell, the quasi-affine expression is the better (according to cmp)
2150 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2151 * is defined on a given cell, then the associated expression
2152 * is the defined one.
2154 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2155 __isl_take isl_pw_aff *pwaff2,
2156 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2157 __isl_take isl_aff *aff2))
2159 int i, j, n;
2160 isl_pw_aff *res;
2161 isl_ctx *ctx;
2162 isl_set *set;
2164 if (!pwaff1 || !pwaff2)
2165 goto error;
2167 ctx = isl_space_get_ctx(pwaff1->dim);
2168 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2169 isl_die(ctx, isl_error_invalid,
2170 "arguments should live in same space", goto error);
2172 if (isl_pw_aff_is_empty(pwaff1)) {
2173 isl_pw_aff_free(pwaff1);
2174 return pwaff2;
2177 if (isl_pw_aff_is_empty(pwaff2)) {
2178 isl_pw_aff_free(pwaff2);
2179 return pwaff1;
2182 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2183 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2185 for (i = 0; i < pwaff1->n; ++i) {
2186 set = isl_set_copy(pwaff1->p[i].set);
2187 for (j = 0; j < pwaff2->n; ++j) {
2188 struct isl_set *common;
2189 isl_set *better;
2191 common = isl_set_intersect(
2192 isl_set_copy(pwaff1->p[i].set),
2193 isl_set_copy(pwaff2->p[j].set));
2194 better = isl_set_from_basic_set(cmp(
2195 isl_aff_copy(pwaff2->p[j].aff),
2196 isl_aff_copy(pwaff1->p[i].aff)));
2197 better = isl_set_intersect(common, better);
2198 if (isl_set_plain_is_empty(better)) {
2199 isl_set_free(better);
2200 continue;
2202 set = isl_set_subtract(set, isl_set_copy(better));
2204 res = isl_pw_aff_add_piece(res, better,
2205 isl_aff_copy(pwaff2->p[j].aff));
2207 res = isl_pw_aff_add_piece(res, set,
2208 isl_aff_copy(pwaff1->p[i].aff));
2211 for (j = 0; j < pwaff2->n; ++j) {
2212 set = isl_set_copy(pwaff2->p[j].set);
2213 for (i = 0; i < pwaff1->n; ++i)
2214 set = isl_set_subtract(set,
2215 isl_set_copy(pwaff1->p[i].set));
2216 res = isl_pw_aff_add_piece(res, set,
2217 isl_aff_copy(pwaff2->p[j].aff));
2220 isl_pw_aff_free(pwaff1);
2221 isl_pw_aff_free(pwaff2);
2223 return res;
2224 error:
2225 isl_pw_aff_free(pwaff1);
2226 isl_pw_aff_free(pwaff2);
2227 return NULL;
2230 /* Compute a piecewise quasi-affine expression with a domain that
2231 * is the union of those of pwaff1 and pwaff2 and such that on each
2232 * cell, the quasi-affine expression is the maximum of those of pwaff1
2233 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2234 * cell, then the associated expression is the defined one.
2236 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2237 __isl_take isl_pw_aff *pwaff2)
2239 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2242 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2243 __isl_take isl_pw_aff *pwaff2)
2245 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2246 &pw_aff_union_max);
2249 /* Compute a piecewise quasi-affine expression with a domain that
2250 * is the union of those of pwaff1 and pwaff2 and such that on each
2251 * cell, the quasi-affine expression is the minimum of those of pwaff1
2252 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2253 * cell, then the associated expression is the defined one.
2255 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2256 __isl_take isl_pw_aff *pwaff2)
2258 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2261 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2262 __isl_take isl_pw_aff *pwaff2)
2264 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2265 &pw_aff_union_min);
2268 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2269 __isl_take isl_pw_aff *pwaff2, int max)
2271 if (max)
2272 return isl_pw_aff_union_max(pwaff1, pwaff2);
2273 else
2274 return isl_pw_aff_union_min(pwaff1, pwaff2);
2277 /* Construct a map with as domain the domain of pwaff and
2278 * one-dimensional range corresponding to the affine expressions.
2280 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2282 int i;
2283 isl_space *dim;
2284 isl_map *map;
2286 if (!pwaff)
2287 return NULL;
2289 dim = isl_pw_aff_get_space(pwaff);
2290 map = isl_map_empty(dim);
2292 for (i = 0; i < pwaff->n; ++i) {
2293 isl_basic_map *bmap;
2294 isl_map *map_i;
2296 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2297 map_i = isl_map_from_basic_map(bmap);
2298 map_i = isl_map_intersect_domain(map_i,
2299 isl_set_copy(pwaff->p[i].set));
2300 map = isl_map_union_disjoint(map, map_i);
2303 isl_pw_aff_free(pwaff);
2305 return map;
2308 /* Construct a map with as domain the domain of pwaff and
2309 * one-dimensional range corresponding to the affine expressions.
2311 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2313 if (!pwaff)
2314 return NULL;
2315 if (isl_space_is_set(pwaff->dim))
2316 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2317 "space of input is not a map",
2318 return isl_pw_aff_free(pwaff));
2319 return map_from_pw_aff(pwaff);
2322 /* Construct a one-dimensional set with as parameter domain
2323 * the domain of pwaff and the single set dimension
2324 * corresponding to the affine expressions.
2326 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2328 if (!pwaff)
2329 return NULL;
2330 if (!isl_space_is_set(pwaff->dim))
2331 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2332 "space of input is not a set",
2333 return isl_pw_aff_free(pwaff));
2334 return map_from_pw_aff(pwaff);
2337 /* Return a set containing those elements in the domain
2338 * of pwaff where it is non-negative.
2340 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2342 int i;
2343 isl_set *set;
2345 if (!pwaff)
2346 return NULL;
2348 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2350 for (i = 0; i < pwaff->n; ++i) {
2351 isl_basic_set *bset;
2352 isl_set *set_i;
2353 int rational;
2355 rational = isl_set_has_rational(pwaff->p[i].set);
2356 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2357 rational);
2358 set_i = isl_set_from_basic_set(bset);
2359 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2360 set = isl_set_union_disjoint(set, set_i);
2363 isl_pw_aff_free(pwaff);
2365 return set;
2368 /* Return a set containing those elements in the domain
2369 * of pwaff where it is zero (if complement is 0) or not zero
2370 * (if complement is 1).
2372 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2373 int complement)
2375 int i;
2376 isl_set *set;
2378 if (!pwaff)
2379 return NULL;
2381 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2383 for (i = 0; i < pwaff->n; ++i) {
2384 isl_basic_set *bset;
2385 isl_set *set_i, *zero;
2386 int rational;
2388 rational = isl_set_has_rational(pwaff->p[i].set);
2389 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2390 rational);
2391 zero = isl_set_from_basic_set(bset);
2392 set_i = isl_set_copy(pwaff->p[i].set);
2393 if (complement)
2394 set_i = isl_set_subtract(set_i, zero);
2395 else
2396 set_i = isl_set_intersect(set_i, zero);
2397 set = isl_set_union_disjoint(set, set_i);
2400 isl_pw_aff_free(pwaff);
2402 return set;
2405 /* Return a set containing those elements in the domain
2406 * of pwaff where it is zero.
2408 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2410 return pw_aff_zero_set(pwaff, 0);
2413 /* Return a set containing those elements in the domain
2414 * of pwaff where it is not zero.
2416 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2418 return pw_aff_zero_set(pwaff, 1);
2421 /* Return a set containing those elements in the shared domain
2422 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2424 * We compute the difference on the shared domain and then construct
2425 * the set of values where this difference is non-negative.
2426 * If strict is set, we first subtract 1 from the difference.
2427 * If equal is set, we only return the elements where pwaff1 and pwaff2
2428 * are equal.
2430 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2431 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2433 isl_set *set1, *set2;
2435 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2436 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2437 set1 = isl_set_intersect(set1, set2);
2438 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2439 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2440 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2442 if (strict) {
2443 isl_space *dim = isl_set_get_space(set1);
2444 isl_aff *aff;
2445 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2446 aff = isl_aff_add_constant_si(aff, -1);
2447 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2448 } else
2449 isl_set_free(set1);
2451 if (equal)
2452 return isl_pw_aff_zero_set(pwaff1);
2453 return isl_pw_aff_nonneg_set(pwaff1);
2456 /* Return a set containing those elements in the shared domain
2457 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2459 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2460 __isl_take isl_pw_aff *pwaff2)
2462 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2465 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2466 __isl_take isl_pw_aff *pwaff2)
2468 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2471 /* Return a set containing those elements in the shared domain
2472 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2474 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2475 __isl_take isl_pw_aff *pwaff2)
2477 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2480 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2481 __isl_take isl_pw_aff *pwaff2)
2483 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2486 /* Return a set containing those elements in the shared domain
2487 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2489 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2490 __isl_take isl_pw_aff *pwaff2)
2492 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2495 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2496 __isl_take isl_pw_aff *pwaff2)
2498 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2501 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2502 __isl_take isl_pw_aff *pwaff2)
2504 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2507 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2508 __isl_take isl_pw_aff *pwaff2)
2510 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2513 /* Return a set containing those elements in the shared domain
2514 * of the elements of list1 and list2 where each element in list1
2515 * has the relation specified by "fn" with each element in list2.
2517 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2518 __isl_take isl_pw_aff_list *list2,
2519 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2520 __isl_take isl_pw_aff *pwaff2))
2522 int i, j;
2523 isl_ctx *ctx;
2524 isl_set *set;
2526 if (!list1 || !list2)
2527 goto error;
2529 ctx = isl_pw_aff_list_get_ctx(list1);
2530 if (list1->n < 1 || list2->n < 1)
2531 isl_die(ctx, isl_error_invalid,
2532 "list should contain at least one element", goto error);
2534 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2535 for (i = 0; i < list1->n; ++i)
2536 for (j = 0; j < list2->n; ++j) {
2537 isl_set *set_ij;
2539 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2540 isl_pw_aff_copy(list2->p[j]));
2541 set = isl_set_intersect(set, set_ij);
2544 isl_pw_aff_list_free(list1);
2545 isl_pw_aff_list_free(list2);
2546 return set;
2547 error:
2548 isl_pw_aff_list_free(list1);
2549 isl_pw_aff_list_free(list2);
2550 return NULL;
2553 /* Return a set containing those elements in the shared domain
2554 * of the elements of list1 and list2 where each element in list1
2555 * is equal to each element in list2.
2557 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2558 __isl_take isl_pw_aff_list *list2)
2560 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2563 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2564 __isl_take isl_pw_aff_list *list2)
2566 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2569 /* Return a set containing those elements in the shared domain
2570 * of the elements of list1 and list2 where each element in list1
2571 * is less than or equal to each element in list2.
2573 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2574 __isl_take isl_pw_aff_list *list2)
2576 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2579 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2580 __isl_take isl_pw_aff_list *list2)
2582 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2585 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2586 __isl_take isl_pw_aff_list *list2)
2588 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2591 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2592 __isl_take isl_pw_aff_list *list2)
2594 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2598 /* Return a set containing those elements in the shared domain
2599 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2601 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2602 __isl_take isl_pw_aff *pwaff2)
2604 isl_set *set_lt, *set_gt;
2606 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2607 isl_pw_aff_copy(pwaff2));
2608 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2609 return isl_set_union_disjoint(set_lt, set_gt);
2612 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2613 __isl_take isl_pw_aff *pwaff2)
2615 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2618 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2619 isl_int v)
2621 int i;
2623 if (isl_int_is_one(v))
2624 return pwaff;
2625 if (!isl_int_is_pos(v))
2626 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2627 "factor needs to be positive",
2628 return isl_pw_aff_free(pwaff));
2629 pwaff = isl_pw_aff_cow(pwaff);
2630 if (!pwaff)
2631 return NULL;
2632 if (pwaff->n == 0)
2633 return pwaff;
2635 for (i = 0; i < pwaff->n; ++i) {
2636 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2637 if (!pwaff->p[i].aff)
2638 return isl_pw_aff_free(pwaff);
2641 return pwaff;
2644 /* Divide "pa" by "f".
2646 __isl_give isl_pw_aff *isl_pw_aff_scale_down_val(__isl_take isl_pw_aff *pa,
2647 __isl_take isl_val *f)
2649 int i;
2651 if (!pa || !f)
2652 goto error;
2654 if (isl_val_is_one(f)) {
2655 isl_val_free(f);
2656 return pa;
2659 if (!isl_val_is_rat(f))
2660 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2661 "expecting rational factor", goto error);
2662 if (!isl_val_is_pos(f))
2663 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2664 "factor needs to be positive", goto error);
2666 pa = isl_pw_aff_cow(pa);
2667 if (!pa)
2668 return NULL;
2669 if (pa->n == 0)
2670 return pa;
2672 for (i = 0; i < pa->n; ++i) {
2673 pa->p[i].aff = isl_aff_scale_down_val(pa->p[i].aff,
2674 isl_val_copy(f));
2675 if (!pa->p[i].aff)
2676 goto error;
2679 isl_val_free(f);
2680 return pa;
2681 error:
2682 isl_pw_aff_free(pa);
2683 isl_val_free(f);
2684 return NULL;
2687 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2689 int i;
2691 pwaff = isl_pw_aff_cow(pwaff);
2692 if (!pwaff)
2693 return NULL;
2694 if (pwaff->n == 0)
2695 return pwaff;
2697 for (i = 0; i < pwaff->n; ++i) {
2698 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2699 if (!pwaff->p[i].aff)
2700 return isl_pw_aff_free(pwaff);
2703 return pwaff;
2706 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2708 int i;
2710 pwaff = isl_pw_aff_cow(pwaff);
2711 if (!pwaff)
2712 return NULL;
2713 if (pwaff->n == 0)
2714 return pwaff;
2716 for (i = 0; i < pwaff->n; ++i) {
2717 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2718 if (!pwaff->p[i].aff)
2719 return isl_pw_aff_free(pwaff);
2722 return pwaff;
2725 /* Assuming that "cond1" and "cond2" are disjoint,
2726 * return an affine expression that is equal to pwaff1 on cond1
2727 * and to pwaff2 on cond2.
2729 static __isl_give isl_pw_aff *isl_pw_aff_select(
2730 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2731 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2733 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2734 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2736 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2739 /* Return an affine expression that is equal to pwaff_true for elements
2740 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2741 * is zero.
2742 * That is, return cond ? pwaff_true : pwaff_false;
2744 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2745 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2747 isl_set *cond_true, *cond_false;
2749 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2750 cond_false = isl_pw_aff_zero_set(cond);
2751 return isl_pw_aff_select(cond_true, pwaff_true,
2752 cond_false, pwaff_false);
2755 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2757 if (!aff)
2758 return -1;
2760 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2763 /* Check whether pwaff is a piecewise constant.
2765 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2767 int i;
2769 if (!pwaff)
2770 return -1;
2772 for (i = 0; i < pwaff->n; ++i) {
2773 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2774 if (is_cst < 0 || !is_cst)
2775 return is_cst;
2778 return 1;
2781 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2782 __isl_take isl_aff *aff2)
2784 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2785 return isl_aff_mul(aff2, aff1);
2787 if (!isl_aff_is_cst(aff2))
2788 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2789 "at least one affine expression should be constant",
2790 goto error);
2792 aff1 = isl_aff_cow(aff1);
2793 if (!aff1 || !aff2)
2794 goto error;
2796 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2797 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2799 isl_aff_free(aff2);
2800 return aff1;
2801 error:
2802 isl_aff_free(aff1);
2803 isl_aff_free(aff2);
2804 return NULL;
2807 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2809 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2810 __isl_take isl_aff *aff2)
2812 int is_cst;
2813 int neg;
2815 is_cst = isl_aff_is_cst(aff2);
2816 if (is_cst < 0)
2817 goto error;
2818 if (!is_cst)
2819 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2820 "second argument should be a constant", goto error);
2822 if (!aff2)
2823 goto error;
2825 neg = isl_int_is_neg(aff2->v->el[1]);
2826 if (neg) {
2827 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2828 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2831 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2832 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2834 if (neg) {
2835 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2836 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2839 isl_aff_free(aff2);
2840 return aff1;
2841 error:
2842 isl_aff_free(aff1);
2843 isl_aff_free(aff2);
2844 return NULL;
2847 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2848 __isl_take isl_pw_aff *pwaff2)
2850 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2853 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2854 __isl_take isl_pw_aff *pwaff2)
2856 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2859 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2860 __isl_take isl_pw_aff *pwaff2)
2862 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2865 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2866 __isl_take isl_pw_aff *pwaff2)
2868 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2871 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2872 __isl_take isl_pw_aff *pwaff2)
2874 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2877 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2878 __isl_take isl_pw_aff *pa2)
2880 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2883 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2885 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2886 __isl_take isl_pw_aff *pa2)
2888 int is_cst;
2890 is_cst = isl_pw_aff_is_cst(pa2);
2891 if (is_cst < 0)
2892 goto error;
2893 if (!is_cst)
2894 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2895 "second argument should be a piecewise constant",
2896 goto error);
2897 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2898 error:
2899 isl_pw_aff_free(pa1);
2900 isl_pw_aff_free(pa2);
2901 return NULL;
2904 /* Compute the quotient of the integer division of "pa1" by "pa2"
2905 * with rounding towards zero.
2906 * "pa2" is assumed to be a piecewise constant.
2908 * In particular, return
2910 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2913 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2914 __isl_take isl_pw_aff *pa2)
2916 int is_cst;
2917 isl_set *cond;
2918 isl_pw_aff *f, *c;
2920 is_cst = isl_pw_aff_is_cst(pa2);
2921 if (is_cst < 0)
2922 goto error;
2923 if (!is_cst)
2924 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2925 "second argument should be a piecewise constant",
2926 goto error);
2928 pa1 = isl_pw_aff_div(pa1, pa2);
2930 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2931 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2932 c = isl_pw_aff_ceil(pa1);
2933 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2934 error:
2935 isl_pw_aff_free(pa1);
2936 isl_pw_aff_free(pa2);
2937 return NULL;
2940 /* Compute the remainder of the integer division of "pa1" by "pa2"
2941 * with rounding towards zero.
2942 * "pa2" is assumed to be a piecewise constant.
2944 * In particular, return
2946 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2949 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2950 __isl_take isl_pw_aff *pa2)
2952 int is_cst;
2953 isl_pw_aff *res;
2955 is_cst = isl_pw_aff_is_cst(pa2);
2956 if (is_cst < 0)
2957 goto error;
2958 if (!is_cst)
2959 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2960 "second argument should be a piecewise constant",
2961 goto error);
2962 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2963 res = isl_pw_aff_mul(pa2, res);
2964 res = isl_pw_aff_sub(pa1, res);
2965 return res;
2966 error:
2967 isl_pw_aff_free(pa1);
2968 isl_pw_aff_free(pa2);
2969 return NULL;
2972 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2973 __isl_take isl_pw_aff *pwaff2)
2975 isl_set *le;
2976 isl_set *dom;
2978 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2979 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2980 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2981 isl_pw_aff_copy(pwaff2));
2982 dom = isl_set_subtract(dom, isl_set_copy(le));
2983 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2986 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2987 __isl_take isl_pw_aff *pwaff2)
2989 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2992 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2993 __isl_take isl_pw_aff *pwaff2)
2995 isl_set *ge;
2996 isl_set *dom;
2998 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2999 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3000 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3001 isl_pw_aff_copy(pwaff2));
3002 dom = isl_set_subtract(dom, isl_set_copy(ge));
3003 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3006 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3007 __isl_take isl_pw_aff *pwaff2)
3009 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3012 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3013 __isl_take isl_pw_aff_list *list,
3014 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3015 __isl_take isl_pw_aff *pwaff2))
3017 int i;
3018 isl_ctx *ctx;
3019 isl_pw_aff *res;
3021 if (!list)
3022 return NULL;
3024 ctx = isl_pw_aff_list_get_ctx(list);
3025 if (list->n < 1)
3026 isl_die(ctx, isl_error_invalid,
3027 "list should contain at least one element",
3028 return isl_pw_aff_list_free(list));
3030 res = isl_pw_aff_copy(list->p[0]);
3031 for (i = 1; i < list->n; ++i)
3032 res = fn(res, isl_pw_aff_copy(list->p[i]));
3034 isl_pw_aff_list_free(list);
3035 return res;
3038 /* Return an isl_pw_aff that maps each element in the intersection of the
3039 * domains of the elements of list to the minimal corresponding affine
3040 * expression.
3042 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3044 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3047 /* Return an isl_pw_aff that maps each element in the intersection of the
3048 * domains of the elements of list to the maximal corresponding affine
3049 * expression.
3051 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3053 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3056 /* Mark the domains of "pwaff" as rational.
3058 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3060 int i;
3062 pwaff = isl_pw_aff_cow(pwaff);
3063 if (!pwaff)
3064 return NULL;
3065 if (pwaff->n == 0)
3066 return pwaff;
3068 for (i = 0; i < pwaff->n; ++i) {
3069 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3070 if (!pwaff->p[i].set)
3071 return isl_pw_aff_free(pwaff);
3074 return pwaff;
3077 /* Mark the domains of the elements of "list" as rational.
3079 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3080 __isl_take isl_pw_aff_list *list)
3082 int i, n;
3084 if (!list)
3085 return NULL;
3086 if (list->n == 0)
3087 return list;
3089 n = list->n;
3090 for (i = 0; i < n; ++i) {
3091 isl_pw_aff *pa;
3093 pa = isl_pw_aff_list_get_pw_aff(list, i);
3094 pa = isl_pw_aff_set_rational(pa);
3095 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3098 return list;
3101 /* Check that the domain space of "aff" matches "space".
3103 * Return 0 on success and -1 on error.
3105 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3106 __isl_keep isl_space *space)
3108 isl_space *aff_space;
3109 int match;
3111 if (!aff || !space)
3112 return -1;
3114 aff_space = isl_aff_get_domain_space(aff);
3116 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3117 if (match < 0)
3118 goto error;
3119 if (!match)
3120 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3121 "parameters don't match", goto error);
3122 match = isl_space_tuple_match(space, isl_dim_in,
3123 aff_space, isl_dim_set);
3124 if (match < 0)
3125 goto error;
3126 if (!match)
3127 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3128 "domains don't match", goto error);
3129 isl_space_free(aff_space);
3130 return 0;
3131 error:
3132 isl_space_free(aff_space);
3133 return -1;
3136 #undef BASE
3137 #define BASE aff
3139 #include <isl_multi_templ.c>
3141 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3142 * domain.
3144 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3145 __isl_take isl_multi_aff *ma)
3147 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3148 return isl_pw_multi_aff_alloc(dom, ma);
3151 /* Create a piecewise multi-affine expression in the given space that maps each
3152 * input dimension to the corresponding output dimension.
3154 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3155 __isl_take isl_space *space)
3157 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3160 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
3161 __isl_take isl_multi_aff *maff2)
3163 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3166 /* Subtract "ma2" from "ma1" and return the result.
3168 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3169 __isl_take isl_multi_aff *ma2)
3171 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3174 /* Given two multi-affine expressions A -> B and C -> D,
3175 * construct a multi-affine expression [A -> C] -> [B -> D].
3177 __isl_give isl_multi_aff *isl_multi_aff_product(
3178 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3180 int i;
3181 isl_aff *aff;
3182 isl_space *space;
3183 isl_multi_aff *res;
3184 int in1, in2, out1, out2;
3186 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
3187 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
3188 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
3189 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
3190 space = isl_space_product(isl_multi_aff_get_space(ma1),
3191 isl_multi_aff_get_space(ma2));
3192 res = isl_multi_aff_alloc(isl_space_copy(space));
3193 space = isl_space_domain(space);
3195 for (i = 0; i < out1; ++i) {
3196 aff = isl_multi_aff_get_aff(ma1, i);
3197 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
3198 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3199 res = isl_multi_aff_set_aff(res, i, aff);
3202 for (i = 0; i < out2; ++i) {
3203 aff = isl_multi_aff_get_aff(ma2, i);
3204 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
3205 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3206 res = isl_multi_aff_set_aff(res, out1 + i, aff);
3209 isl_space_free(space);
3210 isl_multi_aff_free(ma1);
3211 isl_multi_aff_free(ma2);
3212 return res;
3215 /* Exploit the equalities in "eq" to simplify the affine expressions.
3217 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3218 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3220 int i;
3222 maff = isl_multi_aff_cow(maff);
3223 if (!maff || !eq)
3224 goto error;
3226 for (i = 0; i < maff->n; ++i) {
3227 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3228 isl_basic_set_copy(eq));
3229 if (!maff->p[i])
3230 goto error;
3233 isl_basic_set_free(eq);
3234 return maff;
3235 error:
3236 isl_basic_set_free(eq);
3237 isl_multi_aff_free(maff);
3238 return NULL;
3241 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3242 isl_int f)
3244 int i;
3246 maff = isl_multi_aff_cow(maff);
3247 if (!maff)
3248 return NULL;
3250 for (i = 0; i < maff->n; ++i) {
3251 maff->p[i] = isl_aff_scale(maff->p[i], f);
3252 if (!maff->p[i])
3253 return isl_multi_aff_free(maff);
3256 return maff;
3259 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3260 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3262 maff1 = isl_multi_aff_add(maff1, maff2);
3263 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3264 return maff1;
3267 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3269 if (!maff)
3270 return -1;
3272 return 0;
3275 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
3276 __isl_keep isl_multi_aff *maff2)
3278 int i;
3279 int equal;
3281 if (!maff1 || !maff2)
3282 return -1;
3283 if (maff1->n != maff2->n)
3284 return 0;
3285 equal = isl_space_is_equal(maff1->space, maff2->space);
3286 if (equal < 0 || !equal)
3287 return equal;
3289 for (i = 0; i < maff1->n; ++i) {
3290 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
3291 if (equal < 0 || !equal)
3292 return equal;
3295 return 1;
3298 /* Return the set of domain elements where "ma1" is lexicographically
3299 * smaller than or equal to "ma2".
3301 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3302 __isl_take isl_multi_aff *ma2)
3304 return isl_multi_aff_lex_ge_set(ma2, ma1);
3307 /* Return the set of domain elements where "ma1" is lexicographically
3308 * greater than or equal to "ma2".
3310 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3311 __isl_take isl_multi_aff *ma2)
3313 isl_space *space;
3314 isl_map *map1, *map2;
3315 isl_map *map, *ge;
3317 map1 = isl_map_from_multi_aff(ma1);
3318 map2 = isl_map_from_multi_aff(ma2);
3319 map = isl_map_range_product(map1, map2);
3320 space = isl_space_range(isl_map_get_space(map));
3321 space = isl_space_domain(isl_space_unwrap(space));
3322 ge = isl_map_lex_ge(space);
3323 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3325 return isl_map_domain(map);
3328 #undef PW
3329 #define PW isl_pw_multi_aff
3330 #undef EL
3331 #define EL isl_multi_aff
3332 #undef EL_IS_ZERO
3333 #define EL_IS_ZERO is_empty
3334 #undef ZERO
3335 #define ZERO empty
3336 #undef IS_ZERO
3337 #define IS_ZERO is_empty
3338 #undef FIELD
3339 #define FIELD maff
3340 #undef DEFAULT_IS_ZERO
3341 #define DEFAULT_IS_ZERO 0
3343 #define NO_NEG
3344 #define NO_EVAL
3345 #define NO_OPT
3346 #define NO_INVOLVES_DIMS
3347 #define NO_MOVE_DIMS
3348 #define NO_INSERT_DIMS
3349 #define NO_LIFT
3350 #define NO_MORPH
3352 #include <isl_pw_templ.c>
3354 #undef UNION
3355 #define UNION isl_union_pw_multi_aff
3356 #undef PART
3357 #define PART isl_pw_multi_aff
3358 #undef PARTS
3359 #define PARTS pw_multi_aff
3360 #define ALIGN_DOMAIN
3362 #define NO_EVAL
3364 #include <isl_union_templ.c>
3366 /* Given a function "cmp" that returns the set of elements where
3367 * "ma1" is "better" than "ma2", return the intersection of this
3368 * set with "dom1" and "dom2".
3370 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3371 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3372 __isl_keep isl_multi_aff *ma2,
3373 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3374 __isl_take isl_multi_aff *ma2))
3376 isl_set *common;
3377 isl_set *better;
3378 int is_empty;
3380 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3381 is_empty = isl_set_plain_is_empty(common);
3382 if (is_empty >= 0 && is_empty)
3383 return common;
3384 if (is_empty < 0)
3385 return isl_set_free(common);
3386 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3387 better = isl_set_intersect(common, better);
3389 return better;
3392 /* Given a function "cmp" that returns the set of elements where
3393 * "ma1" is "better" than "ma2", return a piecewise multi affine
3394 * expression defined on the union of the definition domains
3395 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3396 * "pma2" on each cell. If only one of the two input functions
3397 * is defined on a given cell, then it is considered the best.
3399 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3400 __isl_take isl_pw_multi_aff *pma1,
3401 __isl_take isl_pw_multi_aff *pma2,
3402 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3403 __isl_take isl_multi_aff *ma2))
3405 int i, j, n;
3406 isl_pw_multi_aff *res = NULL;
3407 isl_ctx *ctx;
3408 isl_set *set = NULL;
3410 if (!pma1 || !pma2)
3411 goto error;
3413 ctx = isl_space_get_ctx(pma1->dim);
3414 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3415 isl_die(ctx, isl_error_invalid,
3416 "arguments should live in the same space", goto error);
3418 if (isl_pw_multi_aff_is_empty(pma1)) {
3419 isl_pw_multi_aff_free(pma1);
3420 return pma2;
3423 if (isl_pw_multi_aff_is_empty(pma2)) {
3424 isl_pw_multi_aff_free(pma2);
3425 return pma1;
3428 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3429 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3431 for (i = 0; i < pma1->n; ++i) {
3432 set = isl_set_copy(pma1->p[i].set);
3433 for (j = 0; j < pma2->n; ++j) {
3434 isl_set *better;
3435 int is_empty;
3437 better = shared_and_better(pma2->p[j].set,
3438 pma1->p[i].set, pma2->p[j].maff,
3439 pma1->p[i].maff, cmp);
3440 is_empty = isl_set_plain_is_empty(better);
3441 if (is_empty < 0 || is_empty) {
3442 isl_set_free(better);
3443 if (is_empty < 0)
3444 goto error;
3445 continue;
3447 set = isl_set_subtract(set, isl_set_copy(better));
3449 res = isl_pw_multi_aff_add_piece(res, better,
3450 isl_multi_aff_copy(pma2->p[j].maff));
3452 res = isl_pw_multi_aff_add_piece(res, set,
3453 isl_multi_aff_copy(pma1->p[i].maff));
3456 for (j = 0; j < pma2->n; ++j) {
3457 set = isl_set_copy(pma2->p[j].set);
3458 for (i = 0; i < pma1->n; ++i)
3459 set = isl_set_subtract(set,
3460 isl_set_copy(pma1->p[i].set));
3461 res = isl_pw_multi_aff_add_piece(res, set,
3462 isl_multi_aff_copy(pma2->p[j].maff));
3465 isl_pw_multi_aff_free(pma1);
3466 isl_pw_multi_aff_free(pma2);
3468 return res;
3469 error:
3470 isl_pw_multi_aff_free(pma1);
3471 isl_pw_multi_aff_free(pma2);
3472 isl_set_free(set);
3473 return isl_pw_multi_aff_free(res);
3476 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3477 __isl_take isl_pw_multi_aff *pma1,
3478 __isl_take isl_pw_multi_aff *pma2)
3480 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3483 /* Given two piecewise multi affine expressions, return a piecewise
3484 * multi-affine expression defined on the union of the definition domains
3485 * of the inputs that is equal to the lexicographic maximum of the two
3486 * inputs on each cell. If only one of the two inputs is defined on
3487 * a given cell, then it is considered to be the maximum.
3489 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3490 __isl_take isl_pw_multi_aff *pma1,
3491 __isl_take isl_pw_multi_aff *pma2)
3493 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3494 &pw_multi_aff_union_lexmax);
3497 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3498 __isl_take isl_pw_multi_aff *pma1,
3499 __isl_take isl_pw_multi_aff *pma2)
3501 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3504 /* Given two piecewise multi affine expressions, return a piecewise
3505 * multi-affine expression defined on the union of the definition domains
3506 * of the inputs that is equal to the lexicographic minimum of the two
3507 * inputs on each cell. If only one of the two inputs is defined on
3508 * a given cell, then it is considered to be the minimum.
3510 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3511 __isl_take isl_pw_multi_aff *pma1,
3512 __isl_take isl_pw_multi_aff *pma2)
3514 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3515 &pw_multi_aff_union_lexmin);
3518 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3519 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3521 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3522 &isl_multi_aff_add);
3525 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3526 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3528 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3529 &pw_multi_aff_add);
3532 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3533 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3535 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3536 &isl_multi_aff_sub);
3539 /* Subtract "pma2" from "pma1" and return the result.
3541 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3542 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3544 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3545 &pw_multi_aff_sub);
3548 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3549 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3551 return isl_pw_multi_aff_union_add_(pma1, pma2);
3554 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3555 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3557 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3558 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3560 int i, j, n;
3561 isl_space *space;
3562 isl_pw_multi_aff *res;
3564 if (!pma1 || !pma2)
3565 goto error;
3567 n = pma1->n * pma2->n;
3568 space = isl_space_product(isl_space_copy(pma1->dim),
3569 isl_space_copy(pma2->dim));
3570 res = isl_pw_multi_aff_alloc_size(space, n);
3572 for (i = 0; i < pma1->n; ++i) {
3573 for (j = 0; j < pma2->n; ++j) {
3574 isl_set *domain;
3575 isl_multi_aff *ma;
3577 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3578 isl_set_copy(pma2->p[j].set));
3579 ma = isl_multi_aff_product(
3580 isl_multi_aff_copy(pma1->p[i].maff),
3581 isl_multi_aff_copy(pma2->p[j].maff));
3582 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3586 isl_pw_multi_aff_free(pma1);
3587 isl_pw_multi_aff_free(pma2);
3588 return res;
3589 error:
3590 isl_pw_multi_aff_free(pma1);
3591 isl_pw_multi_aff_free(pma2);
3592 return NULL;
3595 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3596 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3598 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3599 &pw_multi_aff_product);
3602 /* Construct a map mapping the domain of the piecewise multi-affine expression
3603 * to its range, with each dimension in the range equated to the
3604 * corresponding affine expression on its cell.
3606 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3608 int i;
3609 isl_map *map;
3611 if (!pma)
3612 return NULL;
3614 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3616 for (i = 0; i < pma->n; ++i) {
3617 isl_multi_aff *maff;
3618 isl_basic_map *bmap;
3619 isl_map *map_i;
3621 maff = isl_multi_aff_copy(pma->p[i].maff);
3622 bmap = isl_basic_map_from_multi_aff(maff);
3623 map_i = isl_map_from_basic_map(bmap);
3624 map_i = isl_map_intersect_domain(map_i,
3625 isl_set_copy(pma->p[i].set));
3626 map = isl_map_union_disjoint(map, map_i);
3629 isl_pw_multi_aff_free(pma);
3630 return map;
3633 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3635 if (!pma)
3636 return NULL;
3638 if (!isl_space_is_set(pma->dim))
3639 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3640 "isl_pw_multi_aff cannot be converted into an isl_set",
3641 return isl_pw_multi_aff_free(pma));
3643 return isl_map_from_pw_multi_aff(pma);
3646 /* Given a basic map with a single output dimension that is defined
3647 * in terms of the parameters and input dimensions using an equality,
3648 * extract an isl_aff that expresses the output dimension in terms
3649 * of the parameters and input dimensions.
3651 * Since some applications expect the result of isl_pw_multi_aff_from_map
3652 * to only contain integer affine expressions, we compute the floor
3653 * of the expression before returning.
3655 * This function shares some similarities with
3656 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3658 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3659 __isl_take isl_basic_map *bmap)
3661 int i;
3662 unsigned offset;
3663 unsigned total;
3664 isl_local_space *ls;
3665 isl_aff *aff;
3667 if (!bmap)
3668 return NULL;
3669 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3670 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3671 "basic map should have a single output dimension",
3672 goto error);
3673 offset = isl_basic_map_offset(bmap, isl_dim_out);
3674 total = isl_basic_map_total_dim(bmap);
3675 for (i = 0; i < bmap->n_eq; ++i) {
3676 if (isl_int_is_zero(bmap->eq[i][offset]))
3677 continue;
3678 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3679 1 + total - (offset + 1)) != -1)
3680 continue;
3681 break;
3683 if (i >= bmap->n_eq)
3684 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3685 "unable to find suitable equality", goto error);
3686 ls = isl_basic_map_get_local_space(bmap);
3687 aff = isl_aff_alloc(isl_local_space_domain(ls));
3688 if (!aff)
3689 goto error;
3690 if (isl_int_is_neg(bmap->eq[i][offset]))
3691 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3692 else
3693 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3694 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3695 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3696 isl_basic_map_free(bmap);
3698 aff = isl_aff_remove_unused_divs(aff);
3699 aff = isl_aff_floor(aff);
3700 return aff;
3701 error:
3702 isl_basic_map_free(bmap);
3703 return NULL;
3706 /* Given a basic map where each output dimension is defined
3707 * in terms of the parameters and input dimensions using an equality,
3708 * extract an isl_multi_aff that expresses the output dimensions in terms
3709 * of the parameters and input dimensions.
3711 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3712 __isl_take isl_basic_map *bmap)
3714 int i;
3715 unsigned n_out;
3716 isl_multi_aff *ma;
3718 if (!bmap)
3719 return NULL;
3721 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3722 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3724 for (i = 0; i < n_out; ++i) {
3725 isl_basic_map *bmap_i;
3726 isl_aff *aff;
3728 bmap_i = isl_basic_map_copy(bmap);
3729 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3730 i + 1, n_out - (1 + i));
3731 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3732 aff = extract_isl_aff_from_basic_map(bmap_i);
3733 ma = isl_multi_aff_set_aff(ma, i, aff);
3736 isl_basic_map_free(bmap);
3738 return ma;
3741 /* Create an isl_pw_multi_aff that is equivalent to
3742 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3743 * The given basic map is such that each output dimension is defined
3744 * in terms of the parameters and input dimensions using an equality.
3746 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3747 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3749 isl_multi_aff *ma;
3751 ma = extract_isl_multi_aff_from_basic_map(bmap);
3752 return isl_pw_multi_aff_alloc(domain, ma);
3755 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3756 * This obviously only works if the input "map" is single-valued.
3757 * If so, we compute the lexicographic minimum of the image in the form
3758 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3759 * to its lexicographic minimum.
3760 * If the input is not single-valued, we produce an error.
3762 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3763 __isl_take isl_map *map)
3765 int i;
3766 int sv;
3767 isl_pw_multi_aff *pma;
3769 sv = isl_map_is_single_valued(map);
3770 if (sv < 0)
3771 goto error;
3772 if (!sv)
3773 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3774 "map is not single-valued", goto error);
3775 map = isl_map_make_disjoint(map);
3776 if (!map)
3777 return NULL;
3779 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3781 for (i = 0; i < map->n; ++i) {
3782 isl_pw_multi_aff *pma_i;
3783 isl_basic_map *bmap;
3784 bmap = isl_basic_map_copy(map->p[i]);
3785 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3786 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3789 isl_map_free(map);
3790 return pma;
3791 error:
3792 isl_map_free(map);
3793 return NULL;
3796 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3797 * taking into account that the output dimension at position "d"
3798 * can be represented as
3800 * x = floor((e(...) + c1) / m)
3802 * given that constraint "i" is of the form
3804 * e(...) + c1 - m x >= 0
3807 * Let "map" be of the form
3809 * A -> B
3811 * We construct a mapping
3813 * A -> [A -> x = floor(...)]
3815 * apply that to the map, obtaining
3817 * [A -> x = floor(...)] -> B
3819 * and equate dimension "d" to x.
3820 * We then compute a isl_pw_multi_aff representation of the resulting map
3821 * and plug in the mapping above.
3823 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3824 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3826 isl_ctx *ctx;
3827 isl_space *space;
3828 isl_local_space *ls;
3829 isl_multi_aff *ma;
3830 isl_aff *aff;
3831 isl_vec *v;
3832 isl_map *insert;
3833 int offset;
3834 int n;
3835 int n_in;
3836 isl_pw_multi_aff *pma;
3837 int is_set;
3839 is_set = isl_map_is_set(map);
3841 offset = isl_basic_map_offset(hull, isl_dim_out);
3842 ctx = isl_map_get_ctx(map);
3843 space = isl_space_domain(isl_map_get_space(map));
3844 n_in = isl_space_dim(space, isl_dim_set);
3845 n = isl_space_dim(space, isl_dim_all);
3847 v = isl_vec_alloc(ctx, 1 + 1 + n);
3848 if (v) {
3849 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3850 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3852 isl_basic_map_free(hull);
3854 ls = isl_local_space_from_space(isl_space_copy(space));
3855 aff = isl_aff_alloc_vec(ls, v);
3856 aff = isl_aff_floor(aff);
3857 if (is_set) {
3858 isl_space_free(space);
3859 ma = isl_multi_aff_from_aff(aff);
3860 } else {
3861 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3862 ma = isl_multi_aff_range_product(ma,
3863 isl_multi_aff_from_aff(aff));
3866 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3867 map = isl_map_apply_domain(map, insert);
3868 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3869 pma = isl_pw_multi_aff_from_map(map);
3870 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3872 return pma;
3875 /* Is constraint "c" of the form
3877 * e(...) + c1 - m x >= 0
3879 * or
3881 * -e(...) + c2 + m x >= 0
3883 * where m > 1 and e only depends on parameters and input dimemnsions?
3885 * "offset" is the offset of the output dimensions
3886 * "pos" is the position of output dimension x.
3888 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3890 if (isl_int_is_zero(c[offset + d]))
3891 return 0;
3892 if (isl_int_is_one(c[offset + d]))
3893 return 0;
3894 if (isl_int_is_negone(c[offset + d]))
3895 return 0;
3896 if (isl_seq_first_non_zero(c + offset, d) != -1)
3897 return 0;
3898 if (isl_seq_first_non_zero(c + offset + d + 1,
3899 total - (offset + d + 1)) != -1)
3900 return 0;
3901 return 1;
3904 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3906 * As a special case, we first check if there is any pair of constraints,
3907 * shared by all the basic maps in "map" that force a given dimension
3908 * to be equal to the floor of some affine combination of the input dimensions.
3910 * In particular, if we can find two constraints
3912 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3914 * and
3916 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3918 * where m > 1 and e only depends on parameters and input dimemnsions,
3919 * and such that
3921 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3923 * then we know that we can take
3925 * x = floor((e(...) + c1) / m)
3927 * without having to perform any computation.
3929 * Note that we know that
3931 * c1 + c2 >= 1
3933 * If c1 + c2 were 0, then we would have detected an equality during
3934 * simplification. If c1 + c2 were negative, then we would have detected
3935 * a contradiction.
3937 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3938 __isl_take isl_map *map)
3940 int d, dim;
3941 int i, j, n;
3942 int offset, total;
3943 isl_int sum;
3944 isl_basic_map *hull;
3946 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3947 if (!hull)
3948 goto error;
3950 isl_int_init(sum);
3951 dim = isl_map_dim(map, isl_dim_out);
3952 offset = isl_basic_map_offset(hull, isl_dim_out);
3953 total = 1 + isl_basic_map_total_dim(hull);
3954 n = hull->n_ineq;
3955 for (d = 0; d < dim; ++d) {
3956 for (i = 0; i < n; ++i) {
3957 if (!is_potential_div_constraint(hull->ineq[i],
3958 offset, d, total))
3959 continue;
3960 for (j = i + 1; j < n; ++j) {
3961 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3962 hull->ineq[j] + 1, total - 1))
3963 continue;
3964 isl_int_add(sum, hull->ineq[i][0],
3965 hull->ineq[j][0]);
3966 if (isl_int_abs_lt(sum,
3967 hull->ineq[i][offset + d]))
3968 break;
3971 if (j >= n)
3972 continue;
3973 isl_int_clear(sum);
3974 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3975 j = i;
3976 return pw_multi_aff_from_map_div(map, hull, d, j);
3979 isl_int_clear(sum);
3980 isl_basic_map_free(hull);
3981 return pw_multi_aff_from_map_base(map);
3982 error:
3983 isl_map_free(map);
3984 isl_basic_map_free(hull);
3985 return NULL;
3988 /* Given an affine expression
3990 * [A -> B] -> f(A,B)
3992 * construct an isl_multi_aff
3994 * [A -> B] -> B'
3996 * such that dimension "d" in B' is set to "aff" and the remaining
3997 * dimensions are set equal to the corresponding dimensions in B.
3998 * "n_in" is the dimension of the space A.
3999 * "n_out" is the dimension of the space B.
4001 * If "is_set" is set, then the affine expression is of the form
4003 * [B] -> f(B)
4005 * and we construct an isl_multi_aff
4007 * B -> B'
4009 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4010 unsigned n_in, unsigned n_out, int is_set)
4012 int i;
4013 isl_multi_aff *ma;
4014 isl_space *space, *space2;
4015 isl_local_space *ls;
4017 space = isl_aff_get_domain_space(aff);
4018 ls = isl_local_space_from_space(isl_space_copy(space));
4019 space2 = isl_space_copy(space);
4020 if (!is_set)
4021 space2 = isl_space_range(isl_space_unwrap(space2));
4022 space = isl_space_map_from_domain_and_range(space, space2);
4023 ma = isl_multi_aff_alloc(space);
4024 ma = isl_multi_aff_set_aff(ma, d, aff);
4026 for (i = 0; i < n_out; ++i) {
4027 if (i == d)
4028 continue;
4029 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4030 isl_dim_set, n_in + i);
4031 ma = isl_multi_aff_set_aff(ma, i, aff);
4034 isl_local_space_free(ls);
4036 return ma;
4039 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4040 * taking into account that the dimension at position "d" can be written as
4042 * x = m a + f(..) (1)
4044 * where m is equal to "gcd".
4045 * "i" is the index of the equality in "hull" that defines f(..).
4046 * In particular, the equality is of the form
4048 * f(..) - x + m g(existentials) = 0
4050 * or
4052 * -f(..) + x + m g(existentials) = 0
4054 * We basically plug (1) into "map", resulting in a map with "a"
4055 * in the range instead of "x". The corresponding isl_pw_multi_aff
4056 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4058 * Specifically, given the input map
4060 * A -> B
4062 * We first wrap it into a set
4064 * [A -> B]
4066 * and define (1) on top of the corresponding space, resulting in "aff".
4067 * We use this to create an isl_multi_aff that maps the output position "d"
4068 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4069 * We plug this into the wrapped map, unwrap the result and compute the
4070 * corresponding isl_pw_multi_aff.
4071 * The result is an expression
4073 * A -> T(A)
4075 * We adjust that to
4077 * A -> [A -> T(A)]
4079 * so that we can plug that into "aff", after extending the latter to
4080 * a mapping
4082 * [A -> B] -> B'
4085 * If "map" is actually a set, then there is no "A" space, meaning
4086 * that we do not need to perform any wrapping, and that the result
4087 * of the recursive call is of the form
4089 * [T]
4091 * which is plugged into a mapping of the form
4093 * B -> B'
4095 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4096 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4097 isl_int gcd)
4099 isl_set *set;
4100 isl_space *space;
4101 isl_local_space *ls;
4102 isl_aff *aff;
4103 isl_multi_aff *ma;
4104 isl_pw_multi_aff *pma, *id;
4105 unsigned n_in;
4106 unsigned o_out;
4107 unsigned n_out;
4108 int is_set;
4110 is_set = isl_map_is_set(map);
4112 n_in = isl_basic_map_dim(hull, isl_dim_in);
4113 n_out = isl_basic_map_dim(hull, isl_dim_out);
4114 o_out = isl_basic_map_offset(hull, isl_dim_out);
4116 if (is_set)
4117 set = map;
4118 else
4119 set = isl_map_wrap(map);
4120 space = isl_space_map_from_set(isl_set_get_space(set));
4121 ma = isl_multi_aff_identity(space);
4122 ls = isl_local_space_from_space(isl_set_get_space(set));
4123 aff = isl_aff_alloc(ls);
4124 if (aff) {
4125 isl_int_set_si(aff->v->el[0], 1);
4126 if (isl_int_is_one(hull->eq[i][o_out + d]))
4127 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4128 aff->v->size - 1);
4129 else
4130 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4131 aff->v->size - 1);
4132 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4134 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4135 set = isl_set_preimage_multi_aff(set, ma);
4137 ma = range_map(aff, d, n_in, n_out, is_set);
4139 if (is_set)
4140 map = set;
4141 else
4142 map = isl_set_unwrap(set);
4143 pma = isl_pw_multi_aff_from_map(set);
4145 if (!is_set) {
4146 space = isl_pw_multi_aff_get_domain_space(pma);
4147 space = isl_space_map_from_set(space);
4148 id = isl_pw_multi_aff_identity(space);
4149 pma = isl_pw_multi_aff_range_product(id, pma);
4151 id = isl_pw_multi_aff_from_multi_aff(ma);
4152 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4154 isl_basic_map_free(hull);
4155 return pma;
4158 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4160 * As a special case, we first check if all output dimensions are uniquely
4161 * defined in terms of the parameters and input dimensions over the entire
4162 * domain. If so, we extract the desired isl_pw_multi_aff directly
4163 * from the affine hull of "map" and its domain.
4165 * Otherwise, we check if any of the output dimensions is "strided".
4166 * That is, we check if can be written as
4168 * x = m a + f(..)
4170 * with m greater than 1, a some combination of existentiall quantified
4171 * variables and f and expression in the parameters and input dimensions.
4172 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4174 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4175 * special case.
4177 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4179 int i, j;
4180 int sv;
4181 isl_basic_map *hull;
4182 unsigned n_out;
4183 unsigned o_out;
4184 unsigned n_div;
4185 unsigned o_div;
4186 isl_int gcd;
4188 if (!map)
4189 return NULL;
4191 hull = isl_map_affine_hull(isl_map_copy(map));
4192 sv = isl_basic_map_plain_is_single_valued(hull);
4193 if (sv >= 0 && sv)
4194 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4195 if (sv < 0)
4196 hull = isl_basic_map_free(hull);
4197 if (!hull)
4198 goto error;
4200 n_div = isl_basic_map_dim(hull, isl_dim_div);
4201 o_div = isl_basic_map_offset(hull, isl_dim_div);
4203 if (n_div == 0) {
4204 isl_basic_map_free(hull);
4205 return pw_multi_aff_from_map_check_div(map);
4208 isl_int_init(gcd);
4210 n_out = isl_basic_map_dim(hull, isl_dim_out);
4211 o_out = isl_basic_map_offset(hull, isl_dim_out);
4213 for (i = 0; i < n_out; ++i) {
4214 for (j = 0; j < hull->n_eq; ++j) {
4215 isl_int *eq = hull->eq[j];
4216 isl_pw_multi_aff *res;
4218 if (!isl_int_is_one(eq[o_out + i]) &&
4219 !isl_int_is_negone(eq[o_out + i]))
4220 continue;
4221 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4222 continue;
4223 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4224 n_out - (i + 1)) != -1)
4225 continue;
4226 isl_seq_gcd(eq + o_div, n_div, &gcd);
4227 if (isl_int_is_zero(gcd))
4228 continue;
4229 if (isl_int_is_one(gcd))
4230 continue;
4232 res = pw_multi_aff_from_map_stride(map, hull,
4233 i, j, gcd);
4234 isl_int_clear(gcd);
4235 return res;
4239 isl_int_clear(gcd);
4240 isl_basic_map_free(hull);
4241 return pw_multi_aff_from_map_check_div(map);
4242 error:
4243 isl_map_free(map);
4244 return NULL;
4247 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4249 return isl_pw_multi_aff_from_map(set);
4252 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4253 * add it to *user.
4255 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4257 isl_union_pw_multi_aff **upma = user;
4258 isl_pw_multi_aff *pma;
4260 pma = isl_pw_multi_aff_from_map(map);
4261 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4263 return *upma ? 0 : -1;
4266 /* Try and create an isl_union_pw_multi_aff that is equivalent
4267 * to the given isl_union_map.
4268 * The isl_union_map is required to be single-valued in each space.
4269 * Otherwise, an error is produced.
4271 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4272 __isl_take isl_union_map *umap)
4274 isl_space *space;
4275 isl_union_pw_multi_aff *upma;
4277 space = isl_union_map_get_space(umap);
4278 upma = isl_union_pw_multi_aff_empty(space);
4279 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4280 upma = isl_union_pw_multi_aff_free(upma);
4281 isl_union_map_free(umap);
4283 return upma;
4286 /* Try and create an isl_union_pw_multi_aff that is equivalent
4287 * to the given isl_union_set.
4288 * The isl_union_set is required to be a singleton in each space.
4289 * Otherwise, an error is produced.
4291 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4292 __isl_take isl_union_set *uset)
4294 return isl_union_pw_multi_aff_from_union_map(uset);
4297 /* Return the piecewise affine expression "set ? 1 : 0".
4299 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4301 isl_pw_aff *pa;
4302 isl_space *space = isl_set_get_space(set);
4303 isl_local_space *ls = isl_local_space_from_space(space);
4304 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4305 isl_aff *one = isl_aff_zero_on_domain(ls);
4307 one = isl_aff_add_constant_si(one, 1);
4308 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4309 set = isl_set_complement(set);
4310 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4312 return pa;
4315 /* Plug in "subs" for dimension "type", "pos" of "aff".
4317 * Let i be the dimension to replace and let "subs" be of the form
4319 * f/d
4321 * and "aff" of the form
4323 * (a i + g)/m
4325 * The result is
4327 * (a f + d g')/(m d)
4329 * where g' is the result of plugging in "subs" in each of the integer
4330 * divisions in g.
4332 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4333 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4335 isl_ctx *ctx;
4336 isl_int v;
4338 aff = isl_aff_cow(aff);
4339 if (!aff || !subs)
4340 return isl_aff_free(aff);
4342 ctx = isl_aff_get_ctx(aff);
4343 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4344 isl_die(ctx, isl_error_invalid,
4345 "spaces don't match", return isl_aff_free(aff));
4346 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4347 isl_die(ctx, isl_error_unsupported,
4348 "cannot handle divs yet", return isl_aff_free(aff));
4350 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4351 if (!aff->ls)
4352 return isl_aff_free(aff);
4354 aff->v = isl_vec_cow(aff->v);
4355 if (!aff->v)
4356 return isl_aff_free(aff);
4358 pos += isl_local_space_offset(aff->ls, type);
4360 isl_int_init(v);
4361 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4362 aff->v->size, subs->v->size, v);
4363 isl_int_clear(v);
4365 return aff;
4368 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4369 * expressions in "maff".
4371 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4372 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4373 __isl_keep isl_aff *subs)
4375 int i;
4377 maff = isl_multi_aff_cow(maff);
4378 if (!maff || !subs)
4379 return isl_multi_aff_free(maff);
4381 if (type == isl_dim_in)
4382 type = isl_dim_set;
4384 for (i = 0; i < maff->n; ++i) {
4385 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4386 if (!maff->p[i])
4387 return isl_multi_aff_free(maff);
4390 return maff;
4393 /* Plug in "subs" for dimension "type", "pos" of "pma".
4395 * pma is of the form
4397 * A_i(v) -> M_i(v)
4399 * while subs is of the form
4401 * v' = B_j(v) -> S_j
4403 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4404 * has a contribution in the result, in particular
4406 * C_ij(S_j) -> M_i(S_j)
4408 * Note that plugging in S_j in C_ij may also result in an empty set
4409 * and this contribution should simply be discarded.
4411 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4412 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4413 __isl_keep isl_pw_aff *subs)
4415 int i, j, n;
4416 isl_pw_multi_aff *res;
4418 if (!pma || !subs)
4419 return isl_pw_multi_aff_free(pma);
4421 n = pma->n * subs->n;
4422 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4424 for (i = 0; i < pma->n; ++i) {
4425 for (j = 0; j < subs->n; ++j) {
4426 isl_set *common;
4427 isl_multi_aff *res_ij;
4428 int empty;
4430 common = isl_set_intersect(
4431 isl_set_copy(pma->p[i].set),
4432 isl_set_copy(subs->p[j].set));
4433 common = isl_set_substitute(common,
4434 type, pos, subs->p[j].aff);
4435 empty = isl_set_plain_is_empty(common);
4436 if (empty < 0 || empty) {
4437 isl_set_free(common);
4438 if (empty < 0)
4439 goto error;
4440 continue;
4443 res_ij = isl_multi_aff_substitute(
4444 isl_multi_aff_copy(pma->p[i].maff),
4445 type, pos, subs->p[j].aff);
4447 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4451 isl_pw_multi_aff_free(pma);
4452 return res;
4453 error:
4454 isl_pw_multi_aff_free(pma);
4455 isl_pw_multi_aff_free(res);
4456 return NULL;
4459 /* Compute the preimage of a range of dimensions in the affine expression "src"
4460 * under "ma" and put the result in "dst". The number of dimensions in "src"
4461 * that precede the range is given by "n_before". The number of dimensions
4462 * in the range is given by the number of output dimensions of "ma".
4463 * The number of dimensions that follow the range is given by "n_after".
4464 * If "has_denom" is set (to one),
4465 * then "src" and "dst" have an extra initial denominator.
4466 * "n_div_ma" is the number of existentials in "ma"
4467 * "n_div_bset" is the number of existentials in "src"
4468 * The resulting "dst" (which is assumed to have been allocated by
4469 * the caller) contains coefficients for both sets of existentials,
4470 * first those in "ma" and then those in "src".
4471 * f, c1, c2 and g are temporary objects that have been initialized
4472 * by the caller.
4474 * Let src represent the expression
4476 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4478 * and let ma represent the expressions
4480 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4482 * We start out with the following expression for dst:
4484 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4486 * with the multiplication factor f initially equal to 1
4487 * and f \sum_i b_i v_i kept separately.
4488 * For each x_i that we substitute, we multiply the numerator
4489 * (and denominator) of dst by c_1 = m_i and add the numerator
4490 * of the x_i expression multiplied by c_2 = f b_i,
4491 * after removing the common factors of c_1 and c_2.
4492 * The multiplication factor f also needs to be multiplied by c_1
4493 * for the next x_j, j > i.
4495 void isl_seq_preimage(isl_int *dst, isl_int *src,
4496 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4497 int n_div_ma, int n_div_bmap,
4498 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4500 int i;
4501 int n_param, n_in, n_out;
4502 int o_dst, o_src;
4504 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4505 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4506 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4508 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4509 o_dst = o_src = has_denom + 1 + n_param + n_before;
4510 isl_seq_clr(dst + o_dst, n_in);
4511 o_dst += n_in;
4512 o_src += n_out;
4513 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4514 o_dst += n_after;
4515 o_src += n_after;
4516 isl_seq_clr(dst + o_dst, n_div_ma);
4517 o_dst += n_div_ma;
4518 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4520 isl_int_set_si(f, 1);
4522 for (i = 0; i < n_out; ++i) {
4523 int offset = has_denom + 1 + n_param + n_before + i;
4525 if (isl_int_is_zero(src[offset]))
4526 continue;
4527 isl_int_set(c1, ma->p[i]->v->el[0]);
4528 isl_int_mul(c2, f, src[offset]);
4529 isl_int_gcd(g, c1, c2);
4530 isl_int_divexact(c1, c1, g);
4531 isl_int_divexact(c2, c2, g);
4533 isl_int_mul(f, f, c1);
4534 o_dst = has_denom;
4535 o_src = 1;
4536 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4537 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4538 o_dst += 1 + n_param;
4539 o_src += 1 + n_param;
4540 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4541 o_dst += n_before;
4542 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4543 c2, ma->p[i]->v->el + o_src, n_in);
4544 o_dst += n_in;
4545 o_src += n_in;
4546 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4547 o_dst += n_after;
4548 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4549 c2, ma->p[i]->v->el + o_src, n_div_ma);
4550 o_dst += n_div_ma;
4551 o_src += n_div_ma;
4552 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4553 if (has_denom)
4554 isl_int_mul(dst[0], dst[0], c1);
4558 /* Compute the pullback of "aff" by the function represented by "ma".
4559 * In other words, plug in "ma" in "aff". The result is an affine expression
4560 * defined over the domain space of "ma".
4562 * If "aff" is represented by
4564 * (a(p) + b x + c(divs))/d
4566 * and ma is represented by
4568 * x = D(p) + F(y) + G(divs')
4570 * then the result is
4572 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4574 * The divs in the local space of the input are similarly adjusted
4575 * through a call to isl_local_space_preimage_multi_aff.
4577 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4578 __isl_take isl_multi_aff *ma)
4580 isl_aff *res = NULL;
4581 isl_local_space *ls;
4582 int n_div_aff, n_div_ma;
4583 isl_int f, c1, c2, g;
4585 ma = isl_multi_aff_align_divs(ma);
4586 if (!aff || !ma)
4587 goto error;
4589 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4590 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4592 ls = isl_aff_get_domain_local_space(aff);
4593 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4594 res = isl_aff_alloc(ls);
4595 if (!res)
4596 goto error;
4598 isl_int_init(f);
4599 isl_int_init(c1);
4600 isl_int_init(c2);
4601 isl_int_init(g);
4603 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4604 f, c1, c2, g, 1);
4606 isl_int_clear(f);
4607 isl_int_clear(c1);
4608 isl_int_clear(c2);
4609 isl_int_clear(g);
4611 isl_aff_free(aff);
4612 isl_multi_aff_free(ma);
4613 res = isl_aff_normalize(res);
4614 return res;
4615 error:
4616 isl_aff_free(aff);
4617 isl_multi_aff_free(ma);
4618 isl_aff_free(res);
4619 return NULL;
4622 /* Compute the pullback of "ma1" by the function represented by "ma2".
4623 * In other words, plug in "ma2" in "ma1".
4625 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4626 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4628 int i;
4629 isl_space *space = NULL;
4631 ma2 = isl_multi_aff_align_divs(ma2);
4632 ma1 = isl_multi_aff_cow(ma1);
4633 if (!ma1 || !ma2)
4634 goto error;
4636 space = isl_space_join(isl_multi_aff_get_space(ma2),
4637 isl_multi_aff_get_space(ma1));
4639 for (i = 0; i < ma1->n; ++i) {
4640 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4641 isl_multi_aff_copy(ma2));
4642 if (!ma1->p[i])
4643 goto error;
4646 ma1 = isl_multi_aff_reset_space(ma1, space);
4647 isl_multi_aff_free(ma2);
4648 return ma1;
4649 error:
4650 isl_space_free(space);
4651 isl_multi_aff_free(ma2);
4652 isl_multi_aff_free(ma1);
4653 return NULL;
4656 /* Extend the local space of "dst" to include the divs
4657 * in the local space of "src".
4659 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4660 __isl_keep isl_aff *src)
4662 isl_ctx *ctx;
4663 int *exp1 = NULL;
4664 int *exp2 = NULL;
4665 isl_mat *div;
4667 if (!src || !dst)
4668 return isl_aff_free(dst);
4670 ctx = isl_aff_get_ctx(src);
4671 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4672 isl_die(ctx, isl_error_invalid,
4673 "spaces don't match", goto error);
4675 if (src->ls->div->n_row == 0)
4676 return dst;
4678 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4679 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4680 if (!exp1 || (dst->ls->div->n_row && !exp2))
4681 goto error;
4683 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4684 dst = isl_aff_expand_divs(dst, div, exp2);
4685 free(exp1);
4686 free(exp2);
4688 return dst;
4689 error:
4690 free(exp1);
4691 free(exp2);
4692 return isl_aff_free(dst);
4695 /* Adjust the local spaces of the affine expressions in "maff"
4696 * such that they all have the save divs.
4698 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4699 __isl_take isl_multi_aff *maff)
4701 int i;
4703 if (!maff)
4704 return NULL;
4705 if (maff->n == 0)
4706 return maff;
4707 maff = isl_multi_aff_cow(maff);
4708 if (!maff)
4709 return NULL;
4711 for (i = 1; i < maff->n; ++i)
4712 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4713 for (i = 1; i < maff->n; ++i) {
4714 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4715 if (!maff->p[i])
4716 return isl_multi_aff_free(maff);
4719 return maff;
4722 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4724 aff = isl_aff_cow(aff);
4725 if (!aff)
4726 return NULL;
4728 aff->ls = isl_local_space_lift(aff->ls);
4729 if (!aff->ls)
4730 return isl_aff_free(aff);
4732 return aff;
4735 /* Lift "maff" to a space with extra dimensions such that the result
4736 * has no more existentially quantified variables.
4737 * If "ls" is not NULL, then *ls is assigned the local space that lies
4738 * at the basis of the lifting applied to "maff".
4740 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4741 __isl_give isl_local_space **ls)
4743 int i;
4744 isl_space *space;
4745 unsigned n_div;
4747 if (ls)
4748 *ls = NULL;
4750 if (!maff)
4751 return NULL;
4753 if (maff->n == 0) {
4754 if (ls) {
4755 isl_space *space = isl_multi_aff_get_domain_space(maff);
4756 *ls = isl_local_space_from_space(space);
4757 if (!*ls)
4758 return isl_multi_aff_free(maff);
4760 return maff;
4763 maff = isl_multi_aff_cow(maff);
4764 maff = isl_multi_aff_align_divs(maff);
4765 if (!maff)
4766 return NULL;
4768 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4769 space = isl_multi_aff_get_space(maff);
4770 space = isl_space_lift(isl_space_domain(space), n_div);
4771 space = isl_space_extend_domain_with_range(space,
4772 isl_multi_aff_get_space(maff));
4773 if (!space)
4774 return isl_multi_aff_free(maff);
4775 isl_space_free(maff->space);
4776 maff->space = space;
4778 if (ls) {
4779 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4780 if (!*ls)
4781 return isl_multi_aff_free(maff);
4784 for (i = 0; i < maff->n; ++i) {
4785 maff->p[i] = isl_aff_lift(maff->p[i]);
4786 if (!maff->p[i])
4787 goto error;
4790 return maff;
4791 error:
4792 if (ls)
4793 isl_local_space_free(*ls);
4794 return isl_multi_aff_free(maff);
4798 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4800 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4801 __isl_keep isl_pw_multi_aff *pma, int pos)
4803 int i;
4804 int n_out;
4805 isl_space *space;
4806 isl_pw_aff *pa;
4808 if (!pma)
4809 return NULL;
4811 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4812 if (pos < 0 || pos >= n_out)
4813 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4814 "index out of bounds", return NULL);
4816 space = isl_pw_multi_aff_get_space(pma);
4817 space = isl_space_drop_dims(space, isl_dim_out,
4818 pos + 1, n_out - pos - 1);
4819 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4821 pa = isl_pw_aff_alloc_size(space, pma->n);
4822 for (i = 0; i < pma->n; ++i) {
4823 isl_aff *aff;
4824 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4825 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4828 return pa;
4831 /* Return an isl_pw_multi_aff with the given "set" as domain and
4832 * an unnamed zero-dimensional range.
4834 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4835 __isl_take isl_set *set)
4837 isl_multi_aff *ma;
4838 isl_space *space;
4840 space = isl_set_get_space(set);
4841 space = isl_space_from_domain(space);
4842 ma = isl_multi_aff_zero(space);
4843 return isl_pw_multi_aff_alloc(set, ma);
4846 /* Add an isl_pw_multi_aff with the given "set" as domain and
4847 * an unnamed zero-dimensional range to *user.
4849 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4851 isl_union_pw_multi_aff **upma = user;
4852 isl_pw_multi_aff *pma;
4854 pma = isl_pw_multi_aff_from_domain(set);
4855 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4857 return 0;
4860 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4861 * an unnamed zero-dimensional range.
4863 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4864 __isl_take isl_union_set *uset)
4866 isl_space *space;
4867 isl_union_pw_multi_aff *upma;
4869 if (!uset)
4870 return NULL;
4872 space = isl_union_set_get_space(uset);
4873 upma = isl_union_pw_multi_aff_empty(space);
4875 if (isl_union_set_foreach_set(uset,
4876 &add_pw_multi_aff_from_domain, &upma) < 0)
4877 goto error;
4879 isl_union_set_free(uset);
4880 return upma;
4881 error:
4882 isl_union_set_free(uset);
4883 isl_union_pw_multi_aff_free(upma);
4884 return NULL;
4887 /* Convert "pma" to an isl_map and add it to *umap.
4889 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4891 isl_union_map **umap = user;
4892 isl_map *map;
4894 map = isl_map_from_pw_multi_aff(pma);
4895 *umap = isl_union_map_add_map(*umap, map);
4897 return 0;
4900 /* Construct a union map mapping the domain of the union
4901 * piecewise multi-affine expression to its range, with each dimension
4902 * in the range equated to the corresponding affine expression on its cell.
4904 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4905 __isl_take isl_union_pw_multi_aff *upma)
4907 isl_space *space;
4908 isl_union_map *umap;
4910 if (!upma)
4911 return NULL;
4913 space = isl_union_pw_multi_aff_get_space(upma);
4914 umap = isl_union_map_empty(space);
4916 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4917 &map_from_pw_multi_aff, &umap) < 0)
4918 goto error;
4920 isl_union_pw_multi_aff_free(upma);
4921 return umap;
4922 error:
4923 isl_union_pw_multi_aff_free(upma);
4924 isl_union_map_free(umap);
4925 return NULL;
4928 /* Local data for bin_entry and the callback "fn".
4930 struct isl_union_pw_multi_aff_bin_data {
4931 isl_union_pw_multi_aff *upma2;
4932 isl_union_pw_multi_aff *res;
4933 isl_pw_multi_aff *pma;
4934 int (*fn)(void **entry, void *user);
4937 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4938 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4940 static int bin_entry(void **entry, void *user)
4942 struct isl_union_pw_multi_aff_bin_data *data = user;
4943 isl_pw_multi_aff *pma = *entry;
4945 data->pma = pma;
4946 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4947 data->fn, data) < 0)
4948 return -1;
4950 return 0;
4953 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4954 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4955 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4956 * as *entry. The callback should adjust data->res if desired.
4958 static __isl_give isl_union_pw_multi_aff *bin_op(
4959 __isl_take isl_union_pw_multi_aff *upma1,
4960 __isl_take isl_union_pw_multi_aff *upma2,
4961 int (*fn)(void **entry, void *user))
4963 isl_space *space;
4964 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4966 space = isl_union_pw_multi_aff_get_space(upma2);
4967 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4968 space = isl_union_pw_multi_aff_get_space(upma1);
4969 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4971 if (!upma1 || !upma2)
4972 goto error;
4974 data.upma2 = upma2;
4975 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4976 upma1->table.n);
4977 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4978 &bin_entry, &data) < 0)
4979 goto error;
4981 isl_union_pw_multi_aff_free(upma1);
4982 isl_union_pw_multi_aff_free(upma2);
4983 return data.res;
4984 error:
4985 isl_union_pw_multi_aff_free(upma1);
4986 isl_union_pw_multi_aff_free(upma2);
4987 isl_union_pw_multi_aff_free(data.res);
4988 return NULL;
4991 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4992 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4994 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4995 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4997 isl_space *space;
4999 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5000 isl_pw_multi_aff_get_space(pma2));
5001 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5002 &isl_multi_aff_range_product);
5005 /* Given two isl_pw_multi_affs A -> B and C -> D,
5006 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5008 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5009 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5011 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5012 &pw_multi_aff_range_product);
5015 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5016 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5018 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5019 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5021 isl_space *space;
5023 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5024 isl_pw_multi_aff_get_space(pma2));
5025 space = isl_space_flatten_range(space);
5026 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5027 &isl_multi_aff_flat_range_product);
5030 /* Given two isl_pw_multi_affs A -> B and C -> D,
5031 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5033 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5034 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5036 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5037 &pw_multi_aff_flat_range_product);
5040 /* If data->pma and *entry have the same domain space, then compute
5041 * their flat range product and the result to data->res.
5043 static int flat_range_product_entry(void **entry, void *user)
5045 struct isl_union_pw_multi_aff_bin_data *data = user;
5046 isl_pw_multi_aff *pma2 = *entry;
5048 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
5049 pma2->dim, isl_dim_in))
5050 return 0;
5052 pma2 = isl_pw_multi_aff_flat_range_product(
5053 isl_pw_multi_aff_copy(data->pma),
5054 isl_pw_multi_aff_copy(pma2));
5056 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5058 return 0;
5061 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5062 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5064 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5065 __isl_take isl_union_pw_multi_aff *upma1,
5066 __isl_take isl_union_pw_multi_aff *upma2)
5068 return bin_op(upma1, upma2, &flat_range_product_entry);
5071 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5072 * The parameters are assumed to have been aligned.
5074 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5075 * except that it works on two different isl_pw_* types.
5077 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5078 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5079 __isl_take isl_pw_aff *pa)
5081 int i, j, n;
5082 isl_pw_multi_aff *res = NULL;
5084 if (!pma || !pa)
5085 goto error;
5087 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
5088 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5089 "domains don't match", goto error);
5090 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5091 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5092 "index out of bounds", goto error);
5094 n = pma->n * pa->n;
5095 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5097 for (i = 0; i < pma->n; ++i) {
5098 for (j = 0; j < pa->n; ++j) {
5099 isl_set *common;
5100 isl_multi_aff *res_ij;
5101 int empty;
5103 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5104 isl_set_copy(pa->p[j].set));
5105 empty = isl_set_plain_is_empty(common);
5106 if (empty < 0 || empty) {
5107 isl_set_free(common);
5108 if (empty < 0)
5109 goto error;
5110 continue;
5113 res_ij = isl_multi_aff_set_aff(
5114 isl_multi_aff_copy(pma->p[i].maff), pos,
5115 isl_aff_copy(pa->p[j].aff));
5116 res_ij = isl_multi_aff_gist(res_ij,
5117 isl_set_copy(common));
5119 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5123 isl_pw_multi_aff_free(pma);
5124 isl_pw_aff_free(pa);
5125 return res;
5126 error:
5127 isl_pw_multi_aff_free(pma);
5128 isl_pw_aff_free(pa);
5129 return isl_pw_multi_aff_free(res);
5132 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5134 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5135 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5136 __isl_take isl_pw_aff *pa)
5138 if (!pma || !pa)
5139 goto error;
5140 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5141 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5142 if (!isl_space_has_named_params(pma->dim) ||
5143 !isl_space_has_named_params(pa->dim))
5144 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5145 "unaligned unnamed parameters", goto error);
5146 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5147 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5148 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5149 error:
5150 isl_pw_multi_aff_free(pma);
5151 isl_pw_aff_free(pa);
5152 return NULL;
5155 /* Check that the domain space of "pa" matches "space".
5157 * Return 0 on success and -1 on error.
5159 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5160 __isl_keep isl_space *space)
5162 isl_space *pa_space;
5163 int match;
5165 if (!pa || !space)
5166 return -1;
5168 pa_space = isl_pw_aff_get_space(pa);
5170 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5171 if (match < 0)
5172 goto error;
5173 if (!match)
5174 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5175 "parameters don't match", goto error);
5176 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
5177 if (match < 0)
5178 goto error;
5179 if (!match)
5180 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5181 "domains don't match", goto error);
5182 isl_space_free(pa_space);
5183 return 0;
5184 error:
5185 isl_space_free(pa_space);
5186 return -1;
5189 #undef BASE
5190 #define BASE pw_aff
5192 #include <isl_multi_templ.c>
5194 /* Scale the elements of "pma" by the corresponding elements of "mv".
5196 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5197 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5199 int i;
5201 pma = isl_pw_multi_aff_cow(pma);
5202 if (!pma || !mv)
5203 goto error;
5204 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5205 mv->space, isl_dim_set))
5206 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5207 "spaces don't match", goto error);
5208 if (!isl_space_match(pma->dim, isl_dim_param,
5209 mv->space, isl_dim_param)) {
5210 pma = isl_pw_multi_aff_align_params(pma,
5211 isl_multi_val_get_space(mv));
5212 mv = isl_multi_val_align_params(mv,
5213 isl_pw_multi_aff_get_space(pma));
5214 if (!pma || !mv)
5215 goto error;
5218 for (i = 0; i < pma->n; ++i) {
5219 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5220 isl_multi_val_copy(mv));
5221 if (!pma->p[i].maff)
5222 goto error;
5225 isl_multi_val_free(mv);
5226 return pma;
5227 error:
5228 isl_multi_val_free(mv);
5229 isl_pw_multi_aff_free(pma);
5230 return NULL;
5233 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5234 * mv contains the mv argument.
5235 * res collects the results.
5237 struct isl_union_pw_multi_aff_scale_multi_val_data {
5238 isl_multi_val *mv;
5239 isl_union_pw_multi_aff *res;
5242 /* This function is called for each entry of an isl_union_pw_multi_aff.
5243 * If the space of the entry matches that of data->mv,
5244 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5245 * to data->res.
5247 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5249 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5250 isl_pw_multi_aff *pma = *entry;
5252 if (!pma)
5253 return -1;
5254 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5255 data->mv->space, isl_dim_set))
5256 return 0;
5258 pma = isl_pw_multi_aff_copy(pma);
5259 pma = isl_pw_multi_aff_scale_multi_val(pma,
5260 isl_multi_val_copy(data->mv));
5261 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5262 if (!data->res)
5263 return -1;
5265 return 0;
5268 /* Scale the elements of "upma" by the corresponding elements of "mv",
5269 * for those entries that match the space of "mv".
5271 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5272 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5274 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5276 upma = isl_union_pw_multi_aff_align_params(upma,
5277 isl_multi_val_get_space(mv));
5278 mv = isl_multi_val_align_params(mv,
5279 isl_union_pw_multi_aff_get_space(upma));
5280 if (!upma || !mv)
5281 goto error;
5283 data.mv = mv;
5284 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->dim),
5285 upma->table.n);
5286 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5287 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5288 goto error;
5290 isl_multi_val_free(mv);
5291 isl_union_pw_multi_aff_free(upma);
5292 return data.res;
5293 error:
5294 isl_multi_val_free(mv);
5295 isl_union_pw_multi_aff_free(upma);
5296 return NULL;