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>
21 #ifndef value_pmodulus
22 #define value_pmodulus(ref,val1,val2) (mpz_fdiv_r((ref),(val1),(val2)))
25 #define ALLOC(type) (type*)malloc(sizeof(type))
26 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
29 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
31 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
34 void evalue_set_si(evalue
*ev
, int n
, int d
) {
35 value_set_si(ev
->d
, d
);
37 value_set_si(ev
->x
.n
, n
);
40 void evalue_set(evalue
*ev
, Value n
, Value d
) {
41 value_assign(ev
->d
, d
);
43 value_assign(ev
->x
.n
, n
);
48 evalue
*EP
= ALLOC(evalue
);
50 evalue_set_si(EP
, 0, 1);
56 evalue
*EP
= ALLOC(evalue
);
58 value_set_si(EP
->d
, -2);
63 /* returns an evalue that corresponds to
67 evalue
*evalue_var(int var
)
69 evalue
*EP
= ALLOC(evalue
);
71 value_set_si(EP
->d
,0);
72 EP
->x
.p
= new_enode(polynomial
, 2, var
+ 1);
73 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
74 evalue_set_si(&EP
->x
.p
->arr
[1], 1, 1);
78 void aep_evalue(evalue
*e
, int *ref
) {
83 if (value_notzero_p(e
->d
))
84 return; /* a rational number, its already reduced */
86 return; /* hum... an overflow probably occured */
88 /* First check the components of p */
89 for (i
=0;i
<p
->size
;i
++)
90 aep_evalue(&p
->arr
[i
],ref
);
97 p
->pos
= ref
[p
->pos
-1]+1;
103 void addeliminatedparams_evalue(evalue
*e
,Matrix
*CT
) {
109 if (value_notzero_p(e
->d
))
110 return; /* a rational number, its already reduced */
112 return; /* hum... an overflow probably occured */
115 ref
= (int *)malloc(sizeof(int)*(CT
->NbRows
-1));
116 for(i
=0;i
<CT
->NbRows
-1;i
++)
117 for(j
=0;j
<CT
->NbColumns
;j
++)
118 if(value_notzero_p(CT
->p
[i
][j
])) {
123 /* Transform the references in e, using ref */
127 } /* addeliminatedparams_evalue */
129 static void addeliminatedparams_partition(enode
*p
, Matrix
*CT
, Polyhedron
*CEq
,
130 unsigned nparam
, unsigned MaxRays
)
133 assert(p
->type
== partition
);
136 for (i
= 0; i
< p
->size
/2; i
++) {
137 Polyhedron
*D
= EVALUE_DOMAIN(p
->arr
[2*i
]);
138 Polyhedron
*T
= DomainPreimage(D
, CT
, MaxRays
);
142 T
= DomainIntersection(D
, CEq
, MaxRays
);
145 EVALUE_SET_DOMAIN(p
->arr
[2*i
], T
);
149 void addeliminatedparams_enum(evalue
*e
, Matrix
*CT
, Polyhedron
*CEq
,
150 unsigned MaxRays
, unsigned nparam
)
155 if (CT
->NbRows
== CT
->NbColumns
)
158 if (EVALUE_IS_ZERO(*e
))
161 if (value_notzero_p(e
->d
)) {
164 value_set_si(res
.d
, 0);
165 res
.x
.p
= new_enode(partition
, 2, nparam
);
166 EVALUE_SET_DOMAIN(res
.x
.p
->arr
[0],
167 DomainConstraintSimplify(Polyhedron_Copy(CEq
), MaxRays
));
168 value_clear(res
.x
.p
->arr
[1].d
);
169 res
.x
.p
->arr
[1] = *e
;
177 addeliminatedparams_partition(p
, CT
, CEq
, nparam
, MaxRays
);
178 for (i
= 0; i
< p
->size
/2; i
++)
179 addeliminatedparams_evalue(&p
->arr
[2*i
+1], CT
);
182 static int mod_rational_smaller(evalue
*e1
, evalue
*e2
)
190 assert(value_notzero_p(e1
->d
));
191 assert(value_notzero_p(e2
->d
));
192 value_multiply(m
, e1
->x
.n
, e2
->d
);
193 value_multiply(m2
, e2
->x
.n
, e1
->d
);
196 else if (value_gt(m
, m2
))
206 static int mod_term_smaller_r(evalue
*e1
, evalue
*e2
)
208 if (value_notzero_p(e1
->d
)) {
210 if (value_zero_p(e2
->d
))
212 r
= mod_rational_smaller(e1
, e2
);
213 return r
== -1 ? 0 : r
;
215 if (value_notzero_p(e2
->d
))
217 if (e1
->x
.p
->pos
< e2
->x
.p
->pos
)
219 else if (e1
->x
.p
->pos
> e2
->x
.p
->pos
)
222 int r
= mod_rational_smaller(&e1
->x
.p
->arr
[1], &e2
->x
.p
->arr
[1]);
224 ? mod_term_smaller_r(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0])
229 static int mod_term_smaller(const evalue
*e1
, const evalue
*e2
)
231 assert(value_zero_p(e1
->d
));
232 assert(value_zero_p(e2
->d
));
233 assert(e1
->x
.p
->type
== fractional
|| e1
->x
.p
->type
== flooring
);
234 assert(e2
->x
.p
->type
== fractional
|| e2
->x
.p
->type
== flooring
);
235 return mod_term_smaller_r(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]);
238 static void check_order(const evalue
*e
)
243 if (value_notzero_p(e
->d
))
246 switch (e
->x
.p
->type
) {
248 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
249 check_order(&e
->x
.p
->arr
[2*i
+1]);
252 for (i
= 1; i
< e
->x
.p
->size
; ++i
) {
254 if (value_notzero_p(a
->d
))
256 switch (a
->x
.p
->type
) {
258 assert(mod_term_smaller(&e
->x
.p
->arr
[0], &a
->x
.p
->arr
[0]));
267 for (i
= 0; i
< e
->x
.p
->size
; ++i
) {
269 if (value_notzero_p(a
->d
))
271 switch (a
->x
.p
->type
) {
273 assert(e
->x
.p
->pos
< a
->x
.p
->pos
);
284 for (i
= 1; i
< e
->x
.p
->size
; ++i
) {
286 if (value_notzero_p(a
->d
))
288 switch (a
->x
.p
->type
) {
299 /* Negative pos means inequality */
300 /* s is negative of substitution if m is not zero */
309 struct fixed_param
*fixed
;
314 static int relations_depth(evalue
*e
)
319 value_zero_p(e
->d
) && e
->x
.p
->type
== relation
;
320 e
= &e
->x
.p
->arr
[1], ++d
);
324 static void poly_denom_not_constant(evalue
**pp
, Value
*d
)
329 while (value_zero_p(p
->d
)) {
330 assert(p
->x
.p
->type
== polynomial
);
331 assert(p
->x
.p
->size
== 2);
332 assert(value_notzero_p(p
->x
.p
->arr
[1].d
));
333 value_lcm(*d
, *d
, p
->x
.p
->arr
[1].d
);
339 static void poly_denom(evalue
*p
, Value
*d
)
341 poly_denom_not_constant(&p
, d
);
342 value_lcm(*d
, *d
, p
->d
);
345 static void realloc_substitution(struct subst
*s
, int d
)
347 struct fixed_param
*n
;
350 for (i
= 0; i
< s
->n
; ++i
)
357 static int add_modulo_substitution(struct subst
*s
, evalue
*r
)
363 assert(value_zero_p(r
->d
) && r
->x
.p
->type
== relation
);
366 /* May have been reduced already */
367 if (value_notzero_p(m
->d
))
370 assert(value_zero_p(m
->d
) && m
->x
.p
->type
== fractional
);
371 assert(m
->x
.p
->size
== 3);
373 /* fractional was inverted during reduction
374 * invert it back and move constant in
376 if (!EVALUE_IS_ONE(m
->x
.p
->arr
[2])) {
377 assert(value_pos_p(m
->x
.p
->arr
[2].d
));
378 assert(value_mone_p(m
->x
.p
->arr
[2].x
.n
));
379 value_set_si(m
->x
.p
->arr
[2].x
.n
, 1);
380 value_increment(m
->x
.p
->arr
[1].x
.n
, m
->x
.p
->arr
[1].x
.n
);
381 assert(value_eq(m
->x
.p
->arr
[1].x
.n
, m
->x
.p
->arr
[1].d
));
382 value_set_si(m
->x
.p
->arr
[1].x
.n
, 1);
383 eadd(&m
->x
.p
->arr
[1], &m
->x
.p
->arr
[0]);
384 value_set_si(m
->x
.p
->arr
[1].x
.n
, 0);
385 value_set_si(m
->x
.p
->arr
[1].d
, 1);
388 /* Oops. Nested identical relations. */
389 if (!EVALUE_IS_ZERO(m
->x
.p
->arr
[1]))
392 if (s
->n
>= s
->max
) {
393 int d
= relations_depth(r
);
394 realloc_substitution(s
, d
);
398 assert(value_zero_p(p
->d
) && p
->x
.p
->type
== polynomial
);
399 assert(p
->x
.p
->size
== 2);
402 assert(value_pos_p(f
->x
.n
));
404 value_init(s
->fixed
[s
->n
].m
);
405 value_assign(s
->fixed
[s
->n
].m
, f
->d
);
406 s
->fixed
[s
->n
].pos
= p
->x
.p
->pos
;
407 value_init(s
->fixed
[s
->n
].d
);
408 value_assign(s
->fixed
[s
->n
].d
, f
->x
.n
);
409 value_init(s
->fixed
[s
->n
].s
.d
);
410 evalue_copy(&s
->fixed
[s
->n
].s
, &p
->x
.p
->arr
[0]);
416 static int type_offset(enode
*p
)
418 return p
->type
== fractional
? 1 :
419 p
->type
== flooring
? 1 :
420 p
->type
== relation
? 1 : 0;
423 static void reorder_terms_about(enode
*p
, evalue
*v
)
426 int offset
= type_offset(p
);
428 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
430 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
431 free_evalue_refs(&(p
->arr
[i
]));
437 void evalue_reorder_terms(evalue
*e
)
443 assert(value_zero_p(e
->d
));
445 assert(p
->type
== fractional
||
446 p
->type
== flooring
||
447 p
->type
== polynomial
); /* for now */
449 offset
= type_offset(p
);
451 value_set_si(f
.d
, 0);
452 f
.x
.p
= new_enode(p
->type
, offset
+2, p
->pos
);
454 value_clear(f
.x
.p
->arr
[0].d
);
455 f
.x
.p
->arr
[0] = p
->arr
[0];
457 evalue_set_si(&f
.x
.p
->arr
[offset
], 0, 1);
458 evalue_set_si(&f
.x
.p
->arr
[offset
+1], 1, 1);
459 reorder_terms_about(p
, &f
);
465 static void evalue_reduce_size(evalue
*e
)
469 assert(value_zero_p(e
->d
));
472 offset
= type_offset(p
);
474 /* Try to reduce the degree */
475 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
476 if (!EVALUE_IS_ZERO(p
->arr
[i
]))
478 free_evalue_refs(&p
->arr
[i
]);
483 /* Try to reduce its strength */
484 if (p
->type
== relation
) {
486 free_evalue_refs(&p
->arr
[0]);
487 evalue_set_si(e
, 0, 1);
490 } else if (p
->size
== offset
+1) {
492 memcpy(e
, &p
->arr
[offset
], sizeof(evalue
));
494 free_evalue_refs(&p
->arr
[0]);
499 void _reduce_evalue (evalue
*e
, struct subst
*s
, int fract
) {
505 if (value_notzero_p(e
->d
)) {
507 mpz_fdiv_r(e
->x
.n
, e
->x
.n
, e
->d
);
508 return; /* a rational number, its already reduced */
512 return; /* hum... an overflow probably occured */
514 /* First reduce the components of p */
515 add
= p
->type
== relation
;
516 for (i
=0; i
<p
->size
; i
++) {
518 add
= add_modulo_substitution(s
, e
);
520 if (i
== 0 && p
->type
==fractional
)
521 _reduce_evalue(&p
->arr
[i
], s
, 1);
523 _reduce_evalue(&p
->arr
[i
], s
, fract
);
525 if (add
&& i
== p
->size
-1) {
527 value_clear(s
->fixed
[s
->n
].m
);
528 value_clear(s
->fixed
[s
->n
].d
);
529 free_evalue_refs(&s
->fixed
[s
->n
].s
);
530 } else if (add
&& i
== 1)
531 s
->fixed
[s
->n
-1].pos
*= -1;
534 if (p
->type
==periodic
) {
536 /* Try to reduce the period */
537 for (i
=1; i
<=(p
->size
)/2; i
++) {
538 if ((p
->size
% i
)==0) {
540 /* Can we reduce the size to i ? */
542 for (k
=j
+i
; k
<e
->x
.p
->size
; k
+=i
)
543 if (!eequal(&p
->arr
[j
], &p
->arr
[k
])) goto you_lose
;
546 for (j
=i
; j
<p
->size
; j
++) free_evalue_refs(&p
->arr
[j
]);
550 you_lose
: /* OK, lets not do it */
555 /* Try to reduce its strength */
558 memcpy(e
,&p
->arr
[0],sizeof(evalue
));
562 else if (p
->type
==polynomial
) {
563 for (k
= 0; s
&& k
< s
->n
; ++k
) {
564 if (s
->fixed
[k
].pos
== p
->pos
) {
565 int divide
= value_notone_p(s
->fixed
[k
].d
);
568 if (value_notzero_p(s
->fixed
[k
].m
)) {
571 assert(p
->size
== 2);
572 if (divide
&& value_ne(s
->fixed
[k
].d
, p
->arr
[1].x
.n
))
574 if (!mpz_divisible_p(s
->fixed
[k
].m
, p
->arr
[1].d
))
581 value_assign(d
.d
, s
->fixed
[k
].d
);
583 if (value_notzero_p(s
->fixed
[k
].m
))
584 value_oppose(d
.x
.n
, s
->fixed
[k
].m
);
586 value_set_si(d
.x
.n
, 1);
589 for (i
=p
->size
-1;i
>=1;i
--) {
590 emul(&s
->fixed
[k
].s
, &p
->arr
[i
]);
592 emul(&d
, &p
->arr
[i
]);
593 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
594 free_evalue_refs(&(p
->arr
[i
]));
597 _reduce_evalue(&p
->arr
[0], s
, fract
);
600 free_evalue_refs(&d
);
606 evalue_reduce_size(e
);
608 else if (p
->type
==fractional
) {
612 if (value_notzero_p(p
->arr
[0].d
)) {
614 value_assign(v
.d
, p
->arr
[0].d
);
616 mpz_fdiv_r(v
.x
.n
, p
->arr
[0].x
.n
, p
->arr
[0].d
);
621 evalue
*pp
= &p
->arr
[0];
622 assert(value_zero_p(pp
->d
) && pp
->x
.p
->type
== polynomial
);
623 assert(pp
->x
.p
->size
== 2);
625 /* search for exact duplicate among the modulo inequalities */
627 f
= &pp
->x
.p
->arr
[1];
628 for (k
= 0; s
&& k
< s
->n
; ++k
) {
629 if (-s
->fixed
[k
].pos
== pp
->x
.p
->pos
&&
630 value_eq(s
->fixed
[k
].d
, f
->x
.n
) &&
631 value_eq(s
->fixed
[k
].m
, f
->d
) &&
632 eequal(&s
->fixed
[k
].s
, &pp
->x
.p
->arr
[0]))
639 /* replace { E/m } by { (E-1)/m } + 1/m */
644 evalue_set_si(&extra
, 1, 1);
645 value_assign(extra
.d
, g
);
646 eadd(&extra
, &v
.x
.p
->arr
[1]);
647 free_evalue_refs(&extra
);
649 /* We've been going in circles; stop now */
650 if (value_ge(v
.x
.p
->arr
[1].x
.n
, v
.x
.p
->arr
[1].d
)) {
651 free_evalue_refs(&v
);
653 evalue_set_si(&v
, 0, 1);
658 value_set_si(v
.d
, 0);
659 v
.x
.p
= new_enode(fractional
, 3, -1);
660 evalue_set_si(&v
.x
.p
->arr
[1], 1, 1);
661 value_assign(v
.x
.p
->arr
[1].d
, g
);
662 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
663 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
666 for (f
= &v
.x
.p
->arr
[0]; value_zero_p(f
->d
);
669 value_division(f
->d
, g
, f
->d
);
670 value_multiply(f
->x
.n
, f
->x
.n
, f
->d
);
671 value_assign(f
->d
, g
);
672 value_decrement(f
->x
.n
, f
->x
.n
);
673 mpz_fdiv_r(f
->x
.n
, f
->x
.n
, f
->d
);
675 value_gcd(g
, f
->d
, f
->x
.n
);
676 value_division(f
->d
, f
->d
, g
);
677 value_division(f
->x
.n
, f
->x
.n
, g
);
686 /* reduction may have made this fractional arg smaller */
687 i
= reorder
? p
->size
: 1;
688 for ( ; i
< p
->size
; ++i
)
689 if (value_zero_p(p
->arr
[i
].d
) &&
690 p
->arr
[i
].x
.p
->type
== fractional
&&
691 !mod_term_smaller(e
, &p
->arr
[i
]))
695 value_set_si(v
.d
, 0);
696 v
.x
.p
= new_enode(fractional
, 3, -1);
697 evalue_set_si(&v
.x
.p
->arr
[1], 0, 1);
698 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
699 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
707 evalue
*pp
= &p
->arr
[0];
710 poly_denom_not_constant(&pp
, &m
);
711 mpz_fdiv_r(r
, m
, pp
->d
);
712 if (value_notzero_p(r
)) {
714 value_set_si(v
.d
, 0);
715 v
.x
.p
= new_enode(fractional
, 3, -1);
717 value_multiply(r
, m
, pp
->x
.n
);
718 value_multiply(v
.x
.p
->arr
[1].d
, m
, pp
->d
);
719 value_init(v
.x
.p
->arr
[1].x
.n
);
720 mpz_fdiv_r(v
.x
.p
->arr
[1].x
.n
, r
, pp
->d
);
721 mpz_fdiv_q(r
, r
, pp
->d
);
723 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
724 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
726 while (value_zero_p(pp
->d
))
727 pp
= &pp
->x
.p
->arr
[0];
729 value_assign(pp
->d
, m
);
730 value_assign(pp
->x
.n
, r
);
732 value_gcd(r
, pp
->d
, pp
->x
.n
);
733 value_division(pp
->d
, pp
->d
, r
);
734 value_division(pp
->x
.n
, pp
->x
.n
, r
);
747 for (pp
= &p
->arr
[0]; value_zero_p(pp
->d
);
748 pp
= &pp
->x
.p
->arr
[0]) {
749 f
= &pp
->x
.p
->arr
[1];
750 assert(value_pos_p(f
->d
));
751 mpz_mul_ui(twice
, f
->x
.n
, 2);
752 if (value_lt(twice
, f
->d
))
754 if (value_eq(twice
, f
->d
))
762 value_set_si(v
.d
, 0);
763 v
.x
.p
= new_enode(fractional
, 3, -1);
764 evalue_set_si(&v
.x
.p
->arr
[1], 0, 1);
765 poly_denom(&p
->arr
[0], &twice
);
766 value_assign(v
.x
.p
->arr
[1].d
, twice
);
767 value_decrement(v
.x
.p
->arr
[1].x
.n
, twice
);
768 evalue_set_si(&v
.x
.p
->arr
[2], -1, 1);
769 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
771 for (pp
= &v
.x
.p
->arr
[0]; value_zero_p(pp
->d
);
772 pp
= &pp
->x
.p
->arr
[0]) {
773 f
= &pp
->x
.p
->arr
[1];
774 value_oppose(f
->x
.n
, f
->x
.n
);
775 mpz_fdiv_r(f
->x
.n
, f
->x
.n
, f
->d
);
777 value_division(pp
->d
, twice
, pp
->d
);
778 value_multiply(pp
->x
.n
, pp
->x
.n
, pp
->d
);
779 value_assign(pp
->d
, twice
);
780 value_oppose(pp
->x
.n
, pp
->x
.n
);
781 value_decrement(pp
->x
.n
, pp
->x
.n
);
782 mpz_fdiv_r(pp
->x
.n
, pp
->x
.n
, pp
->d
);
784 /* Maybe we should do this during reduction of
787 value_gcd(twice
, pp
->d
, pp
->x
.n
);
788 value_division(pp
->d
, pp
->d
, twice
);
789 value_division(pp
->x
.n
, pp
->x
.n
, twice
);
799 reorder_terms_about(p
, &v
);
800 _reduce_evalue(&p
->arr
[1], s
, fract
);
803 evalue_reduce_size(e
);
805 else if (p
->type
== flooring
) {
806 /* Replace floor(constant) by its value */
807 if (value_notzero_p(p
->arr
[0].d
)) {
810 value_set_si(v
.d
, 1);
812 mpz_fdiv_q(v
.x
.n
, p
->arr
[0].x
.n
, p
->arr
[0].d
);
813 reorder_terms_about(p
, &v
);
814 _reduce_evalue(&p
->arr
[1], s
, fract
);
816 evalue_reduce_size(e
);
818 else if (p
->type
== relation
) {
819 if (p
->size
== 3 && eequal(&p
->arr
[1], &p
->arr
[2])) {
820 free_evalue_refs(&(p
->arr
[2]));
821 free_evalue_refs(&(p
->arr
[0]));
828 evalue_reduce_size(e
);
829 if (value_notzero_p(e
->d
) || p
!= e
->x
.p
)
836 /* Relation was reduced by means of an identical
837 * inequality => remove
839 if (value_zero_p(m
->d
) && !EVALUE_IS_ZERO(m
->x
.p
->arr
[1]))
842 if (reduced
|| value_notzero_p(p
->arr
[0].d
)) {
843 if (!reduced
&& value_zero_p(p
->arr
[0].x
.n
)) {
845 memcpy(e
,&p
->arr
[1],sizeof(evalue
));
847 free_evalue_refs(&(p
->arr
[2]));
851 memcpy(e
,&p
->arr
[2],sizeof(evalue
));
853 evalue_set_si(e
, 0, 1);
854 free_evalue_refs(&(p
->arr
[1]));
856 free_evalue_refs(&(p
->arr
[0]));
862 } /* reduce_evalue */
864 static void add_substitution(struct subst
*s
, Value
*row
, unsigned dim
)
869 for (k
= 0; k
< dim
; ++k
)
870 if (value_notzero_p(row
[k
+1]))
873 Vector_Normalize_Positive(row
+1, dim
+1, k
);
874 assert(s
->n
< s
->max
);
875 value_init(s
->fixed
[s
->n
].d
);
876 value_init(s
->fixed
[s
->n
].m
);
877 value_assign(s
->fixed
[s
->n
].d
, row
[k
+1]);
878 s
->fixed
[s
->n
].pos
= k
+1;
879 value_set_si(s
->fixed
[s
->n
].m
, 0);
880 r
= &s
->fixed
[s
->n
].s
;
882 for (l
= k
+1; l
< dim
; ++l
)
883 if (value_notzero_p(row
[l
+1])) {
884 value_set_si(r
->d
, 0);
885 r
->x
.p
= new_enode(polynomial
, 2, l
+ 1);
886 value_init(r
->x
.p
->arr
[1].x
.n
);
887 value_oppose(r
->x
.p
->arr
[1].x
.n
, row
[l
+1]);
888 value_set_si(r
->x
.p
->arr
[1].d
, 1);
892 value_oppose(r
->x
.n
, row
[dim
+1]);
893 value_set_si(r
->d
, 1);
897 static void _reduce_evalue_in_domain(evalue
*e
, Polyhedron
*D
, struct subst
*s
)
900 Polyhedron
*orig
= D
;
905 D
= DomainConvex(D
, 0);
906 /* We don't perform any substitutions if the domain is a union.
907 * We may therefore miss out on some possible simplifications,
908 * e.g., if a variable is always even in the whole union,
909 * while there is a relation in the evalue that evaluates
910 * to zero for even values of the variable.
912 if (!D
->next
&& D
->NbEq
) {
916 realloc_substitution(s
, dim
);
918 int d
= relations_depth(e
);
920 NALLOC(s
->fixed
, s
->max
);
923 for (j
= 0; j
< D
->NbEq
; ++j
)
924 add_substitution(s
, D
->Constraint
[j
], dim
);
928 _reduce_evalue(e
, s
, 0);
931 for (j
= 0; j
< s
->n
; ++j
) {
932 value_clear(s
->fixed
[j
].d
);
933 value_clear(s
->fixed
[j
].m
);
934 free_evalue_refs(&s
->fixed
[j
].s
);
939 void reduce_evalue_in_domain(evalue
*e
, Polyhedron
*D
)
941 struct subst s
= { NULL
, 0, 0 };
942 POL_ENSURE_VERTICES(D
);
944 if (EVALUE_IS_ZERO(*e
))
948 evalue_set_si(e
, 0, 1);
951 _reduce_evalue_in_domain(e
, D
, &s
);
956 void reduce_evalue (evalue
*e
) {
957 struct subst s
= { NULL
, 0, 0 };
959 if (value_notzero_p(e
->d
))
960 return; /* a rational number, its already reduced */
962 if (e
->x
.p
->type
== partition
) {
965 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
966 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
968 /* This shouldn't really happen;
969 * Empty domains should not be added.
971 POL_ENSURE_VERTICES(D
);
973 _reduce_evalue_in_domain(&e
->x
.p
->arr
[2*i
+1], D
, &s
);
975 if (emptyQ(D
) || EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
976 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
977 Domain_Free(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]));
978 value_clear(e
->x
.p
->arr
[2*i
].d
);
980 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
981 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
985 if (e
->x
.p
->size
== 0) {
987 evalue_set_si(e
, 0, 1);
990 _reduce_evalue(e
, &s
, 0);
995 static void print_evalue_r(FILE *DST
, const evalue
*e
, const char *const *pname
)
997 if (EVALUE_IS_NAN(*e
)) {
1002 if(value_notzero_p(e
->d
)) {
1003 if(value_notone_p(e
->d
)) {
1004 value_print(DST
,VALUE_FMT
,e
->x
.n
);
1006 value_print(DST
,VALUE_FMT
,e
->d
);
1009 value_print(DST
,VALUE_FMT
,e
->x
.n
);
1013 print_enode(DST
,e
->x
.p
,pname
);
1015 } /* print_evalue */
1017 void print_evalue(FILE *DST
, const evalue
*e
, const char * const *pname
)
1019 print_evalue_r(DST
, e
, pname
);
1020 if (value_notzero_p(e
->d
))
1024 void print_enode(FILE *DST
, enode
*p
, const char *const *pname
)
1029 fprintf(DST
, "NULL");
1035 for (i
=0; i
<p
->size
; i
++) {
1036 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1040 fprintf(DST
, " }\n");
1044 for (i
=p
->size
-1; i
>=0; i
--) {
1045 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1046 if (i
==1) fprintf(DST
, " * %s + ", pname
[p
->pos
-1]);
1048 fprintf(DST
, " * %s^%d + ", pname
[p
->pos
-1], i
);
1050 fprintf(DST
, " )\n");
1054 for (i
=0; i
<p
->size
; i
++) {
1055 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1056 if (i
!=(p
->size
-1)) fprintf(DST
, ", ");
1058 fprintf(DST
," ]_%s", pname
[p
->pos
-1]);
1063 for (i
=p
->size
-1; i
>=1; i
--) {
1064 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1066 fprintf(DST
, " * ");
1067 fprintf(DST
, p
->type
== flooring
? "[" : "{");
1068 print_evalue_r(DST
, &p
->arr
[0], pname
);
1069 fprintf(DST
, p
->type
== flooring
? "]" : "}");
1071 fprintf(DST
, "^%d + ", i
-1);
1073 fprintf(DST
, " + ");
1076 fprintf(DST
, " )\n");
1080 print_evalue_r(DST
, &p
->arr
[0], pname
);
1081 fprintf(DST
, "= 0 ] * \n");
1082 print_evalue_r(DST
, &p
->arr
[1], pname
);
1084 fprintf(DST
, " +\n [ ");
1085 print_evalue_r(DST
, &p
->arr
[0], pname
);
1086 fprintf(DST
, "!= 0 ] * \n");
1087 print_evalue_r(DST
, &p
->arr
[2], pname
);
1091 char **new_names
= NULL
;
1092 const char *const *names
= pname
;
1093 int maxdim
= EVALUE_DOMAIN(p
->arr
[0])->Dimension
;
1094 if (!pname
|| p
->pos
< maxdim
) {
1095 new_names
= ALLOCN(char *, maxdim
);
1096 for (i
= 0; i
< p
->pos
; ++i
) {
1098 new_names
[i
] = (char *)pname
[i
];
1100 new_names
[i
] = ALLOCN(char, 10);
1101 snprintf(new_names
[i
], 10, "%c", 'P'+i
);
1104 for ( ; i
< maxdim
; ++i
) {
1105 new_names
[i
] = ALLOCN(char, 10);
1106 snprintf(new_names
[i
], 10, "_p%d", i
);
1108 names
= (const char**)new_names
;
1111 for (i
=0; i
<p
->size
/2; i
++) {
1112 Print_Domain(DST
, EVALUE_DOMAIN(p
->arr
[2*i
]), names
);
1113 print_evalue_r(DST
, &p
->arr
[2*i
+1], names
);
1114 if (value_notzero_p(p
->arr
[2*i
+1].d
))
1118 if (!pname
|| p
->pos
< maxdim
) {
1119 for (i
= pname
? p
->pos
: 0; i
< maxdim
; ++i
)
1133 * 0 if toplevels of e1 and e2 are at the same level
1134 * <0 if toplevel of e1 should be outside of toplevel of e2
1135 * >0 if toplevel of e2 should be outside of toplevel of e1
1137 static int evalue_level_cmp(const evalue
*e1
, const evalue
*e2
)
1139 if (value_notzero_p(e1
->d
) && value_notzero_p(e2
->d
))
1141 if (value_notzero_p(e1
->d
))
1143 if (value_notzero_p(e2
->d
))
1145 if (e1
->x
.p
->type
== partition
&& e2
->x
.p
->type
== partition
)
1147 if (e1
->x
.p
->type
== partition
)
1149 if (e2
->x
.p
->type
== partition
)
1151 if (e1
->x
.p
->type
== relation
&& e2
->x
.p
->type
== relation
) {
1152 if (eequal(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1154 if (mod_term_smaller(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1159 if (e1
->x
.p
->type
== relation
)
1161 if (e2
->x
.p
->type
== relation
)
1163 if (e1
->x
.p
->type
== polynomial
&& e2
->x
.p
->type
== polynomial
)
1164 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
1165 if (e1
->x
.p
->type
== polynomial
)
1167 if (e2
->x
.p
->type
== polynomial
)
1169 if (e1
->x
.p
->type
== periodic
&& e2
->x
.p
->type
== periodic
)
1170 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
1171 assert(e1
->x
.p
->type
!= periodic
);
1172 assert(e2
->x
.p
->type
!= periodic
);
1173 assert(e1
->x
.p
->type
== e2
->x
.p
->type
);
1174 if (eequal(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1176 if (mod_term_smaller(e1
, e2
))
1182 static void eadd_rev(const evalue
*e1
, evalue
*res
)
1186 evalue_copy(&ev
, e1
);
1188 free_evalue_refs(res
);
1192 static void eadd_rev_cst(const evalue
*e1
, evalue
*res
)
1196 evalue_copy(&ev
, e1
);
1197 eadd(res
, &ev
.x
.p
->arr
[type_offset(ev
.x
.p
)]);
1198 free_evalue_refs(res
);
1202 struct section
{ Polyhedron
* D
; evalue E
; };
1204 void eadd_partitions(const evalue
*e1
, evalue
*res
)
1209 s
= (struct section
*)
1210 malloc((e1
->x
.p
->size
/2+1) * (res
->x
.p
->size
/2+1) *
1211 sizeof(struct section
));
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
);
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);
1223 for (i
= 1; i
< res
->x
.p
->size
/2; ++i
) {
1225 fd
= DomainDifference(fd
, EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1230 fd
= DomainConstraintSimplify(fd
, 0);
1235 value_init(s
[n
].E
.d
);
1236 evalue_copy(&s
[n
].E
, &e1
->x
.p
->arr
[2*j
+1]);
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
) {
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);
1252 fd
= DomainDifference(fd
, EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]), 0);
1253 if (t
!= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]))
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
);
1262 s
[n
].E
= res
->x
.p
->arr
[2*i
+1];
1266 free_evalue_refs(&res
->x
.p
->arr
[2*i
+1]);
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
);
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
;
1286 static void explicit_complement(evalue
*res
)
1288 enode
*rel
= new_enode(relation
, 3, 0);
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);
1301 static void reduce_constant(evalue
*e
)
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
);
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
);
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
)
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
)
1341 // add any element in e1 to the corresponding element in res
1342 i
= type_offset(res
->x
.p
);
1344 assert(eequal(&e1
->x
.p
->arr
[0], &res
->x
.p
->arr
[0]));
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
)
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
*))
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
);
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
]);
1389 static void eadd_periodics(const evalue
*e1
, evalue
*res
)
1395 if (e1
->x
.p
->size
== res
->x
.p
->size
) {
1396 eadd_arrays(e1
, res
, e1
->x
.p
->size
);
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
);
1410 free_evalue_refs(dst
);
1412 evalue_copy(dst
, src
);
1415 void eadd(const evalue
*e1
, evalue
*res
)
1419 if (EVALUE_IS_ZERO(*e1
))
1422 if (EVALUE_IS_NAN(*res
))
1425 if (EVALUE_IS_NAN(*e1
)) {
1426 evalue_assign(res
, e1
);
1430 if (EVALUE_IS_ZERO(*res
)) {
1431 evalue_assign(res
, e1
);
1435 cmp
= evalue_level_cmp(res
, e1
);
1437 switch (e1
->x
.p
->type
) {
1441 eadd_rev_cst(e1
, res
);
1446 } else if (cmp
== 0) {
1447 if (value_notzero_p(e1
->d
)) {
1448 eadd_rationals(e1
, res
);
1450 switch (e1
->x
.p
->type
) {
1452 eadd_partitions(e1
, res
);
1455 eadd_relations(e1
, res
);
1458 assert(e1
->x
.p
->size
== res
->x
.p
->size
);
1465 eadd_periodics(e1
, res
);
1473 switch (res
->x
.p
->type
) {
1477 /* Add to the constant term of a polynomial */
1478 eadd(e1
, &res
->x
.p
->arr
[type_offset(res
->x
.p
)]);
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
]);
1486 fprintf(stderr
, "eadd: cannot add const with vector\n");
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
]);
1503 static void emul_rev(const evalue
*e1
, evalue
*res
)
1507 evalue_copy(&ev
, e1
);
1509 free_evalue_refs(res
);
1513 static void emul_poly(const evalue
*e1
, evalue
*res
)
1515 int i
, j
, offset
= type_offset(res
->x
.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
]))
1526 /* special case pure power */
1527 if (i
== e1
->x
.p
->size
-1) {
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]);
1546 value_set_si(tmp
.d
,0);
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
]);
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
++) {
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
);
1569 void emul_partitions(const evalue
*e1
, evalue
*res
)
1574 s
= (struct section
*)
1575 malloc((e1
->x
.p
->size
/2) * (res
->x
.p
->size
/2) *
1576 sizeof(struct section
));
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
);
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);
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
))
1599 if (DomainIncludes(d
, s
[k
].D
)) {
1600 Domain_Free(s
[k
].D
);
1601 free_evalue_refs(&s
[k
].E
);
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
);
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]);
1625 evalue_set_si(res
, 0, 1);
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
;
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
)
1650 if (e1
->x
.p
->size
< 3 && res
->x
.p
->size
== 3) {
1651 free_evalue_refs(&res
->x
.p
->arr
[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
)
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
]));
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
)
1681 poly_denom(&e1
->x
.p
->arr
[0], &d
.d
);
1682 if (!value_two_p(d
.d
))
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);
1693 evalue_copy(&tmp
, &res
->x
.p
->arr
[2]);
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
);
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
)
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
))
1721 if (EVALUE_IS_ONE(*e1
))
1724 if (EVALUE_IS_ZERO(*e1
)) {
1725 evalue_assign(res
, e1
);
1729 if (EVALUE_IS_NAN(*res
))
1732 if (EVALUE_IS_NAN(*e1
)) {
1733 evalue_assign(res
, e1
);
1737 cmp
= evalue_level_cmp(res
, e1
);
1740 } else if (cmp
== 0) {
1741 if (value_notzero_p(e1
->d
)) {
1742 emul_rationals(e1
, res
);
1744 switch (e1
->x
.p
->type
) {
1746 emul_partitions(e1
, res
);
1749 emul_relations(e1
, res
);
1756 emul_periodics(e1
, res
);
1759 emul_fractionals(e1
, res
);
1765 switch (res
->x
.p
->type
) {
1767 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
)
1768 emul(e1
, &res
->x
.p
->arr
[2*i
+1]);
1775 for (i
= type_offset(res
->x
.p
); i
< res
->x
.p
->size
; ++i
)
1776 emul(e1
, &res
->x
.p
->arr
[i
]);
1782 /* Frees mask content ! */
1783 void emask(evalue
*mask
, evalue
*res
) {
1790 if (EVALUE_IS_ZERO(*res
)) {
1791 free_evalue_refs(mask
);
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
));
1810 evalue_set_si(&mone
, -1, 1);
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);
1818 for (i
= 1; i
< mask
->x
.p
->size
/2; ++i
) {
1820 fd
= DomainDifference(fd
, EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]), 0);
1829 value_init(s
[n
].E
.d
);
1830 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*j
+1]);
1834 for (i
= 0; i
< mask
->x
.p
->size
/2; ++i
) {
1835 if (EVALUE_IS_ONE(mask
->x
.p
->arr
[2*i
+1]))
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
) {
1843 d
= DomainIntersection(EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]),
1844 EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]), 0);
1850 fd
= DomainDifference(fd
, EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]), 0);
1851 if (t
!= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]))
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
);
1861 /* Just ignore; this may have been previously masked off */
1863 if (fd
!= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]))
1867 free_evalue_refs(&mone
);
1868 free_evalue_refs(mask
);
1869 free_evalue_refs(res
);
1872 evalue_set_si(res
, 0, 1);
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
;
1885 void evalue_copy(evalue
*dst
, const evalue
*src
)
1887 value_assign(dst
->d
, src
->d
);
1888 if (EVALUE_IS_NAN(*dst
)) {
1892 if (value_pos_p(src
->d
)) {
1893 value_init(dst
->x
.n
);
1894 value_assign(dst
->x
.n
, src
->x
.n
);
1896 dst
->x
.p
= ecopy(src
->x
.p
);
1899 evalue
*evalue_dup(const evalue
*e
)
1901 evalue
*res
= ALLOC(evalue
);
1903 evalue_copy(res
, e
);
1907 enode
*new_enode(enode_type type
,int size
,int pos
) {
1913 fprintf(stderr
, "Allocating enode of size 0 !\n" );
1916 res
= (enode
*) malloc(sizeof(enode
) + (size
-1)*sizeof(evalue
));
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;
1928 enode
*ecopy(enode
*e
) {
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
])));
1941 value_init(res
->arr
[i
].x
.n
);
1942 value_assign(res
->arr
[i
].x
.n
,e
->arr
[i
].x
.n
);
1948 int ecmp(const evalue
*e1
, const evalue
*e2
)
1954 if (value_notzero_p(e1
->d
) && value_notzero_p(e2
->d
)) {
1958 value_multiply(m
, e1
->x
.n
, e2
->d
);
1959 value_multiply(m2
, e2
->x
.n
, e1
->d
);
1961 if (value_lt(m
, m2
))
1963 else if (value_gt(m
, m2
))
1973 if (value_notzero_p(e1
->d
))
1975 if (value_notzero_p(e2
->d
))
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)
1995 int eequal(const evalue
*e1
, const evalue
*e2
)
2000 if (value_ne(e1
->d
,e2
->d
))
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
))
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
))
2017 /* e1->d == e2->d != 0 AND e1->n == e2->n */
2021 /* e1->d == e2->d == 0 */
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
]) )
2033 void free_evalue_refs(evalue
*e
) {
2038 if (EVALUE_IS_NAN(*e
)) {
2043 if (EVALUE_IS_DOMAIN(*e
)) {
2044 Domain_Free(EVALUE_DOMAIN(*e
));
2047 } else if (value_pos_p(e
->d
)) {
2049 /* 'e' stores a constant */
2051 value_clear(e
->x
.n
);
2054 assert(value_zero_p(e
->d
));
2057 if (!p
) return; /* null pointer */
2058 for (i
=0; i
<p
->size
; i
++) {
2059 free_evalue_refs(&(p
->arr
[i
]));
2063 } /* free_evalue_refs */
2065 void evalue_free(evalue
*e
)
2067 free_evalue_refs(e
);
2071 static void mod2table_r(evalue
*e
, Vector
*periods
, Value m
, int p
,
2072 Vector
* val
, evalue
*res
)
2074 unsigned nparam
= periods
->Size
;
2077 double d
= compute_evalue(e
, val
->p
);
2078 d
*= VALUE_TO_DOUBLE(m
);
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
);
2089 if (value_one_p(periods
->p
[p
]))
2090 mod2table_r(e
, periods
, m
, p
+1, val
, res
);
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);
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
));
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);
2115 value_set_si(e
->x
.n
, 0);
2116 value_set_si(e
->d
, 1);
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
)
2129 if (EVALUE_IS_DOMAIN(*e
) || value_pos_p(e
->d
))
2132 for (i
=0; i
<p
->size
; i
++) {
2133 evalue_mod2table(&(p
->arr
[i
]), nparam
);
2135 if (p
->type
== relation
) {
2140 evalue_copy(©
, &p
->arr
[0]);
2142 rel2table(&p
->arr
[0], 1);
2143 emul(&p
->arr
[0], &p
->arr
[1]);
2145 rel2table(©
, 0);
2146 emul(©
, &p
->arr
[2]);
2147 eadd(&p
->arr
[2], &p
->arr
[1]);
2148 free_evalue_refs(&p
->arr
[2]);
2149 free_evalue_refs(©
);
2151 free_evalue_refs(&p
->arr
[0]);
2155 } else if (p
->type
== fractional
) {
2156 Vector
*periods
= Vector_Alloc(nparam
);
2157 Vector
*val
= Vector_Alloc(nparam
);
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]) {
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
);
2176 mod2table_r(&p
->arr
[0], periods
, tmp
, 0, val
, &EP
);
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
);
2185 eadd(&p
->arr
[1], &res
);
2187 free_evalue_refs(e
);
2188 free_evalue_refs(&EP
);
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
)
2205 Value v
; /* value of the constraint of a row when
2206 parameters are instantiated*/
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
)) {
2221 return in
|| (P
->next
&& in_domain(P
->next
, list_args
));
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
) {
2243 if (p
->type
== polynomial
) {
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
);
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
);
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
);
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
;
2295 for (i
= 0; i
< dim
; ++i
) {
2296 value_init(vals
[i
]);
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
);
2307 for (i
= 0; i
< dim
; ++i
)
2308 value_clear(vals
[i
]);
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
)
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
);
2334 res
= VALUE_TO_DOUBLE(e
->x
.n
);
2337 res
= compute_enode(e
->x
.p
,list_args
);
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
) {
2352 /* double d; int i; */
2354 tmp
= (Value
*) malloc (sizeof(Value
));
2355 assert(tmp
!= NULL
);
2357 value_set_si(*tmp
,0);
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);
2368 return(tmp
); /* no Validity Domain */
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
);
2377 /* d = compute_evalue(&en->EP,list_args);
2379 printf("(double)%lf = %d\n", d, i ); */
2380 value_set_double(*tmp
,compute_evalue(&en
->EP
,list_args
)+.25);
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
)
2396 res
= evalue_zero();
2397 for (i
= p
->size
-1; i
> offset
; --i
) {
2398 c
= evalue_eval(&p
->arr
[i
], values
);
2403 c
= evalue_eval(&p
->arr
[offset
], values
);
2410 evalue
*evalue_eval(const evalue
*e
, Value
*values
)
2417 if (value_notzero_p(e
->d
)) {
2418 res
= ALLOC(evalue
);
2420 evalue_copy(res
, e
);
2423 switch (e
->x
.p
->type
) {
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, ¶m
, values
);
2431 free_evalue_refs(¶m
);
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
);
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
);
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
);
2455 res
= evalue_zero();
2456 evalue_free(param2
);
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
);
2466 res
= evalue_zero();
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
)
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]);
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]);
2507 for (i
= 0; i
< p
->size
; ++i
) {
2508 s
+= evalue_size(&p
->arr
[i
]);
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
);
2520 s
+= enode_size(e
->x
.p
);
2524 static evalue
*find_second(evalue
*base
, evalue
*cst
, evalue
*e
, Value m
)
2526 evalue
*found
= NULL
;
2531 if (value_pos_p(e
->d
) || e
->x
.p
->type
!= fractional
)
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);
2541 evalue_copy(©
, 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];
2549 value_set_si(offset
.x
.n
, -2);
2552 mpz_fdiv_r(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2554 if (eequal(base
, &e
->x
.p
->arr
[0]))
2557 free_evalue_refs(cst
);
2558 free_evalue_refs(&offset
);
2561 for (i
= 1; !found
&& i
< e
->x
.p
->size
; ++i
)
2562 found
= find_second(base
, cst
, &e
->x
.p
->arr
[i
], m
);
2567 static evalue
*find_relation_pair(evalue
*e
)
2570 evalue
*found
= NULL
;
2572 if (EVALUE_IS_DOMAIN(*e
) || value_pos_p(e
->d
))
2575 if (e
->x
.p
->type
== fractional
) {
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
);
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
]);
2599 void evalue_mod2relation(evalue
*e
) {
2602 if (value_zero_p(e
->d
) && e
->x
.p
->type
== partition
) {
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]);
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];
2618 if (e
->x
.p
->size
== 0) {
2620 evalue_set_si(e
, 0, 1);
2626 while ((d
= find_relation_pair(e
)) != NULL
) {
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
);
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
)
2665 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
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
]);
2681 Polyhedron
*D
= EVALUE_DOMAIN(*(evs
[i
]-1));
2684 value_clear((evs
[i
]-1)->d
);
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
);
2702 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2704 if (value_notzero_p(e
->x
.p
->arr
[2*i
+1].d
))
2706 H
= DomainConvex(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), 0);
2709 for (k
= 0; k
< e
->x
.p
->size
/2; ++k
) {
2710 Polyhedron
*D
, *N
, **P
;
2713 P
= &EVALUE_DOMAIN(e
->x
.p
->arr
[2*k
]);
2720 if (D
->NbEq
<= H
->NbEq
) {
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)
2734 D
->next
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
2735 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]) = D
;
2738 free_evalue_refs(&tmp
);
2744 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2746 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
2748 value_clear(e
->x
.p
->arr
[2*i
].d
);
2749 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
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];
2760 H
= DomainConvex(D
, 0);
2761 E
= DomainDifference(H
, D
, 0);
2763 D
= DomainDifference(H
, E
, 0);
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
)
2781 assert(p
->type
== fractional
);
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];
2797 static Polyhedron
*polynomial_projection(enode
*p
, Polyhedron
*D
, Value
*d
,
2802 unsigned dim
= D
->Dimension
;
2803 Matrix
*T
= Matrix_Alloc(2, dim
+1);
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);
2820 static void replace_by_affine(evalue
*e
, Value offset
)
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] */
2835 free_evalue_refs(&inc
);
2838 int evalue_range_reduction_in_domain(evalue
*e
, Polyhedron
*D
)
2847 if (value_notzero_p(e
->d
))
2852 if (p
->type
== relation
) {
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
)) {
2872 evalue_set_si(e
, 0, 1);
2875 free_evalue_refs(&(p
->arr
[1]));
2876 free_evalue_refs(&(p
->arr
[0]));
2882 return r
? r
: evalue_range_reduction_in_domain(e
, D
);
2883 } else if (bounded
&& equal
) {
2886 free_evalue_refs(&(p
->arr
[2]));
2889 free_evalue_refs(&(p
->arr
[0]));
2895 return evalue_range_reduction_in_domain(e
, D
);
2896 } else if (bounded
&& value_eq(min
, max
)) {
2897 /* zero for a single value */
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);
2910 r
= evalue_range_reduction_in_domain(&p
->arr
[1], E
);
2912 r
|= evalue_range_reduction_in_domain(&p
->arr
[2], D
);
2914 _reduce_evalue(&p
->arr
[0].x
.p
->arr
[0], 0, 1);
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
) {
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
);
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
);
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.
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
];
2981 value_init(inc
.x
.n
);
2982 value_set_si(inc
.d
, 1);
2983 value_oppose(inc
.x
.n
, min
);
2986 evalue_copy(&t
, &p
->arr
[0]);
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);
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);
3010 evalue_reorder_terms(&rem
);
3011 evalue_reorder_terms(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
);
3027 _reduce_evalue(&p
->arr
[0], 0, 1);
3029 evalue_reorder_terms(e
);
3039 void evalue_range_reduction(evalue
*e
)
3042 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
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
);
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];
3064 Enumeration
* partition2enumeration(evalue
*EP
)
3067 Enumeration
*en
, *res
= NULL
;
3069 if (EVALUE_IS_ZERO(*EP
)) {
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
));
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];
3089 int evalue_frac2floor_in_domain3(evalue
*e
, Polyhedron
*D
, int shift
)
3098 if (value_notzero_p(e
->d
))
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
) {
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
);
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 */
3135 for (i
= 0; i
< I
->NbConstraints
; ++i
)
3136 if (value_pos_p(I
->Constraint
[i
][1]))
3139 if (i
< I
->NbConstraints
) {
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
)) {
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
);
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]);
3172 free_evalue_refs(&fl
);
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
)
3185 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
) {
3187 if (evalue_frac2floor_in_domain3(e
, NULL
, 0))
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
)
3213 int nparam
= D
->Dimension
- nvar
;
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
);
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);
3237 value_set_si(C
->p
[nr
-2][1 + nvar
], -1);
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
,
3249 static void floor2frac_r(evalue
*e
, int nvar
)
3256 if (value_notzero_p(e
->d
))
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
;
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]);
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
);
3294 int evalue_floor2frac(evalue
*e
)
3299 if (value_notzero_p(e
->d
))
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]);
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
) {
3318 evalue_reorder_terms(e
);
3323 evalue
*esum_over_domain_cst(int nvar
, Polyhedron
*D
, Matrix
*C
)
3326 int nparam
= D
->Dimension
- nvar
;
3330 D
= Constraints2Polyhedron(C
, 0);
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
)));
3345 static void domain_signs(Polyhedron
*D
, int *signs
)
3349 POL_ENSURE_VERTICES(D
);
3350 for (j
= 0; j
< D
->Dimension
; ++j
) {
3352 for (k
= 0; k
< D
->NbRays
; ++k
) {
3353 signs
[j
] = value_sign(D
->Ray
[k
][1+j
]);
3360 static evalue
*esum_over_domain(evalue
*e
, int nvar
, Polyhedron
*D
,
3361 int *signs
, Matrix
*C
, unsigned MaxRays
)
3367 evalue
*factor
= NULL
;
3371 if (EVALUE_IS_ZERO(*e
))
3375 Polyhedron
*DD
= Disjoint_Domain(D
, 0, MaxRays
);
3382 res
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3385 for (Q
= DD
; Q
; Q
= DD
) {
3391 t
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3404 if (value_notzero_p(e
->d
)) {
3407 t
= esum_over_domain_cst(nvar
, D
, C
);
3409 if (!EVALUE_IS_ONE(*e
))
3416 signs
= alloca(sizeof(int) * D
->Dimension
);
3417 domain_signs(D
, signs
);
3420 switch (e
->x
.p
->type
) {
3422 evalue
*pp
= &e
->x
.p
->arr
[0];
3424 if (pp
->x
.p
->pos
> nvar
) {
3425 /* remainder is independent of the summated vars */
3431 floor2frac(&f
, nvar
);
3433 t
= esum_over_domain_cst(nvar
, D
, C
);
3437 free_evalue_refs(&f
);
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]) {
3448 assert(pp
->x
.p
->type
== polynomial
);
3450 if (pos
>= 1 + nvar
)
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],
3460 value_multiply(row
->p
[1 + D
->Dimension
+ 1],
3461 row
->p
[1 + D
->Dimension
+ 1],
3463 value_oppose(row
->p
[1 + nvar
], row
->p
[1 + nvar
]);
3467 int pos
= e
->x
.p
->pos
;
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);
3479 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3480 negative
= signs
[pos
-1] < 0;
3481 value_set_si(row
->p
[0], 1);
3483 value_set_si(row
->p
[pos
], -1);
3484 value_set_si(row
->p
[1 + nvar
], 1);
3486 value_set_si(row
->p
[pos
], 1);
3487 value_set_si(row
->p
[1 + nvar
], -1);
3495 offset
= type_offset(e
->x
.p
);
3497 res
= esum_over_domain(&e
->x
.p
->arr
[offset
], nvar
, D
, signs
, C
, MaxRays
);
3501 evalue_copy(&cum
, factor
);
3505 for (i
= 1; offset
+i
< e
->x
.p
->size
; ++i
) {
3509 C
= esum_add_constraint(nvar
, D
, C
, row
, negative
);
3515 Vector_Print(stderr, P_VALUE_FMT, row);
3517 Matrix_Print(stderr, P_VALUE_FMT, C);
3519 t
= esum_over_domain(&e
->x
.p
->arr
[offset
+i
], nvar
, D
, signs
, C
, MaxRays
);
3524 if (negative
&& (i
% 2))
3534 if (factor
&& offset
+i
+1 < e
->x
.p
->size
)
3541 free_evalue_refs(&cum
);
3542 evalue_free(factor
);
3553 static Polyhedron_Insert(Polyhedron
***next
, Polyhedron
*Q
)
3563 static Polyhedron
*Polyhedron_Split_Into_Orthants(Polyhedron
*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
;
3579 for (I
= D
; I
; I
= I
->next
) {
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);
3599 /* Make arguments of all floors non-negative */
3600 static void shift_floor_in_domain(evalue
*e
, Polyhedron
*D
)
3607 if (value_notzero_p(e
->d
))
3612 for (i
= type_offset(p
); i
< p
->size
; ++i
)
3613 shift_floor_in_domain(&p
->arr
[i
], D
);
3615 if (p
->type
!= flooring
)
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]))
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 */
3637 value_set_si(f
.d
, 1);
3638 value_oppose(f
.x
.n
, m
);
3639 eadd(&f
, &p
->arr
[0]);
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
);
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
) {
3667 evalue
*fe
= evalue_dup(E
);
3668 Polyhedron
*next
= P
->next
;
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
);
3685 /* Initial silly implementation */
3686 void eor(evalue
*e1
, evalue
*res
)
3692 evalue_set_si(&mone
, -1, 1);
3694 evalue_copy(&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
)
3711 if (value_notzero_p(e
->d
)) {
3712 value_lcm(*d
, *d
, e
->d
);
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
)
3728 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3731 if (value_notzero_p(e
->d
)) {
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
);
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
);
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
)
3758 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3761 if (value_notzero_p(e
->d
)) {
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
);
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
);
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
)
3788 if ((value_one_p(n
) && value_one_p(d
)) || EVALUE_IS_ZERO(*e
))
3791 if (value_notzero_p(e
->d
)) {
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
);
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
);
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
)
3818 if (value_notzero_p(e
->d
)) {
3819 value_oppose(e
->x
.n
, e
->x
.n
);
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]);
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
)
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
);
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
);
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
)
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);
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
);
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
)
3887 /* replace { a/m } by (m-1)/m if sign != 0
3888 * and by (m-1)/(2m) if sign == 0
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
);
3897 value_addto(e
->x
.p
->arr
[0].d
, d
, d
);
3899 value_assign(e
->x
.p
->arr
[0].d
, d
);
3900 value_decrement(e
->x
.p
->arr
[0].x
.n
, d
);
3904 reorder_terms_about(p
, &p
->arr
[0]);
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
)
3920 if (value_notzero_p(e
->d
))
3922 assert(e
->x
.p
->type
== partition
);
3923 /* make sure all variables in the domains have a fixed sign */
3925 evalue_split_domains_into_orthants(e
, MaxRays
);
3926 if (EVALUE_IS_ZERO(*e
))
3930 assert(e
->x
.p
->size
>= 2);
3931 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
3933 signs
= alloca(sizeof(int) * dim
);
3936 for (i
= 0; i
< dim
; ++i
)
3938 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
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
)
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
) {
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
));
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);
3974 free_evalue_refs(&split
);
3979 static evalue
*find_fractional_with_max_periods(evalue
*e
, Polyhedron
*D
,
3982 Value
*min
, Value
*max
)
3989 if (value_notzero_p(e
->d
))
3992 if (e
->x
.p
->type
== fractional
) {
3997 I
= polynomial_projection(e
->x
.p
, D
, &d
, &T
);
3998 bounded
= line_minmax(I
, min
, max
); /* frees I */
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
))
4010 f
= evalue_dup(&e
->x
.p
->arr
[0]);
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
,
4029 static void replace_fract_by_affine(evalue
*e
, evalue
*f
, Value val
)
4033 if (value_notzero_p(e
->d
))
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
)
4043 if (!eequal(&e
->x
.p
->arr
[0], f
))
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
)
4063 if (EVALUE_IS_ZERO(*e
))
4065 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
) {
4067 "WARNING: evalue_split_periods called on incorrect evalue type\n");
4075 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
4080 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
4082 f
= find_fractional_with_max_periods(&e
->x
.p
->arr
[2*i
+1], D
, max_periods
,
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
) {
4115 value_clear(p
->arr
[2*i
+1].d
);
4116 p
->arr
[2*i
+1] = e
->x
.p
->arr
[2*i
+1];
4118 evalue_copy(&p
->arr
[2*(i
+j
)+1], &e
->x
.p
->arr
[2*i
+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
);
4147 void evalue_extract_affine(const evalue
*e
, Value
*coeff
, Value
*cst
, Value
*d
)
4149 value_set_si(*d
, 1);
4151 for ( ; value_zero_p(e
->d
); e
= &e
->x
.p
->arr
[0]) {
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
4167 static evalue
*term(int param
, Value c
, Value den
)
4169 evalue
*EP
= ALLOC(evalue
);
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 value_init(EP
->x
.p
->arr
[1].x
.n
);
4175 value_assign(EP
->x
.p
->arr
[1].d
, den
);
4176 value_assign(EP
->x
.p
->arr
[1].x
.n
, c
);
4180 evalue
*affine2evalue(Value
*coeff
, Value denom
, int nvar
)
4183 evalue
*E
= ALLOC(evalue
);
4185 evalue_set(E
, coeff
[nvar
], denom
);
4186 for (i
= 0; i
< nvar
; ++i
) {
4188 if (value_zero_p(coeff
[i
]))
4190 t
= term(i
, coeff
[i
], denom
);
4197 void evalue_substitute(evalue
*e
, evalue
**subs
)
4203 if (value_notzero_p(e
->d
))
4207 assert(p
->type
!= partition
);
4209 for (i
= 0; i
< p
->size
; ++i
)
4210 evalue_substitute(&p
->arr
[i
], subs
);
4212 if (p
->type
== relation
) {
4213 /* For relation a ? b : c, compute (a' ? 1) * b' + (a' ? 0 : 1) * c' */
4217 value_set_si(v
->d
, 0);
4218 v
->x
.p
= new_enode(relation
, 3, 0);
4219 evalue_copy(&v
->x
.p
->arr
[0], &p
->arr
[0]);
4220 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4221 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4222 emul(v
, &p
->arr
[2]);
4227 value_set_si(v
->d
, 0);
4228 v
->x
.p
= new_enode(relation
, 2, 0);
4229 value_clear(v
->x
.p
->arr
[0].d
);
4230 v
->x
.p
->arr
[0] = p
->arr
[0];
4231 evalue_set_si(&v
->x
.p
->arr
[1], 1, 1);
4232 emul(v
, &p
->arr
[1]);
4235 eadd(&p
->arr
[2], &p
->arr
[1]);
4236 free_evalue_refs(&p
->arr
[2]);
4244 if (p
->type
== polynomial
)
4249 value_set_si(v
->d
, 0);
4250 v
->x
.p
= new_enode(p
->type
, 3, -1);
4251 value_clear(v
->x
.p
->arr
[0].d
);
4252 v
->x
.p
->arr
[0] = p
->arr
[0];
4253 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4254 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4257 offset
= type_offset(p
);
4259 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
4260 emul(v
, &p
->arr
[i
]);
4261 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
4262 free_evalue_refs(&(p
->arr
[i
]));
4265 if (p
->type
!= polynomial
)
4269 *e
= p
->arr
[offset
];
4273 /* evalue e is given in terms of "new" parameter; CP maps the new
4274 * parameters back to the old parameters.
4275 * Transforms e such that it refers back to the old parameters and
4276 * adds appropriate constraints to the domain.
4277 * In particular, if CP maps the new parameters onto an affine
4278 * subspace of the old parameters, then the corresponding equalities
4279 * are added to the domain.
4280 * Also, if any of the new parameters was a rational combination
4281 * of the old parameters $p' = (<a, p> + c)/m$, then modulo
4282 * constraints ${<a, p> + c)/m} = 0$ are added to ensure
4283 * the new evalue remains non-zero only for integer parameters
4284 * of the new parameters (which have been removed by the substitution).
4286 void evalue_backsubstitute(evalue
*e
, Matrix
*CP
, unsigned MaxRays
)
4293 unsigned nparam
= CP
->NbColumns
-1;
4297 if (EVALUE_IS_ZERO(*e
))
4300 assert(value_zero_p(e
->d
));
4302 assert(p
->type
== partition
);
4304 inv
= left_inverse(CP
, &eq
);
4305 subs
= ALLOCN(evalue
*, nparam
);
4306 for (i
= 0; i
< nparam
; ++i
)
4307 subs
[i
] = affine2evalue(inv
->p
[i
], inv
->p
[nparam
][inv
->NbColumns
-1],
4310 CEq
= Constraints2Polyhedron(eq
, MaxRays
);
4311 addeliminatedparams_partition(p
, inv
, CEq
, inv
->NbColumns
-1, MaxRays
);
4312 Polyhedron_Free(CEq
);
4314 for (i
= 0; i
< p
->size
/2; ++i
)
4315 evalue_substitute(&p
->arr
[2*i
+1], subs
);
4317 for (i
= 0; i
< nparam
; ++i
)
4318 evalue_free(subs
[i
]);
4322 for (i
= 0; i
< inv
->NbRows
-1; ++i
) {
4323 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &gcd
);
4324 value_gcd(gcd
, gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
4325 if (value_eq(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
4327 Vector_AntiScale(inv
->p
[i
], inv
->p
[i
], gcd
, inv
->NbColumns
);
4328 value_divexact(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1], gcd
);
4330 for (j
= 0; j
< p
->size
/2; ++j
) {
4331 evalue
*arg
= affine2evalue(inv
->p
[i
], gcd
, inv
->NbColumns
-1);
4336 value_set_si(rel
.d
, 0);
4337 rel
.x
.p
= new_enode(relation
, 2, 0);
4338 value_clear(rel
.x
.p
->arr
[1].d
);
4339 rel
.x
.p
->arr
[1] = p
->arr
[2*j
+1];
4340 ev
= &rel
.x
.p
->arr
[0];
4341 value_set_si(ev
->d
, 0);
4342 ev
->x
.p
= new_enode(fractional
, 3, -1);
4343 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
4344 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
4345 value_clear(ev
->x
.p
->arr
[0].d
);
4346 ev
->x
.p
->arr
[0] = *arg
;
4349 p
->arr
[2*j
+1] = rel
;
4360 * \sum_{i=0}^n c_i/d X^i
4362 * where d is the last element in the vector c.
4364 evalue
*evalue_polynomial(Vector
*c
, const evalue
* X
)
4366 unsigned dim
= c
->Size
-2;
4368 evalue
*EP
= ALLOC(evalue
);
4373 if (EVALUE_IS_ZERO(*X
) || dim
== 0) {
4374 evalue_set(EP
, c
->p
[0], c
->p
[dim
+1]);
4375 reduce_constant(EP
);
4379 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
4382 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
4384 for (i
= dim
-1; i
>= 0; --i
) {
4386 value_assign(EC
.x
.n
, c
->p
[i
]);
4389 free_evalue_refs(&EC
);
4393 /* Create an evalue from an array of pairs of domains and evalues. */
4394 evalue
*evalue_from_section_array(struct evalue_section
*s
, int n
)
4399 res
= ALLOC(evalue
);
4403 evalue_set_si(res
, 0, 1);
4405 value_set_si(res
->d
, 0);
4406 res
->x
.p
= new_enode(partition
, 2*n
, s
[0].D
->Dimension
);
4407 for (i
= 0; i
< n
; ++i
) {
4408 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*i
], s
[i
].D
);
4409 value_clear(res
->x
.p
->arr
[2*i
+1].d
);
4410 res
->x
.p
->arr
[2*i
+1] = *s
[i
].E
;
4417 /* shift variables (>= first, 0-based) in polynomial n up (may be negative) */
4418 void evalue_shift_variables(evalue
*e
, int first
, int n
)
4421 if (value_notzero_p(e
->d
))
4423 assert(e
->x
.p
->type
== polynomial
||
4424 e
->x
.p
->type
== flooring
||
4425 e
->x
.p
->type
== fractional
);
4426 if (e
->x
.p
->type
== polynomial
&& e
->x
.p
->pos
>= first
+1) {
4427 assert(e
->x
.p
->pos
+ n
>= 1);
4430 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
4431 evalue_shift_variables(&e
->x
.p
->arr
[i
], first
, n
);
4434 static const evalue
*outer_floor(evalue
*e
, const evalue
*outer
)
4438 if (value_notzero_p(e
->d
))
4440 switch (e
->x
.p
->type
) {
4442 if (!outer
|| evalue_level_cmp(outer
, &e
->x
.p
->arr
[0]) > 0)
4443 return &e
->x
.p
->arr
[0];
4449 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
4450 outer
= outer_floor(&e
->x
.p
->arr
[i
], outer
);
4453 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
4454 outer
= outer_floor(&e
->x
.p
->arr
[2*i
+1], outer
);
4461 /* Find and return outermost floor argument or NULL if e has no floors */
4462 evalue
*evalue_outer_floor(evalue
*e
)
4464 const evalue
*floor
= outer_floor(e
, NULL
);
4465 return floor
? evalue_dup(floor
): NULL
;
4468 static void evalue_set_to_zero(evalue
*e
)
4470 if (EVALUE_IS_ZERO(*e
))
4472 if (value_zero_p(e
->d
)) {
4473 free_evalue_refs(e
);
4477 value_set_si(e
->d
, 1);
4478 value_set_si(e
->x
.n
, 0);
4481 /* Replace (outer) floor with argument "floor" by variable "var" (0-based)
4482 * and drop terms not containing the floor.
4483 * Returns true if e contains the floor.
4485 int evalue_replace_floor(evalue
*e
, const evalue
*floor
, int var
)
4491 if (value_notzero_p(e
->d
))
4493 switch (e
->x
.p
->type
) {
4495 if (!eequal(floor
, &e
->x
.p
->arr
[0]))
4497 e
->x
.p
->type
= polynomial
;
4498 e
->x
.p
->pos
= 1 + var
;
4500 free_evalue_refs(&e
->x
.p
->arr
[0]);
4501 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
4502 e
->x
.p
->arr
[i
] = e
->x
.p
->arr
[i
+1];
4503 evalue_set_to_zero(&e
->x
.p
->arr
[0]);
4508 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
) {
4509 int c
= evalue_replace_floor(&e
->x
.p
->arr
[i
], floor
, var
);
4512 evalue_set_to_zero(&e
->x
.p
->arr
[i
]);
4513 if (c
&& !reorder
&& evalue_level_cmp(&e
->x
.p
->arr
[i
], e
) < 0)
4516 evalue_reduce_size(e
);
4518 evalue_reorder_terms(e
);
4526 /* Replace (outer) floor with argument "floor" by variable zero */
4527 void evalue_drop_floor(evalue
*e
, const evalue
*floor
)
4532 if (value_notzero_p(e
->d
))
4534 switch (e
->x
.p
->type
) {
4536 if (!eequal(floor
, &e
->x
.p
->arr
[0]))
4539 free_evalue_refs(&p
->arr
[0]);
4540 for (i
= 2; i
< p
->size
; ++i
)
4541 free_evalue_refs(&p
->arr
[i
]);
4549 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
4550 evalue_drop_floor(&e
->x
.p
->arr
[i
], floor
);
4551 evalue_reduce_size(e
);