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 static void 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 reorder_terms(&rem
);
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);
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 evalue
*esum_over_domain_cst(int nvar
, Polyhedron
*D
, Matrix
*C
)
3297 int nparam
= D
->Dimension
- nvar
;
3301 D
= Constraints2Polyhedron(C
, 0);
3305 t
= barvinok_enumerate_e(D
, 0, nparam
, 0);
3307 /* Double check that D was not unbounded. */
3308 assert(!(value_pos_p(t
->d
) && value_neg_p(t
->x
.n
)));
3316 static void domain_signs(Polyhedron
*D
, int *signs
)
3320 POL_ENSURE_VERTICES(D
);
3321 for (j
= 0; j
< D
->Dimension
; ++j
) {
3323 for (k
= 0; k
< D
->NbRays
; ++k
) {
3324 signs
[j
] = value_sign(D
->Ray
[k
][1+j
]);
3331 static evalue
*esum_over_domain(evalue
*e
, int nvar
, Polyhedron
*D
,
3332 int *signs
, Matrix
*C
, unsigned MaxRays
)
3338 evalue
*factor
= NULL
;
3342 if (EVALUE_IS_ZERO(*e
))
3346 Polyhedron
*DD
= Disjoint_Domain(D
, 0, MaxRays
);
3353 res
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3356 for (Q
= DD
; Q
; Q
= DD
) {
3362 t
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3375 if (value_notzero_p(e
->d
)) {
3378 t
= esum_over_domain_cst(nvar
, D
, C
);
3380 if (!EVALUE_IS_ONE(*e
))
3387 signs
= alloca(sizeof(int) * D
->Dimension
);
3388 domain_signs(D
, signs
);
3391 switch (e
->x
.p
->type
) {
3393 evalue
*pp
= &e
->x
.p
->arr
[0];
3395 if (pp
->x
.p
->pos
> nvar
) {
3396 /* remainder is independent of the summated vars */
3402 floor2frac(&f
, nvar
);
3404 t
= esum_over_domain_cst(nvar
, D
, C
);
3408 free_evalue_refs(&f
);
3413 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3414 poly_denom(pp
, &row
->p
[1 + nvar
]);
3415 value_set_si(row
->p
[0], 1);
3416 for (pp
= &e
->x
.p
->arr
[0]; value_zero_p(pp
->d
);
3417 pp
= &pp
->x
.p
->arr
[0]) {
3419 assert(pp
->x
.p
->type
== polynomial
);
3421 if (pos
>= 1 + nvar
)
3423 value_assign(row
->p
[pos
], row
->p
[1+nvar
]);
3424 value_division(row
->p
[pos
], row
->p
[pos
], pp
->x
.p
->arr
[1].d
);
3425 value_multiply(row
->p
[pos
], row
->p
[pos
], pp
->x
.p
->arr
[1].x
.n
);
3427 value_assign(row
->p
[1 + D
->Dimension
+ 1], row
->p
[1+nvar
]);
3428 value_division(row
->p
[1 + D
->Dimension
+ 1],
3429 row
->p
[1 + D
->Dimension
+ 1],
3431 value_multiply(row
->p
[1 + D
->Dimension
+ 1],
3432 row
->p
[1 + D
->Dimension
+ 1],
3434 value_oppose(row
->p
[1 + nvar
], row
->p
[1 + nvar
]);
3438 int pos
= e
->x
.p
->pos
;
3441 factor
= ALLOC(evalue
);
3442 value_init(factor
->d
);
3443 value_set_si(factor
->d
, 0);
3444 factor
->x
.p
= new_enode(polynomial
, 2, pos
- nvar
);
3445 evalue_set_si(&factor
->x
.p
->arr
[0], 0, 1);
3446 evalue_set_si(&factor
->x
.p
->arr
[1], 1, 1);
3450 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3451 negative
= signs
[pos
-1] < 0;
3452 value_set_si(row
->p
[0], 1);
3454 value_set_si(row
->p
[pos
], -1);
3455 value_set_si(row
->p
[1 + nvar
], 1);
3457 value_set_si(row
->p
[pos
], 1);
3458 value_set_si(row
->p
[1 + nvar
], -1);
3466 offset
= type_offset(e
->x
.p
);
3468 res
= esum_over_domain(&e
->x
.p
->arr
[offset
], nvar
, D
, signs
, C
, MaxRays
);
3472 evalue_copy(&cum
, factor
);
3476 for (i
= 1; offset
+i
< e
->x
.p
->size
; ++i
) {
3480 C
= esum_add_constraint(nvar
, D
, C
, row
, negative
);
3486 Vector_Print(stderr, P_VALUE_FMT, row);
3488 Matrix_Print(stderr, P_VALUE_FMT, C);
3490 t
= esum_over_domain(&e
->x
.p
->arr
[offset
+i
], nvar
, D
, signs
, C
, MaxRays
);
3495 if (negative
&& (i
% 2))
3505 if (factor
&& offset
+i
+1 < e
->x
.p
->size
)
3512 free_evalue_refs(&cum
);
3513 evalue_free(factor
);
3524 static Polyhedron_Insert(Polyhedron
***next
, Polyhedron
*Q
)
3534 static Polyhedron
*Polyhedron_Split_Into_Orthants(Polyhedron
*P
,
3539 Vector
*c
= Vector_Alloc(1 + P
->Dimension
+ 1);
3540 value_set_si(c
->p
[0], 1);
3542 if (P
->Dimension
== 0)
3543 return Polyhedron_Copy(P
);
3545 for (i
= 0; i
< P
->Dimension
; ++i
) {
3546 Polyhedron
*L
= NULL
;
3547 Polyhedron
**next
= &L
;
3550 for (I
= D
; I
; I
= I
->next
) {
3552 value_set_si(c
->p
[1+i
], 1);
3553 value_set_si(c
->p
[1+P
->Dimension
], 0);
3554 Q
= AddConstraints(c
->p
, 1, I
, MaxRays
);
3555 Polyhedron_Insert(&next
, Q
);
3556 value_set_si(c
->p
[1+i
], -1);
3557 value_set_si(c
->p
[1+P
->Dimension
], -1);
3558 Q
= AddConstraints(c
->p
, 1, I
, MaxRays
);
3559 Polyhedron_Insert(&next
, Q
);
3560 value_set_si(c
->p
[1+i
], 0);
3570 /* Make arguments of all floors non-negative */
3571 static void shift_floor_in_domain(evalue
*e
, Polyhedron
*D
)
3578 if (value_notzero_p(e
->d
))
3583 for (i
= type_offset(p
); i
< p
->size
; ++i
)
3584 shift_floor_in_domain(&p
->arr
[i
], D
);
3586 if (p
->type
!= flooring
)
3592 I
= polynomial_projection(p
, D
, &d
, NULL
);
3593 assert(I
->NbEq
== 0); /* Should have been reduced */
3595 for (i
= 0; i
< I
->NbConstraints
; ++i
)
3596 if (value_pos_p(I
->Constraint
[i
][1]))
3598 assert(i
< I
->NbConstraints
);
3599 if (i
< I
->NbConstraints
) {
3600 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
3601 mpz_fdiv_q(m
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
3602 if (value_neg_p(m
)) {
3603 /* replace [e] by [e-m]+m such that e-m >= 0 */
3608 value_set_si(f
.d
, 1);
3609 value_oppose(f
.x
.n
, m
);
3610 eadd(&f
, &p
->arr
[0]);
3613 value_set_si(f
.d
, 0);
3614 f
.x
.p
= new_enode(flooring
, 3, -1);
3615 value_clear(f
.x
.p
->arr
[0].d
);
3616 f
.x
.p
->arr
[0] = p
->arr
[0];
3617 evalue_set_si(&f
.x
.p
->arr
[2], 1, 1);
3618 value_set_si(f
.x
.p
->arr
[1].d
, 1);
3619 value_init(f
.x
.p
->arr
[1].x
.n
);
3620 value_assign(f
.x
.p
->arr
[1].x
.n
, m
);
3621 reorder_terms_about(p
, &f
);
3632 evalue
*box_summate(Polyhedron
*P
, evalue
*E
, unsigned nvar
, unsigned MaxRays
)
3634 evalue
*sum
= evalue_zero();
3635 Polyhedron
*D
= Polyhedron_Split_Into_Orthants(P
, MaxRays
);
3636 for (P
= D
; P
; P
= P
->next
) {
3638 evalue
*fe
= evalue_dup(E
);
3639 Polyhedron
*next
= P
->next
;
3641 reduce_evalue_in_domain(fe
, P
);
3642 evalue_frac2floor2(fe
, 0);
3643 shift_floor_in_domain(fe
, P
);
3644 t
= esum_over_domain(fe
, nvar
, P
, NULL
, NULL
, MaxRays
);
3656 /* Initial silly implementation */
3657 void eor(evalue
*e1
, evalue
*res
)
3663 evalue_set_si(&mone
, -1, 1);
3665 evalue_copy(&E
, res
);
3671 free_evalue_refs(&E
);
3672 free_evalue_refs(&mone
);
3675 /* computes denominator of polynomial evalue
3676 * d should point to a value initialized to 1
3678 void evalue_denom(const evalue
*e
, Value
*d
)
3682 if (value_notzero_p(e
->d
)) {
3683 value_lcm(*d
, *d
, e
->d
);
3686 assert(e
->x
.p
->type
== polynomial
||
3687 e
->x
.p
->type
== fractional
||
3688 e
->x
.p
->type
== flooring
);
3689 offset
= type_offset(e
->x
.p
);
3690 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3691 evalue_denom(&e
->x
.p
->arr
[i
], d
);
3694 /* Divides the evalue e by the integer n */
3695 void evalue_div(evalue
*e
, Value n
)
3699 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3702 if (value_notzero_p(e
->d
)) {
3705 value_multiply(e
->d
, e
->d
, n
);
3706 value_gcd(gc
, e
->x
.n
, e
->d
);
3707 if (value_notone_p(gc
)) {
3708 value_division(e
->d
, e
->d
, gc
);
3709 value_division(e
->x
.n
, e
->x
.n
, gc
);
3714 if (e
->x
.p
->type
== partition
) {
3715 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3716 evalue_div(&e
->x
.p
->arr
[2*i
+1], n
);
3719 offset
= type_offset(e
->x
.p
);
3720 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3721 evalue_div(&e
->x
.p
->arr
[i
], n
);
3724 /* Multiplies the evalue e by the integer n */
3725 void evalue_mul(evalue
*e
, Value n
)
3729 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3732 if (value_notzero_p(e
->d
)) {
3735 value_multiply(e
->x
.n
, e
->x
.n
, n
);
3736 value_gcd(gc
, e
->x
.n
, e
->d
);
3737 if (value_notone_p(gc
)) {
3738 value_division(e
->d
, e
->d
, gc
);
3739 value_division(e
->x
.n
, e
->x
.n
, gc
);
3744 if (e
->x
.p
->type
== partition
) {
3745 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3746 evalue_mul(&e
->x
.p
->arr
[2*i
+1], n
);
3749 offset
= type_offset(e
->x
.p
);
3750 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3751 evalue_mul(&e
->x
.p
->arr
[i
], n
);
3754 /* Multiplies the evalue e by the n/d */
3755 void evalue_mul_div(evalue
*e
, Value n
, Value d
)
3759 if ((value_one_p(n
) && value_one_p(d
)) || EVALUE_IS_ZERO(*e
))
3762 if (value_notzero_p(e
->d
)) {
3765 value_multiply(e
->x
.n
, e
->x
.n
, n
);
3766 value_multiply(e
->d
, e
->d
, d
);
3767 value_gcd(gc
, e
->x
.n
, e
->d
);
3768 if (value_notone_p(gc
)) {
3769 value_division(e
->d
, e
->d
, gc
);
3770 value_division(e
->x
.n
, e
->x
.n
, gc
);
3775 if (e
->x
.p
->type
== partition
) {
3776 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3777 evalue_mul_div(&e
->x
.p
->arr
[2*i
+1], n
, d
);
3780 offset
= type_offset(e
->x
.p
);
3781 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3782 evalue_mul_div(&e
->x
.p
->arr
[i
], n
, d
);
3785 void evalue_negate(evalue
*e
)
3789 if (value_notzero_p(e
->d
)) {
3790 value_oppose(e
->x
.n
, e
->x
.n
);
3793 if (e
->x
.p
->type
== partition
) {
3794 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3795 evalue_negate(&e
->x
.p
->arr
[2*i
+1]);
3798 offset
= type_offset(e
->x
.p
);
3799 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3800 evalue_negate(&e
->x
.p
->arr
[i
]);
3803 void evalue_add_constant(evalue
*e
, const Value cst
)
3807 if (value_zero_p(e
->d
)) {
3808 if (e
->x
.p
->type
== partition
) {
3809 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3810 evalue_add_constant(&e
->x
.p
->arr
[2*i
+1], cst
);
3813 if (e
->x
.p
->type
== relation
) {
3814 for (i
= 1; i
< e
->x
.p
->size
; ++i
)
3815 evalue_add_constant(&e
->x
.p
->arr
[i
], cst
);
3819 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)];
3820 } while (value_zero_p(e
->d
));
3822 value_addmul(e
->x
.n
, cst
, e
->d
);
3825 static void evalue_frac2polynomial_r(evalue
*e
, int *signs
, int sign
, int in_frac
)
3830 int sign_odd
= sign
;
3832 if (value_notzero_p(e
->d
)) {
3833 if (in_frac
&& sign
* value_sign(e
->x
.n
) < 0) {
3834 value_set_si(e
->x
.n
, 0);
3835 value_set_si(e
->d
, 1);
3840 if (e
->x
.p
->type
== relation
) {
3841 for (i
= e
->x
.p
->size
-1; i
>= 1; --i
)
3842 evalue_frac2polynomial_r(&e
->x
.p
->arr
[i
], signs
, sign
, in_frac
);
3846 if (e
->x
.p
->type
== polynomial
)
3847 sign_odd
*= signs
[e
->x
.p
->pos
-1];
3848 offset
= type_offset(e
->x
.p
);
3849 evalue_frac2polynomial_r(&e
->x
.p
->arr
[offset
], signs
, sign
, in_frac
);
3850 in_frac
|= e
->x
.p
->type
== fractional
;
3851 for (i
= e
->x
.p
->size
-1; i
> offset
; --i
)
3852 evalue_frac2polynomial_r(&e
->x
.p
->arr
[i
], signs
,
3853 (i
- offset
) % 2 ? sign_odd
: sign
, in_frac
);
3855 if (e
->x
.p
->type
!= fractional
)
3858 /* replace { a/m } by (m-1)/m if sign != 0
3859 * and by (m-1)/(2m) if sign == 0
3863 evalue_denom(&e
->x
.p
->arr
[0], &d
);
3864 free_evalue_refs(&e
->x
.p
->arr
[0]);
3865 value_init(e
->x
.p
->arr
[0].d
);
3866 value_init(e
->x
.p
->arr
[0].x
.n
);
3868 value_addto(e
->x
.p
->arr
[0].d
, d
, d
);
3870 value_assign(e
->x
.p
->arr
[0].d
, d
);
3871 value_decrement(e
->x
.p
->arr
[0].x
.n
, d
);
3875 reorder_terms_about(p
, &p
->arr
[0]);
3881 /* Approximate the evalue in fractional representation by a polynomial.
3882 * If sign > 0, the result is an upper bound;
3883 * if sign < 0, the result is a lower bound;
3884 * if sign = 0, the result is an intermediate approximation.
3886 void evalue_frac2polynomial(evalue
*e
, int sign
, unsigned MaxRays
)
3891 if (value_notzero_p(e
->d
))
3893 assert(e
->x
.p
->type
== partition
);
3894 /* make sure all variables in the domains have a fixed sign */
3896 evalue_split_domains_into_orthants(e
, MaxRays
);
3897 if (EVALUE_IS_ZERO(*e
))
3901 assert(e
->x
.p
->size
>= 2);
3902 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
3904 signs
= alloca(sizeof(int) * dim
);
3907 for (i
= 0; i
< dim
; ++i
)
3909 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
3911 domain_signs(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), signs
);
3912 evalue_frac2polynomial_r(&e
->x
.p
->arr
[2*i
+1], signs
, sign
, 0);
3916 /* Split the domains of e (which is assumed to be a partition)
3917 * such that each resulting domain lies entirely in one orthant.
3919 void evalue_split_domains_into_orthants(evalue
*e
, unsigned MaxRays
)
3922 assert(value_zero_p(e
->d
));
3923 assert(e
->x
.p
->type
== partition
);
3924 assert(e
->x
.p
->size
>= 2);
3925 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
3927 for (i
= 0; i
< dim
; ++i
) {
3930 C
= Matrix_Alloc(1, 1 + dim
+ 1);
3931 value_set_si(C
->p
[0][0], 1);
3932 value_init(split
.d
);
3933 value_set_si(split
.d
, 0);
3934 split
.x
.p
= new_enode(partition
, 4, dim
);
3935 value_set_si(C
->p
[0][1+i
], 1);
3936 C2
= Matrix_Copy(C
);
3937 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0], Constraints2Polyhedron(C2
, MaxRays
));
3939 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
3940 value_set_si(C
->p
[0][1+i
], -1);
3941 value_set_si(C
->p
[0][1+dim
], -1);
3942 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2], Constraints2Polyhedron(C
, MaxRays
));
3943 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
3945 free_evalue_refs(&split
);
3950 static evalue
*find_fractional_with_max_periods(evalue
*e
, Polyhedron
*D
,
3953 Value
*min
, Value
*max
)
3960 if (value_notzero_p(e
->d
))
3963 if (e
->x
.p
->type
== fractional
) {
3968 I
= polynomial_projection(e
->x
.p
, D
, &d
, &T
);
3969 bounded
= line_minmax(I
, min
, max
); /* frees I */
3973 value_set_si(mp
, max_periods
);
3974 mpz_fdiv_q(*min
, *min
, d
);
3975 mpz_fdiv_q(*max
, *max
, d
);
3976 value_assign(T
->p
[1][D
->Dimension
], d
);
3977 value_subtract(d
, *max
, *min
);
3978 if (value_ge(d
, mp
))
3981 f
= evalue_dup(&e
->x
.p
->arr
[0]);
3992 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
3993 if ((f
= find_fractional_with_max_periods(&e
->x
.p
->arr
[i
], D
, max_periods
,
4000 static void replace_fract_by_affine(evalue
*e
, evalue
*f
, Value val
)
4004 if (value_notzero_p(e
->d
))
4007 offset
= type_offset(e
->x
.p
);
4008 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
4009 replace_fract_by_affine(&e
->x
.p
->arr
[i
], f
, val
);
4011 if (e
->x
.p
->type
!= fractional
)
4014 if (!eequal(&e
->x
.p
->arr
[0], f
))
4017 replace_by_affine(e
, val
);
4020 /* Look for fractional parts that can be removed by splitting the corresponding
4021 * domain into at most max_periods parts.
4022 * We use a very simply strategy that looks for the first fractional part
4023 * that satisfies the condition, performs the split and then continues
4024 * looking for other fractional parts in the split domains until no
4025 * such fractional part can be found anymore.
4027 void evalue_split_periods(evalue
*e
, int max_periods
, unsigned int MaxRays
)
4034 if (EVALUE_IS_ZERO(*e
))
4036 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
) {
4038 "WARNING: evalue_split_periods called on incorrect evalue type\n");
4046 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
4051 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
4053 f
= find_fractional_with_max_periods(&e
->x
.p
->arr
[2*i
+1], D
, max_periods
,
4058 M
= Matrix_Alloc(2, 2+D
->Dimension
);
4060 value_subtract(d
, max
, min
);
4061 n
= VALUE_TO_INT(d
)+1;
4063 value_set_si(M
->p
[0][0], 1);
4064 Vector_Copy(T
->p
[0], M
->p
[0]+1, D
->Dimension
+1);
4065 value_multiply(d
, max
, T
->p
[1][D
->Dimension
]);
4066 value_subtract(M
->p
[0][1+D
->Dimension
], M
->p
[0][1+D
->Dimension
], d
);
4067 value_set_si(d
, -1);
4068 value_set_si(M
->p
[1][0], 1);
4069 Vector_Scale(T
->p
[0], M
->p
[1]+1, d
, D
->Dimension
+1);
4070 value_addmul(M
->p
[1][1+D
->Dimension
], max
, T
->p
[1][D
->Dimension
]);
4071 value_addto(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
],
4072 T
->p
[1][D
->Dimension
]);
4073 value_decrement(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
]);
4075 p
= new_enode(partition
, e
->x
.p
->size
+ (n
-1)*2, e
->x
.p
->pos
);
4076 for (j
= 0; j
< 2*i
; ++j
) {
4077 value_clear(p
->arr
[j
].d
);
4078 p
->arr
[j
] = e
->x
.p
->arr
[j
];
4080 for (j
= 2*i
+2; j
< e
->x
.p
->size
; ++j
) {
4081 value_clear(p
->arr
[j
+2*(n
-1)].d
);
4082 p
->arr
[j
+2*(n
-1)] = e
->x
.p
->arr
[j
];
4084 for (j
= n
-1; j
>= 0; --j
) {
4086 value_clear(p
->arr
[2*i
+1].d
);
4087 p
->arr
[2*i
+1] = e
->x
.p
->arr
[2*i
+1];
4089 evalue_copy(&p
->arr
[2*(i
+j
)+1], &e
->x
.p
->arr
[2*i
+1]);
4091 value_subtract(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
],
4092 T
->p
[1][D
->Dimension
]);
4093 value_addto(M
->p
[0][1+D
->Dimension
], M
->p
[0][1+D
->Dimension
],
4094 T
->p
[1][D
->Dimension
]);
4096 replace_fract_by_affine(&p
->arr
[2*(i
+j
)+1], f
, max
);
4097 E
= DomainAddConstraints(D
, M
, MaxRays
);
4098 EVALUE_SET_DOMAIN(p
->arr
[2*(i
+j
)], E
);
4099 if (evalue_range_reduction_in_domain(&p
->arr
[2*(i
+j
)+1], E
))
4100 reduce_evalue(&p
->arr
[2*(i
+j
)+1]);
4101 value_decrement(max
, max
);
4103 value_clear(e
->x
.p
->arr
[2*i
].d
);
4118 void evalue_extract_affine(const evalue
*e
, Value
*coeff
, Value
*cst
, Value
*d
)
4120 value_set_si(*d
, 1);
4122 for ( ; value_zero_p(e
->d
); e
= &e
->x
.p
->arr
[0]) {
4124 assert(e
->x
.p
->type
== polynomial
);
4125 assert(e
->x
.p
->size
== 2);
4126 c
= &e
->x
.p
->arr
[1];
4127 value_multiply(coeff
[e
->x
.p
->pos
-1], *d
, c
->x
.n
);
4128 value_division(coeff
[e
->x
.p
->pos
-1], coeff
[e
->x
.p
->pos
-1], c
->d
);
4130 value_multiply(*cst
, *d
, e
->x
.n
);
4131 value_division(*cst
, *cst
, e
->d
);
4134 /* returns an evalue that corresponds to
4138 static evalue
*term(int param
, Value c
, Value den
)
4140 evalue
*EP
= ALLOC(evalue
);
4142 value_set_si(EP
->d
,0);
4143 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
4144 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
4145 value_init(EP
->x
.p
->arr
[1].x
.n
);
4146 value_assign(EP
->x
.p
->arr
[1].d
, den
);
4147 value_assign(EP
->x
.p
->arr
[1].x
.n
, c
);
4151 evalue
*affine2evalue(Value
*coeff
, Value denom
, int nvar
)
4154 evalue
*E
= ALLOC(evalue
);
4156 evalue_set(E
, coeff
[nvar
], denom
);
4157 for (i
= 0; i
< nvar
; ++i
) {
4159 if (value_zero_p(coeff
[i
]))
4161 t
= term(i
, coeff
[i
], denom
);
4168 void evalue_substitute(evalue
*e
, evalue
**subs
)
4174 if (value_notzero_p(e
->d
))
4178 assert(p
->type
!= partition
);
4180 for (i
= 0; i
< p
->size
; ++i
)
4181 evalue_substitute(&p
->arr
[i
], subs
);
4183 if (p
->type
== relation
) {
4184 /* For relation a ? b : c, compute (a' ? 1) * b' + (a' ? 0 : 1) * c' */
4188 value_set_si(v
->d
, 0);
4189 v
->x
.p
= new_enode(relation
, 3, 0);
4190 evalue_copy(&v
->x
.p
->arr
[0], &p
->arr
[0]);
4191 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4192 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4193 emul(v
, &p
->arr
[2]);
4198 value_set_si(v
->d
, 0);
4199 v
->x
.p
= new_enode(relation
, 2, 0);
4200 value_clear(v
->x
.p
->arr
[0].d
);
4201 v
->x
.p
->arr
[0] = p
->arr
[0];
4202 evalue_set_si(&v
->x
.p
->arr
[1], 1, 1);
4203 emul(v
, &p
->arr
[1]);
4206 eadd(&p
->arr
[2], &p
->arr
[1]);
4207 free_evalue_refs(&p
->arr
[2]);
4215 if (p
->type
== polynomial
)
4220 value_set_si(v
->d
, 0);
4221 v
->x
.p
= new_enode(p
->type
, 3, -1);
4222 value_clear(v
->x
.p
->arr
[0].d
);
4223 v
->x
.p
->arr
[0] = p
->arr
[0];
4224 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4225 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4228 offset
= type_offset(p
);
4230 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
4231 emul(v
, &p
->arr
[i
]);
4232 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
4233 free_evalue_refs(&(p
->arr
[i
]));
4236 if (p
->type
!= polynomial
)
4240 *e
= p
->arr
[offset
];
4244 /* evalue e is given in terms of "new" parameter; CP maps the new
4245 * parameters back to the old parameters.
4246 * Transforms e such that it refers back to the old parameters and
4247 * adds appropriate constraints to the domain.
4248 * In particular, if CP maps the new parameters onto an affine
4249 * subspace of the old parameters, then the corresponding equalities
4250 * are added to the domain.
4251 * Also, if any of the new parameters was a rational combination
4252 * of the old parameters $p' = (<a, p> + c)/m$, then modulo
4253 * constraints ${<a, p> + c)/m} = 0$ are added to ensure
4254 * the new evalue remains non-zero only for integer parameters
4255 * of the new parameters (which have been removed by the substitution).
4257 void evalue_backsubstitute(evalue
*e
, Matrix
*CP
, unsigned MaxRays
)
4264 unsigned nparam
= CP
->NbColumns
-1;
4268 if (EVALUE_IS_ZERO(*e
))
4271 assert(value_zero_p(e
->d
));
4273 assert(p
->type
== partition
);
4275 inv
= left_inverse(CP
, &eq
);
4276 subs
= ALLOCN(evalue
*, nparam
);
4277 for (i
= 0; i
< nparam
; ++i
)
4278 subs
[i
] = affine2evalue(inv
->p
[i
], inv
->p
[nparam
][inv
->NbColumns
-1],
4281 CEq
= Constraints2Polyhedron(eq
, MaxRays
);
4282 addeliminatedparams_partition(p
, inv
, CEq
, inv
->NbColumns
-1, MaxRays
);
4283 Polyhedron_Free(CEq
);
4285 for (i
= 0; i
< p
->size
/2; ++i
)
4286 evalue_substitute(&p
->arr
[2*i
+1], subs
);
4288 for (i
= 0; i
< nparam
; ++i
)
4289 evalue_free(subs
[i
]);
4293 for (i
= 0; i
< inv
->NbRows
-1; ++i
) {
4294 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &gcd
);
4295 value_gcd(gcd
, gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
4296 if (value_eq(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
4298 Vector_AntiScale(inv
->p
[i
], inv
->p
[i
], gcd
, inv
->NbColumns
);
4299 value_divexact(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1], gcd
);
4301 for (j
= 0; j
< p
->size
/2; ++j
) {
4302 evalue
*arg
= affine2evalue(inv
->p
[i
], gcd
, inv
->NbColumns
-1);
4307 value_set_si(rel
.d
, 0);
4308 rel
.x
.p
= new_enode(relation
, 2, 0);
4309 value_clear(rel
.x
.p
->arr
[1].d
);
4310 rel
.x
.p
->arr
[1] = p
->arr
[2*j
+1];
4311 ev
= &rel
.x
.p
->arr
[0];
4312 value_set_si(ev
->d
, 0);
4313 ev
->x
.p
= new_enode(fractional
, 3, -1);
4314 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
4315 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
4316 value_clear(ev
->x
.p
->arr
[0].d
);
4317 ev
->x
.p
->arr
[0] = *arg
;
4320 p
->arr
[2*j
+1] = rel
;
4331 * \sum_{i=0}^n c_i/d X^i
4333 * where d is the last element in the vector c.
4335 evalue
*evalue_polynomial(Vector
*c
, const evalue
* X
)
4337 unsigned dim
= c
->Size
-2;
4339 evalue
*EP
= ALLOC(evalue
);
4344 if (EVALUE_IS_ZERO(*X
) || dim
== 0) {
4345 evalue_set(EP
, c
->p
[0], c
->p
[dim
+1]);
4346 reduce_constant(EP
);
4350 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
4353 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
4355 for (i
= dim
-1; i
>= 0; --i
) {
4357 value_assign(EC
.x
.n
, c
->p
[i
]);
4360 free_evalue_refs(&EC
);
4364 /* Create an evalue from an array of pairs of domains and evalues. */
4365 evalue
*evalue_from_section_array(struct evalue_section
*s
, int n
)
4370 res
= ALLOC(evalue
);
4374 evalue_set_si(res
, 0, 1);
4376 value_set_si(res
->d
, 0);
4377 res
->x
.p
= new_enode(partition
, 2*n
, s
[0].D
->Dimension
);
4378 for (i
= 0; i
< n
; ++i
) {
4379 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*i
], s
[i
].D
);
4380 value_clear(res
->x
.p
->arr
[2*i
+1].d
);
4381 res
->x
.p
->arr
[2*i
+1] = *s
[i
].E
;
4388 /* shift variables (>= first, 0-based) in polynomial n up (may be negative) */
4389 void evalue_shift_variables(evalue
*e
, int first
, int n
)
4392 if (value_notzero_p(e
->d
))
4394 assert(e
->x
.p
->type
== polynomial
||
4395 e
->x
.p
->type
== flooring
||
4396 e
->x
.p
->type
== fractional
);
4397 if (e
->x
.p
->type
== polynomial
&& e
->x
.p
->pos
>= first
+1) {
4398 assert(e
->x
.p
->pos
+ n
>= 1);
4401 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
4402 evalue_shift_variables(&e
->x
.p
->arr
[i
], first
, n
);
4405 static const evalue
*outer_floor(evalue
*e
, const evalue
*outer
)
4409 if (value_notzero_p(e
->d
))
4411 switch (e
->x
.p
->type
) {
4413 if (!outer
|| evalue_level_cmp(outer
, &e
->x
.p
->arr
[0]) > 0)
4414 return &e
->x
.p
->arr
[0];
4420 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
4421 outer
= outer_floor(&e
->x
.p
->arr
[i
], outer
);
4424 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
4425 outer
= outer_floor(&e
->x
.p
->arr
[2*i
+1], outer
);
4432 /* Find and return outermost floor argument or NULL if e has no floors */
4433 evalue
*evalue_outer_floor(evalue
*e
)
4435 const evalue
*floor
= outer_floor(e
, NULL
);
4436 return floor
? evalue_dup(floor
): NULL
;
4439 static void evalue_set_to_zero(evalue
*e
)
4441 if (EVALUE_IS_ZERO(*e
))
4443 if (value_zero_p(e
->d
)) {
4444 free_evalue_refs(e
);
4448 value_set_si(e
->d
, 1);
4449 value_set_si(e
->x
.n
, 0);
4452 /* Replace (outer) floor with argument "floor" by variable "var" (0-based)
4453 * and drop terms not containing the floor.
4454 * Returns true if e contains the floor.
4456 int evalue_replace_floor(evalue
*e
, const evalue
*floor
, int var
)
4462 if (value_notzero_p(e
->d
))
4464 switch (e
->x
.p
->type
) {
4466 if (!eequal(floor
, &e
->x
.p
->arr
[0]))
4468 e
->x
.p
->type
= polynomial
;
4469 e
->x
.p
->pos
= 1 + var
;
4471 free_evalue_refs(&e
->x
.p
->arr
[0]);
4472 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
4473 e
->x
.p
->arr
[i
] = e
->x
.p
->arr
[i
+1];
4474 evalue_set_to_zero(&e
->x
.p
->arr
[0]);
4479 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
) {
4480 int c
= evalue_replace_floor(&e
->x
.p
->arr
[i
], floor
, var
);
4483 evalue_set_to_zero(&e
->x
.p
->arr
[i
]);
4484 if (c
&& !reorder
&& evalue_level_cmp(&e
->x
.p
->arr
[i
], e
) < 0)
4487 evalue_reduce_size(e
);
4497 /* Replace (outer) floor with argument "floor" by variable zero */
4498 void evalue_drop_floor(evalue
*e
, const evalue
*floor
)
4503 if (value_notzero_p(e
->d
))
4505 switch (e
->x
.p
->type
) {
4507 if (!eequal(floor
, &e
->x
.p
->arr
[0]))
4510 free_evalue_refs(&p
->arr
[0]);
4511 for (i
= 2; i
< p
->size
; ++i
)
4512 free_evalue_refs(&p
->arr
[i
]);
4520 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
4521 evalue_drop_floor(&e
->x
.p
->arr
[i
], floor
);
4522 evalue_reduce_size(e
);