Add files generated by bison and flex to the distribution
[barvinok.git] / evalue.c
blobbe83fa1878dc185366437f249dd943ac667756a9
1 /***********************************************************************/
2 /* copyright 1997, Doran Wilde */
3 /* copyright 1997-2000, Vincent Loechner */
4 /* copyright 2003-2006, Sven Verdoolaege */
5 /* Permission is granted to copy, use, and distribute */
6 /* for any commercial or noncommercial purpose under the terms */
7 /* of the GNU General Public license, version 2, June 1991 */
8 /* (see file : LICENSE). */
9 /***********************************************************************/
11 #include <alloca.h>
12 #include <assert.h>
13 #include <math.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <barvinok/evalue.h>
17 #include <barvinok/barvinok.h>
18 #include <barvinok/util.h>
19 #include "summate.h"
21 #ifndef value_pmodulus
22 #define value_pmodulus(ref,val1,val2) (mpz_fdiv_r((ref),(val1),(val2)))
23 #endif
25 #define ALLOC(type) (type*)malloc(sizeof(type))
26 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
28 #ifdef __GNUC__
29 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
30 #else
31 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
32 #endif
34 void evalue_set_si(evalue *ev, int n, int d) {
35 value_set_si(ev->d, d);
36 value_init(ev->x.n);
37 value_set_si(ev->x.n, n);
40 void evalue_set(evalue *ev, Value n, Value d) {
41 value_assign(ev->d, d);
42 value_init(ev->x.n);
43 value_assign(ev->x.n, n);
46 void evalue_set_reduce(evalue *ev, Value n, Value d) {
47 value_init(ev->x.n);
48 value_gcd(ev->x.n, n, d);
49 value_divexact(ev->d, d, ev->x.n);
50 value_divexact(ev->x.n, n, ev->x.n);
53 evalue* evalue_zero()
55 evalue *EP = ALLOC(evalue);
56 value_init(EP->d);
57 evalue_set_si(EP, 0, 1);
58 return EP;
61 evalue *evalue_nan()
63 evalue *EP = ALLOC(evalue);
64 value_init(EP->d);
65 value_set_si(EP->d, -2);
66 EP->x.p = NULL;
67 return EP;
70 /* returns an evalue that corresponds to
72 * x_var
74 evalue *evalue_var(int var)
76 evalue *EP = ALLOC(evalue);
77 value_init(EP->d);
78 value_set_si(EP->d,0);
79 EP->x.p = new_enode(polynomial, 2, var + 1);
80 evalue_set_si(&EP->x.p->arr[0], 0, 1);
81 evalue_set_si(&EP->x.p->arr[1], 1, 1);
82 return EP;
85 void aep_evalue(evalue *e, int *ref) {
87 enode *p;
88 int i;
90 if (value_notzero_p(e->d))
91 return; /* a rational number, its already reduced */
92 if(!(p = e->x.p))
93 return; /* hum... an overflow probably occured */
95 /* First check the components of p */
96 for (i=0;i<p->size;i++)
97 aep_evalue(&p->arr[i],ref);
99 /* Then p itself */
100 switch (p->type) {
101 case polynomial:
102 case periodic:
103 case evector:
104 p->pos = ref[p->pos-1]+1;
106 return;
107 } /* aep_evalue */
109 /** Comments */
110 void addeliminatedparams_evalue(evalue *e,Matrix *CT) {
112 enode *p;
113 int i, j;
114 int *ref;
116 if (value_notzero_p(e->d))
117 return; /* a rational number, its already reduced */
118 if(!(p = e->x.p))
119 return; /* hum... an overflow probably occured */
121 /* Compute ref */
122 ref = (int *)malloc(sizeof(int)*(CT->NbRows-1));
123 for(i=0;i<CT->NbRows-1;i++)
124 for(j=0;j<CT->NbColumns;j++)
125 if(value_notzero_p(CT->p[i][j])) {
126 ref[i] = j;
127 break;
130 /* Transform the references in e, using ref */
131 aep_evalue(e,ref);
132 free( ref );
133 return;
134 } /* addeliminatedparams_evalue */
136 static void addeliminatedparams_partition(enode *p, Matrix *CT, Polyhedron *CEq,
137 unsigned nparam, unsigned MaxRays)
139 int i;
140 assert(p->type == partition);
141 p->pos = nparam;
143 for (i = 0; i < p->size/2; i++) {
144 Polyhedron *D = EVALUE_DOMAIN(p->arr[2*i]);
145 Polyhedron *T = DomainPreimage(D, CT, MaxRays);
146 Domain_Free(D);
147 if (CEq) {
148 D = T;
149 T = DomainIntersection(D, CEq, MaxRays);
150 Domain_Free(D);
152 EVALUE_SET_DOMAIN(p->arr[2*i], T);
156 void addeliminatedparams_enum(evalue *e, Matrix *CT, Polyhedron *CEq,
157 unsigned MaxRays, unsigned nparam)
159 enode *p;
160 int i;
162 if (CT->NbRows == CT->NbColumns)
163 return;
165 if (EVALUE_IS_ZERO(*e))
166 return;
168 if (value_notzero_p(e->d)) {
169 evalue res;
170 value_init(res.d);
171 value_set_si(res.d, 0);
172 res.x.p = new_enode(partition, 2, nparam);
173 EVALUE_SET_DOMAIN(res.x.p->arr[0],
174 DomainConstraintSimplify(Polyhedron_Copy(CEq), MaxRays));
175 value_clear(res.x.p->arr[1].d);
176 res.x.p->arr[1] = *e;
177 *e = res;
178 return;
181 p = e->x.p;
182 assert(p);
184 addeliminatedparams_partition(p, CT, CEq, nparam, MaxRays);
185 for (i = 0; i < p->size/2; i++)
186 addeliminatedparams_evalue(&p->arr[2*i+1], CT);
189 static int mod_rational_cmp(evalue *e1, evalue *e2)
191 int r;
192 Value m;
193 Value m2;
194 value_init(m);
195 value_init(m2);
197 assert(value_notzero_p(e1->d));
198 assert(value_notzero_p(e2->d));
199 value_multiply(m, e1->x.n, e2->d);
200 value_multiply(m2, e2->x.n, e1->d);
201 if (value_lt(m, m2))
202 r = -1;
203 else if (value_gt(m, m2))
204 r = 1;
205 else
206 r = 0;
207 value_clear(m);
208 value_clear(m2);
210 return r;
213 static int mod_term_cmp_r(evalue *e1, evalue *e2)
215 if (value_notzero_p(e1->d)) {
216 int r;
217 if (value_zero_p(e2->d))
218 return -1;
219 return mod_rational_cmp(e1, e2);
221 if (value_notzero_p(e2->d))
222 return 1;
223 if (e1->x.p->pos < e2->x.p->pos)
224 return -1;
225 else if (e1->x.p->pos > e2->x.p->pos)
226 return 1;
227 else {
228 int r = mod_rational_cmp(&e1->x.p->arr[1], &e2->x.p->arr[1]);
229 return r == 0
230 ? mod_term_cmp_r(&e1->x.p->arr[0], &e2->x.p->arr[0])
231 : r;
235 static int mod_term_cmp(const evalue *e1, const evalue *e2)
237 assert(value_zero_p(e1->d));
238 assert(value_zero_p(e2->d));
239 assert(e1->x.p->type == fractional || e1->x.p->type == flooring);
240 assert(e2->x.p->type == fractional || e2->x.p->type == flooring);
241 return mod_term_cmp_r(&e1->x.p->arr[0], &e2->x.p->arr[0]);
244 static void check_order(const evalue *e)
246 int i;
247 evalue *a;
249 if (value_notzero_p(e->d))
250 return;
252 switch (e->x.p->type) {
253 case partition:
254 for (i = 0; i < e->x.p->size/2; ++i)
255 check_order(&e->x.p->arr[2*i+1]);
256 break;
257 case relation:
258 for (i = 1; i < e->x.p->size; ++i) {
259 a = &e->x.p->arr[i];
260 if (value_notzero_p(a->d))
261 continue;
262 switch (a->x.p->type) {
263 case relation:
264 assert(mod_term_cmp(&e->x.p->arr[0], &a->x.p->arr[0]) < 0);
265 break;
266 case partition:
267 assert(0);
269 check_order(a);
271 break;
272 case polynomial:
273 for (i = 0; i < e->x.p->size; ++i) {
274 a = &e->x.p->arr[i];
275 if (value_notzero_p(a->d))
276 continue;
277 switch (a->x.p->type) {
278 case polynomial:
279 assert(e->x.p->pos < a->x.p->pos);
280 break;
281 case relation:
282 case partition:
283 assert(0);
285 check_order(a);
287 break;
288 case fractional:
289 case flooring:
290 for (i = 1; i < e->x.p->size; ++i) {
291 a = &e->x.p->arr[i];
292 if (value_notzero_p(a->d))
293 continue;
294 switch (a->x.p->type) {
295 case polynomial:
296 case relation:
297 case partition:
298 assert(0);
301 break;
305 /* Negative pos means inequality */
306 /* s is negative of substitution if m is not zero */
307 struct fixed_param {
308 int pos;
309 evalue s;
310 Value d;
311 Value m;
314 struct subst {
315 struct fixed_param *fixed;
316 int n;
317 int max;
320 static int relations_depth(evalue *e)
322 int d;
324 for (d = 0;
325 value_zero_p(e->d) && e->x.p->type == relation;
326 e = &e->x.p->arr[1], ++d);
327 return d;
330 static void poly_denom_not_constant(evalue **pp, Value *d)
332 evalue *p = *pp;
333 value_set_si(*d, 1);
335 while (value_zero_p(p->d)) {
336 assert(p->x.p->type == polynomial);
337 assert(p->x.p->size == 2);
338 assert(value_notzero_p(p->x.p->arr[1].d));
339 value_lcm(*d, *d, p->x.p->arr[1].d);
340 p = &p->x.p->arr[0];
342 *pp = p;
345 static void poly_denom(evalue *p, Value *d)
347 poly_denom_not_constant(&p, d);
348 value_lcm(*d, *d, p->d);
351 static void realloc_substitution(struct subst *s, int d)
353 struct fixed_param *n;
354 int i;
355 NALLOC(n, s->max+d);
356 for (i = 0; i < s->n; ++i)
357 n[i] = s->fixed[i];
358 free(s->fixed);
359 s->fixed = n;
360 s->max += d;
363 static int add_modulo_substitution(struct subst *s, evalue *r)
365 evalue *p;
366 evalue *f;
367 evalue *m;
369 assert(value_zero_p(r->d) && r->x.p->type == relation);
370 m = &r->x.p->arr[0];
372 /* May have been reduced already */
373 if (value_notzero_p(m->d))
374 return 0;
376 assert(value_zero_p(m->d) && m->x.p->type == fractional);
377 assert(m->x.p->size == 3);
379 /* fractional was inverted during reduction
380 * invert it back and move constant in
382 if (!EVALUE_IS_ONE(m->x.p->arr[2])) {
383 assert(value_pos_p(m->x.p->arr[2].d));
384 assert(value_mone_p(m->x.p->arr[2].x.n));
385 value_set_si(m->x.p->arr[2].x.n, 1);
386 value_increment(m->x.p->arr[1].x.n, m->x.p->arr[1].x.n);
387 assert(value_eq(m->x.p->arr[1].x.n, m->x.p->arr[1].d));
388 value_set_si(m->x.p->arr[1].x.n, 1);
389 eadd(&m->x.p->arr[1], &m->x.p->arr[0]);
390 value_set_si(m->x.p->arr[1].x.n, 0);
391 value_set_si(m->x.p->arr[1].d, 1);
394 /* Oops. Nested identical relations. */
395 if (!EVALUE_IS_ZERO(m->x.p->arr[1]))
396 return 0;
398 if (s->n >= s->max) {
399 int d = relations_depth(r);
400 realloc_substitution(s, d);
403 p = &m->x.p->arr[0];
404 assert(value_zero_p(p->d) && p->x.p->type == polynomial);
405 assert(p->x.p->size == 2);
406 f = &p->x.p->arr[1];
408 assert(value_pos_p(f->x.n));
410 value_init(s->fixed[s->n].m);
411 value_assign(s->fixed[s->n].m, f->d);
412 s->fixed[s->n].pos = p->x.p->pos;
413 value_init(s->fixed[s->n].d);
414 value_assign(s->fixed[s->n].d, f->x.n);
415 value_init(s->fixed[s->n].s.d);
416 evalue_copy(&s->fixed[s->n].s, &p->x.p->arr[0]);
417 ++s->n;
419 return 1;
422 static int type_offset(enode *p)
424 return p->type == fractional ? 1 :
425 p->type == flooring ? 1 :
426 p->type == relation ? 1 : 0;
429 static void reorder_terms_about(enode *p, evalue *v)
431 int i;
432 int offset = type_offset(p);
434 for (i = p->size-1; i >= offset+1; i--) {
435 emul(v, &p->arr[i]);
436 eadd(&p->arr[i], &p->arr[i-1]);
437 free_evalue_refs(&(p->arr[i]));
439 p->size = offset+1;
440 free_evalue_refs(v);
443 void evalue_reorder_terms(evalue *e)
445 enode *p;
446 evalue f;
447 int offset;
449 assert(value_zero_p(e->d));
450 p = e->x.p;
451 assert(p->type == fractional ||
452 p->type == flooring ||
453 p->type == polynomial); /* for now */
455 offset = type_offset(p);
456 value_init(f.d);
457 value_set_si(f.d, 0);
458 f.x.p = new_enode(p->type, offset+2, p->pos);
459 if (offset == 1) {
460 value_clear(f.x.p->arr[0].d);
461 f.x.p->arr[0] = p->arr[0];
463 evalue_set_si(&f.x.p->arr[offset], 0, 1);
464 evalue_set_si(&f.x.p->arr[offset+1], 1, 1);
465 reorder_terms_about(p, &f);
466 value_clear(e->d);
467 *e = p->arr[offset];
468 free(p);
471 static void evalue_reduce_size(evalue *e)
473 int i, offset;
474 enode *p;
475 assert(value_zero_p(e->d));
477 p = e->x.p;
478 offset = type_offset(p);
480 /* Try to reduce the degree */
481 for (i = p->size-1; i >= offset+1; i--) {
482 if (!EVALUE_IS_ZERO(p->arr[i]))
483 break;
484 free_evalue_refs(&p->arr[i]);
486 if (i+1 < p->size)
487 p->size = i+1;
489 /* Try to reduce its strength */
490 if (p->type == relation) {
491 if (p->size == 1) {
492 free_evalue_refs(&p->arr[0]);
493 evalue_set_si(e, 0, 1);
494 free(p);
496 } else if (p->size == offset+1) {
497 value_clear(e->d);
498 memcpy(e, &p->arr[offset], sizeof(evalue));
499 if (offset == 1)
500 free_evalue_refs(&p->arr[0]);
501 free(p);
505 void _reduce_evalue (evalue *e, struct subst *s, int fract) {
507 enode *p;
508 int i, j, k;
509 int add;
511 if (value_notzero_p(e->d)) {
512 if (fract)
513 mpz_fdiv_r(e->x.n, e->x.n, e->d);
514 return; /* a rational number, its already reduced */
517 if(!(p = e->x.p))
518 return; /* hum... an overflow probably occured */
520 /* First reduce the components of p */
521 add = p->type == relation;
522 for (i=0; i<p->size; i++) {
523 if (add && i == 1)
524 add = add_modulo_substitution(s, e);
526 if (i == 0 && p->type==fractional)
527 _reduce_evalue(&p->arr[i], s, 1);
528 else
529 _reduce_evalue(&p->arr[i], s, fract);
531 if (add && i == p->size-1) {
532 --s->n;
533 value_clear(s->fixed[s->n].m);
534 value_clear(s->fixed[s->n].d);
535 free_evalue_refs(&s->fixed[s->n].s);
536 } else if (add && i == 1)
537 s->fixed[s->n-1].pos *= -1;
540 if (p->type==periodic) {
542 /* Try to reduce the period */
543 for (i=1; i<=(p->size)/2; i++) {
544 if ((p->size % i)==0) {
546 /* Can we reduce the size to i ? */
547 for (j=0; j<i; j++)
548 for (k=j+i; k<e->x.p->size; k+=i)
549 if (!eequal(&p->arr[j], &p->arr[k])) goto you_lose;
551 /* OK, lets do it */
552 for (j=i; j<p->size; j++) free_evalue_refs(&p->arr[j]);
553 p->size = i;
554 break;
556 you_lose: /* OK, lets not do it */
557 continue;
561 /* Try to reduce its strength */
562 if (p->size == 1) {
563 value_clear(e->d);
564 memcpy(e,&p->arr[0],sizeof(evalue));
565 free(p);
568 else if (p->type==polynomial) {
569 for (k = 0; s && k < s->n; ++k) {
570 if (s->fixed[k].pos == p->pos) {
571 int divide = value_notone_p(s->fixed[k].d);
572 evalue d;
574 if (value_notzero_p(s->fixed[k].m)) {
575 if (!fract)
576 continue;
577 assert(p->size == 2);
578 if (divide && value_ne(s->fixed[k].d, p->arr[1].x.n))
579 continue;
580 if (!mpz_divisible_p(s->fixed[k].m, p->arr[1].d))
581 continue;
582 divide = 1;
585 if (divide) {
586 value_init(d.d);
587 value_assign(d.d, s->fixed[k].d);
588 value_init(d.x.n);
589 if (value_notzero_p(s->fixed[k].m))
590 value_oppose(d.x.n, s->fixed[k].m);
591 else
592 value_set_si(d.x.n, 1);
595 for (i=p->size-1;i>=1;i--) {
596 emul(&s->fixed[k].s, &p->arr[i]);
597 if (divide)
598 emul(&d, &p->arr[i]);
599 eadd(&p->arr[i], &p->arr[i-1]);
600 free_evalue_refs(&(p->arr[i]));
602 p->size = 1;
603 _reduce_evalue(&p->arr[0], s, fract);
605 if (divide)
606 free_evalue_refs(&d);
608 break;
612 evalue_reduce_size(e);
614 else if (p->type==fractional) {
615 int reorder = 0;
616 evalue v;
618 if (value_notzero_p(p->arr[0].d)) {
619 value_init(v.d);
620 value_assign(v.d, p->arr[0].d);
621 value_init(v.x.n);
622 mpz_fdiv_r(v.x.n, p->arr[0].x.n, p->arr[0].d);
624 reorder = 1;
625 } else {
626 evalue *f, *base;
627 evalue *pp = &p->arr[0];
628 assert(value_zero_p(pp->d) && pp->x.p->type == polynomial);
629 assert(pp->x.p->size == 2);
631 /* search for exact duplicate among the modulo inequalities */
632 do {
633 f = &pp->x.p->arr[1];
634 for (k = 0; s && k < s->n; ++k) {
635 if (-s->fixed[k].pos == pp->x.p->pos &&
636 value_eq(s->fixed[k].d, f->x.n) &&
637 value_eq(s->fixed[k].m, f->d) &&
638 eequal(&s->fixed[k].s, &pp->x.p->arr[0]))
639 break;
641 if (k < s->n) {
642 Value g;
643 value_init(g);
645 /* replace { E/m } by { (E-1)/m } + 1/m */
646 poly_denom(pp, &g);
647 if (reorder) {
648 evalue extra;
649 value_init(extra.d);
650 evalue_set_si(&extra, 1, 1);
651 value_assign(extra.d, g);
652 eadd(&extra, &v.x.p->arr[1]);
653 free_evalue_refs(&extra);
655 /* We've been going in circles; stop now */
656 if (value_ge(v.x.p->arr[1].x.n, v.x.p->arr[1].d)) {
657 free_evalue_refs(&v);
658 value_init(v.d);
659 evalue_set_si(&v, 0, 1);
660 break;
662 } else {
663 value_init(v.d);
664 value_set_si(v.d, 0);
665 v.x.p = new_enode(fractional, 3, -1);
666 evalue_set_si(&v.x.p->arr[1], 1, 1);
667 value_assign(v.x.p->arr[1].d, g);
668 evalue_set_si(&v.x.p->arr[2], 1, 1);
669 evalue_copy(&v.x.p->arr[0], &p->arr[0]);
672 for (f = &v.x.p->arr[0]; value_zero_p(f->d);
673 f = &f->x.p->arr[0])
675 value_division(f->d, g, f->d);
676 value_multiply(f->x.n, f->x.n, f->d);
677 value_assign(f->d, g);
678 value_decrement(f->x.n, f->x.n);
679 mpz_fdiv_r(f->x.n, f->x.n, f->d);
681 value_gcd(g, f->d, f->x.n);
682 value_division(f->d, f->d, g);
683 value_division(f->x.n, f->x.n, g);
685 value_clear(g);
686 pp = &v.x.p->arr[0];
688 reorder = 1;
690 } while (k < s->n);
692 /* reduction may have made this fractional arg smaller */
693 i = reorder ? p->size : 1;
694 for ( ; i < p->size; ++i)
695 if (value_zero_p(p->arr[i].d) &&
696 p->arr[i].x.p->type == fractional &&
697 mod_term_cmp(e, &p->arr[i]) >= 0)
698 break;
699 if (i < p->size) {
700 value_init(v.d);
701 value_set_si(v.d, 0);
702 v.x.p = new_enode(fractional, 3, -1);
703 evalue_set_si(&v.x.p->arr[1], 0, 1);
704 evalue_set_si(&v.x.p->arr[2], 1, 1);
705 evalue_copy(&v.x.p->arr[0], &p->arr[0]);
707 reorder = 1;
710 if (!reorder) {
711 Value m;
712 Value r;
713 evalue *pp = &p->arr[0];
714 value_init(m);
715 value_init(r);
716 poly_denom_not_constant(&pp, &m);
717 mpz_fdiv_r(r, m, pp->d);
718 if (value_notzero_p(r)) {
719 value_init(v.d);
720 value_set_si(v.d, 0);
721 v.x.p = new_enode(fractional, 3, -1);
723 value_multiply(r, m, pp->x.n);
724 value_multiply(v.x.p->arr[1].d, m, pp->d);
725 value_init(v.x.p->arr[1].x.n);
726 mpz_fdiv_r(v.x.p->arr[1].x.n, r, pp->d);
727 mpz_fdiv_q(r, r, pp->d);
729 evalue_set_si(&v.x.p->arr[2], 1, 1);
730 evalue_copy(&v.x.p->arr[0], &p->arr[0]);
731 pp = &v.x.p->arr[0];
732 while (value_zero_p(pp->d))
733 pp = &pp->x.p->arr[0];
735 value_assign(pp->d, m);
736 value_assign(pp->x.n, r);
738 value_gcd(r, pp->d, pp->x.n);
739 value_division(pp->d, pp->d, r);
740 value_division(pp->x.n, pp->x.n, r);
742 reorder = 1;
744 value_clear(m);
745 value_clear(r);
748 if (!reorder) {
749 int invert = 0;
750 Value twice;
751 value_init(twice);
753 for (pp = &p->arr[0]; value_zero_p(pp->d);
754 pp = &pp->x.p->arr[0]) {
755 f = &pp->x.p->arr[1];
756 assert(value_pos_p(f->d));
757 mpz_mul_ui(twice, f->x.n, 2);
758 if (value_lt(twice, f->d))
759 break;
760 if (value_eq(twice, f->d))
761 continue;
762 invert = 1;
763 break;
766 if (invert) {
767 value_init(v.d);
768 value_set_si(v.d, 0);
769 v.x.p = new_enode(fractional, 3, -1);
770 evalue_set_si(&v.x.p->arr[1], 0, 1);
771 poly_denom(&p->arr[0], &twice);
772 value_assign(v.x.p->arr[1].d, twice);
773 value_decrement(v.x.p->arr[1].x.n, twice);
774 evalue_set_si(&v.x.p->arr[2], -1, 1);
775 evalue_copy(&v.x.p->arr[0], &p->arr[0]);
777 for (pp = &v.x.p->arr[0]; value_zero_p(pp->d);
778 pp = &pp->x.p->arr[0]) {
779 f = &pp->x.p->arr[1];
780 value_oppose(f->x.n, f->x.n);
781 mpz_fdiv_r(f->x.n, f->x.n, f->d);
783 value_division(pp->d, twice, pp->d);
784 value_multiply(pp->x.n, pp->x.n, pp->d);
785 value_assign(pp->d, twice);
786 value_oppose(pp->x.n, pp->x.n);
787 value_decrement(pp->x.n, pp->x.n);
788 mpz_fdiv_r(pp->x.n, pp->x.n, pp->d);
790 /* Maybe we should do this during reduction of
791 * the constant.
793 value_gcd(twice, pp->d, pp->x.n);
794 value_division(pp->d, pp->d, twice);
795 value_division(pp->x.n, pp->x.n, twice);
797 reorder = 1;
800 value_clear(twice);
804 if (reorder) {
805 reorder_terms_about(p, &v);
806 _reduce_evalue(&p->arr[1], s, fract);
809 evalue_reduce_size(e);
811 else if (p->type == flooring) {
812 /* Replace floor(constant) by its value */
813 if (value_notzero_p(p->arr[0].d)) {
814 evalue v;
815 value_init(v.d);
816 value_set_si(v.d, 1);
817 value_init(v.x.n);
818 mpz_fdiv_q(v.x.n, p->arr[0].x.n, p->arr[0].d);
819 reorder_terms_about(p, &v);
820 _reduce_evalue(&p->arr[1], s, fract);
822 evalue_reduce_size(e);
824 else if (p->type == relation) {
825 if (p->size == 3 && eequal(&p->arr[1], &p->arr[2])) {
826 free_evalue_refs(&(p->arr[2]));
827 free_evalue_refs(&(p->arr[0]));
828 p->size = 2;
829 value_clear(e->d);
830 *e = p->arr[1];
831 free(p);
832 return;
834 evalue_reduce_size(e);
835 if (value_notzero_p(e->d) || p != e->x.p)
836 return;
837 else {
838 int reduced = 0;
839 evalue *m;
840 m = &p->arr[0];
842 /* Relation was reduced by means of an identical
843 * inequality => remove
845 if (value_zero_p(m->d) && !EVALUE_IS_ZERO(m->x.p->arr[1]))
846 reduced = 1;
848 if (reduced || value_notzero_p(p->arr[0].d)) {
849 if (!reduced && value_zero_p(p->arr[0].x.n)) {
850 value_clear(e->d);
851 memcpy(e,&p->arr[1],sizeof(evalue));
852 if (p->size == 3)
853 free_evalue_refs(&(p->arr[2]));
854 } else {
855 if (p->size == 3) {
856 value_clear(e->d);
857 memcpy(e,&p->arr[2],sizeof(evalue));
858 } else
859 evalue_set_si(e, 0, 1);
860 free_evalue_refs(&(p->arr[1]));
862 free_evalue_refs(&(p->arr[0]));
863 free(p);
867 return;
868 } /* reduce_evalue */
870 static void add_substitution(struct subst *s, Value *row, unsigned dim)
872 int k, l;
873 evalue *r;
875 for (k = 0; k < dim; ++k)
876 if (value_notzero_p(row[k+1]))
877 break;
879 Vector_Normalize_Positive(row+1, dim+1, k);
880 assert(s->n < s->max);
881 value_init(s->fixed[s->n].d);
882 value_init(s->fixed[s->n].m);
883 value_assign(s->fixed[s->n].d, row[k+1]);
884 s->fixed[s->n].pos = k+1;
885 value_set_si(s->fixed[s->n].m, 0);
886 r = &s->fixed[s->n].s;
887 value_init(r->d);
888 for (l = k+1; l < dim; ++l)
889 if (value_notzero_p(row[l+1])) {
890 value_set_si(r->d, 0);
891 r->x.p = new_enode(polynomial, 2, l + 1);
892 value_init(r->x.p->arr[1].x.n);
893 value_oppose(r->x.p->arr[1].x.n, row[l+1]);
894 value_set_si(r->x.p->arr[1].d, 1);
895 r = &r->x.p->arr[0];
897 value_init(r->x.n);
898 value_oppose(r->x.n, row[dim+1]);
899 value_set_si(r->d, 1);
900 ++s->n;
903 static void _reduce_evalue_in_domain(evalue *e, Polyhedron *D, struct subst *s)
905 unsigned dim;
906 Polyhedron *orig = D;
908 s->n = 0;
909 dim = D->Dimension;
910 if (D->next)
911 D = DomainConvex(D, 0);
912 /* We don't perform any substitutions if the domain is a union.
913 * We may therefore miss out on some possible simplifications,
914 * e.g., if a variable is always even in the whole union,
915 * while there is a relation in the evalue that evaluates
916 * to zero for even values of the variable.
918 if (!D->next && D->NbEq) {
919 int j, k;
920 if (s->max < dim) {
921 if (s->max != 0)
922 realloc_substitution(s, dim);
923 else {
924 int d = relations_depth(e);
925 s->max = dim+d;
926 NALLOC(s->fixed, s->max);
929 for (j = 0; j < D->NbEq; ++j)
930 add_substitution(s, D->Constraint[j], dim);
932 if (D != orig)
933 Domain_Free(D);
934 _reduce_evalue(e, s, 0);
935 if (s->n != 0) {
936 int j;
937 for (j = 0; j < s->n; ++j) {
938 value_clear(s->fixed[j].d);
939 value_clear(s->fixed[j].m);
940 free_evalue_refs(&s->fixed[j].s);
945 void reduce_evalue_in_domain(evalue *e, Polyhedron *D)
947 struct subst s = { NULL, 0, 0 };
948 POL_ENSURE_VERTICES(D);
949 if (emptyQ(D)) {
950 if (EVALUE_IS_ZERO(*e))
951 return;
952 free_evalue_refs(e);
953 value_init(e->d);
954 evalue_set_si(e, 0, 1);
955 return;
957 _reduce_evalue_in_domain(e, D, &s);
958 if (s.max != 0)
959 free(s.fixed);
962 void reduce_evalue (evalue *e) {
963 struct subst s = { NULL, 0, 0 };
965 if (value_notzero_p(e->d))
966 return; /* a rational number, its already reduced */
968 if (e->x.p->type == partition) {
969 int i;
970 unsigned dim = -1;
971 for (i = 0; i < e->x.p->size/2; ++i) {
972 Polyhedron *D = EVALUE_DOMAIN(e->x.p->arr[2*i]);
974 /* This shouldn't really happen;
975 * Empty domains should not be added.
977 POL_ENSURE_VERTICES(D);
978 if (!emptyQ(D))
979 _reduce_evalue_in_domain(&e->x.p->arr[2*i+1], D, &s);
981 if (emptyQ(D) || EVALUE_IS_ZERO(e->x.p->arr[2*i+1])) {
982 free_evalue_refs(&e->x.p->arr[2*i+1]);
983 Domain_Free(EVALUE_DOMAIN(e->x.p->arr[2*i]));
984 value_clear(e->x.p->arr[2*i].d);
985 e->x.p->size -= 2;
986 e->x.p->arr[2*i] = e->x.p->arr[e->x.p->size];
987 e->x.p->arr[2*i+1] = e->x.p->arr[e->x.p->size+1];
988 --i;
991 if (e->x.p->size == 0) {
992 free(e->x.p);
993 evalue_set_si(e, 0, 1);
995 } else
996 _reduce_evalue(e, &s, 0);
997 if (s.max != 0)
998 free(s.fixed);
1001 static void print_evalue_r(FILE *DST, const evalue *e, const char **pname)
1003 if (EVALUE_IS_NAN(*e)) {
1004 fprintf(DST, "NaN");
1005 return;
1008 if(value_notzero_p(e->d)) {
1009 if(value_notone_p(e->d)) {
1010 value_print(DST,VALUE_FMT,e->x.n);
1011 fprintf(DST,"/");
1012 value_print(DST,VALUE_FMT,e->d);
1014 else {
1015 value_print(DST,VALUE_FMT,e->x.n);
1018 else
1019 print_enode(DST,e->x.p,pname);
1020 return;
1021 } /* print_evalue */
1023 void print_evalue(FILE *DST, const evalue *e, const char **pname)
1025 print_evalue_r(DST, e, pname);
1026 if (value_notzero_p(e->d))
1027 fprintf(DST, "\n");
1030 void print_enode(FILE *DST, enode *p, const char **pname)
1032 int i;
1034 if (!p) {
1035 fprintf(DST, "NULL");
1036 return;
1038 switch (p->type) {
1039 case evector:
1040 fprintf(DST, "{ ");
1041 for (i=0; i<p->size; i++) {
1042 print_evalue_r(DST, &p->arr[i], pname);
1043 if (i!=(p->size-1))
1044 fprintf(DST, ", ");
1046 fprintf(DST, " }\n");
1047 break;
1048 case polynomial:
1049 fprintf(DST, "( ");
1050 for (i=p->size-1; i>=0; i--) {
1051 print_evalue_r(DST, &p->arr[i], pname);
1052 if (i==1) fprintf(DST, " * %s + ", pname[p->pos-1]);
1053 else if (i>1)
1054 fprintf(DST, " * %s^%d + ", pname[p->pos-1], i);
1056 fprintf(DST, " )\n");
1057 break;
1058 case periodic:
1059 fprintf(DST, "[ ");
1060 for (i=0; i<p->size; i++) {
1061 print_evalue_r(DST, &p->arr[i], pname);
1062 if (i!=(p->size-1)) fprintf(DST, ", ");
1064 fprintf(DST," ]_%s", pname[p->pos-1]);
1065 break;
1066 case flooring:
1067 case fractional:
1068 fprintf(DST, "( ");
1069 for (i=p->size-1; i>=1; i--) {
1070 print_evalue_r(DST, &p->arr[i], pname);
1071 if (i >= 2) {
1072 fprintf(DST, " * ");
1073 fprintf(DST, p->type == flooring ? "[" : "{");
1074 print_evalue_r(DST, &p->arr[0], pname);
1075 fprintf(DST, p->type == flooring ? "]" : "}");
1076 if (i>2)
1077 fprintf(DST, "^%d + ", i-1);
1078 else
1079 fprintf(DST, " + ");
1082 fprintf(DST, " )\n");
1083 break;
1084 case relation:
1085 fprintf(DST, "[ ");
1086 print_evalue_r(DST, &p->arr[0], pname);
1087 fprintf(DST, "= 0 ] * \n");
1088 print_evalue_r(DST, &p->arr[1], pname);
1089 if (p->size > 2) {
1090 fprintf(DST, " +\n [ ");
1091 print_evalue_r(DST, &p->arr[0], pname);
1092 fprintf(DST, "!= 0 ] * \n");
1093 print_evalue_r(DST, &p->arr[2], pname);
1095 break;
1096 case partition: {
1097 char **new_names = NULL;
1098 const char **names = pname;
1099 int maxdim = EVALUE_DOMAIN(p->arr[0])->Dimension;
1100 if (!pname || p->pos < maxdim) {
1101 new_names = ALLOCN(char *, maxdim);
1102 for (i = 0; i < p->pos; ++i) {
1103 if (pname)
1104 new_names[i] = (char *)pname[i];
1105 else {
1106 new_names[i] = ALLOCN(char, 10);
1107 snprintf(new_names[i], 10, "%c", 'P'+i);
1110 for ( ; i < maxdim; ++i) {
1111 new_names[i] = ALLOCN(char, 10);
1112 snprintf(new_names[i], 10, "_p%d", i);
1114 names = (const char**)new_names;
1117 for (i=0; i<p->size/2; i++) {
1118 Print_Domain(DST, EVALUE_DOMAIN(p->arr[2*i]), names);
1119 print_evalue_r(DST, &p->arr[2*i+1], names);
1120 if (value_notzero_p(p->arr[2*i+1].d))
1121 fprintf(DST, "\n");
1124 if (!pname || p->pos < maxdim) {
1125 for (i = pname ? p->pos : 0; i < maxdim; ++i)
1126 free(new_names[i]);
1127 free(new_names);
1130 break;
1132 default:
1133 assert(0);
1135 return;
1136 } /* print_enode */
1138 /* Returns
1139 * 0 if toplevels of e1 and e2 are at the same level
1140 * <0 if toplevel of e1 should be outside of toplevel of e2
1141 * >0 if toplevel of e2 should be outside of toplevel of e1
1143 static int evalue_level_cmp(const evalue *e1, const evalue *e2)
1145 if (value_notzero_p(e1->d) && value_notzero_p(e2->d))
1146 return 0;
1147 if (value_notzero_p(e1->d))
1148 return 1;
1149 if (value_notzero_p(e2->d))
1150 return -1;
1151 if (e1->x.p->type == partition && e2->x.p->type == partition)
1152 return 0;
1153 if (e1->x.p->type == partition)
1154 return -1;
1155 if (e2->x.p->type == partition)
1156 return 1;
1157 if (e1->x.p->type == relation && e2->x.p->type == relation) {
1158 if (eequal(&e1->x.p->arr[0], &e2->x.p->arr[0]))
1159 return 0;
1160 return mod_term_cmp(&e1->x.p->arr[0], &e2->x.p->arr[0]);
1162 if (e1->x.p->type == relation)
1163 return -1;
1164 if (e2->x.p->type == relation)
1165 return 1;
1166 if (e1->x.p->type == polynomial && e2->x.p->type == polynomial)
1167 return e1->x.p->pos - e2->x.p->pos;
1168 if (e1->x.p->type == polynomial)
1169 return -1;
1170 if (e2->x.p->type == polynomial)
1171 return 1;
1172 if (e1->x.p->type == periodic && e2->x.p->type == periodic)
1173 return e1->x.p->pos - e2->x.p->pos;
1174 assert(e1->x.p->type != periodic);
1175 assert(e2->x.p->type != periodic);
1176 assert(e1->x.p->type == e2->x.p->type);
1177 if (eequal(&e1->x.p->arr[0], &e2->x.p->arr[0]))
1178 return 0;
1179 return mod_term_cmp(e1, e2);
1182 static void eadd_rev(const evalue *e1, evalue *res)
1184 evalue ev;
1185 value_init(ev.d);
1186 evalue_copy(&ev, e1);
1187 eadd(res, &ev);
1188 free_evalue_refs(res);
1189 *res = ev;
1192 static void eadd_rev_cst(const evalue *e1, evalue *res)
1194 evalue ev;
1195 value_init(ev.d);
1196 evalue_copy(&ev, e1);
1197 eadd(res, &ev.x.p->arr[type_offset(ev.x.p)]);
1198 free_evalue_refs(res);
1199 *res = ev;
1202 struct section { Polyhedron * D; evalue E; };
1204 void eadd_partitions(const evalue *e1, evalue *res)
1206 int n, i, j;
1207 Polyhedron *d, *fd;
1208 struct section *s;
1209 s = (struct section *)
1210 malloc((e1->x.p->size/2+1) * (res->x.p->size/2+1) *
1211 sizeof(struct section));
1212 assert(s);
1213 assert(e1->x.p->pos == res->x.p->pos);
1214 assert(e1->x.p->pos == EVALUE_DOMAIN(e1->x.p->arr[0])->Dimension);
1215 assert(res->x.p->pos == EVALUE_DOMAIN(res->x.p->arr[0])->Dimension);
1217 n = 0;
1218 for (j = 0; j < e1->x.p->size/2; ++j) {
1219 assert(res->x.p->size >= 2);
1220 fd = DomainDifference(EVALUE_DOMAIN(e1->x.p->arr[2*j]),
1221 EVALUE_DOMAIN(res->x.p->arr[0]), 0);
1222 if (!emptyQ(fd))
1223 for (i = 1; i < res->x.p->size/2; ++i) {
1224 Polyhedron *t = fd;
1225 fd = DomainDifference(fd, EVALUE_DOMAIN(res->x.p->arr[2*i]), 0);
1226 Domain_Free(t);
1227 if (emptyQ(fd))
1228 break;
1230 fd = DomainConstraintSimplify(fd, 0);
1231 if (emptyQ(fd)) {
1232 Domain_Free(fd);
1233 continue;
1235 value_init(s[n].E.d);
1236 evalue_copy(&s[n].E, &e1->x.p->arr[2*j+1]);
1237 s[n].D = fd;
1238 ++n;
1240 for (i = 0; i < res->x.p->size/2; ++i) {
1241 fd = EVALUE_DOMAIN(res->x.p->arr[2*i]);
1242 for (j = 0; j < e1->x.p->size/2; ++j) {
1243 Polyhedron *t;
1244 d = DomainIntersection(EVALUE_DOMAIN(e1->x.p->arr[2*j]),
1245 EVALUE_DOMAIN(res->x.p->arr[2*i]), 0);
1246 d = DomainConstraintSimplify(d, 0);
1247 if (emptyQ(d)) {
1248 Domain_Free(d);
1249 continue;
1251 t = fd;
1252 fd = DomainDifference(fd, EVALUE_DOMAIN(e1->x.p->arr[2*j]), 0);
1253 if (t != EVALUE_DOMAIN(res->x.p->arr[2*i]))
1254 Domain_Free(t);
1255 value_init(s[n].E.d);
1256 evalue_copy(&s[n].E, &res->x.p->arr[2*i+1]);
1257 eadd(&e1->x.p->arr[2*j+1], &s[n].E);
1258 s[n].D = d;
1259 ++n;
1261 if (!emptyQ(fd)) {
1262 s[n].E = res->x.p->arr[2*i+1];
1263 s[n].D = fd;
1264 ++n;
1265 } else {
1266 free_evalue_refs(&res->x.p->arr[2*i+1]);
1267 Domain_Free(fd);
1269 if (fd != EVALUE_DOMAIN(res->x.p->arr[2*i]))
1270 Domain_Free(EVALUE_DOMAIN(res->x.p->arr[2*i]));
1271 value_clear(res->x.p->arr[2*i].d);
1274 free(res->x.p);
1275 assert(n > 0);
1276 res->x.p = new_enode(partition, 2*n, e1->x.p->pos);
1277 for (j = 0; j < n; ++j) {
1278 EVALUE_SET_DOMAIN(res->x.p->arr[2*j], s[j].D);
1279 value_clear(res->x.p->arr[2*j+1].d);
1280 res->x.p->arr[2*j+1] = s[j].E;
1283 free(s);
1286 static void explicit_complement(evalue *res)
1288 enode *rel = new_enode(relation, 3, 0);
1289 assert(rel);
1290 value_clear(rel->arr[0].d);
1291 rel->arr[0] = res->x.p->arr[0];
1292 value_clear(rel->arr[1].d);
1293 rel->arr[1] = res->x.p->arr[1];
1294 value_set_si(rel->arr[2].d, 1);
1295 value_init(rel->arr[2].x.n);
1296 value_set_si(rel->arr[2].x.n, 0);
1297 free(res->x.p);
1298 res->x.p = rel;
1301 static void reduce_constant(evalue *e)
1303 Value g;
1304 value_init(g);
1306 value_gcd(g, e->x.n, e->d);
1307 if (value_notone_p(g)) {
1308 value_division(e->d, e->d,g);
1309 value_division(e->x.n, e->x.n,g);
1311 value_clear(g);
1314 /* Add two rational numbers */
1315 static void eadd_rationals(const evalue *e1, evalue *res)
1317 if (value_eq(e1->d, res->d))
1318 value_addto(res->x.n, res->x.n, e1->x.n);
1319 else {
1320 value_multiply(res->x.n, res->x.n, e1->d);
1321 value_addmul(res->x.n, e1->x.n, res->d);
1322 value_multiply(res->d,e1->d,res->d);
1324 reduce_constant(res);
1327 static void eadd_relations(const evalue *e1, evalue *res)
1329 int i;
1331 if (res->x.p->size < 3 && e1->x.p->size == 3)
1332 explicit_complement(res);
1333 for (i = 1; i < e1->x.p->size; ++i)
1334 eadd(&e1->x.p->arr[i], &res->x.p->arr[i]);
1337 static void eadd_arrays(const evalue *e1, evalue *res, int n)
1339 int i;
1341 // add any element in e1 to the corresponding element in res
1342 i = type_offset(res->x.p);
1343 if (i == 1)
1344 assert(eequal(&e1->x.p->arr[0], &res->x.p->arr[0]));
1345 for (; i < n; i++)
1346 eadd(&e1->x.p->arr[i], &res->x.p->arr[i]);
1349 static void eadd_poly(const evalue *e1, evalue *res)
1351 if (e1->x.p->size > res->x.p->size)
1352 eadd_rev(e1, res);
1353 else
1354 eadd_arrays(e1, res, e1->x.p->size);
1358 * Product or sum of two periodics of the same parameter
1359 * and different periods
1361 static void combine_periodics(const evalue *e1, evalue *res,
1362 void (*op)(const evalue *, evalue*))
1364 Value es, rs;
1365 int i, size;
1366 enode *p;
1368 value_init(es);
1369 value_init(rs);
1370 value_set_si(es, e1->x.p->size);
1371 value_set_si(rs, res->x.p->size);
1372 value_lcm(rs, es, rs);
1373 size = (int)mpz_get_si(rs);
1374 value_clear(es);
1375 value_clear(rs);
1376 p = new_enode(periodic, size, e1->x.p->pos);
1377 for (i = 0; i < res->x.p->size; i++) {
1378 value_clear(p->arr[i].d);
1379 p->arr[i] = res->x.p->arr[i];
1381 for (i = res->x.p->size; i < size; i++)
1382 evalue_copy(&p->arr[i], &res->x.p->arr[i % res->x.p->size]);
1383 for (i = 0; i < size; i++)
1384 op(&e1->x.p->arr[i % e1->x.p->size], &p->arr[i]);
1385 free(res->x.p);
1386 res->x.p = p;
1389 static void eadd_periodics(const evalue *e1, evalue *res)
1391 int i;
1392 int x, y, p;
1393 evalue *ne;
1395 if (e1->x.p->size == res->x.p->size) {
1396 eadd_arrays(e1, res, e1->x.p->size);
1397 return;
1400 combine_periodics(e1, res, eadd);
1403 void evalue_assign(evalue *dst, const evalue *src)
1405 if (value_pos_p(dst->d) && value_pos_p(src->d)) {
1406 value_assign(dst->d, src->d);
1407 value_assign(dst->x.n, src->x.n);
1408 return;
1410 free_evalue_refs(dst);
1411 value_init(dst->d);
1412 evalue_copy(dst, src);
1415 void eadd(const evalue *e1, evalue *res)
1417 int cmp;
1419 if (EVALUE_IS_ZERO(*e1))
1420 return;
1422 if (EVALUE_IS_NAN(*res))
1423 return;
1425 if (EVALUE_IS_NAN(*e1)) {
1426 evalue_assign(res, e1);
1427 return;
1430 if (EVALUE_IS_ZERO(*res)) {
1431 evalue_assign(res, e1);
1432 return;
1435 cmp = evalue_level_cmp(res, e1);
1436 if (cmp > 0) {
1437 switch (e1->x.p->type) {
1438 case polynomial:
1439 case flooring:
1440 case fractional:
1441 eadd_rev_cst(e1, res);
1442 break;
1443 default:
1444 eadd_rev(e1, res);
1446 } else if (cmp == 0) {
1447 if (value_notzero_p(e1->d)) {
1448 eadd_rationals(e1, res);
1449 } else {
1450 switch (e1->x.p->type) {
1451 case partition:
1452 eadd_partitions(e1, res);
1453 break;
1454 case relation:
1455 eadd_relations(e1, res);
1456 break;
1457 case evector:
1458 assert(e1->x.p->size == res->x.p->size);
1459 case polynomial:
1460 case flooring:
1461 case fractional:
1462 eadd_poly(e1, res);
1463 break;
1464 case periodic:
1465 eadd_periodics(e1, res);
1466 break;
1467 default:
1468 assert(0);
1471 } else {
1472 int i;
1473 switch (res->x.p->type) {
1474 case polynomial:
1475 case flooring:
1476 case fractional:
1477 /* Add to the constant term of a polynomial */
1478 eadd(e1, &res->x.p->arr[type_offset(res->x.p)]);
1479 break;
1480 case periodic:
1481 /* Add to all elements of a periodic number */
1482 for (i = 0; i < res->x.p->size; i++)
1483 eadd(e1, &res->x.p->arr[i]);
1484 break;
1485 case evector:
1486 fprintf(stderr, "eadd: cannot add const with vector\n");
1487 break;
1488 case partition:
1489 assert(0);
1490 case relation:
1491 /* Create (zero) complement if needed */
1492 if (res->x.p->size < 3)
1493 explicit_complement(res);
1494 for (i = 1; i < res->x.p->size; ++i)
1495 eadd(e1, &res->x.p->arr[i]);
1496 break;
1497 default:
1498 assert(0);
1501 } /* eadd */
1503 static void emul_rev(const evalue *e1, evalue *res)
1505 evalue ev;
1506 value_init(ev.d);
1507 evalue_copy(&ev, e1);
1508 emul(res, &ev);
1509 free_evalue_refs(res);
1510 *res = ev;
1513 static void emul_poly(const evalue *e1, evalue *res)
1515 int i, j, offset = type_offset(res->x.p);
1516 evalue tmp;
1517 enode *p;
1518 int size = (e1->x.p->size + res->x.p->size - offset - 1);
1520 p = new_enode(res->x.p->type, size, res->x.p->pos);
1522 for (i = offset; i < e1->x.p->size-1; ++i)
1523 if (!EVALUE_IS_ZERO(e1->x.p->arr[i]))
1524 break;
1526 /* special case pure power */
1527 if (i == e1->x.p->size-1) {
1528 if (offset) {
1529 value_clear(p->arr[0].d);
1530 p->arr[0] = res->x.p->arr[0];
1532 for (i = offset; i < e1->x.p->size-1; ++i)
1533 evalue_set_si(&p->arr[i], 0, 1);
1534 for (i = offset; i < res->x.p->size; ++i) {
1535 value_clear(p->arr[i+e1->x.p->size-offset-1].d);
1536 p->arr[i+e1->x.p->size-offset-1] = res->x.p->arr[i];
1537 emul(&e1->x.p->arr[e1->x.p->size-1],
1538 &p->arr[i+e1->x.p->size-offset-1]);
1540 free(res->x.p);
1541 res->x.p = p;
1542 return;
1545 value_init(tmp.d);
1546 value_set_si(tmp.d,0);
1547 tmp.x.p = p;
1548 if (offset)
1549 evalue_copy(&p->arr[0], &e1->x.p->arr[0]);
1550 for (i = offset; i < e1->x.p->size; i++) {
1551 evalue_copy(&tmp.x.p->arr[i], &e1->x.p->arr[i]);
1552 emul(&res->x.p->arr[offset], &tmp.x.p->arr[i]);
1554 for (; i<size; i++)
1555 evalue_set_si(&tmp.x.p->arr[i], 0, 1);
1556 for (i = offset+1; i<res->x.p->size; i++)
1557 for (j = offset; j<e1->x.p->size; j++) {
1558 evalue ev;
1559 value_init(ev.d);
1560 evalue_copy(&ev, &e1->x.p->arr[j]);
1561 emul(&res->x.p->arr[i], &ev);
1562 eadd(&ev, &tmp.x.p->arr[i+j-offset]);
1563 free_evalue_refs(&ev);
1565 free_evalue_refs(res);
1566 *res = tmp;
1569 void emul_partitions(const evalue *e1, evalue *res)
1571 int n, i, j, k;
1572 Polyhedron *d;
1573 struct section *s;
1574 s = (struct section *)
1575 malloc((e1->x.p->size/2) * (res->x.p->size/2) *
1576 sizeof(struct section));
1577 assert(s);
1578 assert(e1->x.p->pos == res->x.p->pos);
1579 assert(e1->x.p->pos == EVALUE_DOMAIN(e1->x.p->arr[0])->Dimension);
1580 assert(res->x.p->pos == EVALUE_DOMAIN(res->x.p->arr[0])->Dimension);
1582 n = 0;
1583 for (i = 0; i < res->x.p->size/2; ++i) {
1584 for (j = 0; j < e1->x.p->size/2; ++j) {
1585 d = DomainIntersection(EVALUE_DOMAIN(e1->x.p->arr[2*j]),
1586 EVALUE_DOMAIN(res->x.p->arr[2*i]), 0);
1587 d = DomainConstraintSimplify(d, 0);
1588 if (emptyQ(d)) {
1589 Domain_Free(d);
1590 continue;
1593 /* This code is only needed because the partitions
1594 are not true partitions.
1596 for (k = 0; k < n; ++k) {
1597 if (DomainIncludes(s[k].D, d))
1598 break;
1599 if (DomainIncludes(d, s[k].D)) {
1600 Domain_Free(s[k].D);
1601 free_evalue_refs(&s[k].E);
1602 if (n > k)
1603 s[k] = s[--n];
1604 --k;
1607 if (k < n) {
1608 Domain_Free(d);
1609 continue;
1612 value_init(s[n].E.d);
1613 evalue_copy(&s[n].E, &res->x.p->arr[2*i+1]);
1614 emul(&e1->x.p->arr[2*j+1], &s[n].E);
1615 s[n].D = d;
1616 ++n;
1618 Domain_Free(EVALUE_DOMAIN(res->x.p->arr[2*i]));
1619 value_clear(res->x.p->arr[2*i].d);
1620 free_evalue_refs(&res->x.p->arr[2*i+1]);
1623 free(res->x.p);
1624 if (n == 0)
1625 evalue_set_si(res, 0, 1);
1626 else {
1627 res->x.p = new_enode(partition, 2*n, e1->x.p->pos);
1628 for (j = 0; j < n; ++j) {
1629 EVALUE_SET_DOMAIN(res->x.p->arr[2*j], s[j].D);
1630 value_clear(res->x.p->arr[2*j+1].d);
1631 res->x.p->arr[2*j+1] = s[j].E;
1635 free(s);
1638 /* Product of two rational numbers */
1639 static void emul_rationals(const evalue *e1, evalue *res)
1641 value_multiply(res->d, e1->d, res->d);
1642 value_multiply(res->x.n, e1->x.n, res->x.n);
1643 reduce_constant(res);
1646 static void emul_relations(const evalue *e1, evalue *res)
1648 int i;
1650 if (e1->x.p->size < 3 && res->x.p->size == 3) {
1651 free_evalue_refs(&res->x.p->arr[2]);
1652 res->x.p->size = 2;
1654 for (i = 1; i < res->x.p->size; ++i)
1655 emul(&e1->x.p->arr[i], &res->x.p->arr[i]);
1658 static void emul_periodics(const evalue *e1, evalue *res)
1660 int i;
1661 evalue *newp;
1662 Value x, y, z;
1663 int ix, iy, lcm;
1665 if (e1->x.p->size == res->x.p->size) {
1666 /* Product of two periodics of the same parameter and period */
1667 for (i = 0; i < res->x.p->size; i++)
1668 emul(&(e1->x.p->arr[i]), &(res->x.p->arr[i]));
1669 return;
1672 combine_periodics(e1, res, emul);
1675 #define value_two_p(val) (mpz_cmp_si(val,2) == 0)
1677 static void emul_fractionals(const evalue *e1, evalue *res)
1679 evalue d;
1680 value_init(d.d);
1681 poly_denom(&e1->x.p->arr[0], &d.d);
1682 if (!value_two_p(d.d))
1683 emul_poly(e1, res);
1684 else {
1685 evalue tmp;
1686 value_init(d.x.n);
1687 value_set_si(d.x.n, 1);
1688 /* { x }^2 == { x }/2 */
1689 /* a0 b0 + (a0 b1 + a1 b0 + a1 b1/2) { x } */
1690 assert(e1->x.p->size == 3);
1691 assert(res->x.p->size == 3);
1692 value_init(tmp.d);
1693 evalue_copy(&tmp, &res->x.p->arr[2]);
1694 emul(&d, &tmp);
1695 eadd(&res->x.p->arr[1], &tmp);
1696 emul(&e1->x.p->arr[2], &tmp);
1697 emul(&e1->x.p->arr[1], &res->x.p->arr[1]);
1698 emul(&e1->x.p->arr[1], &res->x.p->arr[2]);
1699 eadd(&tmp, &res->x.p->arr[2]);
1700 free_evalue_refs(&tmp);
1701 value_clear(d.x.n);
1703 value_clear(d.d);
1706 /* Computes the product of two evalues "e1" and "res" and puts
1707 * the result in "res". You need to make a copy of "res"
1708 * before calling this function if you still need it afterward.
1709 * The vector type of evalues is not treated here
1711 void emul(const evalue *e1, evalue *res)
1713 int cmp;
1715 assert(!(value_zero_p(e1->d) && e1->x.p->type == evector));
1716 assert(!(value_zero_p(res->d) && res->x.p->type == evector));
1718 if (EVALUE_IS_ZERO(*res))
1719 return;
1721 if (EVALUE_IS_ONE(*e1))
1722 return;
1724 if (EVALUE_IS_ZERO(*e1)) {
1725 evalue_assign(res, e1);
1726 return;
1729 if (EVALUE_IS_NAN(*res))
1730 return;
1732 if (EVALUE_IS_NAN(*e1)) {
1733 evalue_assign(res, e1);
1734 return;
1737 cmp = evalue_level_cmp(res, e1);
1738 if (cmp > 0) {
1739 emul_rev(e1, res);
1740 } else if (cmp == 0) {
1741 if (value_notzero_p(e1->d)) {
1742 emul_rationals(e1, res);
1743 } else {
1744 switch (e1->x.p->type) {
1745 case partition:
1746 emul_partitions(e1, res);
1747 break;
1748 case relation:
1749 emul_relations(e1, res);
1750 break;
1751 case polynomial:
1752 case flooring:
1753 emul_poly(e1, res);
1754 break;
1755 case periodic:
1756 emul_periodics(e1, res);
1757 break;
1758 case fractional:
1759 emul_fractionals(e1, res);
1760 break;
1763 } else {
1764 int i;
1765 switch (res->x.p->type) {
1766 case partition:
1767 for (i = 0; i < res->x.p->size/2; ++i)
1768 emul(e1, &res->x.p->arr[2*i+1]);
1769 break;
1770 case relation:
1771 case polynomial:
1772 case periodic:
1773 case flooring:
1774 case fractional:
1775 for (i = type_offset(res->x.p); i < res->x.p->size; ++i)
1776 emul(e1, &res->x.p->arr[i]);
1777 break;
1782 /* Frees mask content ! */
1783 void emask(evalue *mask, evalue *res) {
1784 int n, i, j;
1785 Polyhedron *d, *fd;
1786 struct section *s;
1787 evalue mone;
1788 int pos;
1790 if (EVALUE_IS_ZERO(*res)) {
1791 free_evalue_refs(mask);
1792 return;
1795 assert(value_zero_p(mask->d));
1796 assert(mask->x.p->type == partition);
1797 assert(value_zero_p(res->d));
1798 assert(res->x.p->type == partition);
1799 assert(mask->x.p->pos == res->x.p->pos);
1800 assert(res->x.p->pos == EVALUE_DOMAIN(res->x.p->arr[0])->Dimension);
1801 assert(mask->x.p->pos == EVALUE_DOMAIN(mask->x.p->arr[0])->Dimension);
1802 pos = res->x.p->pos;
1804 s = (struct section *)
1805 malloc((mask->x.p->size/2+1) * (res->x.p->size/2) *
1806 sizeof(struct section));
1807 assert(s);
1809 value_init(mone.d);
1810 evalue_set_si(&mone, -1, 1);
1812 n = 0;
1813 for (j = 0; j < res->x.p->size/2; ++j) {
1814 assert(mask->x.p->size >= 2);
1815 fd = DomainDifference(EVALUE_DOMAIN(res->x.p->arr[2*j]),
1816 EVALUE_DOMAIN(mask->x.p->arr[0]), 0);
1817 if (!emptyQ(fd))
1818 for (i = 1; i < mask->x.p->size/2; ++i) {
1819 Polyhedron *t = fd;
1820 fd = DomainDifference(fd, EVALUE_DOMAIN(mask->x.p->arr[2*i]), 0);
1821 Domain_Free(t);
1822 if (emptyQ(fd))
1823 break;
1825 if (emptyQ(fd)) {
1826 Domain_Free(fd);
1827 continue;
1829 value_init(s[n].E.d);
1830 evalue_copy(&s[n].E, &res->x.p->arr[2*j+1]);
1831 s[n].D = fd;
1832 ++n;
1834 for (i = 0; i < mask->x.p->size/2; ++i) {
1835 if (EVALUE_IS_ONE(mask->x.p->arr[2*i+1]))
1836 continue;
1838 fd = EVALUE_DOMAIN(mask->x.p->arr[2*i]);
1839 eadd(&mone, &mask->x.p->arr[2*i+1]);
1840 emul(&mone, &mask->x.p->arr[2*i+1]);
1841 for (j = 0; j < res->x.p->size/2; ++j) {
1842 Polyhedron *t;
1843 d = DomainIntersection(EVALUE_DOMAIN(res->x.p->arr[2*j]),
1844 EVALUE_DOMAIN(mask->x.p->arr[2*i]), 0);
1845 if (emptyQ(d)) {
1846 Domain_Free(d);
1847 continue;
1849 t = fd;
1850 fd = DomainDifference(fd, EVALUE_DOMAIN(res->x.p->arr[2*j]), 0);
1851 if (t != EVALUE_DOMAIN(mask->x.p->arr[2*i]))
1852 Domain_Free(t);
1853 value_init(s[n].E.d);
1854 evalue_copy(&s[n].E, &res->x.p->arr[2*j+1]);
1855 emul(&mask->x.p->arr[2*i+1], &s[n].E);
1856 s[n].D = d;
1857 ++n;
1860 if (!emptyQ(fd)) {
1861 /* Just ignore; this may have been previously masked off */
1863 if (fd != EVALUE_DOMAIN(mask->x.p->arr[2*i]))
1864 Domain_Free(fd);
1867 free_evalue_refs(&mone);
1868 free_evalue_refs(mask);
1869 free_evalue_refs(res);
1870 value_init(res->d);
1871 if (n == 0)
1872 evalue_set_si(res, 0, 1);
1873 else {
1874 res->x.p = new_enode(partition, 2*n, pos);
1875 for (j = 0; j < n; ++j) {
1876 EVALUE_SET_DOMAIN(res->x.p->arr[2*j], s[j].D);
1877 value_clear(res->x.p->arr[2*j+1].d);
1878 res->x.p->arr[2*j+1] = s[j].E;
1882 free(s);
1885 void evalue_copy(evalue *dst, const evalue *src)
1887 value_assign(dst->d, src->d);
1888 if (EVALUE_IS_NAN(*dst)) {
1889 dst->x.p = NULL;
1890 return;
1892 if (value_pos_p(src->d)) {
1893 value_init(dst->x.n);
1894 value_assign(dst->x.n, src->x.n);
1895 } else
1896 dst->x.p = ecopy(src->x.p);
1899 evalue *evalue_dup(const evalue *e)
1901 evalue *res = ALLOC(evalue);
1902 value_init(res->d);
1903 evalue_copy(res, e);
1904 return res;
1907 enode *new_enode(enode_type type,int size,int pos) {
1909 enode *res;
1910 int i;
1912 if(size == 0) {
1913 fprintf(stderr, "Allocating enode of size 0 !\n" );
1914 return NULL;
1916 res = (enode *) malloc(sizeof(enode) + (size-1)*sizeof(evalue));
1917 res->type = type;
1918 res->size = size;
1919 res->pos = pos;
1920 for(i=0; i<size; i++) {
1921 value_init(res->arr[i].d);
1922 value_set_si(res->arr[i].d,0);
1923 res->arr[i].x.p = 0;
1925 return res;
1926 } /* new_enode */
1928 enode *ecopy(enode *e) {
1930 enode *res;
1931 int i;
1933 res = new_enode(e->type,e->size,e->pos);
1934 for(i=0;i<e->size;++i) {
1935 value_assign(res->arr[i].d,e->arr[i].d);
1936 if(value_zero_p(res->arr[i].d))
1937 res->arr[i].x.p = ecopy(e->arr[i].x.p);
1938 else if (EVALUE_IS_DOMAIN(res->arr[i]))
1939 EVALUE_SET_DOMAIN(res->arr[i], Domain_Copy(EVALUE_DOMAIN(e->arr[i])));
1940 else {
1941 value_init(res->arr[i].x.n);
1942 value_assign(res->arr[i].x.n,e->arr[i].x.n);
1945 return(res);
1946 } /* ecopy */
1948 int ecmp(const evalue *e1, const evalue *e2)
1950 enode *p1, *p2;
1951 int i;
1952 int r;
1954 if (value_notzero_p(e1->d) && value_notzero_p(e2->d)) {
1955 Value m, m2;
1956 value_init(m);
1957 value_init(m2);
1958 value_multiply(m, e1->x.n, e2->d);
1959 value_multiply(m2, e2->x.n, e1->d);
1961 if (value_lt(m, m2))
1962 r = -1;
1963 else if (value_gt(m, m2))
1964 r = 1;
1965 else
1966 r = 0;
1968 value_clear(m);
1969 value_clear(m2);
1971 return r;
1973 if (value_notzero_p(e1->d))
1974 return -1;
1975 if (value_notzero_p(e2->d))
1976 return 1;
1978 p1 = e1->x.p;
1979 p2 = e2->x.p;
1981 if (p1->type != p2->type)
1982 return p1->type - p2->type;
1983 if (p1->pos != p2->pos)
1984 return p1->pos - p2->pos;
1985 if (p1->size != p2->size)
1986 return p1->size - p2->size;
1988 for (i = p1->size-1; i >= 0; --i)
1989 if ((r = ecmp(&p1->arr[i], &p2->arr[i])) != 0)
1990 return r;
1992 return 0;
1995 int eequal(const evalue *e1, const evalue *e2)
1997 int i;
1998 enode *p1, *p2;
2000 if (value_ne(e1->d,e2->d))
2001 return 0;
2003 if (EVALUE_IS_DOMAIN(*e1))
2004 return PolyhedronIncludes(EVALUE_DOMAIN(*e2), EVALUE_DOMAIN(*e1)) &&
2005 PolyhedronIncludes(EVALUE_DOMAIN(*e1), EVALUE_DOMAIN(*e2));
2007 if (EVALUE_IS_NAN(*e1))
2008 return 1;
2010 assert(value_posz_p(e1->d));
2012 /* e1->d == e2->d */
2013 if (value_notzero_p(e1->d)) {
2014 if (value_ne(e1->x.n,e2->x.n))
2015 return 0;
2017 /* e1->d == e2->d != 0 AND e1->n == e2->n */
2018 return 1;
2021 /* e1->d == e2->d == 0 */
2022 p1 = e1->x.p;
2023 p2 = e2->x.p;
2024 if (p1->type != p2->type) return 0;
2025 if (p1->size != p2->size) return 0;
2026 if (p1->pos != p2->pos) return 0;
2027 for (i=0; i<p1->size; i++)
2028 if (!eequal(&p1->arr[i], &p2->arr[i]) )
2029 return 0;
2030 return 1;
2031 } /* eequal */
2033 void free_evalue_refs(evalue *e) {
2035 enode *p;
2036 int i;
2038 if (EVALUE_IS_NAN(*e)) {
2039 value_clear(e->d);
2040 return;
2043 if (EVALUE_IS_DOMAIN(*e)) {
2044 Domain_Free(EVALUE_DOMAIN(*e));
2045 value_clear(e->d);
2046 return;
2047 } else if (value_pos_p(e->d)) {
2049 /* 'e' stores a constant */
2050 value_clear(e->d);
2051 value_clear(e->x.n);
2052 return;
2054 assert(value_zero_p(e->d));
2055 value_clear(e->d);
2056 p = e->x.p;
2057 if (!p) return; /* null pointer */
2058 for (i=0; i<p->size; i++) {
2059 free_evalue_refs(&(p->arr[i]));
2061 free(p);
2062 return;
2063 } /* free_evalue_refs */
2065 void evalue_free(evalue *e)
2067 free_evalue_refs(e);
2068 free(e);
2071 static void mod2table_r(evalue *e, Vector *periods, Value m, int p,
2072 Vector * val, evalue *res)
2074 unsigned nparam = periods->Size;
2076 if (p == nparam) {
2077 double d = compute_evalue(e, val->p);
2078 d *= VALUE_TO_DOUBLE(m);
2079 if (d > 0)
2080 d += .25;
2081 else
2082 d -= .25;
2083 value_assign(res->d, m);
2084 value_init(res->x.n);
2085 value_set_double(res->x.n, d);
2086 mpz_fdiv_r(res->x.n, res->x.n, m);
2087 return;
2089 if (value_one_p(periods->p[p]))
2090 mod2table_r(e, periods, m, p+1, val, res);
2091 else {
2092 Value tmp;
2093 value_init(tmp);
2095 value_assign(tmp, periods->p[p]);
2096 value_set_si(res->d, 0);
2097 res->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
2098 do {
2099 value_decrement(tmp, tmp);
2100 value_assign(val->p[p], tmp);
2101 mod2table_r(e, periods, m, p+1, val,
2102 &res->x.p->arr[VALUE_TO_INT(tmp)]);
2103 } while (value_pos_p(tmp));
2105 value_clear(tmp);
2109 static void rel2table(evalue *e, int zero)
2111 if (value_pos_p(e->d)) {
2112 if (value_zero_p(e->x.n) == zero)
2113 value_set_si(e->x.n, 1);
2114 else
2115 value_set_si(e->x.n, 0);
2116 value_set_si(e->d, 1);
2117 } else {
2118 int i;
2119 for (i = 0; i < e->x.p->size; ++i)
2120 rel2table(&e->x.p->arr[i], zero);
2124 void evalue_mod2table(evalue *e, int nparam)
2126 enode *p;
2127 int i;
2129 if (EVALUE_IS_DOMAIN(*e) || value_pos_p(e->d))
2130 return;
2131 p = e->x.p;
2132 for (i=0; i<p->size; i++) {
2133 evalue_mod2table(&(p->arr[i]), nparam);
2135 if (p->type == relation) {
2136 evalue copy;
2138 if (p->size > 2) {
2139 value_init(copy.d);
2140 evalue_copy(&copy, &p->arr[0]);
2142 rel2table(&p->arr[0], 1);
2143 emul(&p->arr[0], &p->arr[1]);
2144 if (p->size > 2) {
2145 rel2table(&copy, 0);
2146 emul(&copy, &p->arr[2]);
2147 eadd(&p->arr[2], &p->arr[1]);
2148 free_evalue_refs(&p->arr[2]);
2149 free_evalue_refs(&copy);
2151 free_evalue_refs(&p->arr[0]);
2152 value_clear(e->d);
2153 *e = p->arr[1];
2154 free(p);
2155 } else if (p->type == fractional) {
2156 Vector *periods = Vector_Alloc(nparam);
2157 Vector *val = Vector_Alloc(nparam);
2158 Value tmp;
2159 evalue *ev;
2160 evalue EP, res;
2162 value_init(tmp);
2163 value_set_si(tmp, 1);
2164 Vector_Set(periods->p, 1, nparam);
2165 Vector_Set(val->p, 0, nparam);
2166 for (ev = &p->arr[0]; value_zero_p(ev->d); ev = &ev->x.p->arr[0]) {
2167 enode *p = ev->x.p;
2169 assert(p->type == polynomial);
2170 assert(p->size == 2);
2171 value_assign(periods->p[p->pos-1], p->arr[1].d);
2172 value_lcm(tmp, tmp, p->arr[1].d);
2174 value_lcm(tmp, tmp, ev->d);
2175 value_init(EP.d);
2176 mod2table_r(&p->arr[0], periods, tmp, 0, val, &EP);
2178 value_init(res.d);
2179 evalue_set_si(&res, 0, 1);
2180 /* Compute the polynomial using Horner's rule */
2181 for (i=p->size-1;i>1;i--) {
2182 eadd(&p->arr[i], &res);
2183 emul(&EP, &res);
2185 eadd(&p->arr[1], &res);
2187 free_evalue_refs(e);
2188 free_evalue_refs(&EP);
2189 *e = res;
2191 value_clear(tmp);
2192 Vector_Free(val);
2193 Vector_Free(periods);
2195 } /* evalue_mod2table */
2197 /********************************************************/
2198 /* function in domain */
2199 /* check if the parameters in list_args */
2200 /* verifies the constraints of Domain P */
2201 /********************************************************/
2202 int in_domain(Polyhedron *P, Value *list_args)
2204 int row, in = 1;
2205 Value v; /* value of the constraint of a row when
2206 parameters are instantiated*/
2208 value_init(v);
2210 for (row = 0; row < P->NbConstraints; row++) {
2211 Inner_Product(P->Constraint[row]+1, list_args, P->Dimension, &v);
2212 value_addto(v, v, P->Constraint[row][P->Dimension+1]); /*constant part*/
2213 if (value_neg_p(v) ||
2214 value_zero_p(P->Constraint[row][0]) && value_notzero_p(v)) {
2215 in = 0;
2216 break;
2220 value_clear(v);
2221 return in || (P->next && in_domain(P->next, list_args));
2222 } /* in_domain */
2224 /****************************************************/
2225 /* function compute enode */
2226 /* compute the value of enode p with parameters */
2227 /* list "list_args */
2228 /* compute the polynomial or the periodic */
2229 /****************************************************/
2231 static double compute_enode(enode *p, Value *list_args) {
2233 int i;
2234 Value m, param;
2235 double res=0.0;
2237 if (!p)
2238 return(0.);
2240 value_init(m);
2241 value_init(param);
2243 if (p->type == polynomial) {
2244 if (p->size > 1)
2245 value_assign(param,list_args[p->pos-1]);
2247 /* Compute the polynomial using Horner's rule */
2248 for (i=p->size-1;i>0;i--) {
2249 res +=compute_evalue(&p->arr[i],list_args);
2250 res *=VALUE_TO_DOUBLE(param);
2252 res +=compute_evalue(&p->arr[0],list_args);
2254 else if (p->type == fractional) {
2255 double d = compute_evalue(&p->arr[0], list_args);
2256 d -= floor(d+1e-10);
2258 /* Compute the polynomial using Horner's rule */
2259 for (i=p->size-1;i>1;i--) {
2260 res +=compute_evalue(&p->arr[i],list_args);
2261 res *=d;
2263 res +=compute_evalue(&p->arr[1],list_args);
2265 else if (p->type == flooring) {
2266 double d = compute_evalue(&p->arr[0], list_args);
2267 d = floor(d+1e-10);
2269 /* Compute the polynomial using Horner's rule */
2270 for (i=p->size-1;i>1;i--) {
2271 res +=compute_evalue(&p->arr[i],list_args);
2272 res *=d;
2274 res +=compute_evalue(&p->arr[1],list_args);
2276 else if (p->type == periodic) {
2277 value_assign(m,list_args[p->pos-1]);
2279 /* Choose the right element of the periodic */
2280 value_set_si(param,p->size);
2281 value_pmodulus(m,m,param);
2282 res = compute_evalue(&p->arr[VALUE_TO_INT(m)],list_args);
2284 else if (p->type == relation) {
2285 if (fabs(compute_evalue(&p->arr[0], list_args)) < 1e-10)
2286 res = compute_evalue(&p->arr[1], list_args);
2287 else if (p->size > 2)
2288 res = compute_evalue(&p->arr[2], list_args);
2290 else if (p->type == partition) {
2291 int dim = EVALUE_DOMAIN(p->arr[0])->Dimension;
2292 Value *vals = list_args;
2293 if (p->pos < dim) {
2294 NALLOC(vals, dim);
2295 for (i = 0; i < dim; ++i) {
2296 value_init(vals[i]);
2297 if (i < p->pos)
2298 value_assign(vals[i], list_args[i]);
2301 for (i = 0; i < p->size/2; ++i)
2302 if (DomainContains(EVALUE_DOMAIN(p->arr[2*i]), vals, p->pos, 0, 1)) {
2303 res = compute_evalue(&p->arr[2*i+1], vals);
2304 break;
2306 if (p->pos < dim) {
2307 for (i = 0; i < dim; ++i)
2308 value_clear(vals[i]);
2309 free(vals);
2312 else
2313 assert(0);
2314 value_clear(m);
2315 value_clear(param);
2316 return res;
2317 } /* compute_enode */
2319 /*************************************************/
2320 /* return the value of Ehrhart Polynomial */
2321 /* It returns a double, because since it is */
2322 /* a recursive function, some intermediate value */
2323 /* might not be integral */
2324 /*************************************************/
2326 double compute_evalue(const evalue *e, Value *list_args)
2328 double res;
2330 if (value_notzero_p(e->d)) {
2331 if (value_notone_p(e->d))
2332 res = VALUE_TO_DOUBLE(e->x.n) / VALUE_TO_DOUBLE(e->d);
2333 else
2334 res = VALUE_TO_DOUBLE(e->x.n);
2336 else
2337 res = compute_enode(e->x.p,list_args);
2338 return res;
2339 } /* compute_evalue */
2342 /****************************************************/
2343 /* function compute_poly : */
2344 /* Check for the good validity domain */
2345 /* return the number of point in the Polyhedron */
2346 /* in allocated memory */
2347 /* Using the Ehrhart pseudo-polynomial */
2348 /****************************************************/
2349 Value *compute_poly(Enumeration *en,Value *list_args) {
2351 Value *tmp;
2352 /* double d; int i; */
2354 tmp = (Value *) malloc (sizeof(Value));
2355 assert(tmp != NULL);
2356 value_init(*tmp);
2357 value_set_si(*tmp,0);
2359 if(!en)
2360 return(tmp); /* no ehrhart polynomial */
2361 if(en->ValidityDomain) {
2362 if(!en->ValidityDomain->Dimension) { /* no parameters */
2363 value_set_double(*tmp,compute_evalue(&en->EP,list_args)+.25);
2364 return(tmp);
2367 else
2368 return(tmp); /* no Validity Domain */
2369 while(en) {
2370 if(in_domain(en->ValidityDomain,list_args)) {
2372 #ifdef EVAL_EHRHART_DEBUG
2373 Print_Domain(stdout,en->ValidityDomain);
2374 print_evalue(stdout,&en->EP);
2375 #endif
2377 /* d = compute_evalue(&en->EP,list_args);
2378 i = d;
2379 printf("(double)%lf = %d\n", d, i ); */
2380 value_set_double(*tmp,compute_evalue(&en->EP,list_args)+.25);
2381 return(tmp);
2383 else
2384 en=en->next;
2386 value_set_si(*tmp,0);
2387 return(tmp); /* no compatible domain with the arguments */
2388 } /* compute_poly */
2390 static evalue *eval_polynomial(const enode *p, int offset,
2391 evalue *base, Value *values)
2393 int i;
2394 evalue *res, *c;
2396 res = evalue_zero();
2397 for (i = p->size-1; i > offset; --i) {
2398 c = evalue_eval(&p->arr[i], values);
2399 eadd(c, res);
2400 evalue_free(c);
2401 emul(base, res);
2403 c = evalue_eval(&p->arr[offset], values);
2404 eadd(c, res);
2405 evalue_free(c);
2407 return res;
2410 evalue *evalue_eval(const evalue *e, Value *values)
2412 evalue *res = NULL;
2413 evalue param;
2414 evalue *param2;
2415 int i;
2417 if (value_notzero_p(e->d)) {
2418 res = ALLOC(evalue);
2419 value_init(res->d);
2420 evalue_copy(res, e);
2421 return res;
2423 switch (e->x.p->type) {
2424 case polynomial:
2425 value_init(param.x.n);
2426 value_assign(param.x.n, values[e->x.p->pos-1]);
2427 value_init(param.d);
2428 value_set_si(param.d, 1);
2430 res = eval_polynomial(e->x.p, 0, &param, values);
2431 free_evalue_refs(&param);
2432 break;
2433 case fractional:
2434 param2 = evalue_eval(&e->x.p->arr[0], values);
2435 mpz_fdiv_r(param2->x.n, param2->x.n, param2->d);
2437 res = eval_polynomial(e->x.p, 1, param2, values);
2438 evalue_free(param2);
2439 break;
2440 case flooring:
2441 param2 = evalue_eval(&e->x.p->arr[0], values);
2442 mpz_fdiv_q(param2->x.n, param2->x.n, param2->d);
2443 value_set_si(param2->d, 1);
2445 res = eval_polynomial(e->x.p, 1, param2, values);
2446 evalue_free(param2);
2447 break;
2448 case relation:
2449 param2 = evalue_eval(&e->x.p->arr[0], values);
2450 if (value_zero_p(param2->x.n))
2451 res = evalue_eval(&e->x.p->arr[1], values);
2452 else if (e->x.p->size > 2)
2453 res = evalue_eval(&e->x.p->arr[2], values);
2454 else
2455 res = evalue_zero();
2456 evalue_free(param2);
2457 break;
2458 case partition:
2459 assert(e->x.p->pos == EVALUE_DOMAIN(e->x.p->arr[0])->Dimension);
2460 for (i = 0; i < e->x.p->size/2; ++i)
2461 if (in_domain(EVALUE_DOMAIN(e->x.p->arr[2*i]), values)) {
2462 res = evalue_eval(&e->x.p->arr[2*i+1], values);
2463 break;
2465 if (!res)
2466 res = evalue_zero();
2467 break;
2468 default:
2469 assert(0);
2471 return res;
2474 size_t value_size(Value v) {
2475 return (v[0]._mp_size > 0 ? v[0]._mp_size : -v[0]._mp_size)
2476 * sizeof(v[0]._mp_d[0]);
2479 size_t domain_size(Polyhedron *D)
2481 int i, j;
2482 size_t s = sizeof(*D);
2484 for (i = 0; i < D->NbConstraints; ++i)
2485 for (j = 0; j < D->Dimension+2; ++j)
2486 s += value_size(D->Constraint[i][j]);
2489 for (i = 0; i < D->NbRays; ++i)
2490 for (j = 0; j < D->Dimension+2; ++j)
2491 s += value_size(D->Ray[i][j]);
2494 return D->next ? s+domain_size(D->next) : s;
2497 size_t enode_size(enode *p) {
2498 size_t s = sizeof(*p) - sizeof(p->arr[0]);
2499 int i;
2501 if (p->type == partition)
2502 for (i = 0; i < p->size/2; ++i) {
2503 s += domain_size(EVALUE_DOMAIN(p->arr[2*i]));
2504 s += evalue_size(&p->arr[2*i+1]);
2506 else
2507 for (i = 0; i < p->size; ++i) {
2508 s += evalue_size(&p->arr[i]);
2510 return s;
2513 size_t evalue_size(evalue *e)
2515 size_t s = sizeof(*e);
2516 s += value_size(e->d);
2517 if (value_notzero_p(e->d))
2518 s += value_size(e->x.n);
2519 else
2520 s += enode_size(e->x.p);
2521 return s;
2524 static evalue *find_second(evalue *base, evalue *cst, evalue *e, Value m)
2526 evalue *found = NULL;
2527 evalue offset;
2528 evalue copy;
2529 int i;
2531 if (value_pos_p(e->d) || e->x.p->type != fractional)
2532 return NULL;
2534 value_init(offset.d);
2535 value_init(offset.x.n);
2536 poly_denom(&e->x.p->arr[0], &offset.d);
2537 value_lcm(offset.d, m, offset.d);
2538 value_set_si(offset.x.n, 1);
2540 value_init(copy.d);
2541 evalue_copy(&copy, cst);
2543 eadd(&offset, cst);
2544 mpz_fdiv_r(cst->x.n, cst->x.n, cst->d);
2546 if (eequal(base, &e->x.p->arr[0]))
2547 found = &e->x.p->arr[0];
2548 else {
2549 value_set_si(offset.x.n, -2);
2551 eadd(&offset, cst);
2552 mpz_fdiv_r(cst->x.n, cst->x.n, cst->d);
2554 if (eequal(base, &e->x.p->arr[0]))
2555 found = base;
2557 free_evalue_refs(cst);
2558 free_evalue_refs(&offset);
2559 *cst = copy;
2561 for (i = 1; !found && i < e->x.p->size; ++i)
2562 found = find_second(base, cst, &e->x.p->arr[i], m);
2564 return found;
2567 static evalue *find_relation_pair(evalue *e)
2569 int i;
2570 evalue *found = NULL;
2572 if (EVALUE_IS_DOMAIN(*e) || value_pos_p(e->d))
2573 return NULL;
2575 if (e->x.p->type == fractional) {
2576 Value m;
2577 evalue *cst;
2579 value_init(m);
2580 poly_denom(&e->x.p->arr[0], &m);
2582 for (cst = &e->x.p->arr[0]; value_zero_p(cst->d);
2583 cst = &cst->x.p->arr[0])
2586 for (i = 1; !found && i < e->x.p->size; ++i)
2587 found = find_second(&e->x.p->arr[0], cst, &e->x.p->arr[i], m);
2589 value_clear(m);
2592 i = e->x.p->type == relation;
2593 for (; !found && i < e->x.p->size; ++i)
2594 found = find_relation_pair(&e->x.p->arr[i]);
2596 return found;
2599 void evalue_mod2relation(evalue *e) {
2600 evalue *d;
2602 if (value_zero_p(e->d) && e->x.p->type == partition) {
2603 int i;
2605 for (i = 0; i < e->x.p->size/2; ++i) {
2606 evalue_mod2relation(&e->x.p->arr[2*i+1]);
2607 if (EVALUE_IS_ZERO(e->x.p->arr[2*i+1])) {
2608 value_clear(e->x.p->arr[2*i].d);
2609 free_evalue_refs(&e->x.p->arr[2*i+1]);
2610 e->x.p->size -= 2;
2611 if (2*i < e->x.p->size) {
2612 e->x.p->arr[2*i] = e->x.p->arr[e->x.p->size];
2613 e->x.p->arr[2*i+1] = e->x.p->arr[e->x.p->size+1];
2615 --i;
2618 if (e->x.p->size == 0) {
2619 free(e->x.p);
2620 evalue_set_si(e, 0, 1);
2623 return;
2626 while ((d = find_relation_pair(e)) != NULL) {
2627 evalue split;
2628 evalue *ev;
2630 value_init(split.d);
2631 value_set_si(split.d, 0);
2632 split.x.p = new_enode(relation, 3, 0);
2633 evalue_set_si(&split.x.p->arr[1], 1, 1);
2634 evalue_set_si(&split.x.p->arr[2], 1, 1);
2636 ev = &split.x.p->arr[0];
2637 value_set_si(ev->d, 0);
2638 ev->x.p = new_enode(fractional, 3, -1);
2639 evalue_set_si(&ev->x.p->arr[1], 0, 1);
2640 evalue_set_si(&ev->x.p->arr[2], 1, 1);
2641 evalue_copy(&ev->x.p->arr[0], d);
2643 emul(&split, e);
2645 reduce_evalue(e);
2647 free_evalue_refs(&split);
2651 static int evalue_comp(const void * a, const void * b)
2653 const evalue *e1 = *(const evalue **)a;
2654 const evalue *e2 = *(const evalue **)b;
2655 return ecmp(e1, e2);
2658 void evalue_combine(evalue *e)
2660 evalue **evs;
2661 int i, k;
2662 enode *p;
2663 evalue tmp;
2665 if (value_notzero_p(e->d) || e->x.p->type != partition)
2666 return;
2668 NALLOC(evs, e->x.p->size/2);
2669 for (i = 0; i < e->x.p->size/2; ++i)
2670 evs[i] = &e->x.p->arr[2*i+1];
2671 qsort(evs, e->x.p->size/2, sizeof(evs[0]), evalue_comp);
2672 p = new_enode(partition, e->x.p->size, e->x.p->pos);
2673 for (i = 0, k = 0; i < e->x.p->size/2; ++i) {
2674 if (k == 0 || ecmp(&p->arr[2*k-1], evs[i]) != 0) {
2675 value_clear(p->arr[2*k].d);
2676 value_clear(p->arr[2*k+1].d);
2677 p->arr[2*k] = *(evs[i]-1);
2678 p->arr[2*k+1] = *(evs[i]);
2679 ++k;
2680 } else {
2681 Polyhedron *D = EVALUE_DOMAIN(*(evs[i]-1));
2682 Polyhedron *L = D;
2684 value_clear((evs[i]-1)->d);
2686 while (L->next)
2687 L = L->next;
2688 L->next = EVALUE_DOMAIN(p->arr[2*k-2]);
2689 EVALUE_SET_DOMAIN(p->arr[2*k-2], D);
2690 free_evalue_refs(evs[i]);
2694 for (i = 2*k ; i < p->size; ++i)
2695 value_clear(p->arr[i].d);
2697 free(evs);
2698 free(e->x.p);
2699 p->size = 2*k;
2700 e->x.p = p;
2702 for (i = 0; i < e->x.p->size/2; ++i) {
2703 Polyhedron *H;
2704 if (value_notzero_p(e->x.p->arr[2*i+1].d))
2705 continue;
2706 H = DomainConvex(EVALUE_DOMAIN(e->x.p->arr[2*i]), 0);
2707 if (H == NULL)
2708 continue;
2709 for (k = 0; k < e->x.p->size/2; ++k) {
2710 Polyhedron *D, *N, **P;
2711 if (i == k)
2712 continue;
2713 P = &EVALUE_DOMAIN(e->x.p->arr[2*k]);
2714 D = *P;
2715 if (D == NULL)
2716 continue;
2717 for (; D; D = N) {
2718 *P = D;
2719 N = D->next;
2720 if (D->NbEq <= H->NbEq) {
2721 P = &D->next;
2722 continue;
2725 value_init(tmp.d);
2726 tmp.x.p = new_enode(partition, 2, e->x.p->pos);
2727 EVALUE_SET_DOMAIN(tmp.x.p->arr[0], Polyhedron_Copy(D));
2728 evalue_copy(&tmp.x.p->arr[1], &e->x.p->arr[2*i+1]);
2729 reduce_evalue(&tmp);
2730 if (value_notzero_p(tmp.d) ||
2731 ecmp(&tmp.x.p->arr[1], &e->x.p->arr[2*k+1]) != 0)
2732 P = &D->next;
2733 else {
2734 D->next = EVALUE_DOMAIN(e->x.p->arr[2*i]);
2735 EVALUE_DOMAIN(e->x.p->arr[2*i]) = D;
2736 *P = NULL;
2738 free_evalue_refs(&tmp);
2741 Polyhedron_Free(H);
2744 for (i = 0; i < e->x.p->size/2; ++i) {
2745 Polyhedron *H, *E;
2746 Polyhedron *D = EVALUE_DOMAIN(e->x.p->arr[2*i]);
2747 if (!D) {
2748 value_clear(e->x.p->arr[2*i].d);
2749 free_evalue_refs(&e->x.p->arr[2*i+1]);
2750 e->x.p->size -= 2;
2751 if (2*i < e->x.p->size) {
2752 e->x.p->arr[2*i] = e->x.p->arr[e->x.p->size];
2753 e->x.p->arr[2*i+1] = e->x.p->arr[e->x.p->size+1];
2755 --i;
2756 continue;
2758 if (!D->next)
2759 continue;
2760 H = DomainConvex(D, 0);
2761 E = DomainDifference(H, D, 0);
2762 Domain_Free(D);
2763 D = DomainDifference(H, E, 0);
2764 Domain_Free(H);
2765 Domain_Free(E);
2766 EVALUE_SET_DOMAIN(p->arr[2*i], D);
2770 /* Use smallest representative for coefficients in affine form in
2771 * argument of fractional.
2772 * Since any change will make the argument non-standard,
2773 * the containing evalue will have to be reduced again afterward.
2775 static void fractional_minimal_coefficients(enode *p)
2777 evalue *pp;
2778 Value twice;
2779 value_init(twice);
2781 assert(p->type == fractional);
2782 pp = &p->arr[0];
2783 while (value_zero_p(pp->d)) {
2784 assert(pp->x.p->type == polynomial);
2785 assert(pp->x.p->size == 2);
2786 assert(value_notzero_p(pp->x.p->arr[1].d));
2787 mpz_mul_ui(twice, pp->x.p->arr[1].x.n, 2);
2788 if (value_gt(twice, pp->x.p->arr[1].d))
2789 value_subtract(pp->x.p->arr[1].x.n,
2790 pp->x.p->arr[1].x.n, pp->x.p->arr[1].d);
2791 pp = &pp->x.p->arr[0];
2794 value_clear(twice);
2797 static Polyhedron *polynomial_projection(enode *p, Polyhedron *D, Value *d,
2798 Matrix **R)
2800 Polyhedron *I, *H;
2801 evalue *pp;
2802 unsigned dim = D->Dimension;
2803 Matrix *T = Matrix_Alloc(2, dim+1);
2804 assert(T);
2806 assert(p->type == fractional || p->type == flooring);
2807 value_set_si(T->p[1][dim], 1);
2808 evalue_extract_affine(&p->arr[0], T->p[0], &T->p[0][dim], d);
2809 I = DomainImage(D, T, 0);
2810 H = DomainConvex(I, 0);
2811 Domain_Free(I);
2812 if (R)
2813 *R = T;
2814 else
2815 Matrix_Free(T);
2817 return H;
2820 static void replace_by_affine(evalue *e, Value offset)
2822 enode *p;
2823 evalue inc;
2825 p = e->x.p;
2826 value_init(inc.d);
2827 value_init(inc.x.n);
2828 value_set_si(inc.d, 1);
2829 value_oppose(inc.x.n, offset);
2830 eadd(&inc, &p->arr[0]);
2831 reorder_terms_about(p, &p->arr[0]); /* frees arr[0] */
2832 value_clear(e->d);
2833 *e = p->arr[1];
2834 free(p);
2835 free_evalue_refs(&inc);
2838 int evalue_range_reduction_in_domain(evalue *e, Polyhedron *D)
2840 int i;
2841 enode *p;
2842 Value d, min, max;
2843 int r = 0;
2844 Polyhedron *I;
2845 int bounded;
2847 if (value_notzero_p(e->d))
2848 return r;
2850 p = e->x.p;
2852 if (p->type == relation) {
2853 Matrix *T;
2854 int equal;
2855 value_init(d);
2856 value_init(min);
2857 value_init(max);
2859 fractional_minimal_coefficients(p->arr[0].x.p);
2860 I = polynomial_projection(p->arr[0].x.p, D, &d, &T);
2861 bounded = line_minmax(I, &min, &max); /* frees I */
2862 equal = value_eq(min, max);
2863 mpz_cdiv_q(min, min, d);
2864 mpz_fdiv_q(max, max, d);
2866 if (bounded && value_gt(min, max)) {
2867 /* Never zero */
2868 if (p->size == 3) {
2869 value_clear(e->d);
2870 *e = p->arr[2];
2871 } else {
2872 evalue_set_si(e, 0, 1);
2873 r = 1;
2875 free_evalue_refs(&(p->arr[1]));
2876 free_evalue_refs(&(p->arr[0]));
2877 free(p);
2878 value_clear(d);
2879 value_clear(min);
2880 value_clear(max);
2881 Matrix_Free(T);
2882 return r ? r : evalue_range_reduction_in_domain(e, D);
2883 } else if (bounded && equal) {
2884 /* Always zero */
2885 if (p->size == 3)
2886 free_evalue_refs(&(p->arr[2]));
2887 value_clear(e->d);
2888 *e = p->arr[1];
2889 free_evalue_refs(&(p->arr[0]));
2890 free(p);
2891 value_clear(d);
2892 value_clear(min);
2893 value_clear(max);
2894 Matrix_Free(T);
2895 return evalue_range_reduction_in_domain(e, D);
2896 } else if (bounded && value_eq(min, max)) {
2897 /* zero for a single value */
2898 Polyhedron *E;
2899 Matrix *M = Matrix_Alloc(1, D->Dimension+2);
2900 Vector_Copy(T->p[0], M->p[0]+1, D->Dimension+1);
2901 value_multiply(min, min, d);
2902 value_subtract(M->p[0][D->Dimension+1],
2903 M->p[0][D->Dimension+1], min);
2904 E = DomainAddConstraints(D, M, 0);
2905 value_clear(d);
2906 value_clear(min);
2907 value_clear(max);
2908 Matrix_Free(T);
2909 Matrix_Free(M);
2910 r = evalue_range_reduction_in_domain(&p->arr[1], E);
2911 if (p->size == 3)
2912 r |= evalue_range_reduction_in_domain(&p->arr[2], D);
2913 Domain_Free(E);
2914 _reduce_evalue(&p->arr[0].x.p->arr[0], 0, 1);
2915 return r;
2918 value_clear(d);
2919 value_clear(min);
2920 value_clear(max);
2921 Matrix_Free(T);
2922 _reduce_evalue(&p->arr[0].x.p->arr[0], 0, 1);
2925 i = p->type == relation ? 1 :
2926 p->type == fractional ? 1 : 0;
2927 for (; i<p->size; i++)
2928 r |= evalue_range_reduction_in_domain(&p->arr[i], D);
2930 if (p->type != fractional) {
2931 if (r && p->type == polynomial) {
2932 evalue f;
2933 value_init(f.d);
2934 value_set_si(f.d, 0);
2935 f.x.p = new_enode(polynomial, 2, p->pos);
2936 evalue_set_si(&f.x.p->arr[0], 0, 1);
2937 evalue_set_si(&f.x.p->arr[1], 1, 1);
2938 reorder_terms_about(p, &f);
2939 value_clear(e->d);
2940 *e = p->arr[0];
2941 free(p);
2943 return r;
2946 value_init(d);
2947 value_init(min);
2948 value_init(max);
2949 fractional_minimal_coefficients(p);
2950 I = polynomial_projection(p, D, &d, NULL);
2951 bounded = line_minmax(I, &min, &max); /* frees I */
2952 mpz_fdiv_q(min, min, d);
2953 mpz_fdiv_q(max, max, d);
2954 value_subtract(d, max, min);
2956 if (bounded && value_eq(min, max)) {
2957 replace_by_affine(e, min);
2958 r = 1;
2959 } else if (bounded && value_one_p(d) && p->size > 3) {
2960 /* replace {g}^2 by -(g-min)^2 + (2{g}+1)*(g-min) - {g}
2961 * See pages 199-200 of PhD thesis.
2963 evalue rem;
2964 evalue inc;
2965 evalue t;
2966 evalue f;
2967 evalue factor;
2968 value_init(rem.d);
2969 value_set_si(rem.d, 0);
2970 rem.x.p = new_enode(fractional, 3, -1);
2971 evalue_copy(&rem.x.p->arr[0], &p->arr[0]);
2972 value_clear(rem.x.p->arr[1].d);
2973 value_clear(rem.x.p->arr[2].d);
2974 rem.x.p->arr[1] = p->arr[1];
2975 rem.x.p->arr[2] = p->arr[2];
2976 for (i = 3; i < p->size; ++i)
2977 p->arr[i-2] = p->arr[i];
2978 p->size -= 2;
2980 value_init(inc.d);
2981 value_init(inc.x.n);
2982 value_set_si(inc.d, 1);
2983 value_oppose(inc.x.n, min);
2985 value_init(t.d);
2986 evalue_copy(&t, &p->arr[0]);
2987 eadd(&inc, &t);
2989 value_init(f.d);
2990 value_set_si(f.d, 0);
2991 f.x.p = new_enode(fractional, 3, -1);
2992 evalue_copy(&f.x.p->arr[0], &p->arr[0]);
2993 evalue_set_si(&f.x.p->arr[1], 1, 1);
2994 evalue_set_si(&f.x.p->arr[2], 2, 1);
2996 value_init(factor.d);
2997 evalue_set_si(&factor, -1, 1);
2998 emul(&t, &factor);
3000 eadd(&f, &factor);
3001 emul(&t, &factor);
3003 value_clear(f.x.p->arr[1].x.n);
3004 value_clear(f.x.p->arr[2].x.n);
3005 evalue_set_si(&f.x.p->arr[1], 0, 1);
3006 evalue_set_si(&f.x.p->arr[2], -1, 1);
3007 eadd(&f, &factor);
3009 if (r) {
3010 evalue_reorder_terms(&rem);
3011 evalue_reorder_terms(e);
3014 emul(&factor, e);
3015 eadd(&rem, e);
3017 free_evalue_refs(&inc);
3018 free_evalue_refs(&t);
3019 free_evalue_refs(&f);
3020 free_evalue_refs(&factor);
3021 free_evalue_refs(&rem);
3023 evalue_range_reduction_in_domain(e, D);
3025 r = 1;
3026 } else {
3027 _reduce_evalue(&p->arr[0], 0, 1);
3028 if (r)
3029 evalue_reorder_terms(e);
3032 value_clear(d);
3033 value_clear(min);
3034 value_clear(max);
3036 return r;
3039 void evalue_range_reduction(evalue *e)
3041 int i;
3042 if (value_notzero_p(e->d) || e->x.p->type != partition)
3043 return;
3045 for (i = 0; i < e->x.p->size/2; ++i)
3046 if (evalue_range_reduction_in_domain(&e->x.p->arr[2*i+1],
3047 EVALUE_DOMAIN(e->x.p->arr[2*i]))) {
3048 reduce_evalue(&e->x.p->arr[2*i+1]);
3050 if (EVALUE_IS_ZERO(e->x.p->arr[2*i+1])) {
3051 free_evalue_refs(&e->x.p->arr[2*i+1]);
3052 Domain_Free(EVALUE_DOMAIN(e->x.p->arr[2*i]));
3053 value_clear(e->x.p->arr[2*i].d);
3054 e->x.p->size -= 2;
3055 e->x.p->arr[2*i] = e->x.p->arr[e->x.p->size];
3056 e->x.p->arr[2*i+1] = e->x.p->arr[e->x.p->size+1];
3057 --i;
3062 /* Frees EP
3064 Enumeration* partition2enumeration(evalue *EP)
3066 int i;
3067 Enumeration *en, *res = NULL;
3069 if (EVALUE_IS_ZERO(*EP)) {
3070 free(EP);
3071 return res;
3074 for (i = 0; i < EP->x.p->size/2; ++i) {
3075 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[2*i])->Dimension);
3076 en = (Enumeration *)malloc(sizeof(Enumeration));
3077 en->next = res;
3078 res = en;
3079 res->ValidityDomain = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
3080 value_clear(EP->x.p->arr[2*i].d);
3081 res->EP = EP->x.p->arr[2*i+1];
3083 free(EP->x.p);
3084 value_clear(EP->d);
3085 free(EP);
3086 return res;
3089 int evalue_frac2floor_in_domain3(evalue *e, Polyhedron *D, int shift)
3091 enode *p;
3092 int r = 0;
3093 int i;
3094 Polyhedron *I;
3095 Value d, min;
3096 evalue fl;
3098 if (value_notzero_p(e->d))
3099 return r;
3101 p = e->x.p;
3103 i = p->type == relation ? 1 :
3104 p->type == fractional ? 1 : 0;
3105 for (; i<p->size; i++)
3106 r |= evalue_frac2floor_in_domain3(&p->arr[i], D, shift);
3108 if (p->type != fractional) {
3109 if (r && p->type == polynomial) {
3110 evalue f;
3111 value_init(f.d);
3112 value_set_si(f.d, 0);
3113 f.x.p = new_enode(polynomial, 2, p->pos);
3114 evalue_set_si(&f.x.p->arr[0], 0, 1);
3115 evalue_set_si(&f.x.p->arr[1], 1, 1);
3116 reorder_terms_about(p, &f);
3117 value_clear(e->d);
3118 *e = p->arr[0];
3119 free(p);
3121 return r;
3124 if (shift) {
3125 value_init(d);
3126 I = polynomial_projection(p, D, &d, NULL);
3129 Polyhedron_Print(stderr, P_VALUE_FMT, I);
3132 assert(I->NbEq == 0); /* Should have been reduced */
3134 /* Find minimum */
3135 for (i = 0; i < I->NbConstraints; ++i)
3136 if (value_pos_p(I->Constraint[i][1]))
3137 break;
3139 if (i < I->NbConstraints) {
3140 value_init(min);
3141 value_oppose(I->Constraint[i][2], I->Constraint[i][2]);
3142 mpz_cdiv_q(min, I->Constraint[i][2], I->Constraint[i][1]);
3143 if (value_neg_p(min)) {
3144 evalue offset;
3145 mpz_fdiv_q(min, min, d);
3146 value_init(offset.d);
3147 value_set_si(offset.d, 1);
3148 value_init(offset.x.n);
3149 value_oppose(offset.x.n, min);
3150 eadd(&offset, &p->arr[0]);
3151 free_evalue_refs(&offset);
3153 value_clear(min);
3156 Polyhedron_Free(I);
3157 value_clear(d);
3160 value_init(fl.d);
3161 value_set_si(fl.d, 0);
3162 fl.x.p = new_enode(flooring, 3, -1);
3163 evalue_set_si(&fl.x.p->arr[1], 0, 1);
3164 evalue_set_si(&fl.x.p->arr[2], -1, 1);
3165 evalue_copy(&fl.x.p->arr[0], &p->arr[0]);
3167 eadd(&fl, &p->arr[0]);
3168 reorder_terms_about(p, &p->arr[0]);
3169 value_clear(e->d);
3170 *e = p->arr[1];
3171 free(p);
3172 free_evalue_refs(&fl);
3174 return 1;
3177 int evalue_frac2floor_in_domain(evalue *e, Polyhedron *D)
3179 return evalue_frac2floor_in_domain3(e, D, 1);
3182 void evalue_frac2floor2(evalue *e, int shift)
3184 int i;
3185 if (value_notzero_p(e->d) || e->x.p->type != partition) {
3186 if (!shift) {
3187 if (evalue_frac2floor_in_domain3(e, NULL, 0))
3188 reduce_evalue(e);
3190 return;
3193 for (i = 0; i < e->x.p->size/2; ++i)
3194 if (evalue_frac2floor_in_domain3(&e->x.p->arr[2*i+1],
3195 EVALUE_DOMAIN(e->x.p->arr[2*i]), shift))
3196 reduce_evalue(&e->x.p->arr[2*i+1]);
3199 void evalue_frac2floor(evalue *e)
3201 evalue_frac2floor2(e, 1);
3204 /* Add a new variable with lower bound 1 and upper bound specified
3205 * by row. If negative is true, then the new variable has upper
3206 * bound -1 and lower bound specified by row.
3208 static Matrix *esum_add_constraint(int nvar, Polyhedron *D, Matrix *C,
3209 Vector *row, int negative)
3211 int nr, nc;
3212 int i;
3213 int nparam = D->Dimension - nvar;
3215 if (C == 0) {
3216 nr = D->NbConstraints + 2;
3217 nc = D->Dimension + 2 + 1;
3218 C = Matrix_Alloc(nr, nc);
3219 for (i = 0; i < D->NbConstraints; ++i) {
3220 Vector_Copy(D->Constraint[i], C->p[i], 1 + nvar);
3221 Vector_Copy(D->Constraint[i] + 1 + nvar, C->p[i] + 1 + nvar + 1,
3222 D->Dimension + 1 - nvar);
3224 } else {
3225 Matrix *oldC = C;
3226 nr = C->NbRows + 2;
3227 nc = C->NbColumns + 1;
3228 C = Matrix_Alloc(nr, nc);
3229 for (i = 0; i < oldC->NbRows; ++i) {
3230 Vector_Copy(oldC->p[i], C->p[i], 1 + nvar);
3231 Vector_Copy(oldC->p[i] + 1 + nvar, C->p[i] + 1 + nvar + 1,
3232 oldC->NbColumns - 1 - nvar);
3235 value_set_si(C->p[nr-2][0], 1);
3236 if (negative)
3237 value_set_si(C->p[nr-2][1 + nvar], -1);
3238 else
3239 value_set_si(C->p[nr-2][1 + nvar], 1);
3240 value_set_si(C->p[nr-2][nc - 1], -1);
3242 Vector_Copy(row->p, C->p[nr-1], 1 + nvar + 1);
3243 Vector_Copy(row->p + 1 + nvar + 1, C->p[nr-1] + C->NbColumns - 1 - nparam,
3244 1 + nparam);
3246 return C;
3249 static void floor2frac_r(evalue *e, int nvar)
3251 enode *p;
3252 int i;
3253 evalue f;
3254 evalue *pp;
3256 if (value_notzero_p(e->d))
3257 return;
3259 p = e->x.p;
3261 assert(p->type == flooring);
3262 for (i = 1; i < p->size; i++)
3263 floor2frac_r(&p->arr[i], nvar);
3265 for (pp = &p->arr[0]; value_zero_p(pp->d); pp = &pp->x.p->arr[0]) {
3266 assert(pp->x.p->type == polynomial);
3267 pp->x.p->pos -= nvar;
3270 value_init(f.d);
3271 value_set_si(f.d, 0);
3272 f.x.p = new_enode(fractional, 3, -1);
3273 evalue_set_si(&f.x.p->arr[1], 0, 1);
3274 evalue_set_si(&f.x.p->arr[2], -1, 1);
3275 evalue_copy(&f.x.p->arr[0], &p->arr[0]);
3277 eadd(&f, &p->arr[0]);
3278 reorder_terms_about(p, &p->arr[0]);
3279 value_clear(e->d);
3280 *e = p->arr[1];
3281 free(p);
3282 free_evalue_refs(&f);
3285 /* Convert flooring back to fractional and shift position
3286 * of the parameters by nvar
3288 static void floor2frac(evalue *e, int nvar)
3290 floor2frac_r(e, nvar);
3291 reduce_evalue(e);
3294 int evalue_floor2frac(evalue *e)
3296 int i;
3297 int r = 0;
3299 if (value_notzero_p(e->d))
3300 return 0;
3302 if (e->x.p->type == partition) {
3303 for (i = 0; i < e->x.p->size/2; ++i)
3304 if (evalue_floor2frac(&e->x.p->arr[2*i+1]))
3305 reduce_evalue(&e->x.p->arr[2*i+1]);
3306 return 0;
3309 for (i = type_offset(e->x.p); i < e->x.p->size; ++i)
3310 r |= evalue_floor2frac(&e->x.p->arr[i]);
3312 if (e->x.p->type == flooring) {
3313 floor2frac(e, 0);
3314 return 1;
3317 if (r)
3318 evalue_reorder_terms(e);
3320 return r;
3323 evalue *esum_over_domain_cst(int nvar, Polyhedron *D, Matrix *C)
3325 evalue *t;
3326 int nparam = D->Dimension - nvar;
3328 if (C != 0) {
3329 C = Matrix_Copy(C);
3330 D = Constraints2Polyhedron(C, 0);
3331 Matrix_Free(C);
3334 t = barvinok_enumerate_e(D, 0, nparam, 0);
3336 /* Double check that D was not unbounded. */
3337 assert(!(value_pos_p(t->d) && value_neg_p(t->x.n)));
3339 if (C != 0)
3340 Polyhedron_Free(D);
3342 return t;
3345 static void domain_signs(Polyhedron *D, int *signs)
3347 int j, k;
3349 POL_ENSURE_VERTICES(D);
3350 for (j = 0; j < D->Dimension; ++j) {
3351 signs[j] = 0;
3352 for (k = 0; k < D->NbRays; ++k) {
3353 signs[j] = value_sign(D->Ray[k][1+j]);
3354 if (signs[j])
3355 break;
3360 static evalue *esum_over_domain(evalue *e, int nvar, Polyhedron *D,
3361 int *signs, Matrix *C, unsigned MaxRays)
3363 Vector *row = NULL;
3364 int i, offset;
3365 evalue *res;
3366 Matrix *origC;
3367 evalue *factor = NULL;
3368 evalue cum;
3369 int negative = 0;
3371 if (EVALUE_IS_ZERO(*e))
3372 return 0;
3374 if (D->next) {
3375 Polyhedron *DD = Disjoint_Domain(D, 0, MaxRays);
3376 Polyhedron *Q;
3378 Q = DD;
3379 DD = Q->next;
3380 Q->next = 0;
3382 res = esum_over_domain(e, nvar, Q, signs, C, MaxRays);
3383 Polyhedron_Free(Q);
3385 for (Q = DD; Q; Q = DD) {
3386 evalue *t;
3388 DD = Q->next;
3389 Q->next = 0;
3391 t = esum_over_domain(e, nvar, Q, signs, C, MaxRays);
3392 Polyhedron_Free(Q);
3394 if (!res)
3395 res = t;
3396 else if (t) {
3397 eadd(t, res);
3398 evalue_free(t);
3401 return res;
3404 if (value_notzero_p(e->d)) {
3405 evalue *t;
3407 t = esum_over_domain_cst(nvar, D, C);
3409 if (!EVALUE_IS_ONE(*e))
3410 emul(e, t);
3412 return t;
3415 if (!signs) {
3416 signs = alloca(sizeof(int) * D->Dimension);
3417 domain_signs(D, signs);
3420 switch (e->x.p->type) {
3421 case flooring: {
3422 evalue *pp = &e->x.p->arr[0];
3424 if (pp->x.p->pos > nvar) {
3425 /* remainder is independent of the summated vars */
3426 evalue f;
3427 evalue *t;
3429 value_init(f.d);
3430 evalue_copy(&f, e);
3431 floor2frac(&f, nvar);
3433 t = esum_over_domain_cst(nvar, D, C);
3435 emul(&f, t);
3437 free_evalue_refs(&f);
3439 return t;
3442 row = Vector_Alloc(1 + D->Dimension + 1 + 1);
3443 poly_denom(pp, &row->p[1 + nvar]);
3444 value_set_si(row->p[0], 1);
3445 for (pp = &e->x.p->arr[0]; value_zero_p(pp->d);
3446 pp = &pp->x.p->arr[0]) {
3447 int pos;
3448 assert(pp->x.p->type == polynomial);
3449 pos = pp->x.p->pos;
3450 if (pos >= 1 + nvar)
3451 ++pos;
3452 value_assign(row->p[pos], row->p[1+nvar]);
3453 value_division(row->p[pos], row->p[pos], pp->x.p->arr[1].d);
3454 value_multiply(row->p[pos], row->p[pos], pp->x.p->arr[1].x.n);
3456 value_assign(row->p[1 + D->Dimension + 1], row->p[1+nvar]);
3457 value_division(row->p[1 + D->Dimension + 1],
3458 row->p[1 + D->Dimension + 1],
3459 pp->d);
3460 value_multiply(row->p[1 + D->Dimension + 1],
3461 row->p[1 + D->Dimension + 1],
3462 pp->x.n);
3463 value_oppose(row->p[1 + nvar], row->p[1 + nvar]);
3464 break;
3466 case polynomial: {
3467 int pos = e->x.p->pos;
3469 if (pos > nvar) {
3470 factor = ALLOC(evalue);
3471 value_init(factor->d);
3472 value_set_si(factor->d, 0);
3473 factor->x.p = new_enode(polynomial, 2, pos - nvar);
3474 evalue_set_si(&factor->x.p->arr[0], 0, 1);
3475 evalue_set_si(&factor->x.p->arr[1], 1, 1);
3476 break;
3479 row = Vector_Alloc(1 + D->Dimension + 1 + 1);
3480 negative = signs[pos-1] < 0;
3481 value_set_si(row->p[0], 1);
3482 if (negative) {
3483 value_set_si(row->p[pos], -1);
3484 value_set_si(row->p[1 + nvar], 1);
3485 } else {
3486 value_set_si(row->p[pos], 1);
3487 value_set_si(row->p[1 + nvar], -1);
3489 break;
3491 default:
3492 assert(0);
3495 offset = type_offset(e->x.p);
3497 res = esum_over_domain(&e->x.p->arr[offset], nvar, D, signs, C, MaxRays);
3499 if (factor) {
3500 value_init(cum.d);
3501 evalue_copy(&cum, factor);
3504 origC = C;
3505 for (i = 1; offset+i < e->x.p->size; ++i) {
3506 evalue *t;
3507 if (row) {
3508 Matrix *prevC = C;
3509 C = esum_add_constraint(nvar, D, C, row, negative);
3510 if (prevC != origC)
3511 Matrix_Free(prevC);
3514 if (row)
3515 Vector_Print(stderr, P_VALUE_FMT, row);
3516 if (C)
3517 Matrix_Print(stderr, P_VALUE_FMT, C);
3519 t = esum_over_domain(&e->x.p->arr[offset+i], nvar, D, signs, C, MaxRays);
3521 if (t) {
3522 if (factor)
3523 emul(&cum, t);
3524 if (negative && (i % 2))
3525 evalue_negate(t);
3528 if (!res)
3529 res = t;
3530 else if (t) {
3531 eadd(t, res);
3532 evalue_free(t);
3534 if (factor && offset+i+1 < e->x.p->size)
3535 emul(factor, &cum);
3537 if (C != origC)
3538 Matrix_Free(C);
3540 if (factor) {
3541 free_evalue_refs(&cum);
3542 evalue_free(factor);
3545 if (row)
3546 Vector_Free(row);
3548 reduce_evalue(res);
3550 return res;
3553 static Polyhedron_Insert(Polyhedron ***next, Polyhedron *Q)
3555 if (emptyQ(Q))
3556 Polyhedron_Free(Q);
3557 else {
3558 **next = Q;
3559 *next = &(Q->next);
3563 static Polyhedron *Polyhedron_Split_Into_Orthants(Polyhedron *P,
3564 unsigned MaxRays)
3566 int i = 0;
3567 Polyhedron *D = P;
3568 Vector *c = Vector_Alloc(1 + P->Dimension + 1);
3569 value_set_si(c->p[0], 1);
3571 if (P->Dimension == 0)
3572 return Polyhedron_Copy(P);
3574 for (i = 0; i < P->Dimension; ++i) {
3575 Polyhedron *L = NULL;
3576 Polyhedron **next = &L;
3577 Polyhedron *I;
3579 for (I = D; I; I = I->next) {
3580 Polyhedron *Q;
3581 value_set_si(c->p[1+i], 1);
3582 value_set_si(c->p[1+P->Dimension], 0);
3583 Q = AddConstraints(c->p, 1, I, MaxRays);
3584 Polyhedron_Insert(&next, Q);
3585 value_set_si(c->p[1+i], -1);
3586 value_set_si(c->p[1+P->Dimension], -1);
3587 Q = AddConstraints(c->p, 1, I, MaxRays);
3588 Polyhedron_Insert(&next, Q);
3589 value_set_si(c->p[1+i], 0);
3591 if (D != P)
3592 Domain_Free(D);
3593 D = L;
3595 Vector_Free(c);
3596 return D;
3599 /* Make arguments of all floors non-negative */
3600 static void shift_floor_in_domain(evalue *e, Polyhedron *D)
3602 Value d, m;
3603 Polyhedron *I;
3604 int i;
3605 enode *p;
3607 if (value_notzero_p(e->d))
3608 return;
3610 p = e->x.p;
3612 for (i = type_offset(p); i < p->size; ++i)
3613 shift_floor_in_domain(&p->arr[i], D);
3615 if (p->type != flooring)
3616 return;
3618 value_init(d);
3619 value_init(m);
3621 I = polynomial_projection(p, D, &d, NULL);
3622 assert(I->NbEq == 0); /* Should have been reduced */
3624 for (i = 0; i < I->NbConstraints; ++i)
3625 if (value_pos_p(I->Constraint[i][1]))
3626 break;
3627 assert(i < I->NbConstraints);
3628 if (i < I->NbConstraints) {
3629 value_oppose(I->Constraint[i][2], I->Constraint[i][2]);
3630 mpz_fdiv_q(m, I->Constraint[i][2], I->Constraint[i][1]);
3631 if (value_neg_p(m)) {
3632 /* replace [e] by [e-m]+m such that e-m >= 0 */
3633 evalue f;
3635 value_init(f.d);
3636 value_init(f.x.n);
3637 value_set_si(f.d, 1);
3638 value_oppose(f.x.n, m);
3639 eadd(&f, &p->arr[0]);
3640 value_clear(f.x.n);
3642 value_set_si(f.d, 0);
3643 f.x.p = new_enode(flooring, 3, -1);
3644 value_clear(f.x.p->arr[0].d);
3645 f.x.p->arr[0] = p->arr[0];
3646 evalue_set_si(&f.x.p->arr[2], 1, 1);
3647 value_set_si(f.x.p->arr[1].d, 1);
3648 value_init(f.x.p->arr[1].x.n);
3649 value_assign(f.x.p->arr[1].x.n, m);
3650 reorder_terms_about(p, &f);
3651 value_clear(e->d);
3652 *e = p->arr[1];
3653 free(p);
3656 Polyhedron_Free(I);
3657 value_clear(d);
3658 value_clear(m);
3661 evalue *box_summate(Polyhedron *P, evalue *E, unsigned nvar, unsigned MaxRays)
3663 evalue *sum = evalue_zero();
3664 Polyhedron *D = Polyhedron_Split_Into_Orthants(P, MaxRays);
3665 for (P = D; P; P = P->next) {
3666 evalue *t;
3667 evalue *fe = evalue_dup(E);
3668 Polyhedron *next = P->next;
3669 P->next = NULL;
3670 reduce_evalue_in_domain(fe, P);
3671 evalue_frac2floor2(fe, 0);
3672 shift_floor_in_domain(fe, P);
3673 t = esum_over_domain(fe, nvar, P, NULL, NULL, MaxRays);
3674 if (t) {
3675 eadd(t, sum);
3676 evalue_free(t);
3678 evalue_free(fe);
3679 P->next = next;
3681 Domain_Free(D);
3682 return sum;
3685 /* Initial silly implementation */
3686 void eor(evalue *e1, evalue *res)
3688 evalue E;
3689 evalue mone;
3690 value_init(E.d);
3691 value_init(mone.d);
3692 evalue_set_si(&mone, -1, 1);
3694 evalue_copy(&E, res);
3695 eadd(e1, &E);
3696 emul(e1, res);
3697 emul(&mone, res);
3698 eadd(&E, res);
3700 free_evalue_refs(&E);
3701 free_evalue_refs(&mone);
3704 /* computes denominator of polynomial evalue
3705 * d should point to a value initialized to 1
3707 void evalue_denom(const evalue *e, Value *d)
3709 int i, offset;
3711 if (value_notzero_p(e->d)) {
3712 value_lcm(*d, *d, e->d);
3713 return;
3715 assert(e->x.p->type == polynomial ||
3716 e->x.p->type == fractional ||
3717 e->x.p->type == flooring);
3718 offset = type_offset(e->x.p);
3719 for (i = e->x.p->size-1; i >= offset; --i)
3720 evalue_denom(&e->x.p->arr[i], d);
3723 /* Divides the evalue e by the integer n */
3724 void evalue_div(evalue *e, Value n)
3726 int i, offset;
3728 if (value_one_p(n) || EVALUE_IS_ZERO(*e))
3729 return;
3731 if (value_notzero_p(e->d)) {
3732 Value gc;
3733 value_init(gc);
3734 value_multiply(e->d, e->d, n);
3735 value_gcd(gc, e->x.n, e->d);
3736 if (value_notone_p(gc)) {
3737 value_division(e->d, e->d, gc);
3738 value_division(e->x.n, e->x.n, gc);
3740 value_clear(gc);
3741 return;
3743 if (e->x.p->type == partition) {
3744 for (i = 0; i < e->x.p->size/2; ++i)
3745 evalue_div(&e->x.p->arr[2*i+1], n);
3746 return;
3748 offset = type_offset(e->x.p);
3749 for (i = e->x.p->size-1; i >= offset; --i)
3750 evalue_div(&e->x.p->arr[i], n);
3753 /* Multiplies the evalue e by the integer n */
3754 void evalue_mul(evalue *e, Value n)
3756 int i, offset;
3758 if (value_one_p(n) || EVALUE_IS_ZERO(*e))
3759 return;
3761 if (value_notzero_p(e->d)) {
3762 Value gc;
3763 value_init(gc);
3764 value_multiply(e->x.n, e->x.n, n);
3765 value_gcd(gc, e->x.n, e->d);
3766 if (value_notone_p(gc)) {
3767 value_division(e->d, e->d, gc);
3768 value_division(e->x.n, e->x.n, gc);
3770 value_clear(gc);
3771 return;
3773 if (e->x.p->type == partition) {
3774 for (i = 0; i < e->x.p->size/2; ++i)
3775 evalue_mul(&e->x.p->arr[2*i+1], n);
3776 return;
3778 offset = type_offset(e->x.p);
3779 for (i = e->x.p->size-1; i >= offset; --i)
3780 evalue_mul(&e->x.p->arr[i], n);
3783 /* Multiplies the evalue e by the n/d */
3784 void evalue_mul_div(evalue *e, Value n, Value d)
3786 int i, offset;
3788 if ((value_one_p(n) && value_one_p(d)) || EVALUE_IS_ZERO(*e))
3789 return;
3791 if (value_notzero_p(e->d)) {
3792 Value gc;
3793 value_init(gc);
3794 value_multiply(e->x.n, e->x.n, n);
3795 value_multiply(e->d, e->d, d);
3796 value_gcd(gc, e->x.n, e->d);
3797 if (value_notone_p(gc)) {
3798 value_division(e->d, e->d, gc);
3799 value_division(e->x.n, e->x.n, gc);
3801 value_clear(gc);
3802 return;
3804 if (e->x.p->type == partition) {
3805 for (i = 0; i < e->x.p->size/2; ++i)
3806 evalue_mul_div(&e->x.p->arr[2*i+1], n, d);
3807 return;
3809 offset = type_offset(e->x.p);
3810 for (i = e->x.p->size-1; i >= offset; --i)
3811 evalue_mul_div(&e->x.p->arr[i], n, d);
3814 void evalue_negate(evalue *e)
3816 int i, offset;
3818 if (value_notzero_p(e->d)) {
3819 value_oppose(e->x.n, e->x.n);
3820 return;
3822 if (e->x.p->type == partition) {
3823 for (i = 0; i < e->x.p->size/2; ++i)
3824 evalue_negate(&e->x.p->arr[2*i+1]);
3825 return;
3827 offset = type_offset(e->x.p);
3828 for (i = e->x.p->size-1; i >= offset; --i)
3829 evalue_negate(&e->x.p->arr[i]);
3832 void evalue_add_constant(evalue *e, const Value cst)
3834 int i;
3836 if (value_zero_p(e->d)) {
3837 if (e->x.p->type == partition) {
3838 for (i = 0; i < e->x.p->size/2; ++i)
3839 evalue_add_constant(&e->x.p->arr[2*i+1], cst);
3840 return;
3842 if (e->x.p->type == relation) {
3843 for (i = 1; i < e->x.p->size; ++i)
3844 evalue_add_constant(&e->x.p->arr[i], cst);
3845 return;
3847 do {
3848 e = &e->x.p->arr[type_offset(e->x.p)];
3849 } while (value_zero_p(e->d));
3851 value_addmul(e->x.n, cst, e->d);
3854 static void evalue_frac2polynomial_r(evalue *e, int *signs, int sign, int in_frac)
3856 int i, offset;
3857 Value d;
3858 enode *p;
3859 int sign_odd = sign;
3861 if (value_notzero_p(e->d)) {
3862 if (in_frac && sign * value_sign(e->x.n) < 0) {
3863 value_set_si(e->x.n, 0);
3864 value_set_si(e->d, 1);
3866 return;
3869 if (e->x.p->type == relation) {
3870 for (i = e->x.p->size-1; i >= 1; --i)
3871 evalue_frac2polynomial_r(&e->x.p->arr[i], signs, sign, in_frac);
3872 return;
3875 if (e->x.p->type == polynomial)
3876 sign_odd *= signs[e->x.p->pos-1];
3877 offset = type_offset(e->x.p);
3878 evalue_frac2polynomial_r(&e->x.p->arr[offset], signs, sign, in_frac);
3879 in_frac |= e->x.p->type == fractional;
3880 for (i = e->x.p->size-1; i > offset; --i)
3881 evalue_frac2polynomial_r(&e->x.p->arr[i], signs,
3882 (i - offset) % 2 ? sign_odd : sign, in_frac);
3884 if (e->x.p->type != fractional)
3885 return;
3887 /* replace { a/m } by (m-1)/m if sign != 0
3888 * and by (m-1)/(2m) if sign == 0
3890 value_init(d);
3891 value_set_si(d, 1);
3892 evalue_denom(&e->x.p->arr[0], &d);
3893 free_evalue_refs(&e->x.p->arr[0]);
3894 value_init(e->x.p->arr[0].d);
3895 value_init(e->x.p->arr[0].x.n);
3896 if (sign == 0)
3897 value_addto(e->x.p->arr[0].d, d, d);
3898 else
3899 value_assign(e->x.p->arr[0].d, d);
3900 value_decrement(e->x.p->arr[0].x.n, d);
3901 value_clear(d);
3903 p = e->x.p;
3904 reorder_terms_about(p, &p->arr[0]);
3905 value_clear(e->d);
3906 *e = p->arr[1];
3907 free(p);
3910 /* Approximate the evalue in fractional representation by a polynomial.
3911 * If sign > 0, the result is an upper bound;
3912 * if sign < 0, the result is a lower bound;
3913 * if sign = 0, the result is an intermediate approximation.
3915 void evalue_frac2polynomial(evalue *e, int sign, unsigned MaxRays)
3917 int i, dim;
3918 int *signs;
3920 if (value_notzero_p(e->d))
3921 return;
3922 assert(e->x.p->type == partition);
3923 /* make sure all variables in the domains have a fixed sign */
3924 if (sign) {
3925 evalue_split_domains_into_orthants(e, MaxRays);
3926 if (EVALUE_IS_ZERO(*e))
3927 return;
3930 assert(e->x.p->size >= 2);
3931 dim = EVALUE_DOMAIN(e->x.p->arr[0])->Dimension;
3933 signs = alloca(sizeof(int) * dim);
3935 if (!sign)
3936 for (i = 0; i < dim; ++i)
3937 signs[i] = 0;
3938 for (i = 0; i < e->x.p->size/2; ++i) {
3939 if (sign)
3940 domain_signs(EVALUE_DOMAIN(e->x.p->arr[2*i]), signs);
3941 evalue_frac2polynomial_r(&e->x.p->arr[2*i+1], signs, sign, 0);
3945 /* Split the domains of e (which is assumed to be a partition)
3946 * such that each resulting domain lies entirely in one orthant.
3948 void evalue_split_domains_into_orthants(evalue *e, unsigned MaxRays)
3950 int i, dim;
3951 assert(value_zero_p(e->d));
3952 assert(e->x.p->type == partition);
3953 assert(e->x.p->size >= 2);
3954 dim = EVALUE_DOMAIN(e->x.p->arr[0])->Dimension;
3956 for (i = 0; i < dim; ++i) {
3957 evalue split;
3958 Matrix *C, *C2;
3959 C = Matrix_Alloc(1, 1 + dim + 1);
3960 value_set_si(C->p[0][0], 1);
3961 value_init(split.d);
3962 value_set_si(split.d, 0);
3963 split.x.p = new_enode(partition, 4, dim);
3964 value_set_si(C->p[0][1+i], 1);
3965 C2 = Matrix_Copy(C);
3966 EVALUE_SET_DOMAIN(split.x.p->arr[0], Constraints2Polyhedron(C2, MaxRays));
3967 Matrix_Free(C2);
3968 evalue_set_si(&split.x.p->arr[1], 1, 1);
3969 value_set_si(C->p[0][1+i], -1);
3970 value_set_si(C->p[0][1+dim], -1);
3971 EVALUE_SET_DOMAIN(split.x.p->arr[2], Constraints2Polyhedron(C, MaxRays));
3972 evalue_set_si(&split.x.p->arr[3], 1, 1);
3973 emul(&split, e);
3974 free_evalue_refs(&split);
3975 Matrix_Free(C);
3979 static evalue *find_fractional_with_max_periods(evalue *e, Polyhedron *D,
3980 int max_periods,
3981 Matrix **TT,
3982 Value *min, Value *max)
3984 Matrix *T;
3985 evalue *f = NULL;
3986 Value d;
3987 int i;
3989 if (value_notzero_p(e->d))
3990 return NULL;
3992 if (e->x.p->type == fractional) {
3993 Polyhedron *I;
3994 int bounded;
3996 value_init(d);
3997 I = polynomial_projection(e->x.p, D, &d, &T);
3998 bounded = line_minmax(I, min, max); /* frees I */
3999 if (bounded) {
4000 Value mp;
4001 value_init(mp);
4002 value_set_si(mp, max_periods);
4003 mpz_fdiv_q(*min, *min, d);
4004 mpz_fdiv_q(*max, *max, d);
4005 value_assign(T->p[1][D->Dimension], d);
4006 value_subtract(d, *max, *min);
4007 if (value_ge(d, mp))
4008 Matrix_Free(T);
4009 else
4010 f = evalue_dup(&e->x.p->arr[0]);
4011 value_clear(mp);
4012 } else
4013 Matrix_Free(T);
4014 value_clear(d);
4015 if (f) {
4016 *TT = T;
4017 return f;
4021 for (i = type_offset(e->x.p); i < e->x.p->size; ++i)
4022 if ((f = find_fractional_with_max_periods(&e->x.p->arr[i], D, max_periods,
4023 TT, min, max)))
4024 return f;
4026 return NULL;
4029 static void replace_fract_by_affine(evalue *e, evalue *f, Value val)
4031 int i, offset;
4033 if (value_notzero_p(e->d))
4034 return;
4036 offset = type_offset(e->x.p);
4037 for (i = e->x.p->size-1; i >= offset; --i)
4038 replace_fract_by_affine(&e->x.p->arr[i], f, val);
4040 if (e->x.p->type != fractional)
4041 return;
4043 if (!eequal(&e->x.p->arr[0], f))
4044 return;
4046 replace_by_affine(e, val);
4049 /* Look for fractional parts that can be removed by splitting the corresponding
4050 * domain into at most max_periods parts.
4051 * We use a very simply strategy that looks for the first fractional part
4052 * that satisfies the condition, performs the split and then continues
4053 * looking for other fractional parts in the split domains until no
4054 * such fractional part can be found anymore.
4056 void evalue_split_periods(evalue *e, int max_periods, unsigned int MaxRays)
4058 int i, j, n;
4059 Value min;
4060 Value max;
4061 Value d;
4063 if (EVALUE_IS_ZERO(*e))
4064 return;
4065 if (value_notzero_p(e->d) || e->x.p->type != partition) {
4066 fprintf(stderr,
4067 "WARNING: evalue_split_periods called on incorrect evalue type\n");
4068 return;
4071 value_init(min);
4072 value_init(max);
4073 value_init(d);
4075 for (i = 0; i < e->x.p->size/2; ++i) {
4076 enode *p;
4077 evalue *f;
4078 Matrix *T;
4079 Matrix *M;
4080 Polyhedron *D = EVALUE_DOMAIN(e->x.p->arr[2*i]);
4081 Polyhedron *E;
4082 f = find_fractional_with_max_periods(&e->x.p->arr[2*i+1], D, max_periods,
4083 &T, &min, &max);
4084 if (!f)
4085 continue;
4087 M = Matrix_Alloc(2, 2+D->Dimension);
4089 value_subtract(d, max, min);
4090 n = VALUE_TO_INT(d)+1;
4092 value_set_si(M->p[0][0], 1);
4093 Vector_Copy(T->p[0], M->p[0]+1, D->Dimension+1);
4094 value_multiply(d, max, T->p[1][D->Dimension]);
4095 value_subtract(M->p[0][1+D->Dimension], M->p[0][1+D->Dimension], d);
4096 value_set_si(d, -1);
4097 value_set_si(M->p[1][0], 1);
4098 Vector_Scale(T->p[0], M->p[1]+1, d, D->Dimension+1);
4099 value_addmul(M->p[1][1+D->Dimension], max, T->p[1][D->Dimension]);
4100 value_addto(M->p[1][1+D->Dimension], M->p[1][1+D->Dimension],
4101 T->p[1][D->Dimension]);
4102 value_decrement(M->p[1][1+D->Dimension], M->p[1][1+D->Dimension]);
4104 p = new_enode(partition, e->x.p->size + (n-1)*2, e->x.p->pos);
4105 for (j = 0; j < 2*i; ++j) {
4106 value_clear(p->arr[j].d);
4107 p->arr[j] = e->x.p->arr[j];
4109 for (j = 2*i+2; j < e->x.p->size; ++j) {
4110 value_clear(p->arr[j+2*(n-1)].d);
4111 p->arr[j+2*(n-1)] = e->x.p->arr[j];
4113 for (j = n-1; j >= 0; --j) {
4114 if (j == 0) {
4115 value_clear(p->arr[2*i+1].d);
4116 p->arr[2*i+1] = e->x.p->arr[2*i+1];
4117 } else
4118 evalue_copy(&p->arr[2*(i+j)+1], &e->x.p->arr[2*i+1]);
4119 if (j != n-1) {
4120 value_subtract(M->p[1][1+D->Dimension], M->p[1][1+D->Dimension],
4121 T->p[1][D->Dimension]);
4122 value_addto(M->p[0][1+D->Dimension], M->p[0][1+D->Dimension],
4123 T->p[1][D->Dimension]);
4125 replace_fract_by_affine(&p->arr[2*(i+j)+1], f, max);
4126 E = DomainAddConstraints(D, M, MaxRays);
4127 EVALUE_SET_DOMAIN(p->arr[2*(i+j)], E);
4128 if (evalue_range_reduction_in_domain(&p->arr[2*(i+j)+1], E))
4129 reduce_evalue(&p->arr[2*(i+j)+1]);
4130 value_decrement(max, max);
4132 value_clear(e->x.p->arr[2*i].d);
4133 Domain_Free(D);
4134 Matrix_Free(M);
4135 Matrix_Free(T);
4136 evalue_free(f);
4137 free(e->x.p);
4138 e->x.p = p;
4139 --i;
4142 value_clear(d);
4143 value_clear(min);
4144 value_clear(max);
4147 void evalue_extract_affine(const evalue *e, Value *coeff, Value *cst, Value *d)
4149 value_set_si(*d, 1);
4150 evalue_denom(e, d);
4151 for ( ; value_zero_p(e->d); e = &e->x.p->arr[0]) {
4152 evalue *c;
4153 assert(e->x.p->type == polynomial);
4154 assert(e->x.p->size == 2);
4155 c = &e->x.p->arr[1];
4156 value_multiply(coeff[e->x.p->pos-1], *d, c->x.n);
4157 value_division(coeff[e->x.p->pos-1], coeff[e->x.p->pos-1], c->d);
4159 value_multiply(*cst, *d, e->x.n);
4160 value_division(*cst, *cst, e->d);
4163 /* returns an evalue that corresponds to
4165 * c/den x_param
4167 static evalue *term(int param, Value c, Value den)
4169 evalue *EP = ALLOC(evalue);
4170 value_init(EP->d);
4171 value_set_si(EP->d,0);
4172 EP->x.p = new_enode(polynomial, 2, param + 1);
4173 evalue_set_si(&EP->x.p->arr[0], 0, 1);
4174 evalue_set_reduce(&EP->x.p->arr[1], c, den);
4175 return EP;
4178 evalue *affine2evalue(Value *coeff, Value denom, int nvar)
4180 int i;
4181 evalue *E = ALLOC(evalue);
4182 value_init(E->d);
4183 evalue_set_reduce(E, coeff[nvar], denom);
4184 for (i = 0; i < nvar; ++i) {
4185 evalue *t;
4186 if (value_zero_p(coeff[i]))
4187 continue;
4188 t = term(i, coeff[i], denom);
4189 eadd(t, E);
4190 evalue_free(t);
4192 return E;
4195 void evalue_substitute(evalue *e, evalue **subs)
4197 int i, offset;
4198 evalue *v;
4199 enode *p;
4201 if (value_notzero_p(e->d))
4202 return;
4204 p = e->x.p;
4205 assert(p->type != partition);
4207 for (i = 0; i < p->size; ++i)
4208 evalue_substitute(&p->arr[i], subs);
4210 if (p->type == relation) {
4211 /* For relation a ? b : c, compute (a' ? 1) * b' + (a' ? 0 : 1) * c' */
4212 if (p->size == 3) {
4213 v = ALLOC(evalue);
4214 value_init(v->d);
4215 value_set_si(v->d, 0);
4216 v->x.p = new_enode(relation, 3, 0);
4217 evalue_copy(&v->x.p->arr[0], &p->arr[0]);
4218 evalue_set_si(&v->x.p->arr[1], 0, 1);
4219 evalue_set_si(&v->x.p->arr[2], 1, 1);
4220 emul(v, &p->arr[2]);
4221 evalue_free(v);
4223 v = ALLOC(evalue);
4224 value_init(v->d);
4225 value_set_si(v->d, 0);
4226 v->x.p = new_enode(relation, 2, 0);
4227 value_clear(v->x.p->arr[0].d);
4228 v->x.p->arr[0] = p->arr[0];
4229 evalue_set_si(&v->x.p->arr[1], 1, 1);
4230 emul(v, &p->arr[1]);
4231 evalue_free(v);
4232 if (p->size == 3) {
4233 eadd(&p->arr[2], &p->arr[1]);
4234 free_evalue_refs(&p->arr[2]);
4236 value_clear(e->d);
4237 *e = p->arr[1];
4238 free(p);
4239 return;
4242 if (p->type == polynomial)
4243 v = subs[p->pos-1];
4244 else {
4245 v = ALLOC(evalue);
4246 value_init(v->d);
4247 value_set_si(v->d, 0);
4248 v->x.p = new_enode(p->type, 3, -1);
4249 value_clear(v->x.p->arr[0].d);
4250 v->x.p->arr[0] = p->arr[0];
4251 evalue_set_si(&v->x.p->arr[1], 0, 1);
4252 evalue_set_si(&v->x.p->arr[2], 1, 1);
4255 offset = type_offset(p);
4257 for (i = p->size-1; i >= offset+1; i--) {
4258 emul(v, &p->arr[i]);
4259 eadd(&p->arr[i], &p->arr[i-1]);
4260 free_evalue_refs(&(p->arr[i]));
4263 if (p->type != polynomial)
4264 evalue_free(v);
4266 value_clear(e->d);
4267 *e = p->arr[offset];
4268 free(p);
4271 /* evalue e is given in terms of "new" parameter; CP maps the new
4272 * parameters back to the old parameters.
4273 * Transforms e such that it refers back to the old parameters and
4274 * adds appropriate constraints to the domain.
4275 * In particular, if CP maps the new parameters onto an affine
4276 * subspace of the old parameters, then the corresponding equalities
4277 * are added to the domain.
4278 * Also, if any of the new parameters was a rational combination
4279 * of the old parameters $p' = (<a, p> + c)/m$, then modulo
4280 * constraints ${<a, p> + c)/m} = 0$ are added to ensure
4281 * the new evalue remains non-zero only for integer parameters
4282 * of the new parameters (which have been removed by the substitution).
4284 void evalue_backsubstitute(evalue *e, Matrix *CP, unsigned MaxRays)
4286 Matrix *eq;
4287 Matrix *inv;
4288 evalue **subs;
4289 enode *p;
4290 int i, j;
4291 unsigned nparam = CP->NbColumns-1;
4292 Polyhedron *CEq;
4293 Value gcd;
4295 if (EVALUE_IS_ZERO(*e))
4296 return;
4298 assert(value_zero_p(e->d));
4299 p = e->x.p;
4300 assert(p->type == partition);
4302 inv = left_inverse(CP, &eq);
4303 subs = ALLOCN(evalue *, nparam);
4304 for (i = 0; i < nparam; ++i)
4305 subs[i] = affine2evalue(inv->p[i], inv->p[nparam][inv->NbColumns-1],
4306 inv->NbColumns-1);
4308 CEq = Constraints2Polyhedron(eq, MaxRays);
4309 addeliminatedparams_partition(p, inv, CEq, inv->NbColumns-1, MaxRays);
4310 Polyhedron_Free(CEq);
4312 for (i = 0; i < p->size/2; ++i)
4313 evalue_substitute(&p->arr[2*i+1], subs);
4315 for (i = 0; i < nparam; ++i)
4316 evalue_free(subs[i]);
4317 free(subs);
4319 value_init(gcd);
4320 for (i = 0; i < inv->NbRows-1; ++i) {
4321 Vector_Gcd(inv->p[i], inv->NbColumns, &gcd);
4322 value_gcd(gcd, gcd, inv->p[inv->NbRows-1][inv->NbColumns-1]);
4323 if (value_eq(gcd, inv->p[inv->NbRows-1][inv->NbColumns-1]))
4324 continue;
4325 Vector_AntiScale(inv->p[i], inv->p[i], gcd, inv->NbColumns);
4326 value_divexact(gcd, inv->p[inv->NbRows-1][inv->NbColumns-1], gcd);
4328 for (j = 0; j < p->size/2; ++j) {
4329 evalue *arg = affine2evalue(inv->p[i], gcd, inv->NbColumns-1);
4330 evalue *ev;
4331 evalue rel;
4333 value_init(rel.d);
4334 value_set_si(rel.d, 0);
4335 rel.x.p = new_enode(relation, 2, 0);
4336 value_clear(rel.x.p->arr[1].d);
4337 rel.x.p->arr[1] = p->arr[2*j+1];
4338 ev = &rel.x.p->arr[0];
4339 value_set_si(ev->d, 0);
4340 ev->x.p = new_enode(fractional, 3, -1);
4341 evalue_set_si(&ev->x.p->arr[1], 0, 1);
4342 evalue_set_si(&ev->x.p->arr[2], 1, 1);
4343 value_clear(ev->x.p->arr[0].d);
4344 ev->x.p->arr[0] = *arg;
4345 free(arg);
4347 p->arr[2*j+1] = rel;
4350 value_clear(gcd);
4352 Matrix_Free(eq);
4353 Matrix_Free(inv);
4356 /* Computes
4358 * \sum_{i=0}^n c_i/d X^i
4360 * where d is the last element in the vector c.
4362 evalue *evalue_polynomial(Vector *c, const evalue* X)
4364 unsigned dim = c->Size-2;
4365 evalue EC;
4366 evalue *EP = ALLOC(evalue);
4367 int i;
4369 value_init(EP->d);
4371 if (EVALUE_IS_ZERO(*X) || dim == 0) {
4372 evalue_set(EP, c->p[0], c->p[dim+1]);
4373 reduce_constant(EP);
4374 return EP;
4377 evalue_set(EP, c->p[dim], c->p[dim+1]);
4379 value_init(EC.d);
4380 evalue_set(&EC, c->p[dim], c->p[dim+1]);
4382 for (i = dim-1; i >= 0; --i) {
4383 emul(X, EP);
4384 value_assign(EC.x.n, c->p[i]);
4385 eadd(&EC, EP);
4387 free_evalue_refs(&EC);
4388 return EP;
4391 /* Create an evalue from an array of pairs of domains and evalues. */
4392 evalue *evalue_from_section_array(struct evalue_section *s, int n)
4394 int i;
4395 evalue *res;
4397 res = ALLOC(evalue);
4398 value_init(res->d);
4400 if (n == 0)
4401 evalue_set_si(res, 0, 1);
4402 else {
4403 value_set_si(res->d, 0);
4404 res->x.p = new_enode(partition, 2*n, s[0].D->Dimension);
4405 for (i = 0; i < n; ++i) {
4406 EVALUE_SET_DOMAIN(res->x.p->arr[2*i], s[i].D);
4407 value_clear(res->x.p->arr[2*i+1].d);
4408 res->x.p->arr[2*i+1] = *s[i].E;
4409 free(s[i].E);
4412 return res;
4415 /* shift variables (>= first, 0-based) in polynomial n up (may be negative) */
4416 void evalue_shift_variables(evalue *e, int first, int n)
4418 int i;
4419 if (value_notzero_p(e->d))
4420 return;
4421 assert(e->x.p->type == polynomial ||
4422 e->x.p->type == flooring ||
4423 e->x.p->type == fractional);
4424 if (e->x.p->type == polynomial && e->x.p->pos >= first+1) {
4425 assert(e->x.p->pos + n >= 1);
4426 e->x.p->pos += n;
4428 for (i = 0; i < e->x.p->size; ++i)
4429 evalue_shift_variables(&e->x.p->arr[i], first, n);
4432 static const evalue *outer_floor(evalue *e, const evalue *outer)
4434 int i;
4436 if (value_notzero_p(e->d))
4437 return outer;
4438 switch (e->x.p->type) {
4439 case flooring:
4440 if (!outer || evalue_level_cmp(outer, &e->x.p->arr[0]) > 0)
4441 return &e->x.p->arr[0];
4442 else
4443 return outer;
4444 case polynomial:
4445 case fractional:
4446 case relation:
4447 for (i = type_offset(e->x.p); i < e->x.p->size; ++i)
4448 outer = outer_floor(&e->x.p->arr[i], outer);
4449 return outer;
4450 case partition:
4451 for (i = 0; i < e->x.p->size/2; ++i)
4452 outer = outer_floor(&e->x.p->arr[2*i+1], outer);
4453 return outer;
4454 default:
4455 assert(0);
4459 /* Find and return outermost floor argument or NULL if e has no floors */
4460 evalue *evalue_outer_floor(evalue *e)
4462 const evalue *floor = outer_floor(e, NULL);
4463 return floor ? evalue_dup(floor): NULL;
4466 static void evalue_set_to_zero(evalue *e)
4468 if (EVALUE_IS_ZERO(*e))
4469 return;
4470 if (value_zero_p(e->d)) {
4471 free_evalue_refs(e);
4472 value_init(e->d);
4473 value_init(e->x.n);
4475 value_set_si(e->d, 1);
4476 value_set_si(e->x.n, 0);
4479 /* Replace (outer) floor with argument "floor" by variable "var" (0-based)
4480 * and drop terms not containing the floor.
4481 * Returns true if e contains the floor.
4483 int evalue_replace_floor(evalue *e, const evalue *floor, int var)
4485 int i;
4486 int contains = 0;
4487 int reorder = 0;
4489 if (value_notzero_p(e->d))
4490 return 0;
4491 switch (e->x.p->type) {
4492 case flooring:
4493 if (!eequal(floor, &e->x.p->arr[0]))
4494 return 0;
4495 e->x.p->type = polynomial;
4496 e->x.p->pos = 1 + var;
4497 e->x.p->size--;
4498 free_evalue_refs(&e->x.p->arr[0]);
4499 for (i = 0; i < e->x.p->size; ++i)
4500 e->x.p->arr[i] = e->x.p->arr[i+1];
4501 evalue_set_to_zero(&e->x.p->arr[0]);
4502 return 1;
4503 case polynomial:
4504 case fractional:
4505 case relation:
4506 for (i = type_offset(e->x.p); i < e->x.p->size; ++i) {
4507 int c = evalue_replace_floor(&e->x.p->arr[i], floor, var);
4508 contains |= c;
4509 if (!c)
4510 evalue_set_to_zero(&e->x.p->arr[i]);
4511 if (c && !reorder && evalue_level_cmp(&e->x.p->arr[i], e) < 0)
4512 reorder = 1;
4514 evalue_reduce_size(e);
4515 if (reorder)
4516 evalue_reorder_terms(e);
4517 return contains;
4518 case partition:
4519 default:
4520 assert(0);
4524 /* Replace (outer) floor with argument "floor" by variable zero */
4525 void evalue_drop_floor(evalue *e, const evalue *floor)
4527 int i;
4528 enode *p;
4530 if (value_notzero_p(e->d))
4531 return;
4532 switch (e->x.p->type) {
4533 case flooring:
4534 if (!eequal(floor, &e->x.p->arr[0]))
4535 return;
4536 p = e->x.p;
4537 free_evalue_refs(&p->arr[0]);
4538 for (i = 2; i < p->size; ++i)
4539 free_evalue_refs(&p->arr[i]);
4540 value_clear(e->d);
4541 *e = p->arr[1];
4542 free(p);
4543 break;
4544 case polynomial:
4545 case fractional:
4546 case relation:
4547 for (i = type_offset(e->x.p); i < e->x.p->size; ++i)
4548 evalue_drop_floor(&e->x.p->arr[i], floor);
4549 evalue_reduce_size(e);
4550 break;
4551 case partition:
4552 default:
4553 assert(0);