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
)
442 assert(value_zero_p(e
->d
));
444 assert(p
->type
== fractional
); /* for now */
447 value_set_si(f
.d
, 0);
448 f
.x
.p
= new_enode(fractional
, 3, -1);
449 value_clear(f
.x
.p
->arr
[0].d
);
450 f
.x
.p
->arr
[0] = p
->arr
[0];
451 evalue_set_si(&f
.x
.p
->arr
[1], 0, 1);
452 evalue_set_si(&f
.x
.p
->arr
[2], 1, 1);
453 reorder_terms_about(p
, &f
);
459 static void evalue_reduce_size(evalue
*e
)
463 assert(value_zero_p(e
->d
));
466 offset
= type_offset(p
);
468 /* Try to reduce the degree */
469 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
470 if (!EVALUE_IS_ZERO(p
->arr
[i
]))
472 free_evalue_refs(&p
->arr
[i
]);
477 /* Try to reduce its strength */
478 if (p
->type
== relation
) {
480 free_evalue_refs(&p
->arr
[0]);
481 evalue_set_si(e
, 0, 1);
484 } else if (p
->size
== offset
+1) {
486 memcpy(e
, &p
->arr
[offset
], sizeof(evalue
));
488 free_evalue_refs(&p
->arr
[0]);
493 void _reduce_evalue (evalue
*e
, struct subst
*s
, int fract
) {
499 if (value_notzero_p(e
->d
)) {
501 mpz_fdiv_r(e
->x
.n
, e
->x
.n
, e
->d
);
502 return; /* a rational number, its already reduced */
506 return; /* hum... an overflow probably occured */
508 /* First reduce the components of p */
509 add
= p
->type
== relation
;
510 for (i
=0; i
<p
->size
; i
++) {
512 add
= add_modulo_substitution(s
, e
);
514 if (i
== 0 && p
->type
==fractional
)
515 _reduce_evalue(&p
->arr
[i
], s
, 1);
517 _reduce_evalue(&p
->arr
[i
], s
, fract
);
519 if (add
&& i
== p
->size
-1) {
521 value_clear(s
->fixed
[s
->n
].m
);
522 value_clear(s
->fixed
[s
->n
].d
);
523 free_evalue_refs(&s
->fixed
[s
->n
].s
);
524 } else if (add
&& i
== 1)
525 s
->fixed
[s
->n
-1].pos
*= -1;
528 if (p
->type
==periodic
) {
530 /* Try to reduce the period */
531 for (i
=1; i
<=(p
->size
)/2; i
++) {
532 if ((p
->size
% i
)==0) {
534 /* Can we reduce the size to i ? */
536 for (k
=j
+i
; k
<e
->x
.p
->size
; k
+=i
)
537 if (!eequal(&p
->arr
[j
], &p
->arr
[k
])) goto you_lose
;
540 for (j
=i
; j
<p
->size
; j
++) free_evalue_refs(&p
->arr
[j
]);
544 you_lose
: /* OK, lets not do it */
549 /* Try to reduce its strength */
552 memcpy(e
,&p
->arr
[0],sizeof(evalue
));
556 else if (p
->type
==polynomial
) {
557 for (k
= 0; s
&& k
< s
->n
; ++k
) {
558 if (s
->fixed
[k
].pos
== p
->pos
) {
559 int divide
= value_notone_p(s
->fixed
[k
].d
);
562 if (value_notzero_p(s
->fixed
[k
].m
)) {
565 assert(p
->size
== 2);
566 if (divide
&& value_ne(s
->fixed
[k
].d
, p
->arr
[1].x
.n
))
568 if (!mpz_divisible_p(s
->fixed
[k
].m
, p
->arr
[1].d
))
575 value_assign(d
.d
, s
->fixed
[k
].d
);
577 if (value_notzero_p(s
->fixed
[k
].m
))
578 value_oppose(d
.x
.n
, s
->fixed
[k
].m
);
580 value_set_si(d
.x
.n
, 1);
583 for (i
=p
->size
-1;i
>=1;i
--) {
584 emul(&s
->fixed
[k
].s
, &p
->arr
[i
]);
586 emul(&d
, &p
->arr
[i
]);
587 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
588 free_evalue_refs(&(p
->arr
[i
]));
591 _reduce_evalue(&p
->arr
[0], s
, fract
);
594 free_evalue_refs(&d
);
600 evalue_reduce_size(e
);
602 else if (p
->type
==fractional
) {
606 if (value_notzero_p(p
->arr
[0].d
)) {
608 value_assign(v
.d
, p
->arr
[0].d
);
610 mpz_fdiv_r(v
.x
.n
, p
->arr
[0].x
.n
, p
->arr
[0].d
);
615 evalue
*pp
= &p
->arr
[0];
616 assert(value_zero_p(pp
->d
) && pp
->x
.p
->type
== polynomial
);
617 assert(pp
->x
.p
->size
== 2);
619 /* search for exact duplicate among the modulo inequalities */
621 f
= &pp
->x
.p
->arr
[1];
622 for (k
= 0; s
&& k
< s
->n
; ++k
) {
623 if (-s
->fixed
[k
].pos
== pp
->x
.p
->pos
&&
624 value_eq(s
->fixed
[k
].d
, f
->x
.n
) &&
625 value_eq(s
->fixed
[k
].m
, f
->d
) &&
626 eequal(&s
->fixed
[k
].s
, &pp
->x
.p
->arr
[0]))
633 /* replace { E/m } by { (E-1)/m } + 1/m */
638 evalue_set_si(&extra
, 1, 1);
639 value_assign(extra
.d
, g
);
640 eadd(&extra
, &v
.x
.p
->arr
[1]);
641 free_evalue_refs(&extra
);
643 /* We've been going in circles; stop now */
644 if (value_ge(v
.x
.p
->arr
[1].x
.n
, v
.x
.p
->arr
[1].d
)) {
645 free_evalue_refs(&v
);
647 evalue_set_si(&v
, 0, 1);
652 value_set_si(v
.d
, 0);
653 v
.x
.p
= new_enode(fractional
, 3, -1);
654 evalue_set_si(&v
.x
.p
->arr
[1], 1, 1);
655 value_assign(v
.x
.p
->arr
[1].d
, g
);
656 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
657 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
660 for (f
= &v
.x
.p
->arr
[0]; value_zero_p(f
->d
);
663 value_division(f
->d
, g
, f
->d
);
664 value_multiply(f
->x
.n
, f
->x
.n
, f
->d
);
665 value_assign(f
->d
, g
);
666 value_decrement(f
->x
.n
, f
->x
.n
);
667 mpz_fdiv_r(f
->x
.n
, f
->x
.n
, f
->d
);
669 value_gcd(g
, f
->d
, f
->x
.n
);
670 value_division(f
->d
, f
->d
, g
);
671 value_division(f
->x
.n
, f
->x
.n
, g
);
680 /* reduction may have made this fractional arg smaller */
681 i
= reorder
? p
->size
: 1;
682 for ( ; i
< p
->size
; ++i
)
683 if (value_zero_p(p
->arr
[i
].d
) &&
684 p
->arr
[i
].x
.p
->type
== fractional
&&
685 !mod_term_smaller(e
, &p
->arr
[i
]))
689 value_set_si(v
.d
, 0);
690 v
.x
.p
= new_enode(fractional
, 3, -1);
691 evalue_set_si(&v
.x
.p
->arr
[1], 0, 1);
692 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
693 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
701 evalue
*pp
= &p
->arr
[0];
704 poly_denom_not_constant(&pp
, &m
);
705 mpz_fdiv_r(r
, m
, pp
->d
);
706 if (value_notzero_p(r
)) {
708 value_set_si(v
.d
, 0);
709 v
.x
.p
= new_enode(fractional
, 3, -1);
711 value_multiply(r
, m
, pp
->x
.n
);
712 value_multiply(v
.x
.p
->arr
[1].d
, m
, pp
->d
);
713 value_init(v
.x
.p
->arr
[1].x
.n
);
714 mpz_fdiv_r(v
.x
.p
->arr
[1].x
.n
, r
, pp
->d
);
715 mpz_fdiv_q(r
, r
, pp
->d
);
717 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
718 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
720 while (value_zero_p(pp
->d
))
721 pp
= &pp
->x
.p
->arr
[0];
723 value_assign(pp
->d
, m
);
724 value_assign(pp
->x
.n
, r
);
726 value_gcd(r
, pp
->d
, pp
->x
.n
);
727 value_division(pp
->d
, pp
->d
, r
);
728 value_division(pp
->x
.n
, pp
->x
.n
, r
);
741 for (pp
= &p
->arr
[0]; value_zero_p(pp
->d
);
742 pp
= &pp
->x
.p
->arr
[0]) {
743 f
= &pp
->x
.p
->arr
[1];
744 assert(value_pos_p(f
->d
));
745 mpz_mul_ui(twice
, f
->x
.n
, 2);
746 if (value_lt(twice
, f
->d
))
748 if (value_eq(twice
, f
->d
))
756 value_set_si(v
.d
, 0);
757 v
.x
.p
= new_enode(fractional
, 3, -1);
758 evalue_set_si(&v
.x
.p
->arr
[1], 0, 1);
759 poly_denom(&p
->arr
[0], &twice
);
760 value_assign(v
.x
.p
->arr
[1].d
, twice
);
761 value_decrement(v
.x
.p
->arr
[1].x
.n
, twice
);
762 evalue_set_si(&v
.x
.p
->arr
[2], -1, 1);
763 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
765 for (pp
= &v
.x
.p
->arr
[0]; value_zero_p(pp
->d
);
766 pp
= &pp
->x
.p
->arr
[0]) {
767 f
= &pp
->x
.p
->arr
[1];
768 value_oppose(f
->x
.n
, f
->x
.n
);
769 mpz_fdiv_r(f
->x
.n
, f
->x
.n
, f
->d
);
771 value_division(pp
->d
, twice
, pp
->d
);
772 value_multiply(pp
->x
.n
, pp
->x
.n
, pp
->d
);
773 value_assign(pp
->d
, twice
);
774 value_oppose(pp
->x
.n
, pp
->x
.n
);
775 value_decrement(pp
->x
.n
, pp
->x
.n
);
776 mpz_fdiv_r(pp
->x
.n
, pp
->x
.n
, pp
->d
);
778 /* Maybe we should do this during reduction of
781 value_gcd(twice
, pp
->d
, pp
->x
.n
);
782 value_division(pp
->d
, pp
->d
, twice
);
783 value_division(pp
->x
.n
, pp
->x
.n
, twice
);
793 reorder_terms_about(p
, &v
);
794 _reduce_evalue(&p
->arr
[1], s
, fract
);
797 evalue_reduce_size(e
);
799 else if (p
->type
== flooring
) {
800 /* Replace floor(constant) by its value */
801 if (value_notzero_p(p
->arr
[0].d
)) {
804 value_set_si(v
.d
, 1);
806 mpz_fdiv_q(v
.x
.n
, p
->arr
[0].x
.n
, p
->arr
[0].d
);
807 reorder_terms_about(p
, &v
);
808 _reduce_evalue(&p
->arr
[1], s
, fract
);
810 evalue_reduce_size(e
);
812 else if (p
->type
== relation
) {
813 if (p
->size
== 3 && eequal(&p
->arr
[1], &p
->arr
[2])) {
814 free_evalue_refs(&(p
->arr
[2]));
815 free_evalue_refs(&(p
->arr
[0]));
822 evalue_reduce_size(e
);
823 if (value_notzero_p(e
->d
) || p
!= e
->x
.p
)
830 /* Relation was reduced by means of an identical
831 * inequality => remove
833 if (value_zero_p(m
->d
) && !EVALUE_IS_ZERO(m
->x
.p
->arr
[1]))
836 if (reduced
|| value_notzero_p(p
->arr
[0].d
)) {
837 if (!reduced
&& value_zero_p(p
->arr
[0].x
.n
)) {
839 memcpy(e
,&p
->arr
[1],sizeof(evalue
));
841 free_evalue_refs(&(p
->arr
[2]));
845 memcpy(e
,&p
->arr
[2],sizeof(evalue
));
847 evalue_set_si(e
, 0, 1);
848 free_evalue_refs(&(p
->arr
[1]));
850 free_evalue_refs(&(p
->arr
[0]));
856 } /* reduce_evalue */
858 static void add_substitution(struct subst
*s
, Value
*row
, unsigned dim
)
863 for (k
= 0; k
< dim
; ++k
)
864 if (value_notzero_p(row
[k
+1]))
867 Vector_Normalize_Positive(row
+1, dim
+1, k
);
868 assert(s
->n
< s
->max
);
869 value_init(s
->fixed
[s
->n
].d
);
870 value_init(s
->fixed
[s
->n
].m
);
871 value_assign(s
->fixed
[s
->n
].d
, row
[k
+1]);
872 s
->fixed
[s
->n
].pos
= k
+1;
873 value_set_si(s
->fixed
[s
->n
].m
, 0);
874 r
= &s
->fixed
[s
->n
].s
;
876 for (l
= k
+1; l
< dim
; ++l
)
877 if (value_notzero_p(row
[l
+1])) {
878 value_set_si(r
->d
, 0);
879 r
->x
.p
= new_enode(polynomial
, 2, l
+ 1);
880 value_init(r
->x
.p
->arr
[1].x
.n
);
881 value_oppose(r
->x
.p
->arr
[1].x
.n
, row
[l
+1]);
882 value_set_si(r
->x
.p
->arr
[1].d
, 1);
886 value_oppose(r
->x
.n
, row
[dim
+1]);
887 value_set_si(r
->d
, 1);
891 static void _reduce_evalue_in_domain(evalue
*e
, Polyhedron
*D
, struct subst
*s
)
894 Polyhedron
*orig
= D
;
899 D
= DomainConvex(D
, 0);
900 /* We don't perform any substitutions if the domain is a union.
901 * We may therefore miss out on some possible simplifications,
902 * e.g., if a variable is always even in the whole union,
903 * while there is a relation in the evalue that evaluates
904 * to zero for even values of the variable.
906 if (!D
->next
&& D
->NbEq
) {
910 realloc_substitution(s
, dim
);
912 int d
= relations_depth(e
);
914 NALLOC(s
->fixed
, s
->max
);
917 for (j
= 0; j
< D
->NbEq
; ++j
)
918 add_substitution(s
, D
->Constraint
[j
], dim
);
922 _reduce_evalue(e
, s
, 0);
925 for (j
= 0; j
< s
->n
; ++j
) {
926 value_clear(s
->fixed
[j
].d
);
927 value_clear(s
->fixed
[j
].m
);
928 free_evalue_refs(&s
->fixed
[j
].s
);
933 void reduce_evalue_in_domain(evalue
*e
, Polyhedron
*D
)
935 struct subst s
= { NULL
, 0, 0 };
936 POL_ENSURE_VERTICES(D
);
938 if (EVALUE_IS_ZERO(*e
))
942 evalue_set_si(e
, 0, 1);
945 _reduce_evalue_in_domain(e
, D
, &s
);
950 void reduce_evalue (evalue
*e
) {
951 struct subst s
= { NULL
, 0, 0 };
953 if (value_notzero_p(e
->d
))
954 return; /* a rational number, its already reduced */
956 if (e
->x
.p
->type
== partition
) {
959 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
960 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
962 /* This shouldn't really happen;
963 * Empty domains should not be added.
965 POL_ENSURE_VERTICES(D
);
967 _reduce_evalue_in_domain(&e
->x
.p
->arr
[2*i
+1], D
, &s
);
969 if (emptyQ(D
) || EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
970 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
971 Domain_Free(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]));
972 value_clear(e
->x
.p
->arr
[2*i
].d
);
974 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
975 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
979 if (e
->x
.p
->size
== 0) {
981 evalue_set_si(e
, 0, 1);
984 _reduce_evalue(e
, &s
, 0);
989 static void print_evalue_r(FILE *DST
, const evalue
*e
, const char *const *pname
)
991 if (EVALUE_IS_NAN(*e
)) {
996 if(value_notzero_p(e
->d
)) {
997 if(value_notone_p(e
->d
)) {
998 value_print(DST
,VALUE_FMT
,e
->x
.n
);
1000 value_print(DST
,VALUE_FMT
,e
->d
);
1003 value_print(DST
,VALUE_FMT
,e
->x
.n
);
1007 print_enode(DST
,e
->x
.p
,pname
);
1009 } /* print_evalue */
1011 void print_evalue(FILE *DST
, const evalue
*e
, const char * const *pname
)
1013 print_evalue_r(DST
, e
, pname
);
1014 if (value_notzero_p(e
->d
))
1018 void print_enode(FILE *DST
, enode
*p
, const char *const *pname
)
1023 fprintf(DST
, "NULL");
1029 for (i
=0; i
<p
->size
; i
++) {
1030 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1034 fprintf(DST
, " }\n");
1038 for (i
=p
->size
-1; i
>=0; i
--) {
1039 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1040 if (i
==1) fprintf(DST
, " * %s + ", pname
[p
->pos
-1]);
1042 fprintf(DST
, " * %s^%d + ", pname
[p
->pos
-1], i
);
1044 fprintf(DST
, " )\n");
1048 for (i
=0; i
<p
->size
; i
++) {
1049 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1050 if (i
!=(p
->size
-1)) fprintf(DST
, ", ");
1052 fprintf(DST
," ]_%s", pname
[p
->pos
-1]);
1057 for (i
=p
->size
-1; i
>=1; i
--) {
1058 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1060 fprintf(DST
, " * ");
1061 fprintf(DST
, p
->type
== flooring
? "[" : "{");
1062 print_evalue_r(DST
, &p
->arr
[0], pname
);
1063 fprintf(DST
, p
->type
== flooring
? "]" : "}");
1065 fprintf(DST
, "^%d + ", i
-1);
1067 fprintf(DST
, " + ");
1070 fprintf(DST
, " )\n");
1074 print_evalue_r(DST
, &p
->arr
[0], pname
);
1075 fprintf(DST
, "= 0 ] * \n");
1076 print_evalue_r(DST
, &p
->arr
[1], pname
);
1078 fprintf(DST
, " +\n [ ");
1079 print_evalue_r(DST
, &p
->arr
[0], pname
);
1080 fprintf(DST
, "!= 0 ] * \n");
1081 print_evalue_r(DST
, &p
->arr
[2], pname
);
1085 char **new_names
= NULL
;
1086 const char *const *names
= pname
;
1087 int maxdim
= EVALUE_DOMAIN(p
->arr
[0])->Dimension
;
1088 if (!pname
|| p
->pos
< maxdim
) {
1089 new_names
= ALLOCN(char *, maxdim
);
1090 for (i
= 0; i
< p
->pos
; ++i
) {
1092 new_names
[i
] = (char *)pname
[i
];
1094 new_names
[i
] = ALLOCN(char, 10);
1095 snprintf(new_names
[i
], 10, "%c", 'P'+i
);
1098 for ( ; i
< maxdim
; ++i
) {
1099 new_names
[i
] = ALLOCN(char, 10);
1100 snprintf(new_names
[i
], 10, "_p%d", i
);
1102 names
= (const char**)new_names
;
1105 for (i
=0; i
<p
->size
/2; i
++) {
1106 Print_Domain(DST
, EVALUE_DOMAIN(p
->arr
[2*i
]), names
);
1107 print_evalue_r(DST
, &p
->arr
[2*i
+1], names
);
1108 if (value_notzero_p(p
->arr
[2*i
+1].d
))
1112 if (!pname
|| p
->pos
< maxdim
) {
1113 for (i
= pname
? p
->pos
: 0; i
< maxdim
; ++i
)
1127 * 0 if toplevels of e1 and e2 are at the same level
1128 * <0 if toplevel of e1 should be outside of toplevel of e2
1129 * >0 if toplevel of e2 should be outside of toplevel of e1
1131 static int evalue_level_cmp(const evalue
*e1
, const evalue
*e2
)
1133 if (value_notzero_p(e1
->d
) && value_notzero_p(e2
->d
))
1135 if (value_notzero_p(e1
->d
))
1137 if (value_notzero_p(e2
->d
))
1139 if (e1
->x
.p
->type
== partition
&& e2
->x
.p
->type
== partition
)
1141 if (e1
->x
.p
->type
== partition
)
1143 if (e2
->x
.p
->type
== partition
)
1145 if (e1
->x
.p
->type
== relation
&& e2
->x
.p
->type
== relation
) {
1146 if (eequal(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1148 if (mod_term_smaller(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1153 if (e1
->x
.p
->type
== relation
)
1155 if (e2
->x
.p
->type
== relation
)
1157 if (e1
->x
.p
->type
== polynomial
&& e2
->x
.p
->type
== polynomial
)
1158 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
1159 if (e1
->x
.p
->type
== polynomial
)
1161 if (e2
->x
.p
->type
== polynomial
)
1163 if (e1
->x
.p
->type
== periodic
&& e2
->x
.p
->type
== periodic
)
1164 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
1165 assert(e1
->x
.p
->type
!= periodic
);
1166 assert(e2
->x
.p
->type
!= periodic
);
1167 assert(e1
->x
.p
->type
== e2
->x
.p
->type
);
1168 if (eequal(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1170 if (mod_term_smaller(e1
, e2
))
1176 static void eadd_rev(const evalue
*e1
, evalue
*res
)
1180 evalue_copy(&ev
, e1
);
1182 free_evalue_refs(res
);
1186 static void eadd_rev_cst(const evalue
*e1
, evalue
*res
)
1190 evalue_copy(&ev
, e1
);
1191 eadd(res
, &ev
.x
.p
->arr
[type_offset(ev
.x
.p
)]);
1192 free_evalue_refs(res
);
1196 struct section
{ Polyhedron
* D
; evalue E
; };
1198 void eadd_partitions(const evalue
*e1
, evalue
*res
)
1203 s
= (struct section
*)
1204 malloc((e1
->x
.p
->size
/2+1) * (res
->x
.p
->size
/2+1) *
1205 sizeof(struct section
));
1207 assert(e1
->x
.p
->pos
== res
->x
.p
->pos
);
1208 assert(e1
->x
.p
->pos
== EVALUE_DOMAIN(e1
->x
.p
->arr
[0])->Dimension
);
1209 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1212 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1213 assert(res
->x
.p
->size
>= 2);
1214 fd
= DomainDifference(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1215 EVALUE_DOMAIN(res
->x
.p
->arr
[0]), 0);
1217 for (i
= 1; i
< res
->x
.p
->size
/2; ++i
) {
1219 fd
= DomainDifference(fd
, EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1224 fd
= DomainConstraintSimplify(fd
, 0);
1229 value_init(s
[n
].E
.d
);
1230 evalue_copy(&s
[n
].E
, &e1
->x
.p
->arr
[2*j
+1]);
1234 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
) {
1235 fd
= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]);
1236 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1238 d
= DomainIntersection(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1239 EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1240 d
= DomainConstraintSimplify(d
, 0);
1246 fd
= DomainDifference(fd
, EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]), 0);
1247 if (t
!= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]))
1249 value_init(s
[n
].E
.d
);
1250 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*i
+1]);
1251 eadd(&e1
->x
.p
->arr
[2*j
+1], &s
[n
].E
);
1256 s
[n
].E
= res
->x
.p
->arr
[2*i
+1];
1260 free_evalue_refs(&res
->x
.p
->arr
[2*i
+1]);
1263 if (fd
!= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]))
1264 Domain_Free(EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]));
1265 value_clear(res
->x
.p
->arr
[2*i
].d
);
1270 res
->x
.p
= new_enode(partition
, 2*n
, e1
->x
.p
->pos
);
1271 for (j
= 0; j
< n
; ++j
) {
1272 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1273 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1274 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1280 static void explicit_complement(evalue
*res
)
1282 enode
*rel
= new_enode(relation
, 3, 0);
1284 value_clear(rel
->arr
[0].d
);
1285 rel
->arr
[0] = res
->x
.p
->arr
[0];
1286 value_clear(rel
->arr
[1].d
);
1287 rel
->arr
[1] = res
->x
.p
->arr
[1];
1288 value_set_si(rel
->arr
[2].d
, 1);
1289 value_init(rel
->arr
[2].x
.n
);
1290 value_set_si(rel
->arr
[2].x
.n
, 0);
1295 static void reduce_constant(evalue
*e
)
1300 value_gcd(g
, e
->x
.n
, e
->d
);
1301 if (value_notone_p(g
)) {
1302 value_division(e
->d
, e
->d
,g
);
1303 value_division(e
->x
.n
, e
->x
.n
,g
);
1308 /* Add two rational numbers */
1309 static void eadd_rationals(const evalue
*e1
, evalue
*res
)
1311 if (value_eq(e1
->d
, res
->d
))
1312 value_addto(res
->x
.n
, res
->x
.n
, e1
->x
.n
);
1314 value_multiply(res
->x
.n
, res
->x
.n
, e1
->d
);
1315 value_addmul(res
->x
.n
, e1
->x
.n
, res
->d
);
1316 value_multiply(res
->d
,e1
->d
,res
->d
);
1318 reduce_constant(res
);
1321 static void eadd_relations(const evalue
*e1
, evalue
*res
)
1325 if (res
->x
.p
->size
< 3 && e1
->x
.p
->size
== 3)
1326 explicit_complement(res
);
1327 for (i
= 1; i
< e1
->x
.p
->size
; ++i
)
1328 eadd(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1331 static void eadd_arrays(const evalue
*e1
, evalue
*res
, int n
)
1335 // add any element in e1 to the corresponding element in res
1336 i
= type_offset(res
->x
.p
);
1338 assert(eequal(&e1
->x
.p
->arr
[0], &res
->x
.p
->arr
[0]));
1340 eadd(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1343 static void eadd_poly(const evalue
*e1
, evalue
*res
)
1345 if (e1
->x
.p
->size
> res
->x
.p
->size
)
1348 eadd_arrays(e1
, res
, e1
->x
.p
->size
);
1352 * Product or sum of two periodics of the same parameter
1353 * and different periods
1355 static void combine_periodics(const evalue
*e1
, evalue
*res
,
1356 void (*op
)(const evalue
*, evalue
*))
1364 value_set_si(es
, e1
->x
.p
->size
);
1365 value_set_si(rs
, res
->x
.p
->size
);
1366 value_lcm(rs
, es
, rs
);
1367 size
= (int)mpz_get_si(rs
);
1370 p
= new_enode(periodic
, size
, e1
->x
.p
->pos
);
1371 for (i
= 0; i
< res
->x
.p
->size
; i
++) {
1372 value_clear(p
->arr
[i
].d
);
1373 p
->arr
[i
] = res
->x
.p
->arr
[i
];
1375 for (i
= res
->x
.p
->size
; i
< size
; i
++)
1376 evalue_copy(&p
->arr
[i
], &res
->x
.p
->arr
[i
% res
->x
.p
->size
]);
1377 for (i
= 0; i
< size
; i
++)
1378 op(&e1
->x
.p
->arr
[i
% e1
->x
.p
->size
], &p
->arr
[i
]);
1383 static void eadd_periodics(const evalue
*e1
, evalue
*res
)
1389 if (e1
->x
.p
->size
== res
->x
.p
->size
) {
1390 eadd_arrays(e1
, res
, e1
->x
.p
->size
);
1394 combine_periodics(e1
, res
, eadd
);
1397 void evalue_assign(evalue
*dst
, const evalue
*src
)
1399 if (value_pos_p(dst
->d
) && value_pos_p(src
->d
)) {
1400 value_assign(dst
->d
, src
->d
);
1401 value_assign(dst
->x
.n
, src
->x
.n
);
1404 free_evalue_refs(dst
);
1406 evalue_copy(dst
, src
);
1409 void eadd(const evalue
*e1
, evalue
*res
)
1413 if (EVALUE_IS_ZERO(*e1
))
1416 if (EVALUE_IS_NAN(*res
))
1419 if (EVALUE_IS_NAN(*e1
)) {
1420 evalue_assign(res
, e1
);
1424 if (EVALUE_IS_ZERO(*res
)) {
1425 evalue_assign(res
, e1
);
1429 cmp
= evalue_level_cmp(res
, e1
);
1431 switch (e1
->x
.p
->type
) {
1435 eadd_rev_cst(e1
, res
);
1440 } else if (cmp
== 0) {
1441 if (value_notzero_p(e1
->d
)) {
1442 eadd_rationals(e1
, res
);
1444 switch (e1
->x
.p
->type
) {
1446 eadd_partitions(e1
, res
);
1449 eadd_relations(e1
, res
);
1452 assert(e1
->x
.p
->size
== res
->x
.p
->size
);
1459 eadd_periodics(e1
, res
);
1467 switch (res
->x
.p
->type
) {
1471 /* Add to the constant term of a polynomial */
1472 eadd(e1
, &res
->x
.p
->arr
[type_offset(res
->x
.p
)]);
1475 /* Add to all elements of a periodic number */
1476 for (i
= 0; i
< res
->x
.p
->size
; i
++)
1477 eadd(e1
, &res
->x
.p
->arr
[i
]);
1480 fprintf(stderr
, "eadd: cannot add const with vector\n");
1485 /* Create (zero) complement if needed */
1486 if (res
->x
.p
->size
< 3)
1487 explicit_complement(res
);
1488 for (i
= 1; i
< res
->x
.p
->size
; ++i
)
1489 eadd(e1
, &res
->x
.p
->arr
[i
]);
1497 static void emul_rev(const evalue
*e1
, evalue
*res
)
1501 evalue_copy(&ev
, e1
);
1503 free_evalue_refs(res
);
1507 static void emul_poly(const evalue
*e1
, evalue
*res
)
1509 int i
, j
, offset
= type_offset(res
->x
.p
);
1512 int size
= (e1
->x
.p
->size
+ res
->x
.p
->size
- offset
- 1);
1514 p
= new_enode(res
->x
.p
->type
, size
, res
->x
.p
->pos
);
1516 for (i
= offset
; i
< e1
->x
.p
->size
-1; ++i
)
1517 if (!EVALUE_IS_ZERO(e1
->x
.p
->arr
[i
]))
1520 /* special case pure power */
1521 if (i
== e1
->x
.p
->size
-1) {
1523 value_clear(p
->arr
[0].d
);
1524 p
->arr
[0] = res
->x
.p
->arr
[0];
1526 for (i
= offset
; i
< e1
->x
.p
->size
-1; ++i
)
1527 evalue_set_si(&p
->arr
[i
], 0, 1);
1528 for (i
= offset
; i
< res
->x
.p
->size
; ++i
) {
1529 value_clear(p
->arr
[i
+e1
->x
.p
->size
-offset
-1].d
);
1530 p
->arr
[i
+e1
->x
.p
->size
-offset
-1] = res
->x
.p
->arr
[i
];
1531 emul(&e1
->x
.p
->arr
[e1
->x
.p
->size
-1],
1532 &p
->arr
[i
+e1
->x
.p
->size
-offset
-1]);
1540 value_set_si(tmp
.d
,0);
1543 evalue_copy(&p
->arr
[0], &e1
->x
.p
->arr
[0]);
1544 for (i
= offset
; i
< e1
->x
.p
->size
; i
++) {
1545 evalue_copy(&tmp
.x
.p
->arr
[i
], &e1
->x
.p
->arr
[i
]);
1546 emul(&res
->x
.p
->arr
[offset
], &tmp
.x
.p
->arr
[i
]);
1549 evalue_set_si(&tmp
.x
.p
->arr
[i
], 0, 1);
1550 for (i
= offset
+1; i
<res
->x
.p
->size
; i
++)
1551 for (j
= offset
; j
<e1
->x
.p
->size
; j
++) {
1554 evalue_copy(&ev
, &e1
->x
.p
->arr
[j
]);
1555 emul(&res
->x
.p
->arr
[i
], &ev
);
1556 eadd(&ev
, &tmp
.x
.p
->arr
[i
+j
-offset
]);
1557 free_evalue_refs(&ev
);
1559 free_evalue_refs(res
);
1563 void emul_partitions(const evalue
*e1
, evalue
*res
)
1568 s
= (struct section
*)
1569 malloc((e1
->x
.p
->size
/2) * (res
->x
.p
->size
/2) *
1570 sizeof(struct section
));
1572 assert(e1
->x
.p
->pos
== res
->x
.p
->pos
);
1573 assert(e1
->x
.p
->pos
== EVALUE_DOMAIN(e1
->x
.p
->arr
[0])->Dimension
);
1574 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1577 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
) {
1578 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1579 d
= DomainIntersection(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1580 EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1581 d
= DomainConstraintSimplify(d
, 0);
1587 /* This code is only needed because the partitions
1588 are not true partitions.
1590 for (k
= 0; k
< n
; ++k
) {
1591 if (DomainIncludes(s
[k
].D
, d
))
1593 if (DomainIncludes(d
, s
[k
].D
)) {
1594 Domain_Free(s
[k
].D
);
1595 free_evalue_refs(&s
[k
].E
);
1606 value_init(s
[n
].E
.d
);
1607 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*i
+1]);
1608 emul(&e1
->x
.p
->arr
[2*j
+1], &s
[n
].E
);
1612 Domain_Free(EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]));
1613 value_clear(res
->x
.p
->arr
[2*i
].d
);
1614 free_evalue_refs(&res
->x
.p
->arr
[2*i
+1]);
1619 evalue_set_si(res
, 0, 1);
1621 res
->x
.p
= new_enode(partition
, 2*n
, e1
->x
.p
->pos
);
1622 for (j
= 0; j
< n
; ++j
) {
1623 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1624 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1625 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1632 /* Product of two rational numbers */
1633 static void emul_rationals(const evalue
*e1
, evalue
*res
)
1635 value_multiply(res
->d
, e1
->d
, res
->d
);
1636 value_multiply(res
->x
.n
, e1
->x
.n
, res
->x
.n
);
1637 reduce_constant(res
);
1640 static void emul_relations(const evalue
*e1
, evalue
*res
)
1644 if (e1
->x
.p
->size
< 3 && res
->x
.p
->size
== 3) {
1645 free_evalue_refs(&res
->x
.p
->arr
[2]);
1648 for (i
= 1; i
< res
->x
.p
->size
; ++i
)
1649 emul(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1652 static void emul_periodics(const evalue
*e1
, evalue
*res
)
1659 if (e1
->x
.p
->size
== res
->x
.p
->size
) {
1660 /* Product of two periodics of the same parameter and period */
1661 for (i
= 0; i
< res
->x
.p
->size
; i
++)
1662 emul(&(e1
->x
.p
->arr
[i
]), &(res
->x
.p
->arr
[i
]));
1666 combine_periodics(e1
, res
, emul
);
1669 #define value_two_p(val) (mpz_cmp_si(val,2) == 0)
1671 static void emul_fractionals(const evalue
*e1
, evalue
*res
)
1675 poly_denom(&e1
->x
.p
->arr
[0], &d
.d
);
1676 if (!value_two_p(d
.d
))
1681 value_set_si(d
.x
.n
, 1);
1682 /* { x }^2 == { x }/2 */
1683 /* a0 b0 + (a0 b1 + a1 b0 + a1 b1/2) { x } */
1684 assert(e1
->x
.p
->size
== 3);
1685 assert(res
->x
.p
->size
== 3);
1687 evalue_copy(&tmp
, &res
->x
.p
->arr
[2]);
1689 eadd(&res
->x
.p
->arr
[1], &tmp
);
1690 emul(&e1
->x
.p
->arr
[2], &tmp
);
1691 emul(&e1
->x
.p
->arr
[1], &res
->x
.p
->arr
[1]);
1692 emul(&e1
->x
.p
->arr
[1], &res
->x
.p
->arr
[2]);
1693 eadd(&tmp
, &res
->x
.p
->arr
[2]);
1694 free_evalue_refs(&tmp
);
1700 /* Computes the product of two evalues "e1" and "res" and puts
1701 * the result in "res". You need to make a copy of "res"
1702 * before calling this function if you still need it afterward.
1703 * The vector type of evalues is not treated here
1705 void emul(const evalue
*e1
, evalue
*res
)
1709 assert(!(value_zero_p(e1
->d
) && e1
->x
.p
->type
== evector
));
1710 assert(!(value_zero_p(res
->d
) && res
->x
.p
->type
== evector
));
1712 if (EVALUE_IS_ZERO(*res
))
1715 if (EVALUE_IS_ONE(*e1
))
1718 if (EVALUE_IS_ZERO(*e1
)) {
1719 evalue_assign(res
, e1
);
1723 if (EVALUE_IS_NAN(*res
))
1726 if (EVALUE_IS_NAN(*e1
)) {
1727 evalue_assign(res
, e1
);
1731 cmp
= evalue_level_cmp(res
, e1
);
1734 } else if (cmp
== 0) {
1735 if (value_notzero_p(e1
->d
)) {
1736 emul_rationals(e1
, res
);
1738 switch (e1
->x
.p
->type
) {
1740 emul_partitions(e1
, res
);
1743 emul_relations(e1
, res
);
1750 emul_periodics(e1
, res
);
1753 emul_fractionals(e1
, res
);
1759 switch (res
->x
.p
->type
) {
1761 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
)
1762 emul(e1
, &res
->x
.p
->arr
[2*i
+1]);
1769 for (i
= type_offset(res
->x
.p
); i
< res
->x
.p
->size
; ++i
)
1770 emul(e1
, &res
->x
.p
->arr
[i
]);
1776 /* Frees mask content ! */
1777 void emask(evalue
*mask
, evalue
*res
) {
1784 if (EVALUE_IS_ZERO(*res
)) {
1785 free_evalue_refs(mask
);
1789 assert(value_zero_p(mask
->d
));
1790 assert(mask
->x
.p
->type
== partition
);
1791 assert(value_zero_p(res
->d
));
1792 assert(res
->x
.p
->type
== partition
);
1793 assert(mask
->x
.p
->pos
== res
->x
.p
->pos
);
1794 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1795 assert(mask
->x
.p
->pos
== EVALUE_DOMAIN(mask
->x
.p
->arr
[0])->Dimension
);
1796 pos
= res
->x
.p
->pos
;
1798 s
= (struct section
*)
1799 malloc((mask
->x
.p
->size
/2+1) * (res
->x
.p
->size
/2) *
1800 sizeof(struct section
));
1804 evalue_set_si(&mone
, -1, 1);
1807 for (j
= 0; j
< res
->x
.p
->size
/2; ++j
) {
1808 assert(mask
->x
.p
->size
>= 2);
1809 fd
= DomainDifference(EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]),
1810 EVALUE_DOMAIN(mask
->x
.p
->arr
[0]), 0);
1812 for (i
= 1; i
< mask
->x
.p
->size
/2; ++i
) {
1814 fd
= DomainDifference(fd
, EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]), 0);
1823 value_init(s
[n
].E
.d
);
1824 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*j
+1]);
1828 for (i
= 0; i
< mask
->x
.p
->size
/2; ++i
) {
1829 if (EVALUE_IS_ONE(mask
->x
.p
->arr
[2*i
+1]))
1832 fd
= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]);
1833 eadd(&mone
, &mask
->x
.p
->arr
[2*i
+1]);
1834 emul(&mone
, &mask
->x
.p
->arr
[2*i
+1]);
1835 for (j
= 0; j
< res
->x
.p
->size
/2; ++j
) {
1837 d
= DomainIntersection(EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]),
1838 EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]), 0);
1844 fd
= DomainDifference(fd
, EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]), 0);
1845 if (t
!= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]))
1847 value_init(s
[n
].E
.d
);
1848 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*j
+1]);
1849 emul(&mask
->x
.p
->arr
[2*i
+1], &s
[n
].E
);
1855 /* Just ignore; this may have been previously masked off */
1857 if (fd
!= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]))
1861 free_evalue_refs(&mone
);
1862 free_evalue_refs(mask
);
1863 free_evalue_refs(res
);
1866 evalue_set_si(res
, 0, 1);
1868 res
->x
.p
= new_enode(partition
, 2*n
, pos
);
1869 for (j
= 0; j
< n
; ++j
) {
1870 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1871 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1872 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1879 void evalue_copy(evalue
*dst
, const evalue
*src
)
1881 value_assign(dst
->d
, src
->d
);
1882 if (EVALUE_IS_NAN(*dst
)) {
1886 if (value_pos_p(src
->d
)) {
1887 value_init(dst
->x
.n
);
1888 value_assign(dst
->x
.n
, src
->x
.n
);
1890 dst
->x
.p
= ecopy(src
->x
.p
);
1893 evalue
*evalue_dup(const evalue
*e
)
1895 evalue
*res
= ALLOC(evalue
);
1897 evalue_copy(res
, e
);
1901 enode
*new_enode(enode_type type
,int size
,int pos
) {
1907 fprintf(stderr
, "Allocating enode of size 0 !\n" );
1910 res
= (enode
*) malloc(sizeof(enode
) + (size
-1)*sizeof(evalue
));
1914 for(i
=0; i
<size
; i
++) {
1915 value_init(res
->arr
[i
].d
);
1916 value_set_si(res
->arr
[i
].d
,0);
1917 res
->arr
[i
].x
.p
= 0;
1922 enode
*ecopy(enode
*e
) {
1927 res
= new_enode(e
->type
,e
->size
,e
->pos
);
1928 for(i
=0;i
<e
->size
;++i
) {
1929 value_assign(res
->arr
[i
].d
,e
->arr
[i
].d
);
1930 if(value_zero_p(res
->arr
[i
].d
))
1931 res
->arr
[i
].x
.p
= ecopy(e
->arr
[i
].x
.p
);
1932 else if (EVALUE_IS_DOMAIN(res
->arr
[i
]))
1933 EVALUE_SET_DOMAIN(res
->arr
[i
], Domain_Copy(EVALUE_DOMAIN(e
->arr
[i
])));
1935 value_init(res
->arr
[i
].x
.n
);
1936 value_assign(res
->arr
[i
].x
.n
,e
->arr
[i
].x
.n
);
1942 int ecmp(const evalue
*e1
, const evalue
*e2
)
1948 if (value_notzero_p(e1
->d
) && value_notzero_p(e2
->d
)) {
1952 value_multiply(m
, e1
->x
.n
, e2
->d
);
1953 value_multiply(m2
, e2
->x
.n
, e1
->d
);
1955 if (value_lt(m
, m2
))
1957 else if (value_gt(m
, m2
))
1967 if (value_notzero_p(e1
->d
))
1969 if (value_notzero_p(e2
->d
))
1975 if (p1
->type
!= p2
->type
)
1976 return p1
->type
- p2
->type
;
1977 if (p1
->pos
!= p2
->pos
)
1978 return p1
->pos
- p2
->pos
;
1979 if (p1
->size
!= p2
->size
)
1980 return p1
->size
- p2
->size
;
1982 for (i
= p1
->size
-1; i
>= 0; --i
)
1983 if ((r
= ecmp(&p1
->arr
[i
], &p2
->arr
[i
])) != 0)
1989 int eequal(const evalue
*e1
, const evalue
*e2
)
1994 if (value_ne(e1
->d
,e2
->d
))
1997 if (EVALUE_IS_DOMAIN(*e1
))
1998 return PolyhedronIncludes(EVALUE_DOMAIN(*e2
), EVALUE_DOMAIN(*e1
)) &&
1999 PolyhedronIncludes(EVALUE_DOMAIN(*e1
), EVALUE_DOMAIN(*e2
));
2001 if (EVALUE_IS_NAN(*e1
))
2004 assert(value_posz_p(e1
->d
));
2006 /* e1->d == e2->d */
2007 if (value_notzero_p(e1
->d
)) {
2008 if (value_ne(e1
->x
.n
,e2
->x
.n
))
2011 /* e1->d == e2->d != 0 AND e1->n == e2->n */
2015 /* e1->d == e2->d == 0 */
2018 if (p1
->type
!= p2
->type
) return 0;
2019 if (p1
->size
!= p2
->size
) return 0;
2020 if (p1
->pos
!= p2
->pos
) return 0;
2021 for (i
=0; i
<p1
->size
; i
++)
2022 if (!eequal(&p1
->arr
[i
], &p2
->arr
[i
]) )
2027 void free_evalue_refs(evalue
*e
) {
2032 if (EVALUE_IS_NAN(*e
)) {
2037 if (EVALUE_IS_DOMAIN(*e
)) {
2038 Domain_Free(EVALUE_DOMAIN(*e
));
2041 } else if (value_pos_p(e
->d
)) {
2043 /* 'e' stores a constant */
2045 value_clear(e
->x
.n
);
2048 assert(value_zero_p(e
->d
));
2051 if (!p
) return; /* null pointer */
2052 for (i
=0; i
<p
->size
; i
++) {
2053 free_evalue_refs(&(p
->arr
[i
]));
2057 } /* free_evalue_refs */
2059 void evalue_free(evalue
*e
)
2061 free_evalue_refs(e
);
2065 static void mod2table_r(evalue
*e
, Vector
*periods
, Value m
, int p
,
2066 Vector
* val
, evalue
*res
)
2068 unsigned nparam
= periods
->Size
;
2071 double d
= compute_evalue(e
, val
->p
);
2072 d
*= VALUE_TO_DOUBLE(m
);
2077 value_assign(res
->d
, m
);
2078 value_init(res
->x
.n
);
2079 value_set_double(res
->x
.n
, d
);
2080 mpz_fdiv_r(res
->x
.n
, res
->x
.n
, m
);
2083 if (value_one_p(periods
->p
[p
]))
2084 mod2table_r(e
, periods
, m
, p
+1, val
, res
);
2089 value_assign(tmp
, periods
->p
[p
]);
2090 value_set_si(res
->d
, 0);
2091 res
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
2093 value_decrement(tmp
, tmp
);
2094 value_assign(val
->p
[p
], tmp
);
2095 mod2table_r(e
, periods
, m
, p
+1, val
,
2096 &res
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
2097 } while (value_pos_p(tmp
));
2103 static void rel2table(evalue
*e
, int zero
)
2105 if (value_pos_p(e
->d
)) {
2106 if (value_zero_p(e
->x
.n
) == zero
)
2107 value_set_si(e
->x
.n
, 1);
2109 value_set_si(e
->x
.n
, 0);
2110 value_set_si(e
->d
, 1);
2113 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
2114 rel2table(&e
->x
.p
->arr
[i
], zero
);
2118 void evalue_mod2table(evalue
*e
, int nparam
)
2123 if (EVALUE_IS_DOMAIN(*e
) || value_pos_p(e
->d
))
2126 for (i
=0; i
<p
->size
; i
++) {
2127 evalue_mod2table(&(p
->arr
[i
]), nparam
);
2129 if (p
->type
== relation
) {
2134 evalue_copy(©
, &p
->arr
[0]);
2136 rel2table(&p
->arr
[0], 1);
2137 emul(&p
->arr
[0], &p
->arr
[1]);
2139 rel2table(©
, 0);
2140 emul(©
, &p
->arr
[2]);
2141 eadd(&p
->arr
[2], &p
->arr
[1]);
2142 free_evalue_refs(&p
->arr
[2]);
2143 free_evalue_refs(©
);
2145 free_evalue_refs(&p
->arr
[0]);
2149 } else if (p
->type
== fractional
) {
2150 Vector
*periods
= Vector_Alloc(nparam
);
2151 Vector
*val
= Vector_Alloc(nparam
);
2157 value_set_si(tmp
, 1);
2158 Vector_Set(periods
->p
, 1, nparam
);
2159 Vector_Set(val
->p
, 0, nparam
);
2160 for (ev
= &p
->arr
[0]; value_zero_p(ev
->d
); ev
= &ev
->x
.p
->arr
[0]) {
2163 assert(p
->type
== polynomial
);
2164 assert(p
->size
== 2);
2165 value_assign(periods
->p
[p
->pos
-1], p
->arr
[1].d
);
2166 value_lcm(tmp
, tmp
, p
->arr
[1].d
);
2168 value_lcm(tmp
, tmp
, ev
->d
);
2170 mod2table_r(&p
->arr
[0], periods
, tmp
, 0, val
, &EP
);
2173 evalue_set_si(&res
, 0, 1);
2174 /* Compute the polynomial using Horner's rule */
2175 for (i
=p
->size
-1;i
>1;i
--) {
2176 eadd(&p
->arr
[i
], &res
);
2179 eadd(&p
->arr
[1], &res
);
2181 free_evalue_refs(e
);
2182 free_evalue_refs(&EP
);
2187 Vector_Free(periods
);
2189 } /* evalue_mod2table */
2191 /********************************************************/
2192 /* function in domain */
2193 /* check if the parameters in list_args */
2194 /* verifies the constraints of Domain P */
2195 /********************************************************/
2196 int in_domain(Polyhedron
*P
, Value
*list_args
)
2199 Value v
; /* value of the constraint of a row when
2200 parameters are instantiated*/
2204 for (row
= 0; row
< P
->NbConstraints
; row
++) {
2205 Inner_Product(P
->Constraint
[row
]+1, list_args
, P
->Dimension
, &v
);
2206 value_addto(v
, v
, P
->Constraint
[row
][P
->Dimension
+1]); /*constant part*/
2207 if (value_neg_p(v
) ||
2208 value_zero_p(P
->Constraint
[row
][0]) && value_notzero_p(v
)) {
2215 return in
|| (P
->next
&& in_domain(P
->next
, list_args
));
2218 /****************************************************/
2219 /* function compute enode */
2220 /* compute the value of enode p with parameters */
2221 /* list "list_args */
2222 /* compute the polynomial or the periodic */
2223 /****************************************************/
2225 static double compute_enode(enode
*p
, Value
*list_args
) {
2237 if (p
->type
== polynomial
) {
2239 value_assign(param
,list_args
[p
->pos
-1]);
2241 /* Compute the polynomial using Horner's rule */
2242 for (i
=p
->size
-1;i
>0;i
--) {
2243 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2244 res
*=VALUE_TO_DOUBLE(param
);
2246 res
+=compute_evalue(&p
->arr
[0],list_args
);
2248 else if (p
->type
== fractional
) {
2249 double d
= compute_evalue(&p
->arr
[0], list_args
);
2250 d
-= floor(d
+1e-10);
2252 /* Compute the polynomial using Horner's rule */
2253 for (i
=p
->size
-1;i
>1;i
--) {
2254 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2257 res
+=compute_evalue(&p
->arr
[1],list_args
);
2259 else if (p
->type
== flooring
) {
2260 double d
= compute_evalue(&p
->arr
[0], list_args
);
2263 /* Compute the polynomial using Horner's rule */
2264 for (i
=p
->size
-1;i
>1;i
--) {
2265 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2268 res
+=compute_evalue(&p
->arr
[1],list_args
);
2270 else if (p
->type
== periodic
) {
2271 value_assign(m
,list_args
[p
->pos
-1]);
2273 /* Choose the right element of the periodic */
2274 value_set_si(param
,p
->size
);
2275 value_pmodulus(m
,m
,param
);
2276 res
= compute_evalue(&p
->arr
[VALUE_TO_INT(m
)],list_args
);
2278 else if (p
->type
== relation
) {
2279 if (fabs(compute_evalue(&p
->arr
[0], list_args
)) < 1e-10)
2280 res
= compute_evalue(&p
->arr
[1], list_args
);
2281 else if (p
->size
> 2)
2282 res
= compute_evalue(&p
->arr
[2], list_args
);
2284 else if (p
->type
== partition
) {
2285 int dim
= EVALUE_DOMAIN(p
->arr
[0])->Dimension
;
2286 Value
*vals
= list_args
;
2289 for (i
= 0; i
< dim
; ++i
) {
2290 value_init(vals
[i
]);
2292 value_assign(vals
[i
], list_args
[i
]);
2295 for (i
= 0; i
< p
->size
/2; ++i
)
2296 if (DomainContains(EVALUE_DOMAIN(p
->arr
[2*i
]), vals
, p
->pos
, 0, 1)) {
2297 res
= compute_evalue(&p
->arr
[2*i
+1], vals
);
2301 for (i
= 0; i
< dim
; ++i
)
2302 value_clear(vals
[i
]);
2311 } /* compute_enode */
2313 /*************************************************/
2314 /* return the value of Ehrhart Polynomial */
2315 /* It returns a double, because since it is */
2316 /* a recursive function, some intermediate value */
2317 /* might not be integral */
2318 /*************************************************/
2320 double compute_evalue(const evalue
*e
, Value
*list_args
)
2324 if (value_notzero_p(e
->d
)) {
2325 if (value_notone_p(e
->d
))
2326 res
= VALUE_TO_DOUBLE(e
->x
.n
) / VALUE_TO_DOUBLE(e
->d
);
2328 res
= VALUE_TO_DOUBLE(e
->x
.n
);
2331 res
= compute_enode(e
->x
.p
,list_args
);
2333 } /* compute_evalue */
2336 /****************************************************/
2337 /* function compute_poly : */
2338 /* Check for the good validity domain */
2339 /* return the number of point in the Polyhedron */
2340 /* in allocated memory */
2341 /* Using the Ehrhart pseudo-polynomial */
2342 /****************************************************/
2343 Value
*compute_poly(Enumeration
*en
,Value
*list_args
) {
2346 /* double d; int i; */
2348 tmp
= (Value
*) malloc (sizeof(Value
));
2349 assert(tmp
!= NULL
);
2351 value_set_si(*tmp
,0);
2354 return(tmp
); /* no ehrhart polynomial */
2355 if(en
->ValidityDomain
) {
2356 if(!en
->ValidityDomain
->Dimension
) { /* no parameters */
2357 value_set_double(*tmp
,compute_evalue(&en
->EP
,list_args
)+.25);
2362 return(tmp
); /* no Validity Domain */
2364 if(in_domain(en
->ValidityDomain
,list_args
)) {
2366 #ifdef EVAL_EHRHART_DEBUG
2367 Print_Domain(stdout
,en
->ValidityDomain
);
2368 print_evalue(stdout
,&en
->EP
);
2371 /* d = compute_evalue(&en->EP,list_args);
2373 printf("(double)%lf = %d\n", d, i ); */
2374 value_set_double(*tmp
,compute_evalue(&en
->EP
,list_args
)+.25);
2380 value_set_si(*tmp
,0);
2381 return(tmp
); /* no compatible domain with the arguments */
2382 } /* compute_poly */
2384 static evalue
*eval_polynomial(const enode
*p
, int offset
,
2385 evalue
*base
, Value
*values
)
2390 res
= evalue_zero();
2391 for (i
= p
->size
-1; i
> offset
; --i
) {
2392 c
= evalue_eval(&p
->arr
[i
], values
);
2397 c
= evalue_eval(&p
->arr
[offset
], values
);
2404 evalue
*evalue_eval(const evalue
*e
, Value
*values
)
2411 if (value_notzero_p(e
->d
)) {
2412 res
= ALLOC(evalue
);
2414 evalue_copy(res
, e
);
2417 switch (e
->x
.p
->type
) {
2419 value_init(param
.x
.n
);
2420 value_assign(param
.x
.n
, values
[e
->x
.p
->pos
-1]);
2421 value_init(param
.d
);
2422 value_set_si(param
.d
, 1);
2424 res
= eval_polynomial(e
->x
.p
, 0, ¶m
, values
);
2425 free_evalue_refs(¶m
);
2428 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2429 mpz_fdiv_r(param2
->x
.n
, param2
->x
.n
, param2
->d
);
2431 res
= eval_polynomial(e
->x
.p
, 1, param2
, values
);
2432 evalue_free(param2
);
2435 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2436 mpz_fdiv_q(param2
->x
.n
, param2
->x
.n
, param2
->d
);
2437 value_set_si(param2
->d
, 1);
2439 res
= eval_polynomial(e
->x
.p
, 1, param2
, values
);
2440 evalue_free(param2
);
2443 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2444 if (value_zero_p(param2
->x
.n
))
2445 res
= evalue_eval(&e
->x
.p
->arr
[1], values
);
2446 else if (e
->x
.p
->size
> 2)
2447 res
= evalue_eval(&e
->x
.p
->arr
[2], values
);
2449 res
= evalue_zero();
2450 evalue_free(param2
);
2453 assert(e
->x
.p
->pos
== EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
);
2454 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
2455 if (in_domain(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), values
)) {
2456 res
= evalue_eval(&e
->x
.p
->arr
[2*i
+1], values
);
2460 res
= evalue_zero();
2468 size_t value_size(Value v
) {
2469 return (v
[0]._mp_size
> 0 ? v
[0]._mp_size
: -v
[0]._mp_size
)
2470 * sizeof(v
[0]._mp_d
[0]);
2473 size_t domain_size(Polyhedron
*D
)
2476 size_t s
= sizeof(*D
);
2478 for (i
= 0; i
< D
->NbConstraints
; ++i
)
2479 for (j
= 0; j
< D
->Dimension
+2; ++j
)
2480 s
+= value_size(D
->Constraint
[i
][j
]);
2483 for (i = 0; i < D->NbRays; ++i)
2484 for (j = 0; j < D->Dimension+2; ++j)
2485 s += value_size(D->Ray[i][j]);
2488 return D
->next
? s
+domain_size(D
->next
) : s
;
2491 size_t enode_size(enode
*p
) {
2492 size_t s
= sizeof(*p
) - sizeof(p
->arr
[0]);
2495 if (p
->type
== partition
)
2496 for (i
= 0; i
< p
->size
/2; ++i
) {
2497 s
+= domain_size(EVALUE_DOMAIN(p
->arr
[2*i
]));
2498 s
+= evalue_size(&p
->arr
[2*i
+1]);
2501 for (i
= 0; i
< p
->size
; ++i
) {
2502 s
+= evalue_size(&p
->arr
[i
]);
2507 size_t evalue_size(evalue
*e
)
2509 size_t s
= sizeof(*e
);
2510 s
+= value_size(e
->d
);
2511 if (value_notzero_p(e
->d
))
2512 s
+= value_size(e
->x
.n
);
2514 s
+= enode_size(e
->x
.p
);
2518 static evalue
*find_second(evalue
*base
, evalue
*cst
, evalue
*e
, Value m
)
2520 evalue
*found
= NULL
;
2525 if (value_pos_p(e
->d
) || e
->x
.p
->type
!= fractional
)
2528 value_init(offset
.d
);
2529 value_init(offset
.x
.n
);
2530 poly_denom(&e
->x
.p
->arr
[0], &offset
.d
);
2531 value_lcm(offset
.d
, m
, offset
.d
);
2532 value_set_si(offset
.x
.n
, 1);
2535 evalue_copy(©
, cst
);
2538 mpz_fdiv_r(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2540 if (eequal(base
, &e
->x
.p
->arr
[0]))
2541 found
= &e
->x
.p
->arr
[0];
2543 value_set_si(offset
.x
.n
, -2);
2546 mpz_fdiv_r(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2548 if (eequal(base
, &e
->x
.p
->arr
[0]))
2551 free_evalue_refs(cst
);
2552 free_evalue_refs(&offset
);
2555 for (i
= 1; !found
&& i
< e
->x
.p
->size
; ++i
)
2556 found
= find_second(base
, cst
, &e
->x
.p
->arr
[i
], m
);
2561 static evalue
*find_relation_pair(evalue
*e
)
2564 evalue
*found
= NULL
;
2566 if (EVALUE_IS_DOMAIN(*e
) || value_pos_p(e
->d
))
2569 if (e
->x
.p
->type
== fractional
) {
2574 poly_denom(&e
->x
.p
->arr
[0], &m
);
2576 for (cst
= &e
->x
.p
->arr
[0]; value_zero_p(cst
->d
);
2577 cst
= &cst
->x
.p
->arr
[0])
2580 for (i
= 1; !found
&& i
< e
->x
.p
->size
; ++i
)
2581 found
= find_second(&e
->x
.p
->arr
[0], cst
, &e
->x
.p
->arr
[i
], m
);
2586 i
= e
->x
.p
->type
== relation
;
2587 for (; !found
&& i
< e
->x
.p
->size
; ++i
)
2588 found
= find_relation_pair(&e
->x
.p
->arr
[i
]);
2593 void evalue_mod2relation(evalue
*e
) {
2596 if (value_zero_p(e
->d
) && e
->x
.p
->type
== partition
) {
2599 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2600 evalue_mod2relation(&e
->x
.p
->arr
[2*i
+1]);
2601 if (EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
2602 value_clear(e
->x
.p
->arr
[2*i
].d
);
2603 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
2605 if (2*i
< e
->x
.p
->size
) {
2606 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
2607 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
2612 if (e
->x
.p
->size
== 0) {
2614 evalue_set_si(e
, 0, 1);
2620 while ((d
= find_relation_pair(e
)) != NULL
) {
2624 value_init(split
.d
);
2625 value_set_si(split
.d
, 0);
2626 split
.x
.p
= new_enode(relation
, 3, 0);
2627 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2628 evalue_set_si(&split
.x
.p
->arr
[2], 1, 1);
2630 ev
= &split
.x
.p
->arr
[0];
2631 value_set_si(ev
->d
, 0);
2632 ev
->x
.p
= new_enode(fractional
, 3, -1);
2633 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
2634 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
2635 evalue_copy(&ev
->x
.p
->arr
[0], d
);
2641 free_evalue_refs(&split
);
2645 static int evalue_comp(const void * a
, const void * b
)
2647 const evalue
*e1
= *(const evalue
**)a
;
2648 const evalue
*e2
= *(const evalue
**)b
;
2649 return ecmp(e1
, e2
);
2652 void evalue_combine(evalue
*e
)
2659 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
2662 NALLOC(evs
, e
->x
.p
->size
/2);
2663 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
2664 evs
[i
] = &e
->x
.p
->arr
[2*i
+1];
2665 qsort(evs
, e
->x
.p
->size
/2, sizeof(evs
[0]), evalue_comp
);
2666 p
= new_enode(partition
, e
->x
.p
->size
, e
->x
.p
->pos
);
2667 for (i
= 0, k
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2668 if (k
== 0 || ecmp(&p
->arr
[2*k
-1], evs
[i
]) != 0) {
2669 value_clear(p
->arr
[2*k
].d
);
2670 value_clear(p
->arr
[2*k
+1].d
);
2671 p
->arr
[2*k
] = *(evs
[i
]-1);
2672 p
->arr
[2*k
+1] = *(evs
[i
]);
2675 Polyhedron
*D
= EVALUE_DOMAIN(*(evs
[i
]-1));
2678 value_clear((evs
[i
]-1)->d
);
2682 L
->next
= EVALUE_DOMAIN(p
->arr
[2*k
-2]);
2683 EVALUE_SET_DOMAIN(p
->arr
[2*k
-2], D
);
2684 free_evalue_refs(evs
[i
]);
2688 for (i
= 2*k
; i
< p
->size
; ++i
)
2689 value_clear(p
->arr
[i
].d
);
2696 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2698 if (value_notzero_p(e
->x
.p
->arr
[2*i
+1].d
))
2700 H
= DomainConvex(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), 0);
2703 for (k
= 0; k
< e
->x
.p
->size
/2; ++k
) {
2704 Polyhedron
*D
, *N
, **P
;
2707 P
= &EVALUE_DOMAIN(e
->x
.p
->arr
[2*k
]);
2714 if (D
->NbEq
<= H
->NbEq
) {
2720 tmp
.x
.p
= new_enode(partition
, 2, e
->x
.p
->pos
);
2721 EVALUE_SET_DOMAIN(tmp
.x
.p
->arr
[0], Polyhedron_Copy(D
));
2722 evalue_copy(&tmp
.x
.p
->arr
[1], &e
->x
.p
->arr
[2*i
+1]);
2723 reduce_evalue(&tmp
);
2724 if (value_notzero_p(tmp
.d
) ||
2725 ecmp(&tmp
.x
.p
->arr
[1], &e
->x
.p
->arr
[2*k
+1]) != 0)
2728 D
->next
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
2729 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]) = D
;
2732 free_evalue_refs(&tmp
);
2738 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2740 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
2742 value_clear(e
->x
.p
->arr
[2*i
].d
);
2743 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
2745 if (2*i
< e
->x
.p
->size
) {
2746 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
2747 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
2754 H
= DomainConvex(D
, 0);
2755 E
= DomainDifference(H
, D
, 0);
2757 D
= DomainDifference(H
, E
, 0);
2760 EVALUE_SET_DOMAIN(p
->arr
[2*i
], D
);
2764 /* Use smallest representative for coefficients in affine form in
2765 * argument of fractional.
2766 * Since any change will make the argument non-standard,
2767 * the containing evalue will have to be reduced again afterward.
2769 static void fractional_minimal_coefficients(enode
*p
)
2775 assert(p
->type
== fractional
);
2777 while (value_zero_p(pp
->d
)) {
2778 assert(pp
->x
.p
->type
== polynomial
);
2779 assert(pp
->x
.p
->size
== 2);
2780 assert(value_notzero_p(pp
->x
.p
->arr
[1].d
));
2781 mpz_mul_ui(twice
, pp
->x
.p
->arr
[1].x
.n
, 2);
2782 if (value_gt(twice
, pp
->x
.p
->arr
[1].d
))
2783 value_subtract(pp
->x
.p
->arr
[1].x
.n
,
2784 pp
->x
.p
->arr
[1].x
.n
, pp
->x
.p
->arr
[1].d
);
2785 pp
= &pp
->x
.p
->arr
[0];
2791 static Polyhedron
*polynomial_projection(enode
*p
, Polyhedron
*D
, Value
*d
,
2796 unsigned dim
= D
->Dimension
;
2797 Matrix
*T
= Matrix_Alloc(2, dim
+1);
2800 assert(p
->type
== fractional
|| p
->type
== flooring
);
2801 value_set_si(T
->p
[1][dim
], 1);
2802 evalue_extract_affine(&p
->arr
[0], T
->p
[0], &T
->p
[0][dim
], d
);
2803 I
= DomainImage(D
, T
, 0);
2804 H
= DomainConvex(I
, 0);
2814 static void replace_by_affine(evalue
*e
, Value offset
)
2821 value_init(inc
.x
.n
);
2822 value_set_si(inc
.d
, 1);
2823 value_oppose(inc
.x
.n
, offset
);
2824 eadd(&inc
, &p
->arr
[0]);
2825 reorder_terms_about(p
, &p
->arr
[0]); /* frees arr[0] */
2829 free_evalue_refs(&inc
);
2832 int evalue_range_reduction_in_domain(evalue
*e
, Polyhedron
*D
)
2841 if (value_notzero_p(e
->d
))
2846 if (p
->type
== relation
) {
2853 fractional_minimal_coefficients(p
->arr
[0].x
.p
);
2854 I
= polynomial_projection(p
->arr
[0].x
.p
, D
, &d
, &T
);
2855 bounded
= line_minmax(I
, &min
, &max
); /* frees I */
2856 equal
= value_eq(min
, max
);
2857 mpz_cdiv_q(min
, min
, d
);
2858 mpz_fdiv_q(max
, max
, d
);
2860 if (bounded
&& value_gt(min
, max
)) {
2866 evalue_set_si(e
, 0, 1);
2869 free_evalue_refs(&(p
->arr
[1]));
2870 free_evalue_refs(&(p
->arr
[0]));
2876 return r
? r
: evalue_range_reduction_in_domain(e
, D
);
2877 } else if (bounded
&& equal
) {
2880 free_evalue_refs(&(p
->arr
[2]));
2883 free_evalue_refs(&(p
->arr
[0]));
2889 return evalue_range_reduction_in_domain(e
, D
);
2890 } else if (bounded
&& value_eq(min
, max
)) {
2891 /* zero for a single value */
2893 Matrix
*M
= Matrix_Alloc(1, D
->Dimension
+2);
2894 Vector_Copy(T
->p
[0], M
->p
[0]+1, D
->Dimension
+1);
2895 value_multiply(min
, min
, d
);
2896 value_subtract(M
->p
[0][D
->Dimension
+1],
2897 M
->p
[0][D
->Dimension
+1], min
);
2898 E
= DomainAddConstraints(D
, M
, 0);
2904 r
= evalue_range_reduction_in_domain(&p
->arr
[1], E
);
2906 r
|= evalue_range_reduction_in_domain(&p
->arr
[2], D
);
2908 _reduce_evalue(&p
->arr
[0].x
.p
->arr
[0], 0, 1);
2916 _reduce_evalue(&p
->arr
[0].x
.p
->arr
[0], 0, 1);
2919 i
= p
->type
== relation
? 1 :
2920 p
->type
== fractional
? 1 : 0;
2921 for (; i
<p
->size
; i
++)
2922 r
|= evalue_range_reduction_in_domain(&p
->arr
[i
], D
);
2924 if (p
->type
!= fractional
) {
2925 if (r
&& p
->type
== polynomial
) {
2928 value_set_si(f
.d
, 0);
2929 f
.x
.p
= new_enode(polynomial
, 2, p
->pos
);
2930 evalue_set_si(&f
.x
.p
->arr
[0], 0, 1);
2931 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
2932 reorder_terms_about(p
, &f
);
2943 fractional_minimal_coefficients(p
);
2944 I
= polynomial_projection(p
, D
, &d
, NULL
);
2945 bounded
= line_minmax(I
, &min
, &max
); /* frees I */
2946 mpz_fdiv_q(min
, min
, d
);
2947 mpz_fdiv_q(max
, max
, d
);
2948 value_subtract(d
, max
, min
);
2950 if (bounded
&& value_eq(min
, max
)) {
2951 replace_by_affine(e
, min
);
2953 } else if (bounded
&& value_one_p(d
) && p
->size
> 3) {
2954 /* replace {g}^2 by -(g-min)^2 + (2{g}+1)*(g-min) - {g}
2955 * See pages 199-200 of PhD thesis.
2963 value_set_si(rem
.d
, 0);
2964 rem
.x
.p
= new_enode(fractional
, 3, -1);
2965 evalue_copy(&rem
.x
.p
->arr
[0], &p
->arr
[0]);
2966 value_clear(rem
.x
.p
->arr
[1].d
);
2967 value_clear(rem
.x
.p
->arr
[2].d
);
2968 rem
.x
.p
->arr
[1] = p
->arr
[1];
2969 rem
.x
.p
->arr
[2] = p
->arr
[2];
2970 for (i
= 3; i
< p
->size
; ++i
)
2971 p
->arr
[i
-2] = p
->arr
[i
];
2975 value_init(inc
.x
.n
);
2976 value_set_si(inc
.d
, 1);
2977 value_oppose(inc
.x
.n
, min
);
2980 evalue_copy(&t
, &p
->arr
[0]);
2984 value_set_si(f
.d
, 0);
2985 f
.x
.p
= new_enode(fractional
, 3, -1);
2986 evalue_copy(&f
.x
.p
->arr
[0], &p
->arr
[0]);
2987 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
2988 evalue_set_si(&f
.x
.p
->arr
[2], 2, 1);
2990 value_init(factor
.d
);
2991 evalue_set_si(&factor
, -1, 1);
2997 value_clear(f
.x
.p
->arr
[1].x
.n
);
2998 value_clear(f
.x
.p
->arr
[2].x
.n
);
2999 evalue_set_si(&f
.x
.p
->arr
[1], 0, 1);
3000 evalue_set_si(&f
.x
.p
->arr
[2], -1, 1);
3004 reorder_terms(&rem
);
3011 free_evalue_refs(&inc
);
3012 free_evalue_refs(&t
);
3013 free_evalue_refs(&f
);
3014 free_evalue_refs(&factor
);
3015 free_evalue_refs(&rem
);
3017 evalue_range_reduction_in_domain(e
, D
);
3021 _reduce_evalue(&p
->arr
[0], 0, 1);
3033 void evalue_range_reduction(evalue
*e
)
3036 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
3039 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3040 if (evalue_range_reduction_in_domain(&e
->x
.p
->arr
[2*i
+1],
3041 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]))) {
3042 reduce_evalue(&e
->x
.p
->arr
[2*i
+1]);
3044 if (EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
3045 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
3046 Domain_Free(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]));
3047 value_clear(e
->x
.p
->arr
[2*i
].d
);
3049 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
3050 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
3058 Enumeration
* partition2enumeration(evalue
*EP
)
3061 Enumeration
*en
, *res
= NULL
;
3063 if (EVALUE_IS_ZERO(*EP
)) {
3068 for (i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
3069 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
])->Dimension
);
3070 en
= (Enumeration
*)malloc(sizeof(Enumeration
));
3073 res
->ValidityDomain
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
3074 value_clear(EP
->x
.p
->arr
[2*i
].d
);
3075 res
->EP
= EP
->x
.p
->arr
[2*i
+1];
3083 int evalue_frac2floor_in_domain3(evalue
*e
, Polyhedron
*D
, int shift
)
3092 if (value_notzero_p(e
->d
))
3097 i
= p
->type
== relation
? 1 :
3098 p
->type
== fractional
? 1 : 0;
3099 for (; i
<p
->size
; i
++)
3100 r
|= evalue_frac2floor_in_domain3(&p
->arr
[i
], D
, shift
);
3102 if (p
->type
!= fractional
) {
3103 if (r
&& p
->type
== polynomial
) {
3106 value_set_si(f
.d
, 0);
3107 f
.x
.p
= new_enode(polynomial
, 2, p
->pos
);
3108 evalue_set_si(&f
.x
.p
->arr
[0], 0, 1);
3109 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
3110 reorder_terms_about(p
, &f
);
3120 I
= polynomial_projection(p
, D
, &d
, NULL
);
3123 Polyhedron_Print(stderr, P_VALUE_FMT, I);
3126 assert(I
->NbEq
== 0); /* Should have been reduced */
3129 for (i
= 0; i
< I
->NbConstraints
; ++i
)
3130 if (value_pos_p(I
->Constraint
[i
][1]))
3133 if (i
< I
->NbConstraints
) {
3135 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
3136 mpz_cdiv_q(min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
3137 if (value_neg_p(min
)) {
3139 mpz_fdiv_q(min
, min
, d
);
3140 value_init(offset
.d
);
3141 value_set_si(offset
.d
, 1);
3142 value_init(offset
.x
.n
);
3143 value_oppose(offset
.x
.n
, min
);
3144 eadd(&offset
, &p
->arr
[0]);
3145 free_evalue_refs(&offset
);
3155 value_set_si(fl
.d
, 0);
3156 fl
.x
.p
= new_enode(flooring
, 3, -1);
3157 evalue_set_si(&fl
.x
.p
->arr
[1], 0, 1);
3158 evalue_set_si(&fl
.x
.p
->arr
[2], -1, 1);
3159 evalue_copy(&fl
.x
.p
->arr
[0], &p
->arr
[0]);
3161 eadd(&fl
, &p
->arr
[0]);
3162 reorder_terms_about(p
, &p
->arr
[0]);
3166 free_evalue_refs(&fl
);
3171 int evalue_frac2floor_in_domain(evalue
*e
, Polyhedron
*D
)
3173 return evalue_frac2floor_in_domain3(e
, D
, 1);
3176 void evalue_frac2floor2(evalue
*e
, int shift
)
3179 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
) {
3181 if (evalue_frac2floor_in_domain3(e
, NULL
, 0))
3187 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3188 if (evalue_frac2floor_in_domain3(&e
->x
.p
->arr
[2*i
+1],
3189 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), shift
))
3190 reduce_evalue(&e
->x
.p
->arr
[2*i
+1]);
3193 void evalue_frac2floor(evalue
*e
)
3195 evalue_frac2floor2(e
, 1);
3198 /* Add a new variable with lower bound 1 and upper bound specified
3199 * by row. If negative is true, then the new variable has upper
3200 * bound -1 and lower bound specified by row.
3202 static Matrix
*esum_add_constraint(int nvar
, Polyhedron
*D
, Matrix
*C
,
3203 Vector
*row
, int negative
)
3207 int nparam
= D
->Dimension
- nvar
;
3210 nr
= D
->NbConstraints
+ 2;
3211 nc
= D
->Dimension
+ 2 + 1;
3212 C
= Matrix_Alloc(nr
, nc
);
3213 for (i
= 0; i
< D
->NbConstraints
; ++i
) {
3214 Vector_Copy(D
->Constraint
[i
], C
->p
[i
], 1 + nvar
);
3215 Vector_Copy(D
->Constraint
[i
] + 1 + nvar
, C
->p
[i
] + 1 + nvar
+ 1,
3216 D
->Dimension
+ 1 - nvar
);
3221 nc
= C
->NbColumns
+ 1;
3222 C
= Matrix_Alloc(nr
, nc
);
3223 for (i
= 0; i
< oldC
->NbRows
; ++i
) {
3224 Vector_Copy(oldC
->p
[i
], C
->p
[i
], 1 + nvar
);
3225 Vector_Copy(oldC
->p
[i
] + 1 + nvar
, C
->p
[i
] + 1 + nvar
+ 1,
3226 oldC
->NbColumns
- 1 - nvar
);
3229 value_set_si(C
->p
[nr
-2][0], 1);
3231 value_set_si(C
->p
[nr
-2][1 + nvar
], -1);
3233 value_set_si(C
->p
[nr
-2][1 + nvar
], 1);
3234 value_set_si(C
->p
[nr
-2][nc
- 1], -1);
3236 Vector_Copy(row
->p
, C
->p
[nr
-1], 1 + nvar
+ 1);
3237 Vector_Copy(row
->p
+ 1 + nvar
+ 1, C
->p
[nr
-1] + C
->NbColumns
- 1 - nparam
,
3243 static void floor2frac_r(evalue
*e
, int nvar
)
3250 if (value_notzero_p(e
->d
))
3255 assert(p
->type
== flooring
);
3256 for (i
= 1; i
< p
->size
; i
++)
3257 floor2frac_r(&p
->arr
[i
], nvar
);
3259 for (pp
= &p
->arr
[0]; value_zero_p(pp
->d
); pp
= &pp
->x
.p
->arr
[0]) {
3260 assert(pp
->x
.p
->type
== polynomial
);
3261 pp
->x
.p
->pos
-= nvar
;
3265 value_set_si(f
.d
, 0);
3266 f
.x
.p
= new_enode(fractional
, 3, -1);
3267 evalue_set_si(&f
.x
.p
->arr
[1], 0, 1);
3268 evalue_set_si(&f
.x
.p
->arr
[2], -1, 1);
3269 evalue_copy(&f
.x
.p
->arr
[0], &p
->arr
[0]);
3271 eadd(&f
, &p
->arr
[0]);
3272 reorder_terms_about(p
, &p
->arr
[0]);
3276 free_evalue_refs(&f
);
3279 /* Convert flooring back to fractional and shift position
3280 * of the parameters by nvar
3282 static void floor2frac(evalue
*e
, int nvar
)
3284 floor2frac_r(e
, nvar
);
3288 evalue
*esum_over_domain_cst(int nvar
, Polyhedron
*D
, Matrix
*C
)
3291 int nparam
= D
->Dimension
- nvar
;
3295 D
= Constraints2Polyhedron(C
, 0);
3299 t
= barvinok_enumerate_e(D
, 0, nparam
, 0);
3301 /* Double check that D was not unbounded. */
3302 assert(!(value_pos_p(t
->d
) && value_neg_p(t
->x
.n
)));
3310 static void domain_signs(Polyhedron
*D
, int *signs
)
3314 POL_ENSURE_VERTICES(D
);
3315 for (j
= 0; j
< D
->Dimension
; ++j
) {
3317 for (k
= 0; k
< D
->NbRays
; ++k
) {
3318 signs
[j
] = value_sign(D
->Ray
[k
][1+j
]);
3325 static evalue
*esum_over_domain(evalue
*e
, int nvar
, Polyhedron
*D
,
3326 int *signs
, Matrix
*C
, unsigned MaxRays
)
3332 evalue
*factor
= NULL
;
3336 if (EVALUE_IS_ZERO(*e
))
3340 Polyhedron
*DD
= Disjoint_Domain(D
, 0, MaxRays
);
3347 res
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3350 for (Q
= DD
; Q
; Q
= DD
) {
3356 t
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3369 if (value_notzero_p(e
->d
)) {
3372 t
= esum_over_domain_cst(nvar
, D
, C
);
3374 if (!EVALUE_IS_ONE(*e
))
3381 signs
= alloca(sizeof(int) * D
->Dimension
);
3382 domain_signs(D
, signs
);
3385 switch (e
->x
.p
->type
) {
3387 evalue
*pp
= &e
->x
.p
->arr
[0];
3389 if (pp
->x
.p
->pos
> nvar
) {
3390 /* remainder is independent of the summated vars */
3396 floor2frac(&f
, nvar
);
3398 t
= esum_over_domain_cst(nvar
, D
, C
);
3402 free_evalue_refs(&f
);
3407 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3408 poly_denom(pp
, &row
->p
[1 + nvar
]);
3409 value_set_si(row
->p
[0], 1);
3410 for (pp
= &e
->x
.p
->arr
[0]; value_zero_p(pp
->d
);
3411 pp
= &pp
->x
.p
->arr
[0]) {
3413 assert(pp
->x
.p
->type
== polynomial
);
3415 if (pos
>= 1 + nvar
)
3417 value_assign(row
->p
[pos
], row
->p
[1+nvar
]);
3418 value_division(row
->p
[pos
], row
->p
[pos
], pp
->x
.p
->arr
[1].d
);
3419 value_multiply(row
->p
[pos
], row
->p
[pos
], pp
->x
.p
->arr
[1].x
.n
);
3421 value_assign(row
->p
[1 + D
->Dimension
+ 1], row
->p
[1+nvar
]);
3422 value_division(row
->p
[1 + D
->Dimension
+ 1],
3423 row
->p
[1 + D
->Dimension
+ 1],
3425 value_multiply(row
->p
[1 + D
->Dimension
+ 1],
3426 row
->p
[1 + D
->Dimension
+ 1],
3428 value_oppose(row
->p
[1 + nvar
], row
->p
[1 + nvar
]);
3432 int pos
= e
->x
.p
->pos
;
3435 factor
= ALLOC(evalue
);
3436 value_init(factor
->d
);
3437 value_set_si(factor
->d
, 0);
3438 factor
->x
.p
= new_enode(polynomial
, 2, pos
- nvar
);
3439 evalue_set_si(&factor
->x
.p
->arr
[0], 0, 1);
3440 evalue_set_si(&factor
->x
.p
->arr
[1], 1, 1);
3444 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3445 negative
= signs
[pos
-1] < 0;
3446 value_set_si(row
->p
[0], 1);
3448 value_set_si(row
->p
[pos
], -1);
3449 value_set_si(row
->p
[1 + nvar
], 1);
3451 value_set_si(row
->p
[pos
], 1);
3452 value_set_si(row
->p
[1 + nvar
], -1);
3460 offset
= type_offset(e
->x
.p
);
3462 res
= esum_over_domain(&e
->x
.p
->arr
[offset
], nvar
, D
, signs
, C
, MaxRays
);
3466 evalue_copy(&cum
, factor
);
3470 for (i
= 1; offset
+i
< e
->x
.p
->size
; ++i
) {
3474 C
= esum_add_constraint(nvar
, D
, C
, row
, negative
);
3480 Vector_Print(stderr, P_VALUE_FMT, row);
3482 Matrix_Print(stderr, P_VALUE_FMT, C);
3484 t
= esum_over_domain(&e
->x
.p
->arr
[offset
+i
], nvar
, D
, signs
, C
, MaxRays
);
3489 if (negative
&& (i
% 2))
3499 if (factor
&& offset
+i
+1 < e
->x
.p
->size
)
3506 free_evalue_refs(&cum
);
3507 evalue_free(factor
);
3518 static Polyhedron_Insert(Polyhedron
***next
, Polyhedron
*Q
)
3528 static Polyhedron
*Polyhedron_Split_Into_Orthants(Polyhedron
*P
,
3533 Vector
*c
= Vector_Alloc(1 + P
->Dimension
+ 1);
3534 value_set_si(c
->p
[0], 1);
3536 if (P
->Dimension
== 0)
3537 return Polyhedron_Copy(P
);
3539 for (i
= 0; i
< P
->Dimension
; ++i
) {
3540 Polyhedron
*L
= NULL
;
3541 Polyhedron
**next
= &L
;
3544 for (I
= D
; I
; I
= I
->next
) {
3546 value_set_si(c
->p
[1+i
], 1);
3547 value_set_si(c
->p
[1+P
->Dimension
], 0);
3548 Q
= AddConstraints(c
->p
, 1, I
, MaxRays
);
3549 Polyhedron_Insert(&next
, Q
);
3550 value_set_si(c
->p
[1+i
], -1);
3551 value_set_si(c
->p
[1+P
->Dimension
], -1);
3552 Q
= AddConstraints(c
->p
, 1, I
, MaxRays
);
3553 Polyhedron_Insert(&next
, Q
);
3554 value_set_si(c
->p
[1+i
], 0);
3564 /* Make arguments of all floors non-negative */
3565 static void shift_floor_in_domain(evalue
*e
, Polyhedron
*D
)
3572 if (value_notzero_p(e
->d
))
3577 for (i
= type_offset(p
); i
< p
->size
; ++i
)
3578 shift_floor_in_domain(&p
->arr
[i
], D
);
3580 if (p
->type
!= flooring
)
3586 I
= polynomial_projection(p
, D
, &d
, NULL
);
3587 assert(I
->NbEq
== 0); /* Should have been reduced */
3589 for (i
= 0; i
< I
->NbConstraints
; ++i
)
3590 if (value_pos_p(I
->Constraint
[i
][1]))
3592 assert(i
< I
->NbConstraints
);
3593 if (i
< I
->NbConstraints
) {
3594 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
3595 mpz_fdiv_q(m
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
3596 if (value_neg_p(m
)) {
3597 /* replace [e] by [e-m]+m such that e-m >= 0 */
3602 value_set_si(f
.d
, 1);
3603 value_oppose(f
.x
.n
, m
);
3604 eadd(&f
, &p
->arr
[0]);
3607 value_set_si(f
.d
, 0);
3608 f
.x
.p
= new_enode(flooring
, 3, -1);
3609 value_clear(f
.x
.p
->arr
[0].d
);
3610 f
.x
.p
->arr
[0] = p
->arr
[0];
3611 evalue_set_si(&f
.x
.p
->arr
[2], 1, 1);
3612 value_set_si(f
.x
.p
->arr
[1].d
, 1);
3613 value_init(f
.x
.p
->arr
[1].x
.n
);
3614 value_assign(f
.x
.p
->arr
[1].x
.n
, m
);
3615 reorder_terms_about(p
, &f
);
3626 evalue
*box_summate(Polyhedron
*P
, evalue
*E
, unsigned nvar
, unsigned MaxRays
)
3628 evalue
*sum
= evalue_zero();
3629 Polyhedron
*D
= Polyhedron_Split_Into_Orthants(P
, MaxRays
);
3630 for (P
= D
; P
; P
= P
->next
) {
3632 evalue
*fe
= evalue_dup(E
);
3633 Polyhedron
*next
= P
->next
;
3635 reduce_evalue_in_domain(fe
, P
);
3636 evalue_frac2floor2(fe
, 0);
3637 shift_floor_in_domain(fe
, P
);
3638 t
= esum_over_domain(fe
, nvar
, P
, NULL
, NULL
, MaxRays
);
3650 /* Initial silly implementation */
3651 void eor(evalue
*e1
, evalue
*res
)
3657 evalue_set_si(&mone
, -1, 1);
3659 evalue_copy(&E
, res
);
3665 free_evalue_refs(&E
);
3666 free_evalue_refs(&mone
);
3669 /* computes denominator of polynomial evalue
3670 * d should point to a value initialized to 1
3672 void evalue_denom(const evalue
*e
, Value
*d
)
3676 if (value_notzero_p(e
->d
)) {
3677 value_lcm(*d
, *d
, e
->d
);
3680 assert(e
->x
.p
->type
== polynomial
||
3681 e
->x
.p
->type
== fractional
||
3682 e
->x
.p
->type
== flooring
);
3683 offset
= type_offset(e
->x
.p
);
3684 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3685 evalue_denom(&e
->x
.p
->arr
[i
], d
);
3688 /* Divides the evalue e by the integer n */
3689 void evalue_div(evalue
*e
, Value n
)
3693 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3696 if (value_notzero_p(e
->d
)) {
3699 value_multiply(e
->d
, e
->d
, n
);
3700 value_gcd(gc
, e
->x
.n
, e
->d
);
3701 if (value_notone_p(gc
)) {
3702 value_division(e
->d
, e
->d
, gc
);
3703 value_division(e
->x
.n
, e
->x
.n
, gc
);
3708 if (e
->x
.p
->type
== partition
) {
3709 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3710 evalue_div(&e
->x
.p
->arr
[2*i
+1], n
);
3713 offset
= type_offset(e
->x
.p
);
3714 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3715 evalue_div(&e
->x
.p
->arr
[i
], n
);
3718 /* Multiplies the evalue e by the integer n */
3719 void evalue_mul(evalue
*e
, Value n
)
3723 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3726 if (value_notzero_p(e
->d
)) {
3729 value_multiply(e
->x
.n
, e
->x
.n
, n
);
3730 value_gcd(gc
, e
->x
.n
, e
->d
);
3731 if (value_notone_p(gc
)) {
3732 value_division(e
->d
, e
->d
, gc
);
3733 value_division(e
->x
.n
, e
->x
.n
, gc
);
3738 if (e
->x
.p
->type
== partition
) {
3739 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3740 evalue_mul(&e
->x
.p
->arr
[2*i
+1], n
);
3743 offset
= type_offset(e
->x
.p
);
3744 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3745 evalue_mul(&e
->x
.p
->arr
[i
], n
);
3748 /* Multiplies the evalue e by the n/d */
3749 void evalue_mul_div(evalue
*e
, Value n
, Value d
)
3753 if ((value_one_p(n
) && value_one_p(d
)) || EVALUE_IS_ZERO(*e
))
3756 if (value_notzero_p(e
->d
)) {
3759 value_multiply(e
->x
.n
, e
->x
.n
, n
);
3760 value_multiply(e
->d
, e
->d
, d
);
3761 value_gcd(gc
, e
->x
.n
, e
->d
);
3762 if (value_notone_p(gc
)) {
3763 value_division(e
->d
, e
->d
, gc
);
3764 value_division(e
->x
.n
, e
->x
.n
, gc
);
3769 if (e
->x
.p
->type
== partition
) {
3770 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3771 evalue_mul_div(&e
->x
.p
->arr
[2*i
+1], n
, d
);
3774 offset
= type_offset(e
->x
.p
);
3775 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3776 evalue_mul_div(&e
->x
.p
->arr
[i
], n
, d
);
3779 void evalue_negate(evalue
*e
)
3783 if (value_notzero_p(e
->d
)) {
3784 value_oppose(e
->x
.n
, e
->x
.n
);
3787 if (e
->x
.p
->type
== partition
) {
3788 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3789 evalue_negate(&e
->x
.p
->arr
[2*i
+1]);
3792 offset
= type_offset(e
->x
.p
);
3793 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3794 evalue_negate(&e
->x
.p
->arr
[i
]);
3797 void evalue_add_constant(evalue
*e
, const Value cst
)
3801 if (value_zero_p(e
->d
)) {
3802 if (e
->x
.p
->type
== partition
) {
3803 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3804 evalue_add_constant(&e
->x
.p
->arr
[2*i
+1], cst
);
3807 if (e
->x
.p
->type
== relation
) {
3808 for (i
= 1; i
< e
->x
.p
->size
; ++i
)
3809 evalue_add_constant(&e
->x
.p
->arr
[i
], cst
);
3813 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)];
3814 } while (value_zero_p(e
->d
));
3816 value_addmul(e
->x
.n
, cst
, e
->d
);
3819 static void evalue_frac2polynomial_r(evalue
*e
, int *signs
, int sign
, int in_frac
)
3824 int sign_odd
= sign
;
3826 if (value_notzero_p(e
->d
)) {
3827 if (in_frac
&& sign
* value_sign(e
->x
.n
) < 0) {
3828 value_set_si(e
->x
.n
, 0);
3829 value_set_si(e
->d
, 1);
3834 if (e
->x
.p
->type
== relation
) {
3835 for (i
= e
->x
.p
->size
-1; i
>= 1; --i
)
3836 evalue_frac2polynomial_r(&e
->x
.p
->arr
[i
], signs
, sign
, in_frac
);
3840 if (e
->x
.p
->type
== polynomial
)
3841 sign_odd
*= signs
[e
->x
.p
->pos
-1];
3842 offset
= type_offset(e
->x
.p
);
3843 evalue_frac2polynomial_r(&e
->x
.p
->arr
[offset
], signs
, sign
, in_frac
);
3844 in_frac
|= e
->x
.p
->type
== fractional
;
3845 for (i
= e
->x
.p
->size
-1; i
> offset
; --i
)
3846 evalue_frac2polynomial_r(&e
->x
.p
->arr
[i
], signs
,
3847 (i
- offset
) % 2 ? sign_odd
: sign
, in_frac
);
3849 if (e
->x
.p
->type
!= fractional
)
3852 /* replace { a/m } by (m-1)/m if sign != 0
3853 * and by (m-1)/(2m) if sign == 0
3857 evalue_denom(&e
->x
.p
->arr
[0], &d
);
3858 free_evalue_refs(&e
->x
.p
->arr
[0]);
3859 value_init(e
->x
.p
->arr
[0].d
);
3860 value_init(e
->x
.p
->arr
[0].x
.n
);
3862 value_addto(e
->x
.p
->arr
[0].d
, d
, d
);
3864 value_assign(e
->x
.p
->arr
[0].d
, d
);
3865 value_decrement(e
->x
.p
->arr
[0].x
.n
, d
);
3869 reorder_terms_about(p
, &p
->arr
[0]);
3875 /* Approximate the evalue in fractional representation by a polynomial.
3876 * If sign > 0, the result is an upper bound;
3877 * if sign < 0, the result is a lower bound;
3878 * if sign = 0, the result is an intermediate approximation.
3880 void evalue_frac2polynomial(evalue
*e
, int sign
, unsigned MaxRays
)
3885 if (value_notzero_p(e
->d
))
3887 assert(e
->x
.p
->type
== partition
);
3888 /* make sure all variables in the domains have a fixed sign */
3890 evalue_split_domains_into_orthants(e
, MaxRays
);
3891 if (EVALUE_IS_ZERO(*e
))
3895 assert(e
->x
.p
->size
>= 2);
3896 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
3898 signs
= alloca(sizeof(int) * dim
);
3901 for (i
= 0; i
< dim
; ++i
)
3903 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
3905 domain_signs(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), signs
);
3906 evalue_frac2polynomial_r(&e
->x
.p
->arr
[2*i
+1], signs
, sign
, 0);
3910 /* Split the domains of e (which is assumed to be a partition)
3911 * such that each resulting domain lies entirely in one orthant.
3913 void evalue_split_domains_into_orthants(evalue
*e
, unsigned MaxRays
)
3916 assert(value_zero_p(e
->d
));
3917 assert(e
->x
.p
->type
== partition
);
3918 assert(e
->x
.p
->size
>= 2);
3919 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
3921 for (i
= 0; i
< dim
; ++i
) {
3924 C
= Matrix_Alloc(1, 1 + dim
+ 1);
3925 value_set_si(C
->p
[0][0], 1);
3926 value_init(split
.d
);
3927 value_set_si(split
.d
, 0);
3928 split
.x
.p
= new_enode(partition
, 4, dim
);
3929 value_set_si(C
->p
[0][1+i
], 1);
3930 C2
= Matrix_Copy(C
);
3931 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0], Constraints2Polyhedron(C2
, MaxRays
));
3933 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
3934 value_set_si(C
->p
[0][1+i
], -1);
3935 value_set_si(C
->p
[0][1+dim
], -1);
3936 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2], Constraints2Polyhedron(C
, MaxRays
));
3937 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
3939 free_evalue_refs(&split
);
3944 static evalue
*find_fractional_with_max_periods(evalue
*e
, Polyhedron
*D
,
3947 Value
*min
, Value
*max
)
3954 if (value_notzero_p(e
->d
))
3957 if (e
->x
.p
->type
== fractional
) {
3962 I
= polynomial_projection(e
->x
.p
, D
, &d
, &T
);
3963 bounded
= line_minmax(I
, min
, max
); /* frees I */
3967 value_set_si(mp
, max_periods
);
3968 mpz_fdiv_q(*min
, *min
, d
);
3969 mpz_fdiv_q(*max
, *max
, d
);
3970 value_assign(T
->p
[1][D
->Dimension
], d
);
3971 value_subtract(d
, *max
, *min
);
3972 if (value_ge(d
, mp
))
3975 f
= evalue_dup(&e
->x
.p
->arr
[0]);
3986 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
3987 if ((f
= find_fractional_with_max_periods(&e
->x
.p
->arr
[i
], D
, max_periods
,
3994 static void replace_fract_by_affine(evalue
*e
, evalue
*f
, Value val
)
3998 if (value_notzero_p(e
->d
))
4001 offset
= type_offset(e
->x
.p
);
4002 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
4003 replace_fract_by_affine(&e
->x
.p
->arr
[i
], f
, val
);
4005 if (e
->x
.p
->type
!= fractional
)
4008 if (!eequal(&e
->x
.p
->arr
[0], f
))
4011 replace_by_affine(e
, val
);
4014 /* Look for fractional parts that can be removed by splitting the corresponding
4015 * domain into at most max_periods parts.
4016 * We use a very simply strategy that looks for the first fractional part
4017 * that satisfies the condition, performs the split and then continues
4018 * looking for other fractional parts in the split domains until no
4019 * such fractional part can be found anymore.
4021 void evalue_split_periods(evalue
*e
, int max_periods
, unsigned int MaxRays
)
4028 if (EVALUE_IS_ZERO(*e
))
4030 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
) {
4032 "WARNING: evalue_split_periods called on incorrect evalue type\n");
4040 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
4045 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
4047 f
= find_fractional_with_max_periods(&e
->x
.p
->arr
[2*i
+1], D
, max_periods
,
4052 M
= Matrix_Alloc(2, 2+D
->Dimension
);
4054 value_subtract(d
, max
, min
);
4055 n
= VALUE_TO_INT(d
)+1;
4057 value_set_si(M
->p
[0][0], 1);
4058 Vector_Copy(T
->p
[0], M
->p
[0]+1, D
->Dimension
+1);
4059 value_multiply(d
, max
, T
->p
[1][D
->Dimension
]);
4060 value_subtract(M
->p
[0][1+D
->Dimension
], M
->p
[0][1+D
->Dimension
], d
);
4061 value_set_si(d
, -1);
4062 value_set_si(M
->p
[1][0], 1);
4063 Vector_Scale(T
->p
[0], M
->p
[1]+1, d
, D
->Dimension
+1);
4064 value_addmul(M
->p
[1][1+D
->Dimension
], max
, T
->p
[1][D
->Dimension
]);
4065 value_addto(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
],
4066 T
->p
[1][D
->Dimension
]);
4067 value_decrement(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
]);
4069 p
= new_enode(partition
, e
->x
.p
->size
+ (n
-1)*2, e
->x
.p
->pos
);
4070 for (j
= 0; j
< 2*i
; ++j
) {
4071 value_clear(p
->arr
[j
].d
);
4072 p
->arr
[j
] = e
->x
.p
->arr
[j
];
4074 for (j
= 2*i
+2; j
< e
->x
.p
->size
; ++j
) {
4075 value_clear(p
->arr
[j
+2*(n
-1)].d
);
4076 p
->arr
[j
+2*(n
-1)] = e
->x
.p
->arr
[j
];
4078 for (j
= n
-1; j
>= 0; --j
) {
4080 value_clear(p
->arr
[2*i
+1].d
);
4081 p
->arr
[2*i
+1] = e
->x
.p
->arr
[2*i
+1];
4083 evalue_copy(&p
->arr
[2*(i
+j
)+1], &e
->x
.p
->arr
[2*i
+1]);
4085 value_subtract(M
->p
[1][1+D
->Dimension
], M
->p
[1][1+D
->Dimension
],
4086 T
->p
[1][D
->Dimension
]);
4087 value_addto(M
->p
[0][1+D
->Dimension
], M
->p
[0][1+D
->Dimension
],
4088 T
->p
[1][D
->Dimension
]);
4090 replace_fract_by_affine(&p
->arr
[2*(i
+j
)+1], f
, max
);
4091 E
= DomainAddConstraints(D
, M
, MaxRays
);
4092 EVALUE_SET_DOMAIN(p
->arr
[2*(i
+j
)], E
);
4093 if (evalue_range_reduction_in_domain(&p
->arr
[2*(i
+j
)+1], E
))
4094 reduce_evalue(&p
->arr
[2*(i
+j
)+1]);
4095 value_decrement(max
, max
);
4097 value_clear(e
->x
.p
->arr
[2*i
].d
);
4112 void evalue_extract_affine(const evalue
*e
, Value
*coeff
, Value
*cst
, Value
*d
)
4114 value_set_si(*d
, 1);
4116 for ( ; value_zero_p(e
->d
); e
= &e
->x
.p
->arr
[0]) {
4118 assert(e
->x
.p
->type
== polynomial
);
4119 assert(e
->x
.p
->size
== 2);
4120 c
= &e
->x
.p
->arr
[1];
4121 value_multiply(coeff
[e
->x
.p
->pos
-1], *d
, c
->x
.n
);
4122 value_division(coeff
[e
->x
.p
->pos
-1], coeff
[e
->x
.p
->pos
-1], c
->d
);
4124 value_multiply(*cst
, *d
, e
->x
.n
);
4125 value_division(*cst
, *cst
, e
->d
);
4128 /* returns an evalue that corresponds to
4132 static evalue
*term(int param
, Value c
, Value den
)
4134 evalue
*EP
= ALLOC(evalue
);
4136 value_set_si(EP
->d
,0);
4137 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
4138 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
4139 value_init(EP
->x
.p
->arr
[1].x
.n
);
4140 value_assign(EP
->x
.p
->arr
[1].d
, den
);
4141 value_assign(EP
->x
.p
->arr
[1].x
.n
, c
);
4145 evalue
*affine2evalue(Value
*coeff
, Value denom
, int nvar
)
4148 evalue
*E
= ALLOC(evalue
);
4150 evalue_set(E
, coeff
[nvar
], denom
);
4151 for (i
= 0; i
< nvar
; ++i
) {
4153 if (value_zero_p(coeff
[i
]))
4155 t
= term(i
, coeff
[i
], denom
);
4162 void evalue_substitute(evalue
*e
, evalue
**subs
)
4168 if (value_notzero_p(e
->d
))
4172 assert(p
->type
!= partition
);
4174 for (i
= 0; i
< p
->size
; ++i
)
4175 evalue_substitute(&p
->arr
[i
], subs
);
4177 if (p
->type
== relation
) {
4178 /* For relation a ? b : c, compute (a' ? 1) * b' + (a' ? 0 : 1) * c' */
4182 value_set_si(v
->d
, 0);
4183 v
->x
.p
= new_enode(relation
, 3, 0);
4184 evalue_copy(&v
->x
.p
->arr
[0], &p
->arr
[0]);
4185 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4186 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4187 emul(v
, &p
->arr
[2]);
4192 value_set_si(v
->d
, 0);
4193 v
->x
.p
= new_enode(relation
, 2, 0);
4194 value_clear(v
->x
.p
->arr
[0].d
);
4195 v
->x
.p
->arr
[0] = p
->arr
[0];
4196 evalue_set_si(&v
->x
.p
->arr
[1], 1, 1);
4197 emul(v
, &p
->arr
[1]);
4200 eadd(&p
->arr
[2], &p
->arr
[1]);
4201 free_evalue_refs(&p
->arr
[2]);
4209 if (p
->type
== polynomial
)
4214 value_set_si(v
->d
, 0);
4215 v
->x
.p
= new_enode(p
->type
, 3, -1);
4216 value_clear(v
->x
.p
->arr
[0].d
);
4217 v
->x
.p
->arr
[0] = p
->arr
[0];
4218 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4219 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4222 offset
= type_offset(p
);
4224 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
4225 emul(v
, &p
->arr
[i
]);
4226 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
4227 free_evalue_refs(&(p
->arr
[i
]));
4230 if (p
->type
!= polynomial
)
4234 *e
= p
->arr
[offset
];
4238 /* evalue e is given in terms of "new" parameter; CP maps the new
4239 * parameters back to the old parameters.
4240 * Transforms e such that it refers back to the old parameters and
4241 * adds appropriate constraints to the domain.
4242 * In particular, if CP maps the new parameters onto an affine
4243 * subspace of the old parameters, then the corresponding equalities
4244 * are added to the domain.
4245 * Also, if any of the new parameters was a rational combination
4246 * of the old parameters $p' = (<a, p> + c)/m$, then modulo
4247 * constraints ${<a, p> + c)/m} = 0$ are added to ensure
4248 * the new evalue remains non-zero only for integer parameters
4249 * of the new parameters (which have been removed by the substitution).
4251 void evalue_backsubstitute(evalue
*e
, Matrix
*CP
, unsigned MaxRays
)
4258 unsigned nparam
= CP
->NbColumns
-1;
4262 if (EVALUE_IS_ZERO(*e
))
4265 assert(value_zero_p(e
->d
));
4267 assert(p
->type
== partition
);
4269 inv
= left_inverse(CP
, &eq
);
4270 subs
= ALLOCN(evalue
*, nparam
);
4271 for (i
= 0; i
< nparam
; ++i
)
4272 subs
[i
] = affine2evalue(inv
->p
[i
], inv
->p
[nparam
][inv
->NbColumns
-1],
4275 CEq
= Constraints2Polyhedron(eq
, MaxRays
);
4276 addeliminatedparams_partition(p
, inv
, CEq
, inv
->NbColumns
-1, MaxRays
);
4277 Polyhedron_Free(CEq
);
4279 for (i
= 0; i
< p
->size
/2; ++i
)
4280 evalue_substitute(&p
->arr
[2*i
+1], subs
);
4282 for (i
= 0; i
< nparam
; ++i
)
4283 evalue_free(subs
[i
]);
4287 for (i
= 0; i
< inv
->NbRows
-1; ++i
) {
4288 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &gcd
);
4289 value_gcd(gcd
, gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
4290 if (value_eq(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
4292 Vector_AntiScale(inv
->p
[i
], inv
->p
[i
], gcd
, inv
->NbColumns
);
4293 value_divexact(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1], gcd
);
4295 for (j
= 0; j
< p
->size
/2; ++j
) {
4296 evalue
*arg
= affine2evalue(inv
->p
[i
], gcd
, inv
->NbColumns
-1);
4301 value_set_si(rel
.d
, 0);
4302 rel
.x
.p
= new_enode(relation
, 2, 0);
4303 value_clear(rel
.x
.p
->arr
[1].d
);
4304 rel
.x
.p
->arr
[1] = p
->arr
[2*j
+1];
4305 ev
= &rel
.x
.p
->arr
[0];
4306 value_set_si(ev
->d
, 0);
4307 ev
->x
.p
= new_enode(fractional
, 3, -1);
4308 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
4309 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
4310 value_clear(ev
->x
.p
->arr
[0].d
);
4311 ev
->x
.p
->arr
[0] = *arg
;
4314 p
->arr
[2*j
+1] = rel
;
4325 * \sum_{i=0}^n c_i/d X^i
4327 * where d is the last element in the vector c.
4329 evalue
*evalue_polynomial(Vector
*c
, const evalue
* X
)
4331 unsigned dim
= c
->Size
-2;
4333 evalue
*EP
= ALLOC(evalue
);
4338 if (EVALUE_IS_ZERO(*X
) || dim
== 0) {
4339 evalue_set(EP
, c
->p
[0], c
->p
[dim
+1]);
4340 reduce_constant(EP
);
4344 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
4347 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
4349 for (i
= dim
-1; i
>= 0; --i
) {
4351 value_assign(EC
.x
.n
, c
->p
[i
]);
4354 free_evalue_refs(&EC
);
4358 /* Create an evalue from an array of pairs of domains and evalues. */
4359 evalue
*evalue_from_section_array(struct evalue_section
*s
, int n
)
4364 res
= ALLOC(evalue
);
4368 evalue_set_si(res
, 0, 1);
4370 value_set_si(res
->d
, 0);
4371 res
->x
.p
= new_enode(partition
, 2*n
, s
[0].D
->Dimension
);
4372 for (i
= 0; i
< n
; ++i
) {
4373 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*i
], s
[i
].D
);
4374 value_clear(res
->x
.p
->arr
[2*i
+1].d
);
4375 res
->x
.p
->arr
[2*i
+1] = *s
[i
].E
;
4382 /* shift variables (>= first, 0-based) in polynomial n up (may be negative) */
4383 void evalue_shift_variables(evalue
*e
, int first
, int n
)
4386 if (value_notzero_p(e
->d
))
4388 assert(e
->x
.p
->type
== polynomial
||
4389 e
->x
.p
->type
== flooring
||
4390 e
->x
.p
->type
== fractional
);
4391 if (e
->x
.p
->type
== polynomial
&& e
->x
.p
->pos
>= first
+1) {
4392 assert(e
->x
.p
->pos
+ n
>= 1);
4395 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
4396 evalue_shift_variables(&e
->x
.p
->arr
[i
], first
, n
);