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 /***********************************************************************/
16 #include <barvinok/evalue.h>
17 #include <barvinok/barvinok.h>
18 #include <barvinok/util.h>
20 #ifndef value_pmodulus
21 #define value_pmodulus(ref,val1,val2) (mpz_fdiv_r((ref),(val1),(val2)))
24 #define ALLOC(type) (type*)malloc(sizeof(type))
25 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
28 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
30 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
33 void evalue_set_si(evalue
*ev
, int n
, int d
) {
34 value_set_si(ev
->d
, d
);
36 value_set_si(ev
->x
.n
, n
);
39 void evalue_set(evalue
*ev
, Value n
, Value d
) {
40 value_assign(ev
->d
, d
);
42 value_assign(ev
->x
.n
, n
);
47 evalue
*EP
= ALLOC(evalue
);
49 evalue_set_si(EP
, 0, 1);
55 evalue
*EP
= ALLOC(evalue
);
57 value_set_si(EP
->d
, -2);
62 /* returns an evalue that corresponds to
66 evalue
*evalue_var(int var
)
68 evalue
*EP
= ALLOC(evalue
);
70 value_set_si(EP
->d
,0);
71 EP
->x
.p
= new_enode(polynomial
, 2, var
+ 1);
72 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
73 evalue_set_si(&EP
->x
.p
->arr
[1], 1, 1);
77 void aep_evalue(evalue
*e
, int *ref
) {
82 if (value_notzero_p(e
->d
))
83 return; /* a rational number, its already reduced */
85 return; /* hum... an overflow probably occured */
87 /* First check the components of p */
88 for (i
=0;i
<p
->size
;i
++)
89 aep_evalue(&p
->arr
[i
],ref
);
96 p
->pos
= ref
[p
->pos
-1]+1;
102 void addeliminatedparams_evalue(evalue
*e
,Matrix
*CT
) {
108 if (value_notzero_p(e
->d
))
109 return; /* a rational number, its already reduced */
111 return; /* hum... an overflow probably occured */
114 ref
= (int *)malloc(sizeof(int)*(CT
->NbRows
-1));
115 for(i
=0;i
<CT
->NbRows
-1;i
++)
116 for(j
=0;j
<CT
->NbColumns
;j
++)
117 if(value_notzero_p(CT
->p
[i
][j
])) {
122 /* Transform the references in e, using ref */
126 } /* addeliminatedparams_evalue */
128 static void addeliminatedparams_partition(enode
*p
, Matrix
*CT
, Polyhedron
*CEq
,
129 unsigned nparam
, unsigned MaxRays
)
132 assert(p
->type
== partition
);
135 for (i
= 0; i
< p
->size
/2; i
++) {
136 Polyhedron
*D
= EVALUE_DOMAIN(p
->arr
[2*i
]);
137 Polyhedron
*T
= DomainPreimage(D
, CT
, MaxRays
);
141 T
= DomainIntersection(D
, CEq
, MaxRays
);
144 EVALUE_SET_DOMAIN(p
->arr
[2*i
], T
);
148 void addeliminatedparams_enum(evalue
*e
, Matrix
*CT
, Polyhedron
*CEq
,
149 unsigned MaxRays
, unsigned nparam
)
154 if (CT
->NbRows
== CT
->NbColumns
)
157 if (EVALUE_IS_ZERO(*e
))
160 if (value_notzero_p(e
->d
)) {
163 value_set_si(res
.d
, 0);
164 res
.x
.p
= new_enode(partition
, 2, nparam
);
165 EVALUE_SET_DOMAIN(res
.x
.p
->arr
[0],
166 DomainConstraintSimplify(Polyhedron_Copy(CEq
), MaxRays
));
167 value_clear(res
.x
.p
->arr
[1].d
);
168 res
.x
.p
->arr
[1] = *e
;
176 addeliminatedparams_partition(p
, CT
, CEq
, nparam
, MaxRays
);
177 for (i
= 0; i
< p
->size
/2; i
++)
178 addeliminatedparams_evalue(&p
->arr
[2*i
+1], CT
);
181 static int mod_rational_smaller(evalue
*e1
, evalue
*e2
)
189 assert(value_notzero_p(e1
->d
));
190 assert(value_notzero_p(e2
->d
));
191 value_multiply(m
, e1
->x
.n
, e2
->d
);
192 value_multiply(m2
, e2
->x
.n
, e1
->d
);
195 else if (value_gt(m
, m2
))
205 static int mod_term_smaller_r(evalue
*e1
, evalue
*e2
)
207 if (value_notzero_p(e1
->d
)) {
209 if (value_zero_p(e2
->d
))
211 r
= mod_rational_smaller(e1
, e2
);
212 return r
== -1 ? 0 : r
;
214 if (value_notzero_p(e2
->d
))
216 if (e1
->x
.p
->pos
< e2
->x
.p
->pos
)
218 else if (e1
->x
.p
->pos
> e2
->x
.p
->pos
)
221 int r
= mod_rational_smaller(&e1
->x
.p
->arr
[1], &e2
->x
.p
->arr
[1]);
223 ? mod_term_smaller_r(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0])
228 static int mod_term_smaller(const evalue
*e1
, const evalue
*e2
)
230 assert(value_zero_p(e1
->d
));
231 assert(value_zero_p(e2
->d
));
232 assert(e1
->x
.p
->type
== fractional
|| e1
->x
.p
->type
== flooring
);
233 assert(e2
->x
.p
->type
== fractional
|| e2
->x
.p
->type
== flooring
);
234 return mod_term_smaller_r(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]);
237 static void check_order(const evalue
*e
)
242 if (value_notzero_p(e
->d
))
245 switch (e
->x
.p
->type
) {
247 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
248 check_order(&e
->x
.p
->arr
[2*i
+1]);
251 for (i
= 1; i
< e
->x
.p
->size
; ++i
) {
253 if (value_notzero_p(a
->d
))
255 switch (a
->x
.p
->type
) {
257 assert(mod_term_smaller(&e
->x
.p
->arr
[0], &a
->x
.p
->arr
[0]));
266 for (i
= 0; i
< e
->x
.p
->size
; ++i
) {
268 if (value_notzero_p(a
->d
))
270 switch (a
->x
.p
->type
) {
272 assert(e
->x
.p
->pos
< a
->x
.p
->pos
);
283 for (i
= 1; i
< e
->x
.p
->size
; ++i
) {
285 if (value_notzero_p(a
->d
))
287 switch (a
->x
.p
->type
) {
298 /* Negative pos means inequality */
299 /* s is negative of substitution if m is not zero */
308 struct fixed_param
*fixed
;
313 static int relations_depth(evalue
*e
)
318 value_zero_p(e
->d
) && e
->x
.p
->type
== relation
;
319 e
= &e
->x
.p
->arr
[1], ++d
);
323 static void poly_denom_not_constant(evalue
**pp
, Value
*d
)
328 while (value_zero_p(p
->d
)) {
329 assert(p
->x
.p
->type
== polynomial
);
330 assert(p
->x
.p
->size
== 2);
331 assert(value_notzero_p(p
->x
.p
->arr
[1].d
));
332 value_lcm(*d
, *d
, p
->x
.p
->arr
[1].d
);
338 static void poly_denom(evalue
*p
, Value
*d
)
340 poly_denom_not_constant(&p
, d
);
341 value_lcm(*d
, *d
, p
->d
);
344 static void realloc_substitution(struct subst
*s
, int d
)
346 struct fixed_param
*n
;
349 for (i
= 0; i
< s
->n
; ++i
)
356 static int add_modulo_substitution(struct subst
*s
, evalue
*r
)
362 assert(value_zero_p(r
->d
) && r
->x
.p
->type
== relation
);
365 /* May have been reduced already */
366 if (value_notzero_p(m
->d
))
369 assert(value_zero_p(m
->d
) && m
->x
.p
->type
== fractional
);
370 assert(m
->x
.p
->size
== 3);
372 /* fractional was inverted during reduction
373 * invert it back and move constant in
375 if (!EVALUE_IS_ONE(m
->x
.p
->arr
[2])) {
376 assert(value_pos_p(m
->x
.p
->arr
[2].d
));
377 assert(value_mone_p(m
->x
.p
->arr
[2].x
.n
));
378 value_set_si(m
->x
.p
->arr
[2].x
.n
, 1);
379 value_increment(m
->x
.p
->arr
[1].x
.n
, m
->x
.p
->arr
[1].x
.n
);
380 assert(value_eq(m
->x
.p
->arr
[1].x
.n
, m
->x
.p
->arr
[1].d
));
381 value_set_si(m
->x
.p
->arr
[1].x
.n
, 1);
382 eadd(&m
->x
.p
->arr
[1], &m
->x
.p
->arr
[0]);
383 value_set_si(m
->x
.p
->arr
[1].x
.n
, 0);
384 value_set_si(m
->x
.p
->arr
[1].d
, 1);
387 /* Oops. Nested identical relations. */
388 if (!EVALUE_IS_ZERO(m
->x
.p
->arr
[1]))
391 if (s
->n
>= s
->max
) {
392 int d
= relations_depth(r
);
393 realloc_substitution(s
, d
);
397 assert(value_zero_p(p
->d
) && p
->x
.p
->type
== polynomial
);
398 assert(p
->x
.p
->size
== 2);
401 assert(value_pos_p(f
->x
.n
));
403 value_init(s
->fixed
[s
->n
].m
);
404 value_assign(s
->fixed
[s
->n
].m
, f
->d
);
405 s
->fixed
[s
->n
].pos
= p
->x
.p
->pos
;
406 value_init(s
->fixed
[s
->n
].d
);
407 value_assign(s
->fixed
[s
->n
].d
, f
->x
.n
);
408 value_init(s
->fixed
[s
->n
].s
.d
);
409 evalue_copy(&s
->fixed
[s
->n
].s
, &p
->x
.p
->arr
[0]);
415 static int type_offset(enode
*p
)
417 return p
->type
== fractional
? 1 :
418 p
->type
== flooring
? 1 :
419 p
->type
== relation
? 1 : 0;
422 static void reorder_terms_about(enode
*p
, evalue
*v
)
425 int offset
= type_offset(p
);
427 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
429 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
430 free_evalue_refs(&(p
->arr
[i
]));
436 static void reorder_terms(evalue
*e
)
441 assert(value_zero_p(e
->d
));
443 assert(p
->type
== fractional
); /* for now */
446 value_set_si(f
.d
, 0);
447 f
.x
.p
= new_enode(fractional
, 3, -1);
448 value_clear(f
.x
.p
->arr
[0].d
);
449 f
.x
.p
->arr
[0] = p
->arr
[0];
450 evalue_set_si(&f
.x
.p
->arr
[1], 0, 1);
451 evalue_set_si(&f
.x
.p
->arr
[2], 1, 1);
452 reorder_terms_about(p
, &f
);
458 void _reduce_evalue (evalue
*e
, struct subst
*s
, int fract
) {
464 if (value_notzero_p(e
->d
)) {
466 mpz_fdiv_r(e
->x
.n
, e
->x
.n
, e
->d
);
467 return; /* a rational number, its already reduced */
471 return; /* hum... an overflow probably occured */
473 /* First reduce the components of p */
474 add
= p
->type
== relation
;
475 for (i
=0; i
<p
->size
; i
++) {
477 add
= add_modulo_substitution(s
, e
);
479 if (i
== 0 && p
->type
==fractional
)
480 _reduce_evalue(&p
->arr
[i
], s
, 1);
482 _reduce_evalue(&p
->arr
[i
], s
, fract
);
484 if (add
&& i
== p
->size
-1) {
486 value_clear(s
->fixed
[s
->n
].m
);
487 value_clear(s
->fixed
[s
->n
].d
);
488 free_evalue_refs(&s
->fixed
[s
->n
].s
);
489 } else if (add
&& i
== 1)
490 s
->fixed
[s
->n
-1].pos
*= -1;
493 if (p
->type
==periodic
) {
495 /* Try to reduce the period */
496 for (i
=1; i
<=(p
->size
)/2; i
++) {
497 if ((p
->size
% i
)==0) {
499 /* Can we reduce the size to i ? */
501 for (k
=j
+i
; k
<e
->x
.p
->size
; k
+=i
)
502 if (!eequal(&p
->arr
[j
], &p
->arr
[k
])) goto you_lose
;
505 for (j
=i
; j
<p
->size
; j
++) free_evalue_refs(&p
->arr
[j
]);
509 you_lose
: /* OK, lets not do it */
514 /* Try to reduce its strength */
517 memcpy(e
,&p
->arr
[0],sizeof(evalue
));
521 else if (p
->type
==polynomial
) {
522 for (k
= 0; s
&& k
< s
->n
; ++k
) {
523 if (s
->fixed
[k
].pos
== p
->pos
) {
524 int divide
= value_notone_p(s
->fixed
[k
].d
);
527 if (value_notzero_p(s
->fixed
[k
].m
)) {
530 assert(p
->size
== 2);
531 if (divide
&& value_ne(s
->fixed
[k
].d
, p
->arr
[1].x
.n
))
533 if (!mpz_divisible_p(s
->fixed
[k
].m
, p
->arr
[1].d
))
540 value_assign(d
.d
, s
->fixed
[k
].d
);
542 if (value_notzero_p(s
->fixed
[k
].m
))
543 value_oppose(d
.x
.n
, s
->fixed
[k
].m
);
545 value_set_si(d
.x
.n
, 1);
548 for (i
=p
->size
-1;i
>=1;i
--) {
549 emul(&s
->fixed
[k
].s
, &p
->arr
[i
]);
551 emul(&d
, &p
->arr
[i
]);
552 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
553 free_evalue_refs(&(p
->arr
[i
]));
556 _reduce_evalue(&p
->arr
[0], s
, fract
);
559 free_evalue_refs(&d
);
565 /* Try to reduce the degree */
566 for (i
=p
->size
-1;i
>=1;i
--) {
567 if (!(value_one_p(p
->arr
[i
].d
) && value_zero_p(p
->arr
[i
].x
.n
)))
569 /* Zero coefficient */
570 free_evalue_refs(&(p
->arr
[i
]));
575 /* Try to reduce its strength */
578 memcpy(e
,&p
->arr
[0],sizeof(evalue
));
582 else if (p
->type
==fractional
) {
586 if (value_notzero_p(p
->arr
[0].d
)) {
588 value_assign(v
.d
, p
->arr
[0].d
);
590 mpz_fdiv_r(v
.x
.n
, p
->arr
[0].x
.n
, p
->arr
[0].d
);
595 evalue
*pp
= &p
->arr
[0];
596 assert(value_zero_p(pp
->d
) && pp
->x
.p
->type
== polynomial
);
597 assert(pp
->x
.p
->size
== 2);
599 /* search for exact duplicate among the modulo inequalities */
601 f
= &pp
->x
.p
->arr
[1];
602 for (k
= 0; s
&& k
< s
->n
; ++k
) {
603 if (-s
->fixed
[k
].pos
== pp
->x
.p
->pos
&&
604 value_eq(s
->fixed
[k
].d
, f
->x
.n
) &&
605 value_eq(s
->fixed
[k
].m
, f
->d
) &&
606 eequal(&s
->fixed
[k
].s
, &pp
->x
.p
->arr
[0]))
613 /* replace { E/m } by { (E-1)/m } + 1/m */
618 evalue_set_si(&extra
, 1, 1);
619 value_assign(extra
.d
, g
);
620 eadd(&extra
, &v
.x
.p
->arr
[1]);
621 free_evalue_refs(&extra
);
623 /* We've been going in circles; stop now */
624 if (value_ge(v
.x
.p
->arr
[1].x
.n
, v
.x
.p
->arr
[1].d
)) {
625 free_evalue_refs(&v
);
627 evalue_set_si(&v
, 0, 1);
632 value_set_si(v
.d
, 0);
633 v
.x
.p
= new_enode(fractional
, 3, -1);
634 evalue_set_si(&v
.x
.p
->arr
[1], 1, 1);
635 value_assign(v
.x
.p
->arr
[1].d
, g
);
636 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
637 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
640 for (f
= &v
.x
.p
->arr
[0]; value_zero_p(f
->d
);
643 value_division(f
->d
, g
, f
->d
);
644 value_multiply(f
->x
.n
, f
->x
.n
, f
->d
);
645 value_assign(f
->d
, g
);
646 value_decrement(f
->x
.n
, f
->x
.n
);
647 mpz_fdiv_r(f
->x
.n
, f
->x
.n
, f
->d
);
649 value_gcd(g
, f
->d
, f
->x
.n
);
650 value_division(f
->d
, f
->d
, g
);
651 value_division(f
->x
.n
, f
->x
.n
, g
);
660 /* reduction may have made this fractional arg smaller */
661 i
= reorder
? p
->size
: 1;
662 for ( ; i
< p
->size
; ++i
)
663 if (value_zero_p(p
->arr
[i
].d
) &&
664 p
->arr
[i
].x
.p
->type
== fractional
&&
665 !mod_term_smaller(e
, &p
->arr
[i
]))
669 value_set_si(v
.d
, 0);
670 v
.x
.p
= new_enode(fractional
, 3, -1);
671 evalue_set_si(&v
.x
.p
->arr
[1], 0, 1);
672 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
673 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
681 evalue
*pp
= &p
->arr
[0];
684 poly_denom_not_constant(&pp
, &m
);
685 mpz_fdiv_r(r
, m
, pp
->d
);
686 if (value_notzero_p(r
)) {
688 value_set_si(v
.d
, 0);
689 v
.x
.p
= new_enode(fractional
, 3, -1);
691 value_multiply(r
, m
, pp
->x
.n
);
692 value_multiply(v
.x
.p
->arr
[1].d
, m
, pp
->d
);
693 value_init(v
.x
.p
->arr
[1].x
.n
);
694 mpz_fdiv_r(v
.x
.p
->arr
[1].x
.n
, r
, pp
->d
);
695 mpz_fdiv_q(r
, r
, pp
->d
);
697 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
698 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
700 while (value_zero_p(pp
->d
))
701 pp
= &pp
->x
.p
->arr
[0];
703 value_assign(pp
->d
, m
);
704 value_assign(pp
->x
.n
, r
);
706 value_gcd(r
, pp
->d
, pp
->x
.n
);
707 value_division(pp
->d
, pp
->d
, r
);
708 value_division(pp
->x
.n
, pp
->x
.n
, r
);
721 for (pp
= &p
->arr
[0]; value_zero_p(pp
->d
);
722 pp
= &pp
->x
.p
->arr
[0]) {
723 f
= &pp
->x
.p
->arr
[1];
724 assert(value_pos_p(f
->d
));
725 mpz_mul_ui(twice
, f
->x
.n
, 2);
726 if (value_lt(twice
, f
->d
))
728 if (value_eq(twice
, f
->d
))
736 value_set_si(v
.d
, 0);
737 v
.x
.p
= new_enode(fractional
, 3, -1);
738 evalue_set_si(&v
.x
.p
->arr
[1], 0, 1);
739 poly_denom(&p
->arr
[0], &twice
);
740 value_assign(v
.x
.p
->arr
[1].d
, twice
);
741 value_decrement(v
.x
.p
->arr
[1].x
.n
, twice
);
742 evalue_set_si(&v
.x
.p
->arr
[2], -1, 1);
743 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
745 for (pp
= &v
.x
.p
->arr
[0]; value_zero_p(pp
->d
);
746 pp
= &pp
->x
.p
->arr
[0]) {
747 f
= &pp
->x
.p
->arr
[1];
748 value_oppose(f
->x
.n
, f
->x
.n
);
749 mpz_fdiv_r(f
->x
.n
, f
->x
.n
, f
->d
);
751 value_division(pp
->d
, twice
, pp
->d
);
752 value_multiply(pp
->x
.n
, pp
->x
.n
, pp
->d
);
753 value_assign(pp
->d
, twice
);
754 value_oppose(pp
->x
.n
, pp
->x
.n
);
755 value_decrement(pp
->x
.n
, pp
->x
.n
);
756 mpz_fdiv_r(pp
->x
.n
, pp
->x
.n
, pp
->d
);
758 /* Maybe we should do this during reduction of
761 value_gcd(twice
, pp
->d
, pp
->x
.n
);
762 value_division(pp
->d
, pp
->d
, twice
);
763 value_division(pp
->x
.n
, pp
->x
.n
, twice
);
773 reorder_terms_about(p
, &v
);
774 _reduce_evalue(&p
->arr
[1], s
, fract
);
777 /* Try to reduce the degree */
778 for (i
=p
->size
-1;i
>=2;i
--) {
779 if (!(value_one_p(p
->arr
[i
].d
) && value_zero_p(p
->arr
[i
].x
.n
)))
781 /* Zero coefficient */
782 free_evalue_refs(&(p
->arr
[i
]));
787 /* Try to reduce its strength */
790 memcpy(e
,&p
->arr
[1],sizeof(evalue
));
791 free_evalue_refs(&(p
->arr
[0]));
795 else if (p
->type
== flooring
) {
796 /* Try to reduce the degree */
797 for (i
=p
->size
-1;i
>=2;i
--) {
798 if (!EVALUE_IS_ZERO(p
->arr
[i
]))
800 /* Zero coefficient */
801 free_evalue_refs(&(p
->arr
[i
]));
806 /* Try to reduce its strength */
809 memcpy(e
,&p
->arr
[1],sizeof(evalue
));
810 free_evalue_refs(&(p
->arr
[0]));
814 else if (p
->type
== relation
) {
815 if (p
->size
== 3 && eequal(&p
->arr
[1], &p
->arr
[2])) {
816 free_evalue_refs(&(p
->arr
[2]));
817 free_evalue_refs(&(p
->arr
[0]));
824 if (p
->size
== 3 && EVALUE_IS_ZERO(p
->arr
[2])) {
825 free_evalue_refs(&(p
->arr
[2]));
828 if (p
->size
== 2 && EVALUE_IS_ZERO(p
->arr
[1])) {
829 free_evalue_refs(&(p
->arr
[1]));
830 free_evalue_refs(&(p
->arr
[0]));
831 evalue_set_si(e
, 0, 1);
838 /* Relation was reduced by means of an identical
839 * inequality => remove
841 if (value_zero_p(m
->d
) && !EVALUE_IS_ZERO(m
->x
.p
->arr
[1]))
844 if (reduced
|| value_notzero_p(p
->arr
[0].d
)) {
845 if (!reduced
&& value_zero_p(p
->arr
[0].x
.n
)) {
847 memcpy(e
,&p
->arr
[1],sizeof(evalue
));
849 free_evalue_refs(&(p
->arr
[2]));
853 memcpy(e
,&p
->arr
[2],sizeof(evalue
));
855 evalue_set_si(e
, 0, 1);
856 free_evalue_refs(&(p
->arr
[1]));
858 free_evalue_refs(&(p
->arr
[0]));
864 } /* reduce_evalue */
866 static void add_substitution(struct subst
*s
, Value
*row
, unsigned dim
)
871 for (k
= 0; k
< dim
; ++k
)
872 if (value_notzero_p(row
[k
+1]))
875 Vector_Normalize_Positive(row
+1, dim
+1, k
);
876 assert(s
->n
< s
->max
);
877 value_init(s
->fixed
[s
->n
].d
);
878 value_init(s
->fixed
[s
->n
].m
);
879 value_assign(s
->fixed
[s
->n
].d
, row
[k
+1]);
880 s
->fixed
[s
->n
].pos
= k
+1;
881 value_set_si(s
->fixed
[s
->n
].m
, 0);
882 r
= &s
->fixed
[s
->n
].s
;
884 for (l
= k
+1; l
< dim
; ++l
)
885 if (value_notzero_p(row
[l
+1])) {
886 value_set_si(r
->d
, 0);
887 r
->x
.p
= new_enode(polynomial
, 2, l
+ 1);
888 value_init(r
->x
.p
->arr
[1].x
.n
);
889 value_oppose(r
->x
.p
->arr
[1].x
.n
, row
[l
+1]);
890 value_set_si(r
->x
.p
->arr
[1].d
, 1);
894 value_oppose(r
->x
.n
, row
[dim
+1]);
895 value_set_si(r
->d
, 1);
899 static void _reduce_evalue_in_domain(evalue
*e
, Polyhedron
*D
, struct subst
*s
)
902 Polyhedron
*orig
= D
;
907 D
= DomainConvex(D
, 0);
908 /* We don't perform any substitutions if the domain is a union.
909 * We may therefore miss out on some possible simplifications,
910 * e.g., if a variable is always even in the whole union,
911 * while there is a relation in the evalue that evaluates
912 * to zero for even values of the variable.
914 if (!D
->next
&& D
->NbEq
) {
918 realloc_substitution(s
, dim
);
920 int d
= relations_depth(e
);
922 NALLOC(s
->fixed
, s
->max
);
925 for (j
= 0; j
< D
->NbEq
; ++j
)
926 add_substitution(s
, D
->Constraint
[j
], dim
);
930 _reduce_evalue(e
, s
, 0);
933 for (j
= 0; j
< s
->n
; ++j
) {
934 value_clear(s
->fixed
[j
].d
);
935 value_clear(s
->fixed
[j
].m
);
936 free_evalue_refs(&s
->fixed
[j
].s
);
941 void reduce_evalue_in_domain(evalue
*e
, Polyhedron
*D
)
943 struct subst s
= { NULL
, 0, 0 };
945 if (EVALUE_IS_ZERO(*e
))
949 evalue_set_si(e
, 0, 1);
952 _reduce_evalue_in_domain(e
, D
, &s
);
957 void reduce_evalue (evalue
*e
) {
958 struct subst s
= { NULL
, 0, 0 };
960 if (value_notzero_p(e
->d
))
961 return; /* a rational number, its already reduced */
963 if (e
->x
.p
->type
== partition
) {
966 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
967 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
969 /* This shouldn't really happen;
970 * Empty domains should not be added.
972 POL_ENSURE_VERTICES(D
);
974 _reduce_evalue_in_domain(&e
->x
.p
->arr
[2*i
+1], D
, &s
);
976 if (emptyQ(D
) || EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
977 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
978 Domain_Free(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]));
979 value_clear(e
->x
.p
->arr
[2*i
].d
);
981 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
982 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
986 if (e
->x
.p
->size
== 0) {
988 evalue_set_si(e
, 0, 1);
991 _reduce_evalue(e
, &s
, 0);
996 static void print_evalue_r(FILE *DST
, const evalue
*e
, const char *const *pname
)
998 if (EVALUE_IS_NAN(*e
)) {
1003 if(value_notzero_p(e
->d
)) {
1004 if(value_notone_p(e
->d
)) {
1005 value_print(DST
,VALUE_FMT
,e
->x
.n
);
1007 value_print(DST
,VALUE_FMT
,e
->d
);
1010 value_print(DST
,VALUE_FMT
,e
->x
.n
);
1014 print_enode(DST
,e
->x
.p
,pname
);
1016 } /* print_evalue */
1018 void print_evalue(FILE *DST
, const evalue
*e
, const char * const *pname
)
1020 print_evalue_r(DST
, e
, pname
);
1021 if (value_notzero_p(e
->d
))
1025 void print_enode(FILE *DST
, enode
*p
, const char *const *pname
)
1030 fprintf(DST
, "NULL");
1036 for (i
=0; i
<p
->size
; i
++) {
1037 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1041 fprintf(DST
, " }\n");
1045 for (i
=p
->size
-1; i
>=0; i
--) {
1046 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1047 if (i
==1) fprintf(DST
, " * %s + ", pname
[p
->pos
-1]);
1049 fprintf(DST
, " * %s^%d + ", pname
[p
->pos
-1], i
);
1051 fprintf(DST
, " )\n");
1055 for (i
=0; i
<p
->size
; i
++) {
1056 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1057 if (i
!=(p
->size
-1)) fprintf(DST
, ", ");
1059 fprintf(DST
," ]_%s", pname
[p
->pos
-1]);
1064 for (i
=p
->size
-1; i
>=1; i
--) {
1065 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1067 fprintf(DST
, " * ");
1068 fprintf(DST
, p
->type
== flooring
? "[" : "{");
1069 print_evalue_r(DST
, &p
->arr
[0], pname
);
1070 fprintf(DST
, p
->type
== flooring
? "]" : "}");
1072 fprintf(DST
, "^%d + ", i
-1);
1074 fprintf(DST
, " + ");
1077 fprintf(DST
, " )\n");
1081 print_evalue_r(DST
, &p
->arr
[0], pname
);
1082 fprintf(DST
, "= 0 ] * \n");
1083 print_evalue_r(DST
, &p
->arr
[1], pname
);
1085 fprintf(DST
, " +\n [ ");
1086 print_evalue_r(DST
, &p
->arr
[0], pname
);
1087 fprintf(DST
, "!= 0 ] * \n");
1088 print_evalue_r(DST
, &p
->arr
[2], pname
);
1092 char **new_names
= NULL
;
1093 const char *const *names
= pname
;
1094 int maxdim
= EVALUE_DOMAIN(p
->arr
[0])->Dimension
;
1095 if (!pname
|| p
->pos
< maxdim
) {
1096 new_names
= ALLOCN(char *, maxdim
);
1097 for (i
= 0; i
< p
->pos
; ++i
) {
1099 new_names
[i
] = (char *)pname
[i
];
1101 new_names
[i
] = ALLOCN(char, 10);
1102 snprintf(new_names
[i
], 10, "%c", 'P'+i
);
1105 for ( ; i
< maxdim
; ++i
) {
1106 new_names
[i
] = ALLOCN(char, 10);
1107 snprintf(new_names
[i
], 10, "_p%d", i
);
1109 names
= (const char**)new_names
;
1112 for (i
=0; i
<p
->size
/2; i
++) {
1113 Print_Domain(DST
, EVALUE_DOMAIN(p
->arr
[2*i
]), names
);
1114 print_evalue_r(DST
, &p
->arr
[2*i
+1], names
);
1115 if (value_notzero_p(p
->arr
[2*i
+1].d
))
1119 if (!pname
|| p
->pos
< maxdim
) {
1120 for (i
= pname
? p
->pos
: 0; i
< maxdim
; ++i
)
1134 * 0 if toplevels of e1 and e2 are at the same level
1135 * <0 if toplevel of e1 should be outside of toplevel of e2
1136 * >0 if toplevel of e2 should be outside of toplevel of e1
1138 static int evalue_level_cmp(const evalue
*e1
, const evalue
*e2
)
1140 if (value_notzero_p(e1
->d
) && value_notzero_p(e2
->d
))
1142 if (value_notzero_p(e1
->d
))
1144 if (value_notzero_p(e2
->d
))
1146 if (e1
->x
.p
->type
== partition
&& e2
->x
.p
->type
== partition
)
1148 if (e1
->x
.p
->type
== partition
)
1150 if (e2
->x
.p
->type
== partition
)
1152 if (e1
->x
.p
->type
== relation
&& e2
->x
.p
->type
== relation
) {
1153 if (eequal(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1155 if (mod_term_smaller(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1160 if (e1
->x
.p
->type
== relation
)
1162 if (e2
->x
.p
->type
== relation
)
1164 if (e1
->x
.p
->type
== polynomial
&& e2
->x
.p
->type
== polynomial
)
1165 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
1166 if (e1
->x
.p
->type
== polynomial
)
1168 if (e2
->x
.p
->type
== polynomial
)
1170 if (e1
->x
.p
->type
== periodic
&& e2
->x
.p
->type
== periodic
)
1171 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
1172 assert(e1
->x
.p
->type
!= periodic
);
1173 assert(e2
->x
.p
->type
!= periodic
);
1174 assert(e1
->x
.p
->type
== e2
->x
.p
->type
);
1175 if (eequal(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1177 if (mod_term_smaller(e1
, e2
))
1183 static void eadd_rev(const evalue
*e1
, evalue
*res
)
1187 evalue_copy(&ev
, e1
);
1189 free_evalue_refs(res
);
1193 static void eadd_rev_cst(const evalue
*e1
, evalue
*res
)
1197 evalue_copy(&ev
, e1
);
1198 eadd(res
, &ev
.x
.p
->arr
[type_offset(ev
.x
.p
)]);
1199 free_evalue_refs(res
);
1203 struct section
{ Polyhedron
* D
; evalue E
; };
1205 void eadd_partitions(const evalue
*e1
, evalue
*res
)
1210 s
= (struct section
*)
1211 malloc((e1
->x
.p
->size
/2+1) * (res
->x
.p
->size
/2+1) *
1212 sizeof(struct section
));
1214 assert(e1
->x
.p
->pos
== res
->x
.p
->pos
);
1215 assert(e1
->x
.p
->pos
== EVALUE_DOMAIN(e1
->x
.p
->arr
[0])->Dimension
);
1216 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1219 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1220 assert(res
->x
.p
->size
>= 2);
1221 fd
= DomainDifference(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1222 EVALUE_DOMAIN(res
->x
.p
->arr
[0]), 0);
1224 for (i
= 1; i
< res
->x
.p
->size
/2; ++i
) {
1226 fd
= DomainDifference(fd
, EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1231 fd
= DomainConstraintSimplify(fd
, 0);
1236 value_init(s
[n
].E
.d
);
1237 evalue_copy(&s
[n
].E
, &e1
->x
.p
->arr
[2*j
+1]);
1241 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
) {
1242 fd
= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]);
1243 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1245 d
= DomainIntersection(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1246 EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1247 d
= DomainConstraintSimplify(d
, 0);
1253 fd
= DomainDifference(fd
, EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]), 0);
1254 if (t
!= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]))
1256 value_init(s
[n
].E
.d
);
1257 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*i
+1]);
1258 eadd(&e1
->x
.p
->arr
[2*j
+1], &s
[n
].E
);
1263 s
[n
].E
= res
->x
.p
->arr
[2*i
+1];
1267 free_evalue_refs(&res
->x
.p
->arr
[2*i
+1]);
1270 if (fd
!= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]))
1271 Domain_Free(EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]));
1272 value_clear(res
->x
.p
->arr
[2*i
].d
);
1277 res
->x
.p
= new_enode(partition
, 2*n
, e1
->x
.p
->pos
);
1278 for (j
= 0; j
< n
; ++j
) {
1279 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1280 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1281 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1287 static void explicit_complement(evalue
*res
)
1289 enode
*rel
= new_enode(relation
, 3, 0);
1291 value_clear(rel
->arr
[0].d
);
1292 rel
->arr
[0] = res
->x
.p
->arr
[0];
1293 value_clear(rel
->arr
[1].d
);
1294 rel
->arr
[1] = res
->x
.p
->arr
[1];
1295 value_set_si(rel
->arr
[2].d
, 1);
1296 value_init(rel
->arr
[2].x
.n
);
1297 value_set_si(rel
->arr
[2].x
.n
, 0);
1302 static void reduce_constant(evalue
*e
)
1307 value_gcd(g
, e
->x
.n
, e
->d
);
1308 if (value_notone_p(g
)) {
1309 value_division(e
->d
, e
->d
,g
);
1310 value_division(e
->x
.n
, e
->x
.n
,g
);
1315 /* Add two rational numbers */
1316 static void eadd_rationals(const evalue
*e1
, evalue
*res
)
1318 if (value_eq(e1
->d
, res
->d
))
1319 value_addto(res
->x
.n
, res
->x
.n
, e1
->x
.n
);
1321 value_multiply(res
->x
.n
, res
->x
.n
, e1
->d
);
1322 value_addmul(res
->x
.n
, e1
->x
.n
, res
->d
);
1323 value_multiply(res
->d
,e1
->d
,res
->d
);
1325 reduce_constant(res
);
1328 static void eadd_relations(const evalue
*e1
, evalue
*res
)
1332 if (res
->x
.p
->size
< 3 && e1
->x
.p
->size
== 3)
1333 explicit_complement(res
);
1334 for (i
= 1; i
< e1
->x
.p
->size
; ++i
)
1335 eadd(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1338 static void eadd_arrays(const evalue
*e1
, evalue
*res
, int n
)
1342 // add any element in e1 to the corresponding element in res
1343 i
= type_offset(res
->x
.p
);
1345 assert(eequal(&e1
->x
.p
->arr
[0], &res
->x
.p
->arr
[0]));
1347 eadd(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1350 static void eadd_poly(const evalue
*e1
, evalue
*res
)
1352 if (e1
->x
.p
->size
> res
->x
.p
->size
)
1355 eadd_arrays(e1
, res
, e1
->x
.p
->size
);
1358 static void eadd_periodics(const evalue
*e1
, evalue
*res
)
1365 if (e1
->x
.p
->size
== res
->x
.p
->size
) {
1366 eadd_arrays(e1
, res
, e1
->x
.p
->size
);
1369 /* you have to create a new evalue 'ne' in whitch size equals to the lcm
1370 * of the sizes of e1 and res, then to copy res periodicaly in ne, after
1371 * to add periodicaly elements of e1 to elements of ne, and finaly to
1379 value_set_si(ex
, e1
->x
.p
->size
);
1380 value_set_si(ey
, res
->x
.p
->size
);
1381 value_lcm(ep
, ex
, ey
);
1382 p
= (int)mpz_get_si(ep
);
1383 ne
= (evalue
*) malloc(sizeof(evalue
));
1385 value_set_si(ne
->d
, 0);
1387 ne
->x
.p
= new_enode(res
->x
.p
->type
,p
, res
->x
.p
->pos
);
1388 for (i
= 0; i
< p
; i
++)
1389 evalue_copy(&ne
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
%y
]);
1390 for (i
= 0; i
< p
; i
++)
1391 eadd(&e1
->x
.p
->arr
[i
%x
], &ne
->x
.p
->arr
[i
]);
1393 value_assign(res
->d
, ne
->d
);
1400 void evalue_assign(evalue
*dst
, const evalue
*src
)
1402 if (value_pos_p(dst
->d
) && value_pos_p(src
->d
)) {
1403 value_assign(dst
->d
, src
->d
);
1404 value_assign(dst
->x
.n
, src
->x
.n
);
1407 free_evalue_refs(dst
);
1409 evalue_copy(dst
, src
);
1412 void eadd(const evalue
*e1
, evalue
*res
)
1416 if (EVALUE_IS_ZERO(*e1
))
1419 if (EVALUE_IS_NAN(*res
))
1422 if (EVALUE_IS_NAN(*e1
)) {
1423 evalue_assign(res
, e1
);
1427 if (EVALUE_IS_ZERO(*res
)) {
1428 evalue_assign(res
, e1
);
1432 cmp
= evalue_level_cmp(res
, e1
);
1434 switch (e1
->x
.p
->type
) {
1438 eadd_rev_cst(e1
, res
);
1443 } else if (cmp
== 0) {
1444 if (value_notzero_p(e1
->d
)) {
1445 eadd_rationals(e1
, res
);
1447 switch (e1
->x
.p
->type
) {
1449 eadd_partitions(e1
, res
);
1452 eadd_relations(e1
, res
);
1455 assert(e1
->x
.p
->size
== res
->x
.p
->size
);
1462 eadd_periodics(e1
, res
);
1470 switch (res
->x
.p
->type
) {
1474 /* Add to the constant term of a polynomial */
1475 eadd(e1
, &res
->x
.p
->arr
[type_offset(res
->x
.p
)]);
1478 /* Add to all elements of a periodic number */
1479 for (i
= 0; i
< res
->x
.p
->size
; i
++)
1480 eadd(e1
, &res
->x
.p
->arr
[i
]);
1483 fprintf(stderr
, "eadd: cannot add const with vector\n");
1488 /* Create (zero) complement if needed */
1489 if (res
->x
.p
->size
< 3)
1490 explicit_complement(res
);
1491 for (i
= 1; i
< res
->x
.p
->size
; ++i
)
1492 eadd(e1
, &res
->x
.p
->arr
[i
]);
1500 static void emul_rev(const evalue
*e1
, evalue
*res
)
1504 evalue_copy(&ev
, e1
);
1506 free_evalue_refs(res
);
1510 static void emul_poly(const evalue
*e1
, evalue
*res
)
1512 int i
, j
, offset
= type_offset(res
->x
.p
);
1515 int size
= (e1
->x
.p
->size
+ res
->x
.p
->size
- offset
- 1);
1517 p
= new_enode(res
->x
.p
->type
, size
, res
->x
.p
->pos
);
1519 for (i
= offset
; i
< e1
->x
.p
->size
-1; ++i
)
1520 if (!EVALUE_IS_ZERO(e1
->x
.p
->arr
[i
]))
1523 /* special case pure power */
1524 if (i
== e1
->x
.p
->size
-1) {
1526 value_clear(p
->arr
[0].d
);
1527 p
->arr
[0] = res
->x
.p
->arr
[0];
1529 for (i
= offset
; i
< e1
->x
.p
->size
-1; ++i
)
1530 evalue_set_si(&p
->arr
[i
], 0, 1);
1531 for (i
= offset
; i
< res
->x
.p
->size
; ++i
) {
1532 value_clear(p
->arr
[i
+e1
->x
.p
->size
-offset
-1].d
);
1533 p
->arr
[i
+e1
->x
.p
->size
-offset
-1] = res
->x
.p
->arr
[i
];
1534 emul(&e1
->x
.p
->arr
[e1
->x
.p
->size
-1],
1535 &p
->arr
[i
+e1
->x
.p
->size
-offset
-1]);
1543 value_set_si(tmp
.d
,0);
1546 evalue_copy(&p
->arr
[0], &e1
->x
.p
->arr
[0]);
1547 for (i
= offset
; i
< e1
->x
.p
->size
; i
++) {
1548 evalue_copy(&tmp
.x
.p
->arr
[i
], &e1
->x
.p
->arr
[i
]);
1549 emul(&res
->x
.p
->arr
[offset
], &tmp
.x
.p
->arr
[i
]);
1552 evalue_set_si(&tmp
.x
.p
->arr
[i
], 0, 1);
1553 for (i
= offset
+1; i
<res
->x
.p
->size
; i
++)
1554 for (j
= offset
; j
<e1
->x
.p
->size
; j
++) {
1557 evalue_copy(&ev
, &e1
->x
.p
->arr
[j
]);
1558 emul(&res
->x
.p
->arr
[i
], &ev
);
1559 eadd(&ev
, &tmp
.x
.p
->arr
[i
+j
-offset
]);
1560 free_evalue_refs(&ev
);
1562 free_evalue_refs(res
);
1566 void emul_partitions(const evalue
*e1
, evalue
*res
)
1571 s
= (struct section
*)
1572 malloc((e1
->x
.p
->size
/2) * (res
->x
.p
->size
/2) *
1573 sizeof(struct section
));
1575 assert(e1
->x
.p
->pos
== res
->x
.p
->pos
);
1576 assert(e1
->x
.p
->pos
== EVALUE_DOMAIN(e1
->x
.p
->arr
[0])->Dimension
);
1577 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1580 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
) {
1581 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1582 d
= DomainIntersection(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1583 EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1584 d
= DomainConstraintSimplify(d
, 0);
1590 /* This code is only needed because the partitions
1591 are not true partitions.
1593 for (k
= 0; k
< n
; ++k
) {
1594 if (DomainIncludes(s
[k
].D
, d
))
1596 if (DomainIncludes(d
, s
[k
].D
)) {
1597 Domain_Free(s
[k
].D
);
1598 free_evalue_refs(&s
[k
].E
);
1609 value_init(s
[n
].E
.d
);
1610 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*i
+1]);
1611 emul(&e1
->x
.p
->arr
[2*j
+1], &s
[n
].E
);
1615 Domain_Free(EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]));
1616 value_clear(res
->x
.p
->arr
[2*i
].d
);
1617 free_evalue_refs(&res
->x
.p
->arr
[2*i
+1]);
1622 evalue_set_si(res
, 0, 1);
1624 res
->x
.p
= new_enode(partition
, 2*n
, e1
->x
.p
->pos
);
1625 for (j
= 0; j
< n
; ++j
) {
1626 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1627 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1628 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1635 /* Product of two rational numbers */
1636 static void emul_rationals(const evalue
*e1
, evalue
*res
)
1638 value_multiply(res
->d
, e1
->d
, res
->d
);
1639 value_multiply(res
->x
.n
, e1
->x
.n
, res
->x
.n
);
1640 reduce_constant(res
);
1643 static void emul_relations(const evalue
*e1
, evalue
*res
)
1647 if (e1
->x
.p
->size
< 3 && res
->x
.p
->size
== 3) {
1648 free_evalue_refs(&res
->x
.p
->arr
[2]);
1651 for (i
= 1; i
< res
->x
.p
->size
; ++i
)
1652 emul(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1655 static void emul_periodics(const evalue
*e1
, evalue
*res
)
1662 if (e1
->x
.p
->size
== res
->x
.p
->size
) {
1663 /* Product of two periodics of the same parameter and period */
1664 for (i
= 0; i
< res
->x
.p
->size
; i
++)
1665 emul(&(e1
->x
.p
->arr
[i
]), &(res
->x
.p
->arr
[i
]));
1669 /* Product of two periodics of the same parameter and different periods */
1674 iy
= res
->x
.p
->size
;
1675 value_set_si(x
, e1
->x
.p
->size
);
1676 value_set_si(y
, res
->x
.p
->size
);
1678 lcm
= (int)mpz_get_si(z
);
1679 newp
= (evalue
*) malloc(sizeof(evalue
));
1680 value_init(newp
->d
);
1681 value_set_si(newp
->d
, 0);
1682 newp
->x
.p
= new_enode(periodic
, lcm
, e1
->x
.p
->pos
);
1683 for (i
= 0; i
< lcm
; i
++)
1684 evalue_copy(&newp
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
%iy
]);
1685 for (i
= 0; i
< lcm
; i
++)
1686 emul(&e1
->x
.p
->arr
[i
%ix
], &newp
->x
.p
->arr
[i
]);
1687 value_assign(res
->d
, newp
->d
);
1688 res
->x
.p
= newp
->x
.p
;
1694 #define value_two_p(val) (mpz_cmp_si(val,2) == 0)
1696 static void emul_fractionals(const evalue
*e1
, evalue
*res
)
1700 poly_denom(&e1
->x
.p
->arr
[0], &d
.d
);
1701 if (!value_two_p(d
.d
))
1706 value_set_si(d
.x
.n
, 1);
1707 /* { x }^2 == { x }/2 */
1708 /* a0 b0 + (a0 b1 + a1 b0 + a1 b1/2) { x } */
1709 assert(e1
->x
.p
->size
== 3);
1710 assert(res
->x
.p
->size
== 3);
1712 evalue_copy(&tmp
, &res
->x
.p
->arr
[2]);
1714 eadd(&res
->x
.p
->arr
[1], &tmp
);
1715 emul(&e1
->x
.p
->arr
[2], &tmp
);
1716 emul(&e1
->x
.p
->arr
[1], &res
->x
.p
->arr
[1]);
1717 emul(&e1
->x
.p
->arr
[1], &res
->x
.p
->arr
[2]);
1718 eadd(&tmp
, &res
->x
.p
->arr
[2]);
1719 free_evalue_refs(&tmp
);
1725 /* Computes the product of two evalues "e1" and "res" and puts
1726 * the result in "res". You need to make a copy of "res"
1727 * before calling this function if you still need it afterward.
1728 * The vector type of evalues is not treated here
1730 void emul(const evalue
*e1
, evalue
*res
)
1734 assert(!(value_zero_p(e1
->d
) && e1
->x
.p
->type
== evector
));
1735 assert(!(value_zero_p(res
->d
) && res
->x
.p
->type
== evector
));
1737 if (EVALUE_IS_ZERO(*res
))
1740 if (EVALUE_IS_ONE(*e1
))
1743 if (EVALUE_IS_ZERO(*e1
)) {
1744 evalue_assign(res
, e1
);
1748 if (EVALUE_IS_NAN(*res
))
1751 if (EVALUE_IS_NAN(*e1
)) {
1752 evalue_assign(res
, e1
);
1756 cmp
= evalue_level_cmp(res
, e1
);
1759 } else if (cmp
== 0) {
1760 if (value_notzero_p(e1
->d
)) {
1761 emul_rationals(e1
, res
);
1763 switch (e1
->x
.p
->type
) {
1765 emul_partitions(e1
, res
);
1768 emul_relations(e1
, res
);
1775 emul_periodics(e1
, res
);
1778 emul_fractionals(e1
, res
);
1784 switch (res
->x
.p
->type
) {
1786 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
)
1787 emul(e1
, &res
->x
.p
->arr
[2*i
+1]);
1794 for (i
= type_offset(res
->x
.p
); i
< res
->x
.p
->size
; ++i
)
1795 emul(e1
, &res
->x
.p
->arr
[i
]);
1801 /* Frees mask content ! */
1802 void emask(evalue
*mask
, evalue
*res
) {
1809 if (EVALUE_IS_ZERO(*res
)) {
1810 free_evalue_refs(mask
);
1814 assert(value_zero_p(mask
->d
));
1815 assert(mask
->x
.p
->type
== partition
);
1816 assert(value_zero_p(res
->d
));
1817 assert(res
->x
.p
->type
== partition
);
1818 assert(mask
->x
.p
->pos
== res
->x
.p
->pos
);
1819 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1820 assert(mask
->x
.p
->pos
== EVALUE_DOMAIN(mask
->x
.p
->arr
[0])->Dimension
);
1821 pos
= res
->x
.p
->pos
;
1823 s
= (struct section
*)
1824 malloc((mask
->x
.p
->size
/2+1) * (res
->x
.p
->size
/2) *
1825 sizeof(struct section
));
1829 evalue_set_si(&mone
, -1, 1);
1832 for (j
= 0; j
< res
->x
.p
->size
/2; ++j
) {
1833 assert(mask
->x
.p
->size
>= 2);
1834 fd
= DomainDifference(EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]),
1835 EVALUE_DOMAIN(mask
->x
.p
->arr
[0]), 0);
1837 for (i
= 1; i
< mask
->x
.p
->size
/2; ++i
) {
1839 fd
= DomainDifference(fd
, EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]), 0);
1848 value_init(s
[n
].E
.d
);
1849 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*j
+1]);
1853 for (i
= 0; i
< mask
->x
.p
->size
/2; ++i
) {
1854 if (EVALUE_IS_ONE(mask
->x
.p
->arr
[2*i
+1]))
1857 fd
= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]);
1858 eadd(&mone
, &mask
->x
.p
->arr
[2*i
+1]);
1859 emul(&mone
, &mask
->x
.p
->arr
[2*i
+1]);
1860 for (j
= 0; j
< res
->x
.p
->size
/2; ++j
) {
1862 d
= DomainIntersection(EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]),
1863 EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]), 0);
1869 fd
= DomainDifference(fd
, EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]), 0);
1870 if (t
!= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]))
1872 value_init(s
[n
].E
.d
);
1873 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*j
+1]);
1874 emul(&mask
->x
.p
->arr
[2*i
+1], &s
[n
].E
);
1880 /* Just ignore; this may have been previously masked off */
1882 if (fd
!= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]))
1886 free_evalue_refs(&mone
);
1887 free_evalue_refs(mask
);
1888 free_evalue_refs(res
);
1891 evalue_set_si(res
, 0, 1);
1893 res
->x
.p
= new_enode(partition
, 2*n
, pos
);
1894 for (j
= 0; j
< n
; ++j
) {
1895 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1896 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1897 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1904 void evalue_copy(evalue
*dst
, const evalue
*src
)
1906 value_assign(dst
->d
, src
->d
);
1907 if (EVALUE_IS_NAN(*dst
)) {
1911 if (value_pos_p(src
->d
)) {
1912 value_init(dst
->x
.n
);
1913 value_assign(dst
->x
.n
, src
->x
.n
);
1915 dst
->x
.p
= ecopy(src
->x
.p
);
1918 evalue
*evalue_dup(const evalue
*e
)
1920 evalue
*res
= ALLOC(evalue
);
1922 evalue_copy(res
, e
);
1926 enode
*new_enode(enode_type type
,int size
,int pos
) {
1932 fprintf(stderr
, "Allocating enode of size 0 !\n" );
1935 res
= (enode
*) malloc(sizeof(enode
) + (size
-1)*sizeof(evalue
));
1939 for(i
=0; i
<size
; i
++) {
1940 value_init(res
->arr
[i
].d
);
1941 value_set_si(res
->arr
[i
].d
,0);
1942 res
->arr
[i
].x
.p
= 0;
1947 enode
*ecopy(enode
*e
) {
1952 res
= new_enode(e
->type
,e
->size
,e
->pos
);
1953 for(i
=0;i
<e
->size
;++i
) {
1954 value_assign(res
->arr
[i
].d
,e
->arr
[i
].d
);
1955 if(value_zero_p(res
->arr
[i
].d
))
1956 res
->arr
[i
].x
.p
= ecopy(e
->arr
[i
].x
.p
);
1957 else if (EVALUE_IS_DOMAIN(res
->arr
[i
]))
1958 EVALUE_SET_DOMAIN(res
->arr
[i
], Domain_Copy(EVALUE_DOMAIN(e
->arr
[i
])));
1960 value_init(res
->arr
[i
].x
.n
);
1961 value_assign(res
->arr
[i
].x
.n
,e
->arr
[i
].x
.n
);
1967 int ecmp(const evalue
*e1
, const evalue
*e2
)
1973 if (value_notzero_p(e1
->d
) && value_notzero_p(e2
->d
)) {
1977 value_multiply(m
, e1
->x
.n
, e2
->d
);
1978 value_multiply(m2
, e2
->x
.n
, e1
->d
);
1980 if (value_lt(m
, m2
))
1982 else if (value_gt(m
, m2
))
1992 if (value_notzero_p(e1
->d
))
1994 if (value_notzero_p(e2
->d
))
2000 if (p1
->type
!= p2
->type
)
2001 return p1
->type
- p2
->type
;
2002 if (p1
->pos
!= p2
->pos
)
2003 return p1
->pos
- p2
->pos
;
2004 if (p1
->size
!= p2
->size
)
2005 return p1
->size
- p2
->size
;
2007 for (i
= p1
->size
-1; i
>= 0; --i
)
2008 if ((r
= ecmp(&p1
->arr
[i
], &p2
->arr
[i
])) != 0)
2014 int eequal(const evalue
*e1
, const evalue
*e2
)
2019 if (value_ne(e1
->d
,e2
->d
))
2022 /* e1->d == e2->d */
2023 if (value_notzero_p(e1
->d
)) {
2024 if (value_ne(e1
->x
.n
,e2
->x
.n
))
2027 /* e1->d == e2->d != 0 AND e1->n == e2->n */
2031 /* e1->d == e2->d == 0 */
2034 if (p1
->type
!= p2
->type
) return 0;
2035 if (p1
->size
!= p2
->size
) return 0;
2036 if (p1
->pos
!= p2
->pos
) return 0;
2037 for (i
=0; i
<p1
->size
; i
++)
2038 if (!eequal(&p1
->arr
[i
], &p2
->arr
[i
]) )
2043 void free_evalue_refs(evalue
*e
) {
2048 if (EVALUE_IS_NAN(*e
)) {
2053 if (EVALUE_IS_DOMAIN(*e
)) {
2054 Domain_Free(EVALUE_DOMAIN(*e
));
2057 } else if (value_pos_p(e
->d
)) {
2059 /* 'e' stores a constant */
2061 value_clear(e
->x
.n
);
2064 assert(value_zero_p(e
->d
));
2067 if (!p
) return; /* null pointer */
2068 for (i
=0; i
<p
->size
; i
++) {
2069 free_evalue_refs(&(p
->arr
[i
]));
2073 } /* free_evalue_refs */
2075 void evalue_free(evalue
*e
)
2077 free_evalue_refs(e
);
2081 static void mod2table_r(evalue
*e
, Vector
*periods
, Value m
, int p
,
2082 Vector
* val
, evalue
*res
)
2084 unsigned nparam
= periods
->Size
;
2087 double d
= compute_evalue(e
, val
->p
);
2088 d
*= VALUE_TO_DOUBLE(m
);
2093 value_assign(res
->d
, m
);
2094 value_init(res
->x
.n
);
2095 value_set_double(res
->x
.n
, d
);
2096 mpz_fdiv_r(res
->x
.n
, res
->x
.n
, m
);
2099 if (value_one_p(periods
->p
[p
]))
2100 mod2table_r(e
, periods
, m
, p
+1, val
, res
);
2105 value_assign(tmp
, periods
->p
[p
]);
2106 value_set_si(res
->d
, 0);
2107 res
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
2109 value_decrement(tmp
, tmp
);
2110 value_assign(val
->p
[p
], tmp
);
2111 mod2table_r(e
, periods
, m
, p
+1, val
,
2112 &res
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
2113 } while (value_pos_p(tmp
));
2119 static void rel2table(evalue
*e
, int zero
)
2121 if (value_pos_p(e
->d
)) {
2122 if (value_zero_p(e
->x
.n
) == zero
)
2123 value_set_si(e
->x
.n
, 1);
2125 value_set_si(e
->x
.n
, 0);
2126 value_set_si(e
->d
, 1);
2129 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
2130 rel2table(&e
->x
.p
->arr
[i
], zero
);
2134 void evalue_mod2table(evalue
*e
, int nparam
)
2139 if (EVALUE_IS_DOMAIN(*e
) || value_pos_p(e
->d
))
2142 for (i
=0; i
<p
->size
; i
++) {
2143 evalue_mod2table(&(p
->arr
[i
]), nparam
);
2145 if (p
->type
== relation
) {
2150 evalue_copy(©
, &p
->arr
[0]);
2152 rel2table(&p
->arr
[0], 1);
2153 emul(&p
->arr
[0], &p
->arr
[1]);
2155 rel2table(©
, 0);
2156 emul(©
, &p
->arr
[2]);
2157 eadd(&p
->arr
[2], &p
->arr
[1]);
2158 free_evalue_refs(&p
->arr
[2]);
2159 free_evalue_refs(©
);
2161 free_evalue_refs(&p
->arr
[0]);
2165 } else if (p
->type
== fractional
) {
2166 Vector
*periods
= Vector_Alloc(nparam
);
2167 Vector
*val
= Vector_Alloc(nparam
);
2173 value_set_si(tmp
, 1);
2174 Vector_Set(periods
->p
, 1, nparam
);
2175 Vector_Set(val
->p
, 0, nparam
);
2176 for (ev
= &p
->arr
[0]; value_zero_p(ev
->d
); ev
= &ev
->x
.p
->arr
[0]) {
2179 assert(p
->type
== polynomial
);
2180 assert(p
->size
== 2);
2181 value_assign(periods
->p
[p
->pos
-1], p
->arr
[1].d
);
2182 value_lcm(tmp
, tmp
, p
->arr
[1].d
);
2184 value_lcm(tmp
, tmp
, ev
->d
);
2186 mod2table_r(&p
->arr
[0], periods
, tmp
, 0, val
, &EP
);
2189 evalue_set_si(&res
, 0, 1);
2190 /* Compute the polynomial using Horner's rule */
2191 for (i
=p
->size
-1;i
>1;i
--) {
2192 eadd(&p
->arr
[i
], &res
);
2195 eadd(&p
->arr
[1], &res
);
2197 free_evalue_refs(e
);
2198 free_evalue_refs(&EP
);
2203 Vector_Free(periods
);
2205 } /* evalue_mod2table */
2207 /********************************************************/
2208 /* function in domain */
2209 /* check if the parameters in list_args */
2210 /* verifies the constraints of Domain P */
2211 /********************************************************/
2212 int in_domain(Polyhedron
*P
, Value
*list_args
)
2215 Value v
; /* value of the constraint of a row when
2216 parameters are instantiated*/
2220 for (row
= 0; row
< P
->NbConstraints
; row
++) {
2221 Inner_Product(P
->Constraint
[row
]+1, list_args
, P
->Dimension
, &v
);
2222 value_addto(v
, v
, P
->Constraint
[row
][P
->Dimension
+1]); /*constant part*/
2223 if (value_neg_p(v
) ||
2224 value_zero_p(P
->Constraint
[row
][0]) && value_notzero_p(v
)) {
2231 return in
|| (P
->next
&& in_domain(P
->next
, list_args
));
2234 /****************************************************/
2235 /* function compute enode */
2236 /* compute the value of enode p with parameters */
2237 /* list "list_args */
2238 /* compute the polynomial or the periodic */
2239 /****************************************************/
2241 static double compute_enode(enode
*p
, Value
*list_args
) {
2253 if (p
->type
== polynomial
) {
2255 value_assign(param
,list_args
[p
->pos
-1]);
2257 /* Compute the polynomial using Horner's rule */
2258 for (i
=p
->size
-1;i
>0;i
--) {
2259 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2260 res
*=VALUE_TO_DOUBLE(param
);
2262 res
+=compute_evalue(&p
->arr
[0],list_args
);
2264 else if (p
->type
== fractional
) {
2265 double d
= compute_evalue(&p
->arr
[0], list_args
);
2266 d
-= floor(d
+1e-10);
2268 /* Compute the polynomial using Horner's rule */
2269 for (i
=p
->size
-1;i
>1;i
--) {
2270 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2273 res
+=compute_evalue(&p
->arr
[1],list_args
);
2275 else if (p
->type
== flooring
) {
2276 double d
= compute_evalue(&p
->arr
[0], list_args
);
2279 /* Compute the polynomial using Horner's rule */
2280 for (i
=p
->size
-1;i
>1;i
--) {
2281 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2284 res
+=compute_evalue(&p
->arr
[1],list_args
);
2286 else if (p
->type
== periodic
) {
2287 value_assign(m
,list_args
[p
->pos
-1]);
2289 /* Choose the right element of the periodic */
2290 value_set_si(param
,p
->size
);
2291 value_pmodulus(m
,m
,param
);
2292 res
= compute_evalue(&p
->arr
[VALUE_TO_INT(m
)],list_args
);
2294 else if (p
->type
== relation
) {
2295 if (fabs(compute_evalue(&p
->arr
[0], list_args
)) < 1e-10)
2296 res
= compute_evalue(&p
->arr
[1], list_args
);
2297 else if (p
->size
> 2)
2298 res
= compute_evalue(&p
->arr
[2], list_args
);
2300 else if (p
->type
== partition
) {
2301 int dim
= EVALUE_DOMAIN(p
->arr
[0])->Dimension
;
2302 Value
*vals
= list_args
;
2305 for (i
= 0; i
< dim
; ++i
) {
2306 value_init(vals
[i
]);
2308 value_assign(vals
[i
], list_args
[i
]);
2311 for (i
= 0; i
< p
->size
/2; ++i
)
2312 if (DomainContains(EVALUE_DOMAIN(p
->arr
[2*i
]), vals
, p
->pos
, 0, 1)) {
2313 res
= compute_evalue(&p
->arr
[2*i
+1], vals
);
2317 for (i
= 0; i
< dim
; ++i
)
2318 value_clear(vals
[i
]);
2327 } /* compute_enode */
2329 /*************************************************/
2330 /* return the value of Ehrhart Polynomial */
2331 /* It returns a double, because since it is */
2332 /* a recursive function, some intermediate value */
2333 /* might not be integral */
2334 /*************************************************/
2336 double compute_evalue(const evalue
*e
, Value
*list_args
)
2340 if (value_notzero_p(e
->d
)) {
2341 if (value_notone_p(e
->d
))
2342 res
= VALUE_TO_DOUBLE(e
->x
.n
) / VALUE_TO_DOUBLE(e
->d
);
2344 res
= VALUE_TO_DOUBLE(e
->x
.n
);
2347 res
= compute_enode(e
->x
.p
,list_args
);
2349 } /* compute_evalue */
2352 /****************************************************/
2353 /* function compute_poly : */
2354 /* Check for the good validity domain */
2355 /* return the number of point in the Polyhedron */
2356 /* in allocated memory */
2357 /* Using the Ehrhart pseudo-polynomial */
2358 /****************************************************/
2359 Value
*compute_poly(Enumeration
*en
,Value
*list_args
) {
2362 /* double d; int i; */
2364 tmp
= (Value
*) malloc (sizeof(Value
));
2365 assert(tmp
!= NULL
);
2367 value_set_si(*tmp
,0);
2370 return(tmp
); /* no ehrhart polynomial */
2371 if(en
->ValidityDomain
) {
2372 if(!en
->ValidityDomain
->Dimension
) { /* no parameters */
2373 value_set_double(*tmp
,compute_evalue(&en
->EP
,list_args
)+.25);
2378 return(tmp
); /* no Validity Domain */
2380 if(in_domain(en
->ValidityDomain
,list_args
)) {
2382 #ifdef EVAL_EHRHART_DEBUG
2383 Print_Domain(stdout
,en
->ValidityDomain
);
2384 print_evalue(stdout
,&en
->EP
);
2387 /* d = compute_evalue(&en->EP,list_args);
2389 printf("(double)%lf = %d\n", d, i ); */
2390 value_set_double(*tmp
,compute_evalue(&en
->EP
,list_args
)+.25);
2396 value_set_si(*tmp
,0);
2397 return(tmp
); /* no compatible domain with the arguments */
2398 } /* compute_poly */
2400 static evalue
*eval_polynomial(const enode
*p
, int offset
,
2401 evalue
*base
, Value
*values
)
2406 res
= evalue_zero();
2407 for (i
= p
->size
-1; i
> offset
; --i
) {
2408 c
= evalue_eval(&p
->arr
[i
], values
);
2413 c
= evalue_eval(&p
->arr
[offset
], values
);
2420 evalue
*evalue_eval(const evalue
*e
, Value
*values
)
2427 if (value_notzero_p(e
->d
)) {
2428 res
= ALLOC(evalue
);
2430 evalue_copy(res
, e
);
2433 switch (e
->x
.p
->type
) {
2435 value_init(param
.x
.n
);
2436 value_assign(param
.x
.n
, values
[e
->x
.p
->pos
-1]);
2437 value_init(param
.d
);
2438 value_set_si(param
.d
, 1);
2440 res
= eval_polynomial(e
->x
.p
, 0, ¶m
, values
);
2441 free_evalue_refs(¶m
);
2444 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2445 mpz_fdiv_r(param2
->x
.n
, param2
->x
.n
, param2
->d
);
2447 res
= eval_polynomial(e
->x
.p
, 1, param2
, values
);
2448 evalue_free(param2
);
2451 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2452 mpz_fdiv_q(param2
->x
.n
, param2
->x
.n
, param2
->d
);
2453 value_set_si(param2
->d
, 1);
2455 res
= eval_polynomial(e
->x
.p
, 1, param2
, values
);
2456 evalue_free(param2
);
2459 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2460 if (value_zero_p(param2
->x
.n
))
2461 res
= evalue_eval(&e
->x
.p
->arr
[1], values
);
2462 else if (e
->x
.p
->size
> 2)
2463 res
= evalue_eval(&e
->x
.p
->arr
[2], values
);
2465 res
= evalue_zero();
2466 evalue_free(param2
);
2469 assert(e
->x
.p
->pos
== EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
);
2470 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
2471 if (in_domain(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), values
)) {
2472 res
= evalue_eval(&e
->x
.p
->arr
[2*i
+1], values
);
2476 res
= evalue_zero();
2484 size_t value_size(Value v
) {
2485 return (v
[0]._mp_size
> 0 ? v
[0]._mp_size
: -v
[0]._mp_size
)
2486 * sizeof(v
[0]._mp_d
[0]);
2489 size_t domain_size(Polyhedron
*D
)
2492 size_t s
= sizeof(*D
);
2494 for (i
= 0; i
< D
->NbConstraints
; ++i
)
2495 for (j
= 0; j
< D
->Dimension
+2; ++j
)
2496 s
+= value_size(D
->Constraint
[i
][j
]);
2499 for (i = 0; i < D->NbRays; ++i)
2500 for (j = 0; j < D->Dimension+2; ++j)
2501 s += value_size(D->Ray[i][j]);
2504 return D
->next
? s
+domain_size(D
->next
) : s
;
2507 size_t enode_size(enode
*p
) {
2508 size_t s
= sizeof(*p
) - sizeof(p
->arr
[0]);
2511 if (p
->type
== partition
)
2512 for (i
= 0; i
< p
->size
/2; ++i
) {
2513 s
+= domain_size(EVALUE_DOMAIN(p
->arr
[2*i
]));
2514 s
+= evalue_size(&p
->arr
[2*i
+1]);
2517 for (i
= 0; i
< p
->size
; ++i
) {
2518 s
+= evalue_size(&p
->arr
[i
]);
2523 size_t evalue_size(evalue
*e
)
2525 size_t s
= sizeof(*e
);
2526 s
+= value_size(e
->d
);
2527 if (value_notzero_p(e
->d
))
2528 s
+= value_size(e
->x
.n
);
2530 s
+= enode_size(e
->x
.p
);
2534 static evalue
*find_second(evalue
*base
, evalue
*cst
, evalue
*e
, Value m
)
2536 evalue
*found
= NULL
;
2541 if (value_pos_p(e
->d
) || e
->x
.p
->type
!= fractional
)
2544 value_init(offset
.d
);
2545 value_init(offset
.x
.n
);
2546 poly_denom(&e
->x
.p
->arr
[0], &offset
.d
);
2547 value_lcm(offset
.d
, m
, offset
.d
);
2548 value_set_si(offset
.x
.n
, 1);
2551 evalue_copy(©
, cst
);
2554 mpz_fdiv_r(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2556 if (eequal(base
, &e
->x
.p
->arr
[0]))
2557 found
= &e
->x
.p
->arr
[0];
2559 value_set_si(offset
.x
.n
, -2);
2562 mpz_fdiv_r(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2564 if (eequal(base
, &e
->x
.p
->arr
[0]))
2567 free_evalue_refs(cst
);
2568 free_evalue_refs(&offset
);
2571 for (i
= 1; !found
&& i
< e
->x
.p
->size
; ++i
)
2572 found
= find_second(base
, cst
, &e
->x
.p
->arr
[i
], m
);
2577 static evalue
*find_relation_pair(evalue
*e
)
2580 evalue
*found
= NULL
;
2582 if (EVALUE_IS_DOMAIN(*e
) || value_pos_p(e
->d
))
2585 if (e
->x
.p
->type
== fractional
) {
2590 poly_denom(&e
->x
.p
->arr
[0], &m
);
2592 for (cst
= &e
->x
.p
->arr
[0]; value_zero_p(cst
->d
);
2593 cst
= &cst
->x
.p
->arr
[0])
2596 for (i
= 1; !found
&& i
< e
->x
.p
->size
; ++i
)
2597 found
= find_second(&e
->x
.p
->arr
[0], cst
, &e
->x
.p
->arr
[i
], m
);
2602 i
= e
->x
.p
->type
== relation
;
2603 for (; !found
&& i
< e
->x
.p
->size
; ++i
)
2604 found
= find_relation_pair(&e
->x
.p
->arr
[i
]);
2609 void evalue_mod2relation(evalue
*e
) {
2612 if (value_zero_p(e
->d
) && e
->x
.p
->type
== partition
) {
2615 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2616 evalue_mod2relation(&e
->x
.p
->arr
[2*i
+1]);
2617 if (EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
2618 value_clear(e
->x
.p
->arr
[2*i
].d
);
2619 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
2621 if (2*i
< e
->x
.p
->size
) {
2622 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
2623 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
2628 if (e
->x
.p
->size
== 0) {
2630 evalue_set_si(e
, 0, 1);
2636 while ((d
= find_relation_pair(e
)) != NULL
) {
2640 value_init(split
.d
);
2641 value_set_si(split
.d
, 0);
2642 split
.x
.p
= new_enode(relation
, 3, 0);
2643 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2644 evalue_set_si(&split
.x
.p
->arr
[2], 1, 1);
2646 ev
= &split
.x
.p
->arr
[0];
2647 value_set_si(ev
->d
, 0);
2648 ev
->x
.p
= new_enode(fractional
, 3, -1);
2649 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
2650 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
2651 evalue_copy(&ev
->x
.p
->arr
[0], d
);
2657 free_evalue_refs(&split
);
2661 static int evalue_comp(const void * a
, const void * b
)
2663 const evalue
*e1
= *(const evalue
**)a
;
2664 const evalue
*e2
= *(const evalue
**)b
;
2665 return ecmp(e1
, e2
);
2668 void evalue_combine(evalue
*e
)
2675 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
2678 NALLOC(evs
, e
->x
.p
->size
/2);
2679 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
2680 evs
[i
] = &e
->x
.p
->arr
[2*i
+1];
2681 qsort(evs
, e
->x
.p
->size
/2, sizeof(evs
[0]), evalue_comp
);
2682 p
= new_enode(partition
, e
->x
.p
->size
, e
->x
.p
->pos
);
2683 for (i
= 0, k
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2684 if (k
== 0 || ecmp(&p
->arr
[2*k
-1], evs
[i
]) != 0) {
2685 value_clear(p
->arr
[2*k
].d
);
2686 value_clear(p
->arr
[2*k
+1].d
);
2687 p
->arr
[2*k
] = *(evs
[i
]-1);
2688 p
->arr
[2*k
+1] = *(evs
[i
]);
2691 Polyhedron
*D
= EVALUE_DOMAIN(*(evs
[i
]-1));
2694 value_clear((evs
[i
]-1)->d
);
2698 L
->next
= EVALUE_DOMAIN(p
->arr
[2*k
-2]);
2699 EVALUE_SET_DOMAIN(p
->arr
[2*k
-2], D
);
2700 free_evalue_refs(evs
[i
]);
2704 for (i
= 2*k
; i
< p
->size
; ++i
)
2705 value_clear(p
->arr
[i
].d
);
2712 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2714 if (value_notzero_p(e
->x
.p
->arr
[2*i
+1].d
))
2716 H
= DomainConvex(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), 0);
2719 for (k
= 0; k
< e
->x
.p
->size
/2; ++k
) {
2720 Polyhedron
*D
, *N
, **P
;
2723 P
= &EVALUE_DOMAIN(e
->x
.p
->arr
[2*k
]);
2730 if (D
->NbEq
<= H
->NbEq
) {
2736 tmp
.x
.p
= new_enode(partition
, 2, e
->x
.p
->pos
);
2737 EVALUE_SET_DOMAIN(tmp
.x
.p
->arr
[0], Polyhedron_Copy(D
));
2738 evalue_copy(&tmp
.x
.p
->arr
[1], &e
->x
.p
->arr
[2*i
+1]);
2739 reduce_evalue(&tmp
);
2740 if (value_notzero_p(tmp
.d
) ||
2741 ecmp(&tmp
.x
.p
->arr
[1], &e
->x
.p
->arr
[2*k
+1]) != 0)
2744 D
->next
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
2745 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]) = D
;
2748 free_evalue_refs(&tmp
);
2754 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2756 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
2758 value_clear(e
->x
.p
->arr
[2*i
].d
);
2759 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
2761 if (2*i
< e
->x
.p
->size
) {
2762 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
2763 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
2770 H
= DomainConvex(D
, 0);
2771 E
= DomainDifference(H
, D
, 0);
2773 D
= DomainDifference(H
, E
, 0);
2776 EVALUE_SET_DOMAIN(p
->arr
[2*i
], D
);
2780 /* Use smallest representative for coefficients in affine form in
2781 * argument of fractional.
2782 * Since any change will make the argument non-standard,
2783 * the containing evalue will have to be reduced again afterward.
2785 static void fractional_minimal_coefficients(enode
*p
)
2791 assert(p
->type
== fractional
);
2793 while (value_zero_p(pp
->d
)) {
2794 assert(pp
->x
.p
->type
== polynomial
);
2795 assert(pp
->x
.p
->size
== 2);
2796 assert(value_notzero_p(pp
->x
.p
->arr
[1].d
));
2797 mpz_mul_ui(twice
, pp
->x
.p
->arr
[1].x
.n
, 2);
2798 if (value_gt(twice
, pp
->x
.p
->arr
[1].d
))
2799 value_subtract(pp
->x
.p
->arr
[1].x
.n
,
2800 pp
->x
.p
->arr
[1].x
.n
, pp
->x
.p
->arr
[1].d
);
2801 pp
= &pp
->x
.p
->arr
[0];
2807 static Polyhedron
*polynomial_projection(enode
*p
, Polyhedron
*D
, Value
*d
,
2812 unsigned dim
= D
->Dimension
;
2813 Matrix
*T
= Matrix_Alloc(2, dim
+1);
2816 assert(p
->type
== fractional
|| p
->type
== flooring
);
2817 value_set_si(T
->p
[1][dim
], 1);
2818 evalue_extract_affine(&p
->arr
[0], T
->p
[0], &T
->p
[0][dim
], d
);
2819 I
= DomainImage(D
, T
, 0);
2820 H
= DomainConvex(I
, 0);
2830 static void replace_by_affine(evalue
*e
, Value offset
)
2837 value_init(inc
.x
.n
);
2838 value_set_si(inc
.d
, 1);
2839 value_oppose(inc
.x
.n
, offset
);
2840 eadd(&inc
, &p
->arr
[0]);
2841 reorder_terms_about(p
, &p
->arr
[0]); /* frees arr[0] */
2845 free_evalue_refs(&inc
);
2848 int evalue_range_reduction_in_domain(evalue
*e
, Polyhedron
*D
)
2857 if (value_notzero_p(e
->d
))
2862 if (p
->type
== relation
) {
2869 fractional_minimal_coefficients(p
->arr
[0].x
.p
);
2870 I
= polynomial_projection(p
->arr
[0].x
.p
, D
, &d
, &T
);
2871 bounded
= line_minmax(I
, &min
, &max
); /* frees I */
2872 equal
= value_eq(min
, max
);
2873 mpz_cdiv_q(min
, min
, d
);
2874 mpz_fdiv_q(max
, max
, d
);
2876 if (bounded
&& value_gt(min
, max
)) {
2882 evalue_set_si(e
, 0, 1);
2885 free_evalue_refs(&(p
->arr
[1]));
2886 free_evalue_refs(&(p
->arr
[0]));
2892 return r
? r
: evalue_range_reduction_in_domain(e
, D
);
2893 } else if (bounded
&& equal
) {
2896 free_evalue_refs(&(p
->arr
[2]));
2899 free_evalue_refs(&(p
->arr
[0]));
2905 return evalue_range_reduction_in_domain(e
, D
);
2906 } else if (bounded
&& value_eq(min
, max
)) {
2907 /* zero for a single value */
2909 Matrix
*M
= Matrix_Alloc(1, D
->Dimension
+2);
2910 Vector_Copy(T
->p
[0], M
->p
[0]+1, D
->Dimension
+1);
2911 value_multiply(min
, min
, d
);
2912 value_subtract(M
->p
[0][D
->Dimension
+1],
2913 M
->p
[0][D
->Dimension
+1], min
);
2914 E
= DomainAddConstraints(D
, M
, 0);
2920 r
= evalue_range_reduction_in_domain(&p
->arr
[1], E
);
2922 r
|= evalue_range_reduction_in_domain(&p
->arr
[2], D
);
2924 _reduce_evalue(&p
->arr
[0].x
.p
->arr
[0], 0, 1);
2932 _reduce_evalue(&p
->arr
[0].x
.p
->arr
[0], 0, 1);
2935 i
= p
->type
== relation
? 1 :
2936 p
->type
== fractional
? 1 : 0;
2937 for (; i
<p
->size
; i
++)
2938 r
|= evalue_range_reduction_in_domain(&p
->arr
[i
], D
);
2940 if (p
->type
!= fractional
) {
2941 if (r
&& p
->type
== polynomial
) {
2944 value_set_si(f
.d
, 0);
2945 f
.x
.p
= new_enode(polynomial
, 2, p
->pos
);
2946 evalue_set_si(&f
.x
.p
->arr
[0], 0, 1);
2947 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
2948 reorder_terms_about(p
, &f
);
2959 fractional_minimal_coefficients(p
);
2960 I
= polynomial_projection(p
, D
, &d
, NULL
);
2961 bounded
= line_minmax(I
, &min
, &max
); /* frees I */
2962 mpz_fdiv_q(min
, min
, d
);
2963 mpz_fdiv_q(max
, max
, d
);
2964 value_subtract(d
, max
, min
);
2966 if (bounded
&& value_eq(min
, max
)) {
2967 replace_by_affine(e
, min
);
2969 } else if (bounded
&& value_one_p(d
) && p
->size
> 3) {
2970 /* replace {g}^2 by -(g-min)^2 + (2{g}+1)*(g-min) - {g}
2971 * See pages 199-200 of PhD thesis.
2979 value_set_si(rem
.d
, 0);
2980 rem
.x
.p
= new_enode(fractional
, 3, -1);
2981 evalue_copy(&rem
.x
.p
->arr
[0], &p
->arr
[0]);
2982 value_clear(rem
.x
.p
->arr
[1].d
);
2983 value_clear(rem
.x
.p
->arr
[2].d
);
2984 rem
.x
.p
->arr
[1] = p
->arr
[1];
2985 rem
.x
.p
->arr
[2] = p
->arr
[2];
2986 for (i
= 3; i
< p
->size
; ++i
)
2987 p
->arr
[i
-2] = p
->arr
[i
];
2991 value_init(inc
.x
.n
);
2992 value_set_si(inc
.d
, 1);
2993 value_oppose(inc
.x
.n
, min
);
2996 evalue_copy(&t
, &p
->arr
[0]);
3000 value_set_si(f
.d
, 0);
3001 f
.x
.p
= new_enode(fractional
, 3, -1);
3002 evalue_copy(&f
.x
.p
->arr
[0], &p
->arr
[0]);
3003 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
3004 evalue_set_si(&f
.x
.p
->arr
[2], 2, 1);
3006 value_init(factor
.d
);
3007 evalue_set_si(&factor
, -1, 1);
3013 value_clear(f
.x
.p
->arr
[1].x
.n
);
3014 value_clear(f
.x
.p
->arr
[2].x
.n
);
3015 evalue_set_si(&f
.x
.p
->arr
[1], 0, 1);
3016 evalue_set_si(&f
.x
.p
->arr
[2], -1, 1);
3020 reorder_terms(&rem
);
3027 free_evalue_refs(&inc
);
3028 free_evalue_refs(&t
);
3029 free_evalue_refs(&f
);
3030 free_evalue_refs(&factor
);
3031 free_evalue_refs(&rem
);
3033 evalue_range_reduction_in_domain(e
, D
);
3037 _reduce_evalue(&p
->arr
[0], 0, 1);
3049 void evalue_range_reduction(evalue
*e
)
3052 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
3055 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3056 if (evalue_range_reduction_in_domain(&e
->x
.p
->arr
[2*i
+1],
3057 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]))) {
3058 reduce_evalue(&e
->x
.p
->arr
[2*i
+1]);
3060 if (EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
3061 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
3062 Domain_Free(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]));
3063 value_clear(e
->x
.p
->arr
[2*i
].d
);
3065 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
3066 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
3074 Enumeration
* partition2enumeration(evalue
*EP
)
3077 Enumeration
*en
, *res
= NULL
;
3079 if (EVALUE_IS_ZERO(*EP
)) {
3084 for (i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
3085 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
])->Dimension
);
3086 en
= (Enumeration
*)malloc(sizeof(Enumeration
));
3089 res
->ValidityDomain
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
3090 value_clear(EP
->x
.p
->arr
[2*i
].d
);
3091 res
->EP
= EP
->x
.p
->arr
[2*i
+1];
3099 int evalue_frac2floor_in_domain3(evalue
*e
, Polyhedron
*D
, int shift
)
3108 if (value_notzero_p(e
->d
))
3113 i
= p
->type
== relation
? 1 :
3114 p
->type
== fractional
? 1 : 0;
3115 for (; i
<p
->size
; i
++)
3116 r
|= evalue_frac2floor_in_domain3(&p
->arr
[i
], D
, shift
);
3118 if (p
->type
!= fractional
) {
3119 if (r
&& p
->type
== polynomial
) {
3122 value_set_si(f
.d
, 0);
3123 f
.x
.p
= new_enode(polynomial
, 2, p
->pos
);
3124 evalue_set_si(&f
.x
.p
->arr
[0], 0, 1);
3125 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
3126 reorder_terms_about(p
, &f
);
3136 I
= polynomial_projection(p
, D
, &d
, NULL
);
3139 Polyhedron_Print(stderr, P_VALUE_FMT, I);
3142 assert(I
->NbEq
== 0); /* Should have been reduced */
3145 for (i
= 0; i
< I
->NbConstraints
; ++i
)
3146 if (value_pos_p(I
->Constraint
[i
][1]))
3149 if (i
< I
->NbConstraints
) {
3151 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
3152 mpz_cdiv_q(min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
3153 if (value_neg_p(min
)) {
3155 mpz_fdiv_q(min
, min
, d
);
3156 value_init(offset
.d
);
3157 value_set_si(offset
.d
, 1);
3158 value_init(offset
.x
.n
);
3159 value_oppose(offset
.x
.n
, min
);
3160 eadd(&offset
, &p
->arr
[0]);
3161 free_evalue_refs(&offset
);
3171 value_set_si(fl
.d
, 0);
3172 fl
.x
.p
= new_enode(flooring
, 3, -1);
3173 evalue_set_si(&fl
.x
.p
->arr
[1], 0, 1);
3174 evalue_set_si(&fl
.x
.p
->arr
[2], -1, 1);
3175 evalue_copy(&fl
.x
.p
->arr
[0], &p
->arr
[0]);
3177 eadd(&fl
, &p
->arr
[0]);
3178 reorder_terms_about(p
, &p
->arr
[0]);
3182 free_evalue_refs(&fl
);
3187 int evalue_frac2floor_in_domain(evalue
*e
, Polyhedron
*D
)
3189 return evalue_frac2floor_in_domain3(e
, D
, 1);
3192 void evalue_frac2floor2(evalue
*e
, int shift
)
3195 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
) {
3197 if (evalue_frac2floor_in_domain3(e
, NULL
, 0))
3203 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3204 if (evalue_frac2floor_in_domain3(&e
->x
.p
->arr
[2*i
+1],
3205 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), shift
))
3206 reduce_evalue(&e
->x
.p
->arr
[2*i
+1]);
3209 void evalue_frac2floor(evalue
*e
)
3211 evalue_frac2floor2(e
, 1);
3214 /* Add a new variable with lower bound 1 and upper bound specified
3215 * by row. If negative is true, then the new variable has upper
3216 * bound -1 and lower bound specified by row.
3218 static Matrix
*esum_add_constraint(int nvar
, Polyhedron
*D
, Matrix
*C
,
3219 Vector
*row
, int negative
)
3223 int nparam
= D
->Dimension
- nvar
;
3226 nr
= D
->NbConstraints
+ 2;
3227 nc
= D
->Dimension
+ 2 + 1;
3228 C
= Matrix_Alloc(nr
, nc
);
3229 for (i
= 0; i
< D
->NbConstraints
; ++i
) {
3230 Vector_Copy(D
->Constraint
[i
], C
->p
[i
], 1 + nvar
);
3231 Vector_Copy(D
->Constraint
[i
] + 1 + nvar
, C
->p
[i
] + 1 + nvar
+ 1,
3232 D
->Dimension
+ 1 - nvar
);
3237 nc
= C
->NbColumns
+ 1;
3238 C
= Matrix_Alloc(nr
, nc
);
3239 for (i
= 0; i
< oldC
->NbRows
; ++i
) {
3240 Vector_Copy(oldC
->p
[i
], C
->p
[i
], 1 + nvar
);
3241 Vector_Copy(oldC
->p
[i
] + 1 + nvar
, C
->p
[i
] + 1 + nvar
+ 1,
3242 oldC
->NbColumns
- 1 - nvar
);
3245 value_set_si(C
->p
[nr
-2][0], 1);
3247 value_set_si(C
->p
[nr
-2][1 + nvar
], -1);
3249 value_set_si(C
->p
[nr
-2][1 + nvar
], 1);
3250 value_set_si(C
->p
[nr
-2][nc
- 1], -1);
3252 Vector_Copy(row
->p
, C
->p
[nr
-1], 1 + nvar
+ 1);
3253 Vector_Copy(row
->p
+ 1 + nvar
+ 1, C
->p
[nr
-1] + C
->NbColumns
- 1 - nparam
,
3259 static void floor2frac_r(evalue
*e
, int nvar
)
3266 if (value_notzero_p(e
->d
))
3271 assert(p
->type
== flooring
);
3272 for (i
= 1; i
< p
->size
; i
++)
3273 floor2frac_r(&p
->arr
[i
], nvar
);
3275 for (pp
= &p
->arr
[0]; value_zero_p(pp
->d
); pp
= &pp
->x
.p
->arr
[0]) {
3276 assert(pp
->x
.p
->type
== polynomial
);
3277 pp
->x
.p
->pos
-= nvar
;
3281 value_set_si(f
.d
, 0);
3282 f
.x
.p
= new_enode(fractional
, 3, -1);
3283 evalue_set_si(&f
.x
.p
->arr
[1], 0, 1);
3284 evalue_set_si(&f
.x
.p
->arr
[2], -1, 1);
3285 evalue_copy(&f
.x
.p
->arr
[0], &p
->arr
[0]);
3287 eadd(&f
, &p
->arr
[0]);
3288 reorder_terms_about(p
, &p
->arr
[0]);
3292 free_evalue_refs(&f
);
3295 /* Convert flooring back to fractional and shift position
3296 * of the parameters by nvar
3298 static void floor2frac(evalue
*e
, int nvar
)
3300 floor2frac_r(e
, nvar
);
3304 evalue
*esum_over_domain_cst(int nvar
, Polyhedron
*D
, Matrix
*C
)
3307 int nparam
= D
->Dimension
- nvar
;
3311 D
= Constraints2Polyhedron(C
, 0);
3315 t
= barvinok_enumerate_e(D
, 0, nparam
, 0);
3317 /* Double check that D was not unbounded. */
3318 assert(!(value_pos_p(t
->d
) && value_neg_p(t
->x
.n
)));
3326 static evalue
*esum_over_domain(evalue
*e
, int nvar
, Polyhedron
*D
,
3327 int *signs
, Matrix
*C
, unsigned MaxRays
)
3333 evalue
*factor
= NULL
;
3337 if (EVALUE_IS_ZERO(*e
))
3341 Polyhedron
*DD
= Disjoint_Domain(D
, 0, MaxRays
);
3348 res
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3351 for (Q
= DD
; Q
; Q
= DD
) {
3357 t
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3370 if (value_notzero_p(e
->d
)) {
3373 t
= esum_over_domain_cst(nvar
, D
, C
);
3375 if (!EVALUE_IS_ONE(*e
))
3381 switch (e
->x
.p
->type
) {
3383 evalue
*pp
= &e
->x
.p
->arr
[0];
3385 if (pp
->x
.p
->pos
> nvar
) {
3386 /* remainder is independent of the summated vars */
3392 floor2frac(&f
, nvar
);
3394 t
= esum_over_domain_cst(nvar
, D
, C
);
3398 free_evalue_refs(&f
);
3403 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3404 poly_denom(pp
, &row
->p
[1 + nvar
]);
3405 value_set_si(row
->p
[0], 1);
3406 for (pp
= &e
->x
.p
->arr
[0]; value_zero_p(pp
->d
);
3407 pp
= &pp
->x
.p
->arr
[0]) {
3409 assert(pp
->x
.p
->type
== polynomial
);
3411 if (pos
>= 1 + nvar
)
3413 value_assign(row
->p
[pos
], row
->p
[1+nvar
]);
3414 value_division(row
->p
[pos
], row
->p
[pos
], pp
->x
.p
->arr
[1].d
);
3415 value_multiply(row
->p
[pos
], row
->p
[pos
], pp
->x
.p
->arr
[1].x
.n
);
3417 value_assign(row
->p
[1 + D
->Dimension
+ 1], row
->p
[1+nvar
]);
3418 value_division(row
->p
[1 + D
->Dimension
+ 1],
3419 row
->p
[1 + D
->Dimension
+ 1],
3421 value_multiply(row
->p
[1 + D
->Dimension
+ 1],
3422 row
->p
[1 + D
->Dimension
+ 1],
3424 value_oppose(row
->p
[1 + nvar
], row
->p
[1 + nvar
]);
3428 int pos
= e
->x
.p
->pos
;
3431 factor
= ALLOC(evalue
);
3432 value_init(factor
->d
);
3433 value_set_si(factor
->d
, 0);
3434 factor
->x
.p
= new_enode(polynomial
, 2, pos
- nvar
);
3435 evalue_set_si(&factor
->x
.p
->arr
[0], 0, 1);
3436 evalue_set_si(&factor
->x
.p
->arr
[1], 1, 1);
3440 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3441 negative
= signs
[pos
-1] < 0;
3442 value_set_si(row
->p
[0], 1);
3444 value_set_si(row
->p
[pos
], -1);
3445 value_set_si(row
->p
[1 + nvar
], 1);
3447 value_set_si(row
->p
[pos
], 1);
3448 value_set_si(row
->p
[1 + nvar
], -1);
3456 offset
= type_offset(e
->x
.p
);
3458 res
= esum_over_domain(&e
->x
.p
->arr
[offset
], nvar
, D
, signs
, C
, MaxRays
);
3462 evalue_copy(&cum
, factor
);
3466 for (i
= 1; offset
+i
< e
->x
.p
->size
; ++i
) {
3470 C
= esum_add_constraint(nvar
, D
, C
, row
, negative
);
3476 Vector_Print(stderr, P_VALUE_FMT, row);
3478 Matrix_Print(stderr, P_VALUE_FMT, C);
3480 t
= esum_over_domain(&e
->x
.p
->arr
[offset
+i
], nvar
, D
, signs
, C
, MaxRays
);
3485 if (negative
&& (i
% 2))
3495 if (factor
&& offset
+i
+1 < e
->x
.p
->size
)
3502 free_evalue_refs(&cum
);
3503 evalue_free(factor
);
3514 static void domain_signs(Polyhedron
*D
, int *signs
)
3518 POL_ENSURE_VERTICES(D
);
3519 for (j
= 0; j
< D
->Dimension
; ++j
) {
3521 for (k
= 0; k
< D
->NbRays
; ++k
) {
3522 signs
[j
] = value_sign(D
->Ray
[k
][1+j
]);
3529 static void shift_floor_in_domain(evalue
*e
, Polyhedron
*D
)
3536 if (value_notzero_p(e
->d
))
3541 for (i
= type_offset(p
); i
< p
->size
; ++i
)
3542 shift_floor_in_domain(&p
->arr
[i
], D
);
3544 if (p
->type
!= flooring
)
3550 I
= polynomial_projection(p
, D
, &d
, NULL
);
3551 assert(I
->NbEq
== 0); /* Should have been reduced */
3553 for (i
= 0; i
< I
->NbConstraints
; ++i
)
3554 if (value_pos_p(I
->Constraint
[i
][1]))
3556 assert(i
< I
->NbConstraints
);
3557 if (i
< I
->NbConstraints
) {
3558 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
3559 mpz_fdiv_q(m
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
3560 if (value_neg_p(m
)) {
3561 /* replace [e] by [e-m]+m such that e-m >= 0 */
3566 value_set_si(f
.d
, 1);
3567 value_oppose(f
.x
.n
, m
);
3568 eadd(&f
, &p
->arr
[0]);
3571 value_set_si(f
.d
, 0);
3572 f
.x
.p
= new_enode(flooring
, 3, -1);
3573 value_clear(f
.x
.p
->arr
[0].d
);
3574 f
.x
.p
->arr
[0] = p
->arr
[0];
3575 evalue_set_si(&f
.x
.p
->arr
[2], 1, 1);
3576 value_set_si(f
.x
.p
->arr
[1].d
, 1);
3577 value_init(f
.x
.p
->arr
[1].x
.n
);
3578 value_assign(f
.x
.p
->arr
[1].x
.n
, m
);
3579 reorder_terms_about(p
, &f
);
3590 /* Make arguments of all floors non-negative */
3591 static void shift_floor_arguments(evalue
*e
)
3595 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
3598 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3599 shift_floor_in_domain(&e
->x
.p
->arr
[2*i
+1],
3600 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]));
3603 evalue
*evalue_sum(evalue
*e
, int nvar
, unsigned MaxRays
)
3607 evalue
*res
= ALLOC(evalue
);
3611 if (nvar
== 0 || EVALUE_IS_ZERO(*e
)) {
3612 evalue_copy(res
, e
);
3616 evalue_split_domains_into_orthants(e
, MaxRays
);
3618 evalue_frac2floor2(e
, 0);
3619 evalue_set_si(res
, 0, 1);
3621 assert(value_zero_p(e
->d
));
3622 assert(e
->x
.p
->type
== partition
);
3623 shift_floor_arguments(e
);
3625 assert(e
->x
.p
->size
>= 2);
3626 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
3628 signs
= alloca(sizeof(int) * dim
);
3630 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
3632 domain_signs(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), signs
);
3633 t
= esum_over_domain(&e
->x
.p
->arr
[2*i
+1], nvar
,
3634 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), signs
, 0,
3645 evalue
*esum(evalue
*e
, int nvar
)
3647 return evalue_sum(e
, nvar
, 0);
3650 /* Initial silly implementation */
3651 void eor(evalue
*e1
, evalue
*res
)
3657 evalue_set_si(&mone
, -1, 1);
3659 evalue_copy(&E
, res
);
3665 free_evalue_refs(&E
);
3666 free_evalue_refs(&mone
);
3669 /* computes denominator of polynomial evalue
3670 * d should point to a value initialized to 1
3672 void evalue_denom(const evalue
*e
, Value
*d
)
3676 if (value_notzero_p(e
->d
)) {
3677 value_lcm(*d
, *d
, e
->d
);
3680 assert(e
->x
.p
->type
== polynomial
||
3681 e
->x
.p
->type
== fractional
||
3682 e
->x
.p
->type
== flooring
);
3683 offset
= type_offset(e
->x
.p
);
3684 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3685 evalue_denom(&e
->x
.p
->arr
[i
], d
);
3688 /* Divides the evalue e by the integer n */
3689 void evalue_div(evalue
*e
, Value n
)
3693 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3696 if (value_notzero_p(e
->d
)) {
3699 value_multiply(e
->d
, e
->d
, n
);
3700 value_gcd(gc
, e
->x
.n
, e
->d
);
3701 if (value_notone_p(gc
)) {
3702 value_division(e
->d
, e
->d
, gc
);
3703 value_division(e
->x
.n
, e
->x
.n
, gc
);
3708 if (e
->x
.p
->type
== partition
) {
3709 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3710 evalue_div(&e
->x
.p
->arr
[2*i
+1], n
);
3713 offset
= type_offset(e
->x
.p
);
3714 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3715 evalue_div(&e
->x
.p
->arr
[i
], n
);
3718 /* Multiplies the evalue e by the integer n */
3719 void evalue_mul(evalue
*e
, Value n
)
3723 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3726 if (value_notzero_p(e
->d
)) {
3729 value_multiply(e
->x
.n
, e
->x
.n
, n
);
3730 value_gcd(gc
, e
->x
.n
, e
->d
);
3731 if (value_notone_p(gc
)) {
3732 value_division(e
->d
, e
->d
, gc
);
3733 value_division(e
->x
.n
, e
->x
.n
, gc
);
3738 if (e
->x
.p
->type
== partition
) {
3739 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3740 evalue_mul(&e
->x
.p
->arr
[2*i
+1], n
);
3743 offset
= type_offset(e
->x
.p
);
3744 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3745 evalue_mul(&e
->x
.p
->arr
[i
], n
);
3748 /* Multiplies the evalue e by the n/d */
3749 void evalue_mul_div(evalue
*e
, Value n
, Value d
)
3753 if ((value_one_p(n
) && value_one_p(d
)) || EVALUE_IS_ZERO(*e
))
3756 if (value_notzero_p(e
->d
)) {
3759 value_multiply(e
->x
.n
, e
->x
.n
, n
);
3760 value_multiply(e
->d
, e
->d
, d
);
3761 value_gcd(gc
, e
->x
.n
, e
->d
);
3762 if (value_notone_p(gc
)) {
3763 value_division(e
->d
, e
->d
, gc
);
3764 value_division(e
->x
.n
, e
->x
.n
, gc
);
3769 if (e
->x
.p
->type
== partition
) {
3770 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3771 evalue_mul_div(&e
->x
.p
->arr
[2*i
+1], n
, d
);
3774 offset
= type_offset(e
->x
.p
);
3775 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3776 evalue_mul_div(&e
->x
.p
->arr
[i
], n
, d
);
3779 void evalue_negate(evalue
*e
)
3783 if (value_notzero_p(e
->d
)) {
3784 value_oppose(e
->x
.n
, e
->x
.n
);
3787 if (e
->x
.p
->type
== partition
) {
3788 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3789 evalue_negate(&e
->x
.p
->arr
[2*i
+1]);
3792 offset
= type_offset(e
->x
.p
);
3793 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3794 evalue_negate(&e
->x
.p
->arr
[i
]);
3797 void evalue_add_constant(evalue
*e
, const Value cst
)
3801 if (value_zero_p(e
->d
)) {
3802 if (e
->x
.p
->type
== partition
) {
3803 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3804 evalue_add_constant(&e
->x
.p
->arr
[2*i
+1], cst
);
3807 if (e
->x
.p
->type
== relation
) {
3808 for (i
= 1; i
< e
->x
.p
->size
; ++i
)
3809 evalue_add_constant(&e
->x
.p
->arr
[i
], cst
);
3813 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)];
3814 } while (value_zero_p(e
->d
));
3816 value_addmul(e
->x
.n
, cst
, e
->d
);
3819 static void evalue_frac2polynomial_r(evalue
*e
, int *signs
, int sign
, int in_frac
)
3824 int sign_odd
= sign
;
3826 if (value_notzero_p(e
->d
)) {
3827 if (in_frac
&& sign
* value_sign(e
->x
.n
) < 0) {
3828 value_set_si(e
->x
.n
, 0);
3829 value_set_si(e
->d
, 1);
3834 if (e
->x
.p
->type
== relation
) {
3835 for (i
= e
->x
.p
->size
-1; i
>= 1; --i
)
3836 evalue_frac2polynomial_r(&e
->x
.p
->arr
[i
], signs
, sign
, in_frac
);
3840 if (e
->x
.p
->type
== polynomial
)
3841 sign_odd
*= signs
[e
->x
.p
->pos
-1];
3842 offset
= type_offset(e
->x
.p
);
3843 evalue_frac2polynomial_r(&e
->x
.p
->arr
[offset
], signs
, sign
, in_frac
);
3844 in_frac
|= e
->x
.p
->type
== fractional
;
3845 for (i
= e
->x
.p
->size
-1; i
> offset
; --i
)
3846 evalue_frac2polynomial_r(&e
->x
.p
->arr
[i
], signs
,
3847 (i
- offset
) % 2 ? sign_odd
: sign
, in_frac
);
3849 if (e
->x
.p
->type
!= fractional
)
3852 /* replace { a/m } by (m-1)/m if sign != 0
3853 * and by (m-1)/(2m) if sign == 0
3857 evalue_denom(&e
->x
.p
->arr
[0], &d
);
3858 free_evalue_refs(&e
->x
.p
->arr
[0]);
3859 value_init(e
->x
.p
->arr
[0].d
);
3860 value_init(e
->x
.p
->arr
[0].x
.n
);
3862 value_addto(e
->x
.p
->arr
[0].d
, d
, d
);
3864 value_assign(e
->x
.p
->arr
[0].d
, d
);
3865 value_decrement(e
->x
.p
->arr
[0].x
.n
, d
);
3869 reorder_terms_about(p
, &p
->arr
[0]);
3875 /* Approximate the evalue in fractional representation by a polynomial.
3876 * If sign > 0, the result is an upper bound;
3877 * if sign < 0, the result is a lower bound;
3878 * if sign = 0, the result is an intermediate approximation.
3880 void evalue_frac2polynomial(evalue
*e
, int sign
, unsigned MaxRays
)
3885 if (value_notzero_p(e
->d
))
3887 assert(e
->x
.p
->type
== partition
);
3888 /* make sure all variables in the domains have a fixed sign */
3890 evalue_split_domains_into_orthants(e
, MaxRays
);
3891 if (EVALUE_IS_ZERO(*e
))
3895 assert(e
->x
.p
->size
>= 2);
3896 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
3898 signs
= alloca(sizeof(int) * dim
);
3901 for (i
= 0; i
< dim
; ++i
)
3903 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
3905 domain_signs(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), signs
);
3906 evalue_frac2polynomial_r(&e
->x
.p
->arr
[2*i
+1], signs
, sign
, 0);
3910 /* Split the domains of e (which is assumed to be a partition)
3911 * such that each resulting domain lies entirely in one orthant.
3913 void evalue_split_domains_into_orthants(evalue
*e
, unsigned MaxRays
)
3916 assert(value_zero_p(e
->d
));
3917 assert(e
->x
.p
->type
== partition
);
3918 assert(e
->x
.p
->size
>= 2);
3919 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
3921 for (i
= 0; i
< dim
; ++i
) {
3924 C
= Matrix_Alloc(1, 1 + dim
+ 1);
3925 value_set_si(C
->p
[0][0], 1);
3926 value_init(split
.d
);
3927 value_set_si(split
.d
, 0);
3928 split
.x
.p
= new_enode(partition
, 4, dim
);
3929 value_set_si(C
->p
[0][1+i
], 1);
3930 C2
= Matrix_Copy(C
);
3931 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0], Constraints2Polyhedron(C2
, MaxRays
));
3933 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
3934 value_set_si(C
->p
[0][1+i
], -1);
3935 value_set_si(C
->p
[0][1+dim
], -1);
3936 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2], Constraints2Polyhedron(C
, MaxRays
));
3937 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
3939 free_evalue_refs(&split
);
3944 static evalue
*find_fractional_with_max_periods(evalue
*e
, Polyhedron
*D
,
3947 Value
*min
, Value
*max
)
3954 if (value_notzero_p(e
->d
))
3957 if (e
->x
.p
->type
== fractional
) {
3962 I
= polynomial_projection(e
->x
.p
, D
, &d
, &T
);
3963 bounded
= line_minmax(I
, min
, max
); /* frees I */
3967 value_set_si(mp
, max_periods
);
3968 mpz_fdiv_q(*min
, *min
, d
);
3969 mpz_fdiv_q(*max
, *max
, d
);
3970 value_assign(T
->p
[1][D
->Dimension
], d
);
3971 value_subtract(d
, *max
, *min
);
3972 if (value_ge(d
, mp
))
3975 f
= evalue_dup(&e
->x
.p
->arr
[0]);
3986 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
3987 if ((f
= find_fractional_with_max_periods(&e
->x
.p
->arr
[i
], D
, max_periods
,
3994 static void replace_fract_by_affine(evalue
*e
, evalue
*f
, Value val
)
3998 if (value_notzero_p(e
->d
))
4001 offset
= type_offset(e
->x
.p
);
4002 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
4003 replace_fract_by_affine(&e
->x
.p
->arr
[i
], f
, val
);
4005 if (e
->x
.p
->type
!= fractional
)
4008 if (!eequal(&e
->x
.p
->arr
[0], f
))
4011 replace_by_affine(e
, val
);
4014 /* Look for fractional parts that can be removed by splitting the corresponding
4015 * domain into at most max_periods parts.
4016 * We use a very simply strategy that looks for the first fractional part
4017 * that satisfies the condition, performs the split and then continues
4018 * looking for other fractional parts in the split domains until no
4019 * such fractional part can be found anymore.
4021 void evalue_split_periods(evalue
*e
, int max_periods
, unsigned int MaxRays
)
4028 if (EVALUE_IS_ZERO(*e
))
4030 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
) {
4032 "WARNING: evalue_split_periods called on incorrect evalue type\n");
4040 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
4045 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
4047 f
= find_fractional_with_max_periods(&e
->x
.p
->arr
[2*i
+1], D
, max_periods
,
4052 M
= Matrix_Alloc(2, 2+D
->Dimension
);
4054 value_subtract(d
, max
, min
);
4055 n
= VALUE_TO_INT(d
)+1;
4057 value_set_si(M
->p
[0][0], 1);
4058 Vector_Copy(T
->p
[0], M
->p
[0]+1, D
->Dimension
+1);
4059 value_multiply(d
, max
, T
->p
[1][D
->Dimension
]);
4060 value_subtract(M
->p
[0][1+D
->Dimension
], M
->p
[0][1+D
->Dimension
], d
);
4061 value_set_si(d
, -1);
4062 value_set_si(M
->p
[1][0], 1);
4063 Vector_Scale(T
->p
[0], M
->p
[1]+1, d
, D
->Dimension
+1);
4064 value_addmul(M
->p
[1][1+D
->Dimension
], max
, T
->p
[1][D
->Dimension
]);
4065 value_addto(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
],
4066 T
->p
[1][D
->Dimension
]);
4067 value_decrement(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
]);
4069 p
= new_enode(partition
, e
->x
.p
->size
+ (n
-1)*2, e
->x
.p
->pos
);
4070 for (j
= 0; j
< 2*i
; ++j
) {
4071 value_clear(p
->arr
[j
].d
);
4072 p
->arr
[j
] = e
->x
.p
->arr
[j
];
4074 for (j
= 2*i
+2; j
< e
->x
.p
->size
; ++j
) {
4075 value_clear(p
->arr
[j
+2*(n
-1)].d
);
4076 p
->arr
[j
+2*(n
-1)] = e
->x
.p
->arr
[j
];
4078 for (j
= n
-1; j
>= 0; --j
) {
4080 value_clear(p
->arr
[2*i
+1].d
);
4081 p
->arr
[2*i
+1] = e
->x
.p
->arr
[2*i
+1];
4083 evalue_copy(&p
->arr
[2*(i
+j
)+1], &e
->x
.p
->arr
[2*i
+1]);
4085 value_subtract(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
],
4086 T
->p
[1][D
->Dimension
]);
4087 value_addto(M
->p
[0][1+D
->Dimension
], M
->p
[0][1+D
->Dimension
],
4088 T
->p
[1][D
->Dimension
]);
4090 replace_fract_by_affine(&p
->arr
[2*(i
+j
)+1], f
, max
);
4091 E
= DomainAddConstraints(D
, M
, MaxRays
);
4092 EVALUE_SET_DOMAIN(p
->arr
[2*(i
+j
)], E
);
4093 if (evalue_range_reduction_in_domain(&p
->arr
[2*(i
+j
)+1], E
))
4094 reduce_evalue(&p
->arr
[2*(i
+j
)+1]);
4095 value_decrement(max
, max
);
4097 value_clear(e
->x
.p
->arr
[2*i
].d
);
4112 void evalue_extract_affine(const evalue
*e
, Value
*coeff
, Value
*cst
, Value
*d
)
4114 value_set_si(*d
, 1);
4116 for ( ; value_zero_p(e
->d
); e
= &e
->x
.p
->arr
[0]) {
4118 assert(e
->x
.p
->type
== polynomial
);
4119 assert(e
->x
.p
->size
== 2);
4120 c
= &e
->x
.p
->arr
[1];
4121 value_multiply(coeff
[e
->x
.p
->pos
-1], *d
, c
->x
.n
);
4122 value_division(coeff
[e
->x
.p
->pos
-1], coeff
[e
->x
.p
->pos
-1], c
->d
);
4124 value_multiply(*cst
, *d
, e
->x
.n
);
4125 value_division(*cst
, *cst
, e
->d
);
4128 /* returns an evalue that corresponds to
4132 static evalue
*term(int param
, Value c
, Value den
)
4134 evalue
*EP
= ALLOC(evalue
);
4136 value_set_si(EP
->d
,0);
4137 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
4138 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
4139 value_init(EP
->x
.p
->arr
[1].x
.n
);
4140 value_assign(EP
->x
.p
->arr
[1].d
, den
);
4141 value_assign(EP
->x
.p
->arr
[1].x
.n
, c
);
4145 evalue
*affine2evalue(Value
*coeff
, Value denom
, int nvar
)
4148 evalue
*E
= ALLOC(evalue
);
4150 evalue_set(E
, coeff
[nvar
], denom
);
4151 for (i
= 0; i
< nvar
; ++i
) {
4153 if (value_zero_p(coeff
[i
]))
4155 t
= term(i
, coeff
[i
], denom
);
4162 void evalue_substitute(evalue
*e
, evalue
**subs
)
4168 if (value_notzero_p(e
->d
))
4172 assert(p
->type
!= partition
);
4174 for (i
= 0; i
< p
->size
; ++i
)
4175 evalue_substitute(&p
->arr
[i
], subs
);
4177 if (p
->type
== relation
) {
4178 /* For relation a ? b : c, compute (a' ? 1) * b' + (a' ? 0 : 1) * c' */
4182 value_set_si(v
->d
, 0);
4183 v
->x
.p
= new_enode(relation
, 3, 0);
4184 evalue_copy(&v
->x
.p
->arr
[0], &p
->arr
[0]);
4185 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4186 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4187 emul(v
, &p
->arr
[2]);
4192 value_set_si(v
->d
, 0);
4193 v
->x
.p
= new_enode(relation
, 2, 0);
4194 value_clear(v
->x
.p
->arr
[0].d
);
4195 v
->x
.p
->arr
[0] = p
->arr
[0];
4196 evalue_set_si(&v
->x
.p
->arr
[1], 1, 1);
4197 emul(v
, &p
->arr
[1]);
4200 eadd(&p
->arr
[2], &p
->arr
[1]);
4201 free_evalue_refs(&p
->arr
[2]);
4209 if (p
->type
== polynomial
)
4214 value_set_si(v
->d
, 0);
4215 v
->x
.p
= new_enode(p
->type
, 3, -1);
4216 value_clear(v
->x
.p
->arr
[0].d
);
4217 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);
4222 offset
= type_offset(p
);
4224 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
4225 emul(v
, &p
->arr
[i
]);
4226 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
4227 free_evalue_refs(&(p
->arr
[i
]));
4230 if (p
->type
!= polynomial
)
4234 *e
= p
->arr
[offset
];
4238 /* evalue e is given in terms of "new" parameter; CP maps the new
4239 * parameters back to the old parameters.
4240 * Transforms e such that it refers back to the old parameters and
4241 * adds appropriate constraints to the domain.
4242 * In particular, if CP maps the new parameters onto an affine
4243 * subspace of the old parameters, then the corresponding equalities
4244 * are added to the domain.
4245 * Also, if any of the new parameters was a rational combination
4246 * of the old parameters $p' = (<a, p> + c)/m$, then modulo
4247 * constraints ${<a, p> + c)/m} = 0$ are added to ensure
4248 * the new evalue remains non-zero only for integer parameters
4249 * of the new parameters (which have been removed by the substitution).
4251 void evalue_backsubstitute(evalue
*e
, Matrix
*CP
, unsigned MaxRays
)
4258 unsigned nparam
= CP
->NbColumns
-1;
4262 if (EVALUE_IS_ZERO(*e
))
4265 assert(value_zero_p(e
->d
));
4267 assert(p
->type
== partition
);
4269 inv
= left_inverse(CP
, &eq
);
4270 subs
= ALLOCN(evalue
*, nparam
);
4271 for (i
= 0; i
< nparam
; ++i
)
4272 subs
[i
] = affine2evalue(inv
->p
[i
], inv
->p
[nparam
][inv
->NbColumns
-1],
4275 CEq
= Constraints2Polyhedron(eq
, MaxRays
);
4276 addeliminatedparams_partition(p
, inv
, CEq
, inv
->NbColumns
-1, MaxRays
);
4277 Polyhedron_Free(CEq
);
4279 for (i
= 0; i
< p
->size
/2; ++i
)
4280 evalue_substitute(&p
->arr
[2*i
+1], subs
);
4282 for (i
= 0; i
< nparam
; ++i
)
4283 evalue_free(subs
[i
]);
4287 for (i
= 0; i
< inv
->NbRows
-1; ++i
) {
4288 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &gcd
);
4289 value_gcd(gcd
, gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
4290 if (value_eq(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
4292 Vector_AntiScale(inv
->p
[i
], inv
->p
[i
], gcd
, inv
->NbColumns
);
4293 value_divexact(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1], gcd
);
4295 for (j
= 0; j
< p
->size
/2; ++j
) {
4296 evalue
*arg
= affine2evalue(inv
->p
[i
], gcd
, inv
->NbColumns
-1);
4301 value_set_si(rel
.d
, 0);
4302 rel
.x
.p
= new_enode(relation
, 2, 0);
4303 value_clear(rel
.x
.p
->arr
[1].d
);
4304 rel
.x
.p
->arr
[1] = p
->arr
[2*j
+1];
4305 ev
= &rel
.x
.p
->arr
[0];
4306 value_set_si(ev
->d
, 0);
4307 ev
->x
.p
= new_enode(fractional
, 3, -1);
4308 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
4309 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
4310 value_clear(ev
->x
.p
->arr
[0].d
);
4311 ev
->x
.p
->arr
[0] = *arg
;
4314 p
->arr
[2*j
+1] = rel
;
4325 * \sum_{i=0}^n c_i/d X^i
4327 * where d is the last element in the vector c.
4329 evalue
*evalue_polynomial(Vector
*c
, const evalue
* X
)
4331 unsigned dim
= c
->Size
-2;
4333 evalue
*EP
= ALLOC(evalue
);
4338 if (EVALUE_IS_ZERO(*X
) || dim
== 0) {
4339 evalue_set(EP
, c
->p
[0], c
->p
[dim
+1]);
4340 reduce_constant(EP
);
4344 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
4347 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
4349 for (i
= dim
-1; i
>= 0; --i
) {
4351 value_assign(EC
.x
.n
, c
->p
[i
]);
4354 free_evalue_refs(&EC
);
4358 /* Create an evalue from an array of pairs of domains and evalues. */
4359 evalue
*evalue_from_section_array(struct evalue_section
*s
, int n
)
4364 res
= ALLOC(evalue
);
4368 evalue_set_si(res
, 0, 1);
4370 value_set_si(res
->d
, 0);
4371 res
->x
.p
= new_enode(partition
, 2*n
, s
[0].D
->Dimension
);
4372 for (i
= 0; i
< n
; ++i
) {
4373 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*i
], s
[i
].D
);
4374 value_clear(res
->x
.p
->arr
[2*i
+1].d
);
4375 res
->x
.p
->arr
[2*i
+1] = *s
[i
].E
;