update isl to version 0.08
[barvinok.git] / evalue_isl.c
blobb8000b67d94b23c1e2d4ca3cd68bdec4fe2ee0e7
1 #include <isl/aff.h>
2 #include <isl_set_polylib.h>
3 #include <isl/constraint.h>
4 #include <isl/seq.h>
5 #include <barvinok/evalue.h>
7 static __isl_give isl_qpolynomial *extract_base(__isl_take isl_space *dim,
8 const evalue *e)
10 int i;
11 isl_ctx *ctx;
12 isl_vec *v;
13 isl_local_space *ls;
14 isl_aff *aff;
15 isl_qpolynomial *base, *c;
16 unsigned nparam;
18 if (!dim)
19 return NULL;
21 if (e->x.p->type == polynomial)
22 return isl_qpolynomial_var_on_domain(dim, isl_dim_param, e->x.p->pos - 1);
24 ctx = isl_space_get_ctx(dim);
25 nparam = isl_space_dim(dim, isl_dim_param);
26 v = isl_vec_alloc(ctx, 2 + nparam);
27 if (!v)
28 goto error;
30 isl_seq_clr(v->el + 2, nparam);
31 evalue_extract_affine(&e->x.p->arr[0], v->el + 2, &v->el[1], &v->el[0]);
33 ls = isl_local_space_from_space(isl_space_copy(dim));
34 aff = isl_aff_zero_on_domain(ls);
35 aff = isl_aff_set_constant(aff, v->el[1]);
36 aff = isl_aff_set_denominator(aff, v->el[0]);
38 for (i = 0; i < nparam; ++i)
39 aff = isl_aff_set_coefficient(aff, isl_dim_param, i,
40 v->el[2 + i]);
42 aff = isl_aff_floor(aff);
43 base = isl_qpolynomial_from_aff(aff);
45 if (e->x.p->type == fractional) {
46 base = isl_qpolynomial_neg(base);
48 c = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(dim), v->el[1], v->el[0]);
49 base = isl_qpolynomial_add(base, c);
51 for (i = 0; i < nparam; ++i) {
52 isl_qpolynomial *t;
53 c = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(dim),
54 v->el[2 + i], v->el[0]);
55 t = isl_qpolynomial_var_on_domain(isl_space_copy(dim),
56 isl_dim_param, i);
57 t = isl_qpolynomial_mul(c, t);
58 base = isl_qpolynomial_add(base, t);
62 isl_vec_free(v);
63 isl_space_free(dim);
65 return base;
66 error:
67 isl_space_free(dim);
68 return NULL;
71 static int type_offset(enode *p)
73 return p->type == fractional ? 1 :
74 p->type == flooring ? 1 : 0;
77 __isl_give isl_qpolynomial *isl_qpolynomial_from_evalue(__isl_take isl_space *dim,
78 const evalue *e)
80 int i;
81 isl_qpolynomial *qp;
82 isl_qpolynomial *base;
83 int offset;
85 if (EVALUE_IS_NAN(*e))
86 return isl_qpolynomial_infty_on_domain(dim);
87 if (value_notzero_p(e->d))
88 return isl_qpolynomial_rat_cst_on_domain(dim, e->x.n, e->d);
90 offset = type_offset(e->x.p);
92 assert(e->x.p->type == polynomial ||
93 e->x.p->type == flooring ||
94 e->x.p->type == fractional);
95 assert(e->x.p->size >= 1 + offset);
97 base = extract_base(isl_space_copy(dim), e);
98 qp = isl_qpolynomial_from_evalue(isl_space_copy(dim),
99 &e->x.p->arr[e->x.p->size - 1]);
101 for (i = e->x.p->size - 2; i >= offset; --i) {
102 qp = isl_qpolynomial_mul(qp, isl_qpolynomial_copy(base));
103 qp = isl_qpolynomial_add(qp,
104 isl_qpolynomial_from_evalue(isl_space_copy(dim),
105 &e->x.p->arr[i]));
108 isl_qpolynomial_free(base);
109 isl_space_free(dim);
111 return qp;
114 static __isl_give isl_pw_qpolynomial *guarded_evalue2pwqp(__isl_take isl_set *set,
115 const evalue *e);
117 static __isl_give isl_pw_qpolynomial *relation2pwqp(__isl_take isl_set *set,
118 const evalue *e)
120 int i;
121 isl_vec *vec;
122 isl_space *dim;
123 isl_ctx *ctx;
124 unsigned nparam;
125 isl_pw_qpolynomial *pwqp;
126 struct isl_constraint *c;
127 struct isl_basic_set *bset;
128 struct isl_set *guard;
129 const evalue *fract;
131 if (!set || !e)
132 goto error;
134 if (e->x.p->size == 1) {
135 dim = isl_set_get_space(set);
136 isl_set_free(set);
137 return isl_pw_qpolynomial_zero(dim);
140 ctx = isl_set_get_ctx(set);
141 isl_assert(ctx, e->x.p->size > 0, goto error);
142 isl_assert(ctx, e->x.p->size <= 3, goto error);
143 isl_assert(ctx, value_zero_p(e->x.p->arr[0].d), goto error);
144 isl_assert(ctx, e->x.p->arr[0].x.p->type == fractional, goto error);
145 fract = &e->x.p->arr[0];
147 nparam = isl_set_dim(set, isl_dim_param);
148 vec = isl_vec_alloc(ctx, 2 + nparam + 1);
149 if (!vec)
150 goto error;
152 isl_seq_clr(vec->el + 2, nparam);
153 evalue_extract_affine(&fract->x.p->arr[0],
154 vec->el + 2, &vec->el[1], &vec->el[0]);
156 dim = isl_set_get_space(set);
157 dim = isl_space_add_dims(dim, isl_dim_set, 1);
159 bset = isl_basic_set_universe(isl_space_copy(dim));
160 c = isl_equality_alloc(isl_local_space_from_space(dim));
161 isl_int_neg(vec->el[0], vec->el[0]);
162 isl_constraint_set_coefficient(c, isl_dim_set, 0, vec->el[0]);
163 isl_constraint_set_constant(c, vec->el[1]);
164 for (i = 0; i < nparam; ++i)
165 isl_constraint_set_coefficient(c, isl_dim_param, i, vec->el[2+i]);
166 bset = isl_basic_set_add_constraint(bset, c);
167 bset = isl_basic_set_params(bset);
168 guard = isl_set_from_basic_set(bset);
169 isl_vec_free(vec);
171 pwqp = guarded_evalue2pwqp(isl_set_intersect(isl_set_copy(set),
172 isl_set_copy(guard)),
173 &e->x.p->arr[1]);
175 if (e->x.p->size == 3) {
176 isl_pw_qpolynomial *pwqpc;
177 guard = isl_set_complement(guard);
178 pwqpc = guarded_evalue2pwqp(isl_set_intersect(isl_set_copy(set),
179 isl_set_copy(guard)),
180 &e->x.p->arr[2]);
181 pwqp = isl_pw_qpolynomial_add_disjoint(pwqp, pwqpc);
184 isl_set_free(set);
185 isl_set_free(guard);
187 return pwqp;
188 error:
189 isl_set_free(set);
190 return NULL;
193 static __isl_give isl_pw_qpolynomial *guarded_evalue2pwqp(__isl_take isl_set *set,
194 const evalue *e)
196 isl_qpolynomial *qp;
198 if (value_zero_p(e->d) && e->x.p->type == relation)
199 return relation2pwqp(set, e);
201 qp = isl_qpolynomial_from_evalue(isl_set_get_space(set), e);
203 return isl_pw_qpolynomial_alloc(set, qp);
206 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_evalue(__isl_take isl_space *dim, const evalue *e)
208 int i;
209 isl_space *pw_space;
210 isl_pw_qpolynomial *pwqp;
212 if (!dim || !e)
213 goto error;
214 if (EVALUE_IS_ZERO(*e))
215 return isl_pw_qpolynomial_zero(dim);
217 if (value_notzero_p(e->d)) {
218 isl_set *set = isl_set_universe(isl_space_copy(dim));
219 isl_qpolynomial *qp = isl_qpolynomial_rat_cst_on_domain(dim, e->x.n, e->d);
220 return isl_pw_qpolynomial_alloc(set, qp);
223 assert(!EVALUE_IS_NAN(*e));
225 assert(e->x.p->type == partition);
227 pw_space = isl_space_copy(dim);
228 pw_space = isl_space_from_domain(pw_space);
229 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
230 pwqp = isl_pw_qpolynomial_zero(pw_space);
232 for (i = 0; i < e->x.p->size/2; ++i) {
233 Polyhedron *D = EVALUE_DOMAIN(e->x.p->arr[2 * i]);
234 isl_set *set = isl_set_new_from_polylib(D, isl_space_copy(dim));
235 isl_pw_qpolynomial *pwqp_i;
237 pwqp_i = guarded_evalue2pwqp(set, &e->x.p->arr[2 * i + 1]);
239 pwqp = isl_pw_qpolynomial_add_disjoint(pwqp, pwqp_i);
242 isl_space_free(dim);
244 return pwqp;
245 error:
246 isl_space_free(dim);
247 return NULL;
250 static evalue *evalue_pow(evalue *e, int exp)
252 evalue *pow;
254 if (exp == 1)
255 return e;
257 pow = evalue_dup(e);
258 while (--exp > 0)
259 emul(e, pow);
261 evalue_free(e);
263 return pow;
266 static evalue *div2evalue(__isl_take isl_aff *div)
268 int i;
269 isl_ctx *ctx;
270 isl_vec *vec;
271 unsigned dim;
272 unsigned nparam;
273 evalue *e;
274 evalue *aff;
276 if (!div)
277 return NULL;
279 dim = isl_aff_dim(div, isl_dim_in);
280 nparam = isl_aff_dim(div, isl_dim_param);
282 ctx = isl_aff_get_ctx(div);
283 vec = isl_vec_alloc(ctx, 1 + dim + nparam + 1);
284 if (!vec)
285 goto error;
286 for (i = 0; i < dim; ++i)
287 isl_aff_get_coefficient(div, isl_dim_in, i, &vec->el[1 + i]);
288 for (i = 0; i < nparam; ++i)
289 isl_aff_get_coefficient(div, isl_dim_param, i,
290 &vec->el[1 + dim + i]);
291 isl_aff_get_denominator(div, &vec->el[0]);
292 isl_aff_get_constant(div, &vec->el[1 + dim + nparam]);
294 e = isl_alloc_type(ctx, evalue);
295 if (!e)
296 goto error;
297 value_init(e->d);
298 value_set_si(e->d, 0);
299 e->x.p = new_enode(flooring, 3, -1);
300 evalue_set_si(&e->x.p->arr[1], 0, 1);
301 evalue_set_si(&e->x.p->arr[2], 1, 1);
302 value_clear(e->x.p->arr[0].d);
303 aff = affine2evalue(vec->el + 1, vec->el[0], dim + nparam);
304 e->x.p->arr[0] = *aff;
305 free(aff);
306 isl_vec_free(vec);
307 isl_aff_free(div);
308 return e;
309 error:
310 isl_vec_free(vec);
311 isl_aff_free(div);
312 return NULL;
315 static int add_term(__isl_take isl_term *term, void *user)
317 int i;
318 evalue *sum = (evalue *)user;
319 unsigned nparam;
320 unsigned dim;
321 unsigned n_div;
322 isl_ctx *ctx;
323 isl_int n, d;
324 evalue *e;
326 if (!term)
327 return -1;
329 nparam = isl_term_dim(term, isl_dim_param);
330 dim = isl_term_dim(term, isl_dim_set);
331 n_div = isl_term_dim(term, isl_dim_div);
333 ctx = isl_term_get_ctx(term);
334 e = isl_alloc_type(ctx, evalue);
335 if (!e)
336 goto error;
338 isl_int_init(n);
339 isl_int_init(d);
341 isl_term_get_num(term, &n);
342 isl_term_get_den(term, &d);
343 value_init(e->d);
344 evalue_set(e, n, d);
346 for (i = 0; i < dim; ++i) {
347 evalue *pow;
348 int exp = isl_term_get_exp(term, isl_dim_set, i);
350 if (!exp)
351 continue;
353 pow = evalue_pow(evalue_var(i), exp);
354 emul(pow, e);
355 evalue_free(pow);
358 for (i = 0; i < nparam; ++i) {
359 evalue *pow;
360 int exp = isl_term_get_exp(term, isl_dim_param, i);
362 if (!exp)
363 continue;
365 pow = evalue_pow(evalue_var(dim + i), exp);
366 emul(pow, e);
367 evalue_free(pow);
370 for (i = 0; i < n_div; ++i) {
371 evalue *pow;
372 evalue *floor;
373 isl_aff *div;
374 int exp = isl_term_get_exp(term, isl_dim_div, i);
376 if (!exp)
377 continue;
379 div = isl_term_get_div(term, i);
380 floor = div2evalue(div);
381 pow = evalue_pow(floor, exp);
382 emul(pow, e);
383 evalue_free(pow);
386 eadd(e, sum);
387 evalue_free(e);
389 isl_int_clear(n);
390 isl_int_clear(d);
392 isl_term_free(term);
394 return 0;
395 error:
396 isl_term_free(term);
397 return -1;
400 evalue *isl_qpolynomial_to_evalue(__isl_keep isl_qpolynomial *qp)
402 evalue *e;
404 e = evalue_zero();
405 if (!e)
406 return NULL;
408 if (isl_qpolynomial_foreach_term(qp, add_term, e) < 0)
409 goto error;
411 return e;
412 error:
413 evalue_free(e);
414 return NULL;
417 static int add_guarded_qp(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp,
418 void *user)
420 Polyhedron *D;
421 evalue *e = NULL;
422 evalue *f;
423 evalue *sum = (evalue *)user;
424 unsigned dim;
426 e = isl_alloc_type(isl_set_get_ctx(set), evalue);
427 if (!e)
428 goto error;
430 D = isl_set_to_polylib(set);
431 if (!D)
432 goto error;
434 f = isl_qpolynomial_to_evalue(qp);
435 if (!e) {
436 Domain_Free(D);
437 goto error;
440 dim = isl_set_dim(set, isl_dim_param) + isl_set_dim(set, isl_dim_set);
441 value_init(e->d);
442 e->x.p = new_enode(partition, 2, D->Dimension);
443 EVALUE_SET_DOMAIN(e->x.p->arr[0], D);
445 value_clear(e->x.p->arr[1].d);
446 e->x.p->arr[1] = *f;
447 free(f);
449 eadd(e, sum);
450 evalue_free(e);
452 isl_set_free(set);
453 isl_qpolynomial_free(qp);
455 return 0;
456 error:
457 free(e);
458 isl_set_free(set);
459 isl_qpolynomial_free(qp);
460 return -1;
463 evalue *isl_pw_qpolynomial_to_evalue(__isl_keep isl_pw_qpolynomial *pwqp)
465 evalue *e;
467 if (!pwqp)
468 return NULL;
469 e = evalue_zero();
471 if (isl_pw_qpolynomial_foreach_piece(pwqp, add_guarded_qp, e))
472 goto error;
474 return e;
475 error:
476 evalue_free(e);
477 return NULL;