1 /***********************************************************************/
2 /* copyright 1997, Doran Wilde */
3 /* copyright 1997-2000, Vincent Loechner */
4 /* copyright 1999, Emmanuel Jeannot */
5 /* copyright 2003, Rachid Seghir */
6 /* copyright 2003-2006, Sven Verdoolaege */
7 /* Permission is granted to copy, use, and distribute */
8 /* for any commercial or noncommercial purpose under the terms */
9 /* of the GNU General Public License, either version 2 */
10 /* of the License, or (at your option) any later version. */
11 /* (see file : LICENSE). */
12 /***********************************************************************/
18 #include <barvinok/evalue.h>
19 #include <barvinok/barvinok.h>
20 #include <barvinok/util.h>
23 #ifndef value_pmodulus
24 #define value_pmodulus(ref,val1,val2) (mpz_fdiv_r((ref),(val1),(val2)))
27 #define ALLOC(type) (type*)malloc(sizeof(type))
28 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
31 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
33 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
36 void evalue_set_si(evalue
*ev
, int n
, int d
) {
37 value_set_si(ev
->d
, d
);
39 value_set_si(ev
->x
.n
, n
);
42 void evalue_set(evalue
*ev
, Value n
, Value d
) {
43 value_assign(ev
->d
, d
);
45 value_assign(ev
->x
.n
, n
);
48 void evalue_set_reduce(evalue
*ev
, Value n
, Value d
) {
50 value_gcd(ev
->x
.n
, n
, d
);
51 value_divexact(ev
->d
, d
, ev
->x
.n
);
52 value_divexact(ev
->x
.n
, n
, ev
->x
.n
);
57 evalue
*EP
= ALLOC(evalue
);
59 evalue_set_si(EP
, 0, 1);
65 evalue
*EP
= ALLOC(evalue
);
67 value_set_si(EP
->d
, -2);
72 /* returns an evalue that corresponds to
76 evalue
*evalue_var(int var
)
78 evalue
*EP
= ALLOC(evalue
);
80 value_set_si(EP
->d
,0);
81 EP
->x
.p
= new_enode(polynomial
, 2, var
+ 1);
82 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
83 evalue_set_si(&EP
->x
.p
->arr
[1], 1, 1);
87 void aep_evalue(evalue
*e
, int *ref
) {
92 if (value_notzero_p(e
->d
))
93 return; /* a rational number, its already reduced */
95 return; /* hum... an overflow probably occured */
97 /* First check the components of p */
98 for (i
=0;i
<p
->size
;i
++)
99 aep_evalue(&p
->arr
[i
],ref
);
106 p
->pos
= ref
[p
->pos
-1]+1;
112 void addeliminatedparams_evalue(evalue
*e
,Matrix
*CT
) {
118 if (value_notzero_p(e
->d
))
119 return; /* a rational number, its already reduced */
121 return; /* hum... an overflow probably occured */
124 ref
= (int *)malloc(sizeof(int)*(CT
->NbRows
-1));
125 for(i
=0;i
<CT
->NbRows
-1;i
++)
126 for(j
=0;j
<CT
->NbColumns
;j
++)
127 if(value_notzero_p(CT
->p
[i
][j
])) {
132 /* Transform the references in e, using ref */
136 } /* addeliminatedparams_evalue */
138 static void addeliminatedparams_partition(enode
*p
, Matrix
*CT
, Polyhedron
*CEq
,
139 unsigned nparam
, unsigned MaxRays
)
142 assert(p
->type
== partition
);
145 for (i
= 0; i
< p
->size
/2; i
++) {
146 Polyhedron
*D
= EVALUE_DOMAIN(p
->arr
[2*i
]);
147 Polyhedron
*T
= DomainPreimage(D
, CT
, MaxRays
);
151 T
= DomainIntersection(D
, CEq
, MaxRays
);
154 EVALUE_SET_DOMAIN(p
->arr
[2*i
], T
);
158 void addeliminatedparams_enum(evalue
*e
, Matrix
*CT
, Polyhedron
*CEq
,
159 unsigned MaxRays
, unsigned nparam
)
164 if (CT
->NbRows
== CT
->NbColumns
)
167 if (EVALUE_IS_ZERO(*e
))
170 if (value_notzero_p(e
->d
)) {
173 value_set_si(res
.d
, 0);
174 res
.x
.p
= new_enode(partition
, 2, nparam
);
175 EVALUE_SET_DOMAIN(res
.x
.p
->arr
[0],
176 DomainConstraintSimplify(Polyhedron_Copy(CEq
), MaxRays
));
177 value_clear(res
.x
.p
->arr
[1].d
);
178 res
.x
.p
->arr
[1] = *e
;
186 addeliminatedparams_partition(p
, CT
, CEq
, nparam
, MaxRays
);
187 for (i
= 0; i
< p
->size
/2; i
++)
188 addeliminatedparams_evalue(&p
->arr
[2*i
+1], CT
);
191 static int mod_rational_cmp(evalue
*e1
, evalue
*e2
)
199 assert(value_notzero_p(e1
->d
));
200 assert(value_notzero_p(e2
->d
));
201 value_multiply(m
, e1
->x
.n
, e2
->d
);
202 value_multiply(m2
, e2
->x
.n
, e1
->d
);
205 else if (value_gt(m
, m2
))
215 static int mod_term_cmp_r(evalue
*e1
, evalue
*e2
)
217 if (value_notzero_p(e1
->d
)) {
219 if (value_zero_p(e2
->d
))
221 return mod_rational_cmp(e1
, e2
);
223 if (value_notzero_p(e2
->d
))
225 if (e1
->x
.p
->pos
< e2
->x
.p
->pos
)
227 else if (e1
->x
.p
->pos
> e2
->x
.p
->pos
)
230 int r
= mod_rational_cmp(&e1
->x
.p
->arr
[1], &e2
->x
.p
->arr
[1]);
232 ? mod_term_cmp_r(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0])
237 static int mod_term_cmp(const evalue
*e1
, const evalue
*e2
)
239 assert(value_zero_p(e1
->d
));
240 assert(value_zero_p(e2
->d
));
241 assert(e1
->x
.p
->type
== fractional
|| e1
->x
.p
->type
== flooring
);
242 assert(e2
->x
.p
->type
== fractional
|| e2
->x
.p
->type
== flooring
);
243 return mod_term_cmp_r(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]);
246 static void check_order(const evalue
*e
)
251 if (value_notzero_p(e
->d
))
254 switch (e
->x
.p
->type
) {
256 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
257 check_order(&e
->x
.p
->arr
[2*i
+1]);
260 for (i
= 1; i
< e
->x
.p
->size
; ++i
) {
262 if (value_notzero_p(a
->d
))
264 switch (a
->x
.p
->type
) {
266 assert(mod_term_cmp(&e
->x
.p
->arr
[0], &a
->x
.p
->arr
[0]) < 0);
275 for (i
= 0; i
< e
->x
.p
->size
; ++i
) {
277 if (value_notzero_p(a
->d
))
279 switch (a
->x
.p
->type
) {
281 assert(e
->x
.p
->pos
< a
->x
.p
->pos
);
292 for (i
= 1; i
< e
->x
.p
->size
; ++i
) {
294 if (value_notzero_p(a
->d
))
296 switch (a
->x
.p
->type
) {
307 /* Negative pos means inequality */
308 /* s is negative of substitution if m is not zero */
317 struct fixed_param
*fixed
;
322 static int relations_depth(evalue
*e
)
327 value_zero_p(e
->d
) && e
->x
.p
->type
== relation
;
328 e
= &e
->x
.p
->arr
[1], ++d
);
332 static void poly_denom_not_constant(evalue
**pp
, Value
*d
)
337 while (value_zero_p(p
->d
)) {
338 assert(p
->x
.p
->type
== polynomial
);
339 assert(p
->x
.p
->size
== 2);
340 assert(value_notzero_p(p
->x
.p
->arr
[1].d
));
341 value_lcm(*d
, *d
, p
->x
.p
->arr
[1].d
);
347 static void poly_denom(evalue
*p
, Value
*d
)
349 poly_denom_not_constant(&p
, d
);
350 value_lcm(*d
, *d
, p
->d
);
353 static void realloc_substitution(struct subst
*s
, int d
)
355 struct fixed_param
*n
;
358 for (i
= 0; i
< s
->n
; ++i
)
365 static int add_modulo_substitution(struct subst
*s
, evalue
*r
)
371 assert(value_zero_p(r
->d
) && r
->x
.p
->type
== relation
);
374 /* May have been reduced already */
375 if (value_notzero_p(m
->d
))
378 assert(value_zero_p(m
->d
) && m
->x
.p
->type
== fractional
);
379 assert(m
->x
.p
->size
== 3);
381 /* fractional was inverted during reduction
382 * invert it back and move constant in
384 if (!EVALUE_IS_ONE(m
->x
.p
->arr
[2])) {
385 assert(value_pos_p(m
->x
.p
->arr
[2].d
));
386 assert(value_mone_p(m
->x
.p
->arr
[2].x
.n
));
387 value_set_si(m
->x
.p
->arr
[2].x
.n
, 1);
388 value_increment(m
->x
.p
->arr
[1].x
.n
, m
->x
.p
->arr
[1].x
.n
);
389 assert(value_eq(m
->x
.p
->arr
[1].x
.n
, m
->x
.p
->arr
[1].d
));
390 value_set_si(m
->x
.p
->arr
[1].x
.n
, 1);
391 eadd(&m
->x
.p
->arr
[1], &m
->x
.p
->arr
[0]);
392 value_set_si(m
->x
.p
->arr
[1].x
.n
, 0);
393 value_set_si(m
->x
.p
->arr
[1].d
, 1);
396 /* Oops. Nested identical relations. */
397 if (!EVALUE_IS_ZERO(m
->x
.p
->arr
[1]))
400 if (s
->n
>= s
->max
) {
401 int d
= relations_depth(r
);
402 realloc_substitution(s
, d
);
406 assert(value_zero_p(p
->d
) && p
->x
.p
->type
== polynomial
);
407 assert(p
->x
.p
->size
== 2);
410 assert(value_pos_p(f
->x
.n
));
412 value_init(s
->fixed
[s
->n
].m
);
413 value_assign(s
->fixed
[s
->n
].m
, f
->d
);
414 s
->fixed
[s
->n
].pos
= p
->x
.p
->pos
;
415 value_init(s
->fixed
[s
->n
].d
);
416 value_assign(s
->fixed
[s
->n
].d
, f
->x
.n
);
417 value_init(s
->fixed
[s
->n
].s
.d
);
418 evalue_copy(&s
->fixed
[s
->n
].s
, &p
->x
.p
->arr
[0]);
424 static int type_offset(enode
*p
)
426 return p
->type
== fractional
? 1 :
427 p
->type
== flooring
? 1 :
428 p
->type
== relation
? 1 : 0;
431 static void reorder_terms_about(enode
*p
, evalue
*v
)
434 int offset
= type_offset(p
);
436 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
438 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
439 free_evalue_refs(&(p
->arr
[i
]));
445 void evalue_reorder_terms(evalue
*e
)
451 assert(value_zero_p(e
->d
));
453 assert(p
->type
== fractional
||
454 p
->type
== flooring
||
455 p
->type
== polynomial
); /* for now */
457 offset
= type_offset(p
);
459 value_set_si(f
.d
, 0);
460 f
.x
.p
= new_enode(p
->type
, offset
+2, p
->pos
);
462 value_clear(f
.x
.p
->arr
[0].d
);
463 f
.x
.p
->arr
[0] = p
->arr
[0];
465 evalue_set_si(&f
.x
.p
->arr
[offset
], 0, 1);
466 evalue_set_si(&f
.x
.p
->arr
[offset
+1], 1, 1);
467 reorder_terms_about(p
, &f
);
473 static void evalue_reduce_size(evalue
*e
)
477 assert(value_zero_p(e
->d
));
480 offset
= type_offset(p
);
482 /* Try to reduce the degree */
483 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
484 if (!EVALUE_IS_ZERO(p
->arr
[i
]))
486 free_evalue_refs(&p
->arr
[i
]);
491 /* Try to reduce its strength */
492 if (p
->type
== relation
) {
494 free_evalue_refs(&p
->arr
[0]);
495 evalue_set_si(e
, 0, 1);
498 } else if (p
->size
== offset
+1) {
500 memcpy(e
, &p
->arr
[offset
], sizeof(evalue
));
502 free_evalue_refs(&p
->arr
[0]);
507 #define value_two_p(val) (mpz_cmp_si(val,2) == 0)
509 /* This function is called after the argument of the fractional part
510 * in a polynomial expression in this fractional part has been reduced.
511 * If the polynomial expression is of degree at least two, then
512 * check if the argument happens to have been reduced to an affine
513 * expression with denominator two. If so, then emul_fractionals
514 * assumes that the polynomial expression in this fractional part
515 * is affine so we reduce the higher degree polynomial to an affine
518 * In particular, since the denominator of the fractional part is two,
519 * then the fractional part can only take on two values, 0 and 1/2.
520 * This means that { f(x)/2 }^2 = 1/2 { f(x)/2 } so that we can repeatedly
523 * a_n { f(x)/2 }^n with n >= 2
527 * a_n/2 { f(x)/2 }^{n-1}
529 static void reduce_fractional(evalue
*e
)
534 if (e
->x
.p
->size
<= 3)
538 poly_denom(&e
->x
.p
->arr
[0], &d
.d
);
539 if (value_two_p(d
.d
)) {
541 value_set_si(d
.x
.n
, 1);
542 for (i
= e
->x
.p
->size
- 1; i
>= 3; --i
) {
543 emul(&d
, &e
->x
.p
->arr
[i
]);
544 eadd(&e
->x
.p
->arr
[i
], &e
->x
.p
->arr
[i
- 1]);
545 free_evalue_refs(&e
->x
.p
->arr
[i
]);
553 void _reduce_evalue (evalue
*e
, struct subst
*s
, int fract
) {
559 if (value_notzero_p(e
->d
)) {
561 mpz_fdiv_r(e
->x
.n
, e
->x
.n
, e
->d
);
562 return; /* a rational number, its already reduced */
566 return; /* hum... an overflow probably occured */
568 /* First reduce the components of p */
569 add
= p
->type
== relation
;
570 for (i
=0; i
<p
->size
; i
++) {
572 add
= add_modulo_substitution(s
, e
);
574 if (i
== 0 && p
->type
==fractional
) {
575 _reduce_evalue(&p
->arr
[i
], s
, 1);
576 reduce_fractional(e
);
578 _reduce_evalue(&p
->arr
[i
], s
, fract
);
580 if (add
&& i
== p
->size
-1) {
582 value_clear(s
->fixed
[s
->n
].m
);
583 value_clear(s
->fixed
[s
->n
].d
);
584 free_evalue_refs(&s
->fixed
[s
->n
].s
);
585 } else if (add
&& i
== 1)
586 s
->fixed
[s
->n
-1].pos
*= -1;
589 if (p
->type
==periodic
) {
591 /* Try to reduce the period */
592 for (i
=1; i
<=(p
->size
)/2; i
++) {
593 if ((p
->size
% i
)==0) {
595 /* Can we reduce the size to i ? */
597 for (k
=j
+i
; k
<e
->x
.p
->size
; k
+=i
)
598 if (!eequal(&p
->arr
[j
], &p
->arr
[k
])) goto you_lose
;
601 for (j
=i
; j
<p
->size
; j
++) free_evalue_refs(&p
->arr
[j
]);
605 you_lose
: /* OK, lets not do it */
610 /* Try to reduce its strength */
613 memcpy(e
,&p
->arr
[0],sizeof(evalue
));
617 else if (p
->type
==polynomial
) {
618 for (k
= 0; s
&& k
< s
->n
; ++k
) {
619 if (s
->fixed
[k
].pos
== p
->pos
) {
620 int divide
= value_notone_p(s
->fixed
[k
].d
);
623 if (value_notzero_p(s
->fixed
[k
].m
)) {
626 assert(p
->size
== 2);
627 if (divide
&& value_ne(s
->fixed
[k
].d
, p
->arr
[1].x
.n
))
629 if (!mpz_divisible_p(s
->fixed
[k
].m
, p
->arr
[1].d
))
636 value_assign(d
.d
, s
->fixed
[k
].d
);
638 if (value_notzero_p(s
->fixed
[k
].m
))
639 value_oppose(d
.x
.n
, s
->fixed
[k
].m
);
641 value_set_si(d
.x
.n
, 1);
644 for (i
=p
->size
-1;i
>=1;i
--) {
645 emul(&s
->fixed
[k
].s
, &p
->arr
[i
]);
647 emul(&d
, &p
->arr
[i
]);
648 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
649 free_evalue_refs(&(p
->arr
[i
]));
652 _reduce_evalue(&p
->arr
[0], s
, fract
);
655 free_evalue_refs(&d
);
661 evalue_reduce_size(e
);
663 else if (p
->type
==fractional
) {
667 if (value_notzero_p(p
->arr
[0].d
)) {
669 value_assign(v
.d
, p
->arr
[0].d
);
671 mpz_fdiv_r(v
.x
.n
, p
->arr
[0].x
.n
, p
->arr
[0].d
);
676 evalue
*pp
= &p
->arr
[0];
677 assert(value_zero_p(pp
->d
) && pp
->x
.p
->type
== polynomial
);
678 assert(pp
->x
.p
->size
== 2);
680 /* search for exact duplicate among the modulo inequalities */
682 f
= &pp
->x
.p
->arr
[1];
683 for (k
= 0; s
&& k
< s
->n
; ++k
) {
684 if (-s
->fixed
[k
].pos
== pp
->x
.p
->pos
&&
685 value_eq(s
->fixed
[k
].d
, f
->x
.n
) &&
686 value_eq(s
->fixed
[k
].m
, f
->d
) &&
687 eequal(&s
->fixed
[k
].s
, &pp
->x
.p
->arr
[0]))
694 /* replace { E/m } by { (E-1)/m } + 1/m */
699 evalue_set_si(&extra
, 1, 1);
700 value_assign(extra
.d
, g
);
701 eadd(&extra
, &v
.x
.p
->arr
[1]);
702 free_evalue_refs(&extra
);
704 /* We've been going in circles; stop now */
705 if (value_ge(v
.x
.p
->arr
[1].x
.n
, v
.x
.p
->arr
[1].d
)) {
706 free_evalue_refs(&v
);
708 evalue_set_si(&v
, 0, 1);
713 value_set_si(v
.d
, 0);
714 v
.x
.p
= new_enode(fractional
, 3, -1);
715 evalue_set_si(&v
.x
.p
->arr
[1], 1, 1);
716 value_assign(v
.x
.p
->arr
[1].d
, g
);
717 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
718 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
721 for (f
= &v
.x
.p
->arr
[0]; value_zero_p(f
->d
);
724 value_division(f
->d
, g
, f
->d
);
725 value_multiply(f
->x
.n
, f
->x
.n
, f
->d
);
726 value_assign(f
->d
, g
);
727 value_decrement(f
->x
.n
, f
->x
.n
);
728 mpz_fdiv_r(f
->x
.n
, f
->x
.n
, f
->d
);
730 value_gcd(g
, f
->d
, f
->x
.n
);
731 value_division(f
->d
, f
->d
, g
);
732 value_division(f
->x
.n
, f
->x
.n
, g
);
741 /* reduction may have made this fractional arg smaller */
742 i
= reorder
? p
->size
: 1;
743 for ( ; i
< p
->size
; ++i
)
744 if (value_zero_p(p
->arr
[i
].d
) &&
745 p
->arr
[i
].x
.p
->type
== fractional
&&
746 mod_term_cmp(e
, &p
->arr
[i
]) >= 0)
750 value_set_si(v
.d
, 0);
751 v
.x
.p
= new_enode(fractional
, 3, -1);
752 evalue_set_si(&v
.x
.p
->arr
[1], 0, 1);
753 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
754 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
762 evalue
*pp
= &p
->arr
[0];
765 poly_denom_not_constant(&pp
, &m
);
766 mpz_fdiv_r(r
, m
, pp
->d
);
767 if (value_notzero_p(r
)) {
769 value_set_si(v
.d
, 0);
770 v
.x
.p
= new_enode(fractional
, 3, -1);
772 value_multiply(r
, m
, pp
->x
.n
);
773 value_multiply(v
.x
.p
->arr
[1].d
, m
, pp
->d
);
774 value_init(v
.x
.p
->arr
[1].x
.n
);
775 mpz_fdiv_r(v
.x
.p
->arr
[1].x
.n
, r
, pp
->d
);
776 mpz_fdiv_q(r
, r
, pp
->d
);
778 evalue_set_si(&v
.x
.p
->arr
[2], 1, 1);
779 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
781 while (value_zero_p(pp
->d
))
782 pp
= &pp
->x
.p
->arr
[0];
784 value_assign(pp
->d
, m
);
785 value_assign(pp
->x
.n
, r
);
787 value_gcd(r
, pp
->d
, pp
->x
.n
);
788 value_division(pp
->d
, pp
->d
, r
);
789 value_division(pp
->x
.n
, pp
->x
.n
, r
);
802 for (pp
= &p
->arr
[0]; value_zero_p(pp
->d
);
803 pp
= &pp
->x
.p
->arr
[0]) {
804 f
= &pp
->x
.p
->arr
[1];
805 assert(value_pos_p(f
->d
));
806 mpz_mul_ui(twice
, f
->x
.n
, 2);
807 if (value_lt(twice
, f
->d
))
809 if (value_eq(twice
, f
->d
))
817 value_set_si(v
.d
, 0);
818 v
.x
.p
= new_enode(fractional
, 3, -1);
819 evalue_set_si(&v
.x
.p
->arr
[1], 0, 1);
820 poly_denom(&p
->arr
[0], &twice
);
821 value_assign(v
.x
.p
->arr
[1].d
, twice
);
822 value_decrement(v
.x
.p
->arr
[1].x
.n
, twice
);
823 evalue_set_si(&v
.x
.p
->arr
[2], -1, 1);
824 evalue_copy(&v
.x
.p
->arr
[0], &p
->arr
[0]);
826 for (pp
= &v
.x
.p
->arr
[0]; value_zero_p(pp
->d
);
827 pp
= &pp
->x
.p
->arr
[0]) {
828 f
= &pp
->x
.p
->arr
[1];
829 value_oppose(f
->x
.n
, f
->x
.n
);
830 mpz_fdiv_r(f
->x
.n
, f
->x
.n
, f
->d
);
832 value_division(pp
->d
, twice
, pp
->d
);
833 value_multiply(pp
->x
.n
, pp
->x
.n
, pp
->d
);
834 value_assign(pp
->d
, twice
);
835 value_oppose(pp
->x
.n
, pp
->x
.n
);
836 value_decrement(pp
->x
.n
, pp
->x
.n
);
837 mpz_fdiv_r(pp
->x
.n
, pp
->x
.n
, pp
->d
);
839 /* Maybe we should do this during reduction of
842 value_gcd(twice
, pp
->d
, pp
->x
.n
);
843 value_division(pp
->d
, pp
->d
, twice
);
844 value_division(pp
->x
.n
, pp
->x
.n
, twice
);
854 reorder_terms_about(p
, &v
);
855 _reduce_evalue(&p
->arr
[1], s
, fract
);
858 evalue_reduce_size(e
);
860 else if (p
->type
== flooring
) {
861 /* Replace floor(constant) by its value */
862 if (value_notzero_p(p
->arr
[0].d
)) {
865 value_set_si(v
.d
, 1);
867 mpz_fdiv_q(v
.x
.n
, p
->arr
[0].x
.n
, p
->arr
[0].d
);
868 reorder_terms_about(p
, &v
);
869 _reduce_evalue(&p
->arr
[1], s
, fract
);
871 evalue_reduce_size(e
);
873 else if (p
->type
== relation
) {
874 if (p
->size
== 3 && eequal(&p
->arr
[1], &p
->arr
[2])) {
875 free_evalue_refs(&(p
->arr
[2]));
876 free_evalue_refs(&(p
->arr
[0]));
883 evalue_reduce_size(e
);
884 if (value_notzero_p(e
->d
) || p
!= e
->x
.p
)
891 /* Relation was reduced by means of an identical
892 * inequality => remove
894 if (value_zero_p(m
->d
) && !EVALUE_IS_ZERO(m
->x
.p
->arr
[1]))
897 if (reduced
|| value_notzero_p(p
->arr
[0].d
)) {
898 if (!reduced
&& value_zero_p(p
->arr
[0].x
.n
)) {
900 memcpy(e
,&p
->arr
[1],sizeof(evalue
));
902 free_evalue_refs(&(p
->arr
[2]));
906 memcpy(e
,&p
->arr
[2],sizeof(evalue
));
908 evalue_set_si(e
, 0, 1);
909 free_evalue_refs(&(p
->arr
[1]));
911 free_evalue_refs(&(p
->arr
[0]));
917 } /* reduce_evalue */
919 static void add_substitution(struct subst
*s
, Value
*row
, unsigned dim
)
924 for (k
= 0; k
< dim
; ++k
)
925 if (value_notzero_p(row
[k
+1]))
928 Vector_Normalize_Positive(row
+1, dim
+1, k
);
929 assert(s
->n
< s
->max
);
930 value_init(s
->fixed
[s
->n
].d
);
931 value_init(s
->fixed
[s
->n
].m
);
932 value_assign(s
->fixed
[s
->n
].d
, row
[k
+1]);
933 s
->fixed
[s
->n
].pos
= k
+1;
934 value_set_si(s
->fixed
[s
->n
].m
, 0);
935 r
= &s
->fixed
[s
->n
].s
;
937 for (l
= k
+1; l
< dim
; ++l
)
938 if (value_notzero_p(row
[l
+1])) {
939 value_set_si(r
->d
, 0);
940 r
->x
.p
= new_enode(polynomial
, 2, l
+ 1);
941 value_init(r
->x
.p
->arr
[1].x
.n
);
942 value_oppose(r
->x
.p
->arr
[1].x
.n
, row
[l
+1]);
943 value_set_si(r
->x
.p
->arr
[1].d
, 1);
947 value_oppose(r
->x
.n
, row
[dim
+1]);
948 value_set_si(r
->d
, 1);
952 static void _reduce_evalue_in_domain(evalue
*e
, Polyhedron
*D
, struct subst
*s
)
955 Polyhedron
*orig
= D
;
960 D
= DomainConvex(D
, 0);
961 /* We don't perform any substitutions if the domain is a union.
962 * We may therefore miss out on some possible simplifications,
963 * e.g., if a variable is always even in the whole union,
964 * while there is a relation in the evalue that evaluates
965 * to zero for even values of the variable.
967 if (!D
->next
&& D
->NbEq
) {
971 realloc_substitution(s
, dim
);
973 int d
= relations_depth(e
);
975 NALLOC(s
->fixed
, s
->max
);
978 for (j
= 0; j
< D
->NbEq
; ++j
)
979 add_substitution(s
, D
->Constraint
[j
], dim
);
983 _reduce_evalue(e
, s
, 0);
986 for (j
= 0; j
< s
->n
; ++j
) {
987 value_clear(s
->fixed
[j
].d
);
988 value_clear(s
->fixed
[j
].m
);
989 free_evalue_refs(&s
->fixed
[j
].s
);
994 void reduce_evalue_in_domain(evalue
*e
, Polyhedron
*D
)
996 struct subst s
= { NULL
, 0, 0 };
997 POL_ENSURE_VERTICES(D
);
999 if (EVALUE_IS_ZERO(*e
))
1001 free_evalue_refs(e
);
1003 evalue_set_si(e
, 0, 1);
1006 _reduce_evalue_in_domain(e
, D
, &s
);
1011 void reduce_evalue (evalue
*e
) {
1012 struct subst s
= { NULL
, 0, 0 };
1014 if (value_notzero_p(e
->d
))
1015 return; /* a rational number, its already reduced */
1017 if (e
->x
.p
->type
== partition
) {
1019 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
1020 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
1022 /* This shouldn't really happen;
1023 * Empty domains should not be added.
1025 POL_ENSURE_VERTICES(D
);
1027 _reduce_evalue_in_domain(&e
->x
.p
->arr
[2*i
+1], D
, &s
);
1029 if (emptyQ(D
) || EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
1030 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
1031 Domain_Free(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]));
1032 value_clear(e
->x
.p
->arr
[2*i
].d
);
1034 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
1035 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
1039 if (e
->x
.p
->size
== 0) {
1041 evalue_set_si(e
, 0, 1);
1044 _reduce_evalue(e
, &s
, 0);
1049 static void print_evalue_r(FILE *DST
, const evalue
*e
, const char **pname
)
1051 if (EVALUE_IS_NAN(*e
)) {
1052 fprintf(DST
, "NaN");
1056 if(value_notzero_p(e
->d
)) {
1057 if(value_notone_p(e
->d
)) {
1058 value_print(DST
,VALUE_FMT
,e
->x
.n
);
1060 value_print(DST
,VALUE_FMT
,e
->d
);
1063 value_print(DST
,VALUE_FMT
,e
->x
.n
);
1067 print_enode(DST
,e
->x
.p
,pname
);
1069 } /* print_evalue */
1071 void print_evalue(FILE *DST
, const evalue
*e
, const char **pname
)
1073 print_evalue_r(DST
, e
, pname
);
1074 if (value_notzero_p(e
->d
))
1078 void print_enode(FILE *DST
, enode
*p
, const char **pname
)
1083 fprintf(DST
, "NULL");
1089 for (i
=0; i
<p
->size
; i
++) {
1090 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1094 fprintf(DST
, " }\n");
1098 for (i
=p
->size
-1; i
>=0; i
--) {
1099 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1100 if (i
==1) fprintf(DST
, " * %s + ", pname
[p
->pos
-1]);
1102 fprintf(DST
, " * %s^%d + ", pname
[p
->pos
-1], i
);
1104 fprintf(DST
, " )\n");
1108 for (i
=0; i
<p
->size
; i
++) {
1109 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1110 if (i
!=(p
->size
-1)) fprintf(DST
, ", ");
1112 fprintf(DST
," ]_%s", pname
[p
->pos
-1]);
1117 for (i
=p
->size
-1; i
>=1; i
--) {
1118 print_evalue_r(DST
, &p
->arr
[i
], pname
);
1120 fprintf(DST
, " * ");
1121 fprintf(DST
, p
->type
== flooring
? "[" : "{");
1122 print_evalue_r(DST
, &p
->arr
[0], pname
);
1123 fprintf(DST
, p
->type
== flooring
? "]" : "}");
1125 fprintf(DST
, "^%d + ", i
-1);
1127 fprintf(DST
, " + ");
1130 fprintf(DST
, " )\n");
1134 print_evalue_r(DST
, &p
->arr
[0], pname
);
1135 fprintf(DST
, "= 0 ] * \n");
1136 print_evalue_r(DST
, &p
->arr
[1], pname
);
1138 fprintf(DST
, " +\n [ ");
1139 print_evalue_r(DST
, &p
->arr
[0], pname
);
1140 fprintf(DST
, "!= 0 ] * \n");
1141 print_evalue_r(DST
, &p
->arr
[2], pname
);
1145 char **new_names
= NULL
;
1146 const char **names
= pname
;
1147 int maxdim
= EVALUE_DOMAIN(p
->arr
[0])->Dimension
;
1148 if (!pname
|| p
->pos
< maxdim
) {
1149 new_names
= ALLOCN(char *, maxdim
);
1150 for (i
= 0; i
< p
->pos
; ++i
) {
1152 new_names
[i
] = (char *)pname
[i
];
1154 new_names
[i
] = ALLOCN(char, 10);
1155 snprintf(new_names
[i
], 10, "%c", 'P'+i
);
1158 for ( ; i
< maxdim
; ++i
) {
1159 new_names
[i
] = ALLOCN(char, 10);
1160 snprintf(new_names
[i
], 10, "_p%d", i
);
1162 names
= (const char**)new_names
;
1165 for (i
=0; i
<p
->size
/2; i
++) {
1166 Print_Domain(DST
, EVALUE_DOMAIN(p
->arr
[2*i
]), names
);
1167 print_evalue_r(DST
, &p
->arr
[2*i
+1], names
);
1168 if (value_notzero_p(p
->arr
[2*i
+1].d
))
1172 if (!pname
|| p
->pos
< maxdim
) {
1173 for (i
= pname
? p
->pos
: 0; i
< maxdim
; ++i
)
1187 * 0 if toplevels of e1 and e2 are at the same level
1188 * <0 if toplevel of e1 should be outside of toplevel of e2
1189 * >0 if toplevel of e2 should be outside of toplevel of e1
1191 static int evalue_level_cmp(const evalue
*e1
, const evalue
*e2
)
1193 if (value_notzero_p(e1
->d
) && value_notzero_p(e2
->d
))
1195 if (value_notzero_p(e1
->d
))
1197 if (value_notzero_p(e2
->d
))
1199 if (e1
->x
.p
->type
== partition
&& e2
->x
.p
->type
== partition
)
1201 if (e1
->x
.p
->type
== partition
)
1203 if (e2
->x
.p
->type
== partition
)
1205 if (e1
->x
.p
->type
== relation
&& e2
->x
.p
->type
== relation
) {
1206 if (eequal(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1208 return mod_term_cmp(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]);
1210 if (e1
->x
.p
->type
== relation
)
1212 if (e2
->x
.p
->type
== relation
)
1214 if (e1
->x
.p
->type
== polynomial
&& e2
->x
.p
->type
== polynomial
)
1215 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
1216 if (e1
->x
.p
->type
== polynomial
)
1218 if (e2
->x
.p
->type
== polynomial
)
1220 if (e1
->x
.p
->type
== periodic
&& e2
->x
.p
->type
== periodic
)
1221 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
1222 assert(e1
->x
.p
->type
!= periodic
);
1223 assert(e2
->x
.p
->type
!= periodic
);
1224 assert(e1
->x
.p
->type
== e2
->x
.p
->type
);
1225 if (eequal(&e1
->x
.p
->arr
[0], &e2
->x
.p
->arr
[0]))
1227 return mod_term_cmp(e1
, e2
);
1230 static void eadd_rev(const evalue
*e1
, evalue
*res
)
1234 evalue_copy(&ev
, e1
);
1236 free_evalue_refs(res
);
1240 static void eadd_rev_cst(const evalue
*e1
, evalue
*res
)
1244 evalue_copy(&ev
, e1
);
1245 eadd(res
, &ev
.x
.p
->arr
[type_offset(ev
.x
.p
)]);
1246 free_evalue_refs(res
);
1250 struct section
{ Polyhedron
* D
; evalue E
; };
1252 void eadd_partitions(const evalue
*e1
, evalue
*res
)
1257 s
= (struct section
*)
1258 malloc((e1
->x
.p
->size
/2+1) * (res
->x
.p
->size
/2+1) *
1259 sizeof(struct section
));
1261 assert(e1
->x
.p
->pos
== res
->x
.p
->pos
);
1262 assert(e1
->x
.p
->pos
== EVALUE_DOMAIN(e1
->x
.p
->arr
[0])->Dimension
);
1263 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1266 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1267 assert(res
->x
.p
->size
>= 2);
1268 fd
= DomainDifference(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1269 EVALUE_DOMAIN(res
->x
.p
->arr
[0]), 0);
1271 for (i
= 1; i
< res
->x
.p
->size
/2; ++i
) {
1273 fd
= DomainDifference(fd
, EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1278 fd
= DomainConstraintSimplify(fd
, 0);
1283 value_init(s
[n
].E
.d
);
1284 evalue_copy(&s
[n
].E
, &e1
->x
.p
->arr
[2*j
+1]);
1288 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
) {
1289 fd
= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]);
1290 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1292 d
= DomainIntersection(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1293 EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1294 d
= DomainConstraintSimplify(d
, 0);
1300 fd
= DomainDifference(fd
, EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]), 0);
1301 if (t
!= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]))
1303 value_init(s
[n
].E
.d
);
1304 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*i
+1]);
1305 eadd(&e1
->x
.p
->arr
[2*j
+1], &s
[n
].E
);
1310 s
[n
].E
= res
->x
.p
->arr
[2*i
+1];
1314 free_evalue_refs(&res
->x
.p
->arr
[2*i
+1]);
1317 if (fd
!= EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]))
1318 Domain_Free(EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]));
1319 value_clear(res
->x
.p
->arr
[2*i
].d
);
1324 res
->x
.p
= new_enode(partition
, 2*n
, e1
->x
.p
->pos
);
1325 for (j
= 0; j
< n
; ++j
) {
1326 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1327 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1328 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1334 static void explicit_complement(evalue
*res
)
1336 enode
*rel
= new_enode(relation
, 3, 0);
1338 value_clear(rel
->arr
[0].d
);
1339 rel
->arr
[0] = res
->x
.p
->arr
[0];
1340 value_clear(rel
->arr
[1].d
);
1341 rel
->arr
[1] = res
->x
.p
->arr
[1];
1342 value_set_si(rel
->arr
[2].d
, 1);
1343 value_init(rel
->arr
[2].x
.n
);
1344 value_set_si(rel
->arr
[2].x
.n
, 0);
1349 static void reduce_constant(evalue
*e
)
1354 value_gcd(g
, e
->x
.n
, e
->d
);
1355 if (value_notone_p(g
)) {
1356 value_division(e
->d
, e
->d
,g
);
1357 value_division(e
->x
.n
, e
->x
.n
,g
);
1362 /* Add two rational numbers */
1363 static void eadd_rationals(const evalue
*e1
, evalue
*res
)
1365 if (value_eq(e1
->d
, res
->d
))
1366 value_addto(res
->x
.n
, res
->x
.n
, e1
->x
.n
);
1368 value_multiply(res
->x
.n
, res
->x
.n
, e1
->d
);
1369 value_addmul(res
->x
.n
, e1
->x
.n
, res
->d
);
1370 value_multiply(res
->d
,e1
->d
,res
->d
);
1372 reduce_constant(res
);
1375 static void eadd_relations(const evalue
*e1
, evalue
*res
)
1379 if (res
->x
.p
->size
< 3 && e1
->x
.p
->size
== 3)
1380 explicit_complement(res
);
1381 for (i
= 1; i
< e1
->x
.p
->size
; ++i
)
1382 eadd(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1385 static void eadd_arrays(const evalue
*e1
, evalue
*res
, int n
)
1389 // add any element in e1 to the corresponding element in res
1390 i
= type_offset(res
->x
.p
);
1392 assert(eequal(&e1
->x
.p
->arr
[0], &res
->x
.p
->arr
[0]));
1394 eadd(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1397 static void eadd_poly(const evalue
*e1
, evalue
*res
)
1399 if (e1
->x
.p
->size
> res
->x
.p
->size
)
1402 eadd_arrays(e1
, res
, e1
->x
.p
->size
);
1406 * Product or sum of two periodics of the same parameter
1407 * and different periods
1409 static void combine_periodics(const evalue
*e1
, evalue
*res
,
1410 void (*op
)(const evalue
*, evalue
*))
1418 value_set_si(es
, e1
->x
.p
->size
);
1419 value_set_si(rs
, res
->x
.p
->size
);
1420 value_lcm(rs
, es
, rs
);
1421 size
= (int)mpz_get_si(rs
);
1424 p
= new_enode(periodic
, size
, e1
->x
.p
->pos
);
1425 for (i
= 0; i
< res
->x
.p
->size
; i
++) {
1426 value_clear(p
->arr
[i
].d
);
1427 p
->arr
[i
] = res
->x
.p
->arr
[i
];
1429 for (i
= res
->x
.p
->size
; i
< size
; i
++)
1430 evalue_copy(&p
->arr
[i
], &res
->x
.p
->arr
[i
% res
->x
.p
->size
]);
1431 for (i
= 0; i
< size
; i
++)
1432 op(&e1
->x
.p
->arr
[i
% e1
->x
.p
->size
], &p
->arr
[i
]);
1437 static void eadd_periodics(const evalue
*e1
, evalue
*res
)
1443 if (e1
->x
.p
->size
== res
->x
.p
->size
) {
1444 eadd_arrays(e1
, res
, e1
->x
.p
->size
);
1448 combine_periodics(e1
, res
, eadd
);
1451 void evalue_assign(evalue
*dst
, const evalue
*src
)
1453 if (value_pos_p(dst
->d
) && value_pos_p(src
->d
)) {
1454 value_assign(dst
->d
, src
->d
);
1455 value_assign(dst
->x
.n
, src
->x
.n
);
1458 free_evalue_refs(dst
);
1460 evalue_copy(dst
, src
);
1463 void eadd(const evalue
*e1
, evalue
*res
)
1467 if (EVALUE_IS_ZERO(*e1
))
1470 if (EVALUE_IS_NAN(*res
))
1473 if (EVALUE_IS_NAN(*e1
)) {
1474 evalue_assign(res
, e1
);
1478 if (EVALUE_IS_ZERO(*res
)) {
1479 evalue_assign(res
, e1
);
1483 cmp
= evalue_level_cmp(res
, e1
);
1485 switch (e1
->x
.p
->type
) {
1489 eadd_rev_cst(e1
, res
);
1494 } else if (cmp
== 0) {
1495 if (value_notzero_p(e1
->d
)) {
1496 eadd_rationals(e1
, res
);
1498 switch (e1
->x
.p
->type
) {
1500 eadd_partitions(e1
, res
);
1503 eadd_relations(e1
, res
);
1506 assert(e1
->x
.p
->size
== res
->x
.p
->size
);
1513 eadd_periodics(e1
, res
);
1521 switch (res
->x
.p
->type
) {
1525 /* Add to the constant term of a polynomial */
1526 eadd(e1
, &res
->x
.p
->arr
[type_offset(res
->x
.p
)]);
1529 /* Add to all elements of a periodic number */
1530 for (i
= 0; i
< res
->x
.p
->size
; i
++)
1531 eadd(e1
, &res
->x
.p
->arr
[i
]);
1534 fprintf(stderr
, "eadd: cannot add const with vector\n");
1539 /* Create (zero) complement if needed */
1540 if (res
->x
.p
->size
< 3)
1541 explicit_complement(res
);
1542 for (i
= 1; i
< res
->x
.p
->size
; ++i
)
1543 eadd(e1
, &res
->x
.p
->arr
[i
]);
1551 static void emul_rev(const evalue
*e1
, evalue
*res
)
1555 evalue_copy(&ev
, e1
);
1557 free_evalue_refs(res
);
1561 static void emul_poly(const evalue
*e1
, evalue
*res
)
1563 int i
, j
, offset
= type_offset(res
->x
.p
);
1566 int size
= (e1
->x
.p
->size
+ res
->x
.p
->size
- offset
- 1);
1568 p
= new_enode(res
->x
.p
->type
, size
, res
->x
.p
->pos
);
1570 for (i
= offset
; i
< e1
->x
.p
->size
-1; ++i
)
1571 if (!EVALUE_IS_ZERO(e1
->x
.p
->arr
[i
]))
1574 /* special case pure power */
1575 if (i
== e1
->x
.p
->size
-1) {
1577 value_clear(p
->arr
[0].d
);
1578 p
->arr
[0] = res
->x
.p
->arr
[0];
1580 for (i
= offset
; i
< e1
->x
.p
->size
-1; ++i
)
1581 evalue_set_si(&p
->arr
[i
], 0, 1);
1582 for (i
= offset
; i
< res
->x
.p
->size
; ++i
) {
1583 value_clear(p
->arr
[i
+e1
->x
.p
->size
-offset
-1].d
);
1584 p
->arr
[i
+e1
->x
.p
->size
-offset
-1] = res
->x
.p
->arr
[i
];
1585 emul(&e1
->x
.p
->arr
[e1
->x
.p
->size
-1],
1586 &p
->arr
[i
+e1
->x
.p
->size
-offset
-1]);
1594 value_set_si(tmp
.d
,0);
1597 evalue_copy(&p
->arr
[0], &e1
->x
.p
->arr
[0]);
1598 for (i
= offset
; i
< e1
->x
.p
->size
; i
++) {
1599 evalue_copy(&tmp
.x
.p
->arr
[i
], &e1
->x
.p
->arr
[i
]);
1600 emul(&res
->x
.p
->arr
[offset
], &tmp
.x
.p
->arr
[i
]);
1603 evalue_set_si(&tmp
.x
.p
->arr
[i
], 0, 1);
1604 for (i
= offset
+1; i
<res
->x
.p
->size
; i
++)
1605 for (j
= offset
; j
<e1
->x
.p
->size
; j
++) {
1608 evalue_copy(&ev
, &e1
->x
.p
->arr
[j
]);
1609 emul(&res
->x
.p
->arr
[i
], &ev
);
1610 eadd(&ev
, &tmp
.x
.p
->arr
[i
+j
-offset
]);
1611 free_evalue_refs(&ev
);
1613 free_evalue_refs(res
);
1617 void emul_partitions(const evalue
*e1
, evalue
*res
)
1622 s
= (struct section
*)
1623 malloc((e1
->x
.p
->size
/2) * (res
->x
.p
->size
/2) *
1624 sizeof(struct section
));
1626 assert(e1
->x
.p
->pos
== res
->x
.p
->pos
);
1627 assert(e1
->x
.p
->pos
== EVALUE_DOMAIN(e1
->x
.p
->arr
[0])->Dimension
);
1628 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1631 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
) {
1632 for (j
= 0; j
< e1
->x
.p
->size
/2; ++j
) {
1633 d
= DomainIntersection(EVALUE_DOMAIN(e1
->x
.p
->arr
[2*j
]),
1634 EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]), 0);
1635 d
= DomainConstraintSimplify(d
, 0);
1641 /* This code is only needed because the partitions
1642 are not true partitions.
1644 for (k
= 0; k
< n
; ++k
) {
1645 if (DomainIncludes(s
[k
].D
, d
))
1647 if (DomainIncludes(d
, s
[k
].D
)) {
1648 Domain_Free(s
[k
].D
);
1649 free_evalue_refs(&s
[k
].E
);
1660 value_init(s
[n
].E
.d
);
1661 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*i
+1]);
1662 emul(&e1
->x
.p
->arr
[2*j
+1], &s
[n
].E
);
1666 Domain_Free(EVALUE_DOMAIN(res
->x
.p
->arr
[2*i
]));
1667 value_clear(res
->x
.p
->arr
[2*i
].d
);
1668 free_evalue_refs(&res
->x
.p
->arr
[2*i
+1]);
1673 evalue_set_si(res
, 0, 1);
1675 res
->x
.p
= new_enode(partition
, 2*n
, e1
->x
.p
->pos
);
1676 for (j
= 0; j
< n
; ++j
) {
1677 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1678 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1679 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1686 /* Product of two rational numbers */
1687 static void emul_rationals(const evalue
*e1
, evalue
*res
)
1689 value_multiply(res
->d
, e1
->d
, res
->d
);
1690 value_multiply(res
->x
.n
, e1
->x
.n
, res
->x
.n
);
1691 reduce_constant(res
);
1694 static void emul_relations(const evalue
*e1
, evalue
*res
)
1698 if (e1
->x
.p
->size
< 3 && res
->x
.p
->size
== 3) {
1699 free_evalue_refs(&res
->x
.p
->arr
[2]);
1702 for (i
= 1; i
< res
->x
.p
->size
; ++i
)
1703 emul(&e1
->x
.p
->arr
[i
], &res
->x
.p
->arr
[i
]);
1706 static void emul_periodics(const evalue
*e1
, evalue
*res
)
1713 if (e1
->x
.p
->size
== res
->x
.p
->size
) {
1714 /* Product of two periodics of the same parameter and period */
1715 for (i
= 0; i
< res
->x
.p
->size
; i
++)
1716 emul(&(e1
->x
.p
->arr
[i
]), &(res
->x
.p
->arr
[i
]));
1720 combine_periodics(e1
, res
, emul
);
1723 /* Multiply two polynomial expressions in the same fractional part.
1725 * If the denominator of the fractional part is two, then the fractional
1726 * part can only take on two values, 0 and 1/2.
1727 * This means that { f(x)/2 }^2 = 1/2 { f(x)/2 } so that
1729 * (a0 + a1 { f(x)/2 }) * (b0 + b1 { f(x)/2 })
1730 * = a0 b0 + (a0 b1 + a1 b0 + a1 b1/2) { f(x)/2 }
1732 * Since we can always reduce higher degree polynomials this way
1733 * we assume that the inputs are degree-1 polynomials.
1734 * Note in particular that we always start out with degree-1 polynomials
1735 * and that if we obtain an argument with a denominator of two
1736 * as a result of a substitution, then the polynomial expression
1737 * is reduced in reduce_fractional.
1739 static void emul_fractionals(const evalue
*e1
, evalue
*res
)
1743 poly_denom(&e1
->x
.p
->arr
[0], &d
.d
);
1744 if (!value_two_p(d
.d
))
1749 value_set_si(d
.x
.n
, 1);
1750 /* { x }^2 == { x }/2 */
1751 /* a0 b0 + (a0 b1 + a1 b0 + a1 b1/2) { x } */
1752 assert(e1
->x
.p
->size
== 3);
1753 assert(res
->x
.p
->size
== 3);
1755 evalue_copy(&tmp
, &res
->x
.p
->arr
[2]);
1757 eadd(&res
->x
.p
->arr
[1], &tmp
);
1758 emul(&e1
->x
.p
->arr
[2], &tmp
);
1759 emul(&e1
->x
.p
->arr
[1], &res
->x
.p
->arr
[1]);
1760 emul(&e1
->x
.p
->arr
[1], &res
->x
.p
->arr
[2]);
1761 eadd(&tmp
, &res
->x
.p
->arr
[2]);
1762 free_evalue_refs(&tmp
);
1768 /* Computes the product of two evalues "e1" and "res" and puts
1769 * the result in "res". You need to make a copy of "res"
1770 * before calling this function if you still need it afterward.
1771 * The vector type of evalues is not treated here
1773 void emul(const evalue
*e1
, evalue
*res
)
1777 assert(!(value_zero_p(e1
->d
) && e1
->x
.p
->type
== evector
));
1778 assert(!(value_zero_p(res
->d
) && res
->x
.p
->type
== evector
));
1780 if (EVALUE_IS_ZERO(*res
))
1783 if (EVALUE_IS_ONE(*e1
))
1786 if (EVALUE_IS_ZERO(*e1
)) {
1787 evalue_assign(res
, e1
);
1791 if (EVALUE_IS_NAN(*res
))
1794 if (EVALUE_IS_NAN(*e1
)) {
1795 evalue_assign(res
, e1
);
1799 cmp
= evalue_level_cmp(res
, e1
);
1802 } else if (cmp
== 0) {
1803 if (value_notzero_p(e1
->d
)) {
1804 emul_rationals(e1
, res
);
1806 switch (e1
->x
.p
->type
) {
1808 emul_partitions(e1
, res
);
1811 emul_relations(e1
, res
);
1818 emul_periodics(e1
, res
);
1821 emul_fractionals(e1
, res
);
1827 switch (res
->x
.p
->type
) {
1829 for (i
= 0; i
< res
->x
.p
->size
/2; ++i
)
1830 emul(e1
, &res
->x
.p
->arr
[2*i
+1]);
1837 for (i
= type_offset(res
->x
.p
); i
< res
->x
.p
->size
; ++i
)
1838 emul(e1
, &res
->x
.p
->arr
[i
]);
1844 /* Frees mask content ! */
1845 void emask(evalue
*mask
, evalue
*res
) {
1852 if (EVALUE_IS_ZERO(*res
)) {
1853 free_evalue_refs(mask
);
1857 assert(value_zero_p(mask
->d
));
1858 assert(mask
->x
.p
->type
== partition
);
1859 assert(value_zero_p(res
->d
));
1860 assert(res
->x
.p
->type
== partition
);
1861 assert(mask
->x
.p
->pos
== res
->x
.p
->pos
);
1862 assert(res
->x
.p
->pos
== EVALUE_DOMAIN(res
->x
.p
->arr
[0])->Dimension
);
1863 assert(mask
->x
.p
->pos
== EVALUE_DOMAIN(mask
->x
.p
->arr
[0])->Dimension
);
1864 pos
= res
->x
.p
->pos
;
1866 s
= (struct section
*)
1867 malloc((mask
->x
.p
->size
/2+1) * (res
->x
.p
->size
/2) *
1868 sizeof(struct section
));
1872 evalue_set_si(&mone
, -1, 1);
1875 for (j
= 0; j
< res
->x
.p
->size
/2; ++j
) {
1876 assert(mask
->x
.p
->size
>= 2);
1877 fd
= DomainDifference(EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]),
1878 EVALUE_DOMAIN(mask
->x
.p
->arr
[0]), 0);
1880 for (i
= 1; i
< mask
->x
.p
->size
/2; ++i
) {
1882 fd
= DomainDifference(fd
, EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]), 0);
1891 value_init(s
[n
].E
.d
);
1892 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*j
+1]);
1896 for (i
= 0; i
< mask
->x
.p
->size
/2; ++i
) {
1897 if (EVALUE_IS_ONE(mask
->x
.p
->arr
[2*i
+1]))
1900 fd
= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]);
1901 eadd(&mone
, &mask
->x
.p
->arr
[2*i
+1]);
1902 emul(&mone
, &mask
->x
.p
->arr
[2*i
+1]);
1903 for (j
= 0; j
< res
->x
.p
->size
/2; ++j
) {
1905 d
= DomainIntersection(EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]),
1906 EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]), 0);
1912 fd
= DomainDifference(fd
, EVALUE_DOMAIN(res
->x
.p
->arr
[2*j
]), 0);
1913 if (t
!= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]))
1915 value_init(s
[n
].E
.d
);
1916 evalue_copy(&s
[n
].E
, &res
->x
.p
->arr
[2*j
+1]);
1917 emul(&mask
->x
.p
->arr
[2*i
+1], &s
[n
].E
);
1923 /* Just ignore; this may have been previously masked off */
1925 if (fd
!= EVALUE_DOMAIN(mask
->x
.p
->arr
[2*i
]))
1929 free_evalue_refs(&mone
);
1930 free_evalue_refs(mask
);
1931 free_evalue_refs(res
);
1934 evalue_set_si(res
, 0, 1);
1936 res
->x
.p
= new_enode(partition
, 2*n
, pos
);
1937 for (j
= 0; j
< n
; ++j
) {
1938 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*j
], s
[j
].D
);
1939 value_clear(res
->x
.p
->arr
[2*j
+1].d
);
1940 res
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1947 void evalue_copy(evalue
*dst
, const evalue
*src
)
1949 value_assign(dst
->d
, src
->d
);
1950 if (EVALUE_IS_NAN(*dst
)) {
1954 if (value_pos_p(src
->d
)) {
1955 value_init(dst
->x
.n
);
1956 value_assign(dst
->x
.n
, src
->x
.n
);
1958 dst
->x
.p
= ecopy(src
->x
.p
);
1961 evalue
*evalue_dup(const evalue
*e
)
1963 evalue
*res
= ALLOC(evalue
);
1965 evalue_copy(res
, e
);
1969 enode
*new_enode(enode_type type
,int size
,int pos
) {
1975 fprintf(stderr
, "Allocating enode of size 0 !\n" );
1978 res
= (enode
*) malloc(sizeof(enode
) + (size
-1)*sizeof(evalue
));
1982 for(i
=0; i
<size
; i
++) {
1983 value_init(res
->arr
[i
].d
);
1984 value_set_si(res
->arr
[i
].d
,0);
1985 res
->arr
[i
].x
.p
= 0;
1990 enode
*ecopy(enode
*e
) {
1995 res
= new_enode(e
->type
,e
->size
,e
->pos
);
1996 for(i
=0;i
<e
->size
;++i
) {
1997 value_assign(res
->arr
[i
].d
,e
->arr
[i
].d
);
1998 if(value_zero_p(res
->arr
[i
].d
))
1999 res
->arr
[i
].x
.p
= ecopy(e
->arr
[i
].x
.p
);
2000 else if (EVALUE_IS_DOMAIN(res
->arr
[i
]))
2001 EVALUE_SET_DOMAIN(res
->arr
[i
], Domain_Copy(EVALUE_DOMAIN(e
->arr
[i
])));
2003 value_init(res
->arr
[i
].x
.n
);
2004 value_assign(res
->arr
[i
].x
.n
,e
->arr
[i
].x
.n
);
2010 int ecmp(const evalue
*e1
, const evalue
*e2
)
2016 if (value_notzero_p(e1
->d
) && value_notzero_p(e2
->d
)) {
2020 value_multiply(m
, e1
->x
.n
, e2
->d
);
2021 value_multiply(m2
, e2
->x
.n
, e1
->d
);
2023 if (value_lt(m
, m2
))
2025 else if (value_gt(m
, m2
))
2035 if (value_notzero_p(e1
->d
))
2037 if (value_notzero_p(e2
->d
))
2043 if (p1
->type
!= p2
->type
)
2044 return p1
->type
- p2
->type
;
2045 if (p1
->pos
!= p2
->pos
)
2046 return p1
->pos
- p2
->pos
;
2047 if (p1
->size
!= p2
->size
)
2048 return p1
->size
- p2
->size
;
2050 for (i
= p1
->size
-1; i
>= 0; --i
)
2051 if ((r
= ecmp(&p1
->arr
[i
], &p2
->arr
[i
])) != 0)
2057 int eequal(const evalue
*e1
, const evalue
*e2
)
2062 if (value_ne(e1
->d
,e2
->d
))
2065 if (EVALUE_IS_DOMAIN(*e1
))
2066 return PolyhedronIncludes(EVALUE_DOMAIN(*e2
), EVALUE_DOMAIN(*e1
)) &&
2067 PolyhedronIncludes(EVALUE_DOMAIN(*e1
), EVALUE_DOMAIN(*e2
));
2069 if (EVALUE_IS_NAN(*e1
))
2072 assert(value_posz_p(e1
->d
));
2074 /* e1->d == e2->d */
2075 if (value_notzero_p(e1
->d
)) {
2076 if (value_ne(e1
->x
.n
,e2
->x
.n
))
2079 /* e1->d == e2->d != 0 AND e1->n == e2->n */
2083 /* e1->d == e2->d == 0 */
2086 if (p1
->type
!= p2
->type
) return 0;
2087 if (p1
->size
!= p2
->size
) return 0;
2088 if (p1
->pos
!= p2
->pos
) return 0;
2089 for (i
=0; i
<p1
->size
; i
++)
2090 if (!eequal(&p1
->arr
[i
], &p2
->arr
[i
]) )
2095 void free_evalue_refs(evalue
*e
) {
2100 if (EVALUE_IS_NAN(*e
)) {
2105 if (EVALUE_IS_DOMAIN(*e
)) {
2106 Domain_Free(EVALUE_DOMAIN(*e
));
2109 } else if (value_pos_p(e
->d
)) {
2111 /* 'e' stores a constant */
2113 value_clear(e
->x
.n
);
2116 assert(value_zero_p(e
->d
));
2119 if (!p
) return; /* null pointer */
2120 for (i
=0; i
<p
->size
; i
++) {
2121 free_evalue_refs(&(p
->arr
[i
]));
2125 } /* free_evalue_refs */
2127 void evalue_free(evalue
*e
)
2129 free_evalue_refs(e
);
2133 static void mod2table_r(evalue
*e
, Vector
*periods
, Value m
, int p
,
2134 Vector
* val
, evalue
*res
)
2136 unsigned nparam
= periods
->Size
;
2139 double d
= compute_evalue(e
, val
->p
);
2140 d
*= VALUE_TO_DOUBLE(m
);
2145 value_assign(res
->d
, m
);
2146 value_init(res
->x
.n
);
2147 value_set_double(res
->x
.n
, d
);
2148 mpz_fdiv_r(res
->x
.n
, res
->x
.n
, m
);
2151 if (value_one_p(periods
->p
[p
]))
2152 mod2table_r(e
, periods
, m
, p
+1, val
, res
);
2157 value_assign(tmp
, periods
->p
[p
]);
2158 value_set_si(res
->d
, 0);
2159 res
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
2161 value_decrement(tmp
, tmp
);
2162 value_assign(val
->p
[p
], tmp
);
2163 mod2table_r(e
, periods
, m
, p
+1, val
,
2164 &res
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
2165 } while (value_pos_p(tmp
));
2171 static void rel2table(evalue
*e
, int zero
)
2173 if (value_pos_p(e
->d
)) {
2174 if (value_zero_p(e
->x
.n
) == zero
)
2175 value_set_si(e
->x
.n
, 1);
2177 value_set_si(e
->x
.n
, 0);
2178 value_set_si(e
->d
, 1);
2181 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
2182 rel2table(&e
->x
.p
->arr
[i
], zero
);
2186 void evalue_mod2table(evalue
*e
, int nparam
)
2191 if (EVALUE_IS_DOMAIN(*e
) || value_pos_p(e
->d
))
2194 for (i
=0; i
<p
->size
; i
++) {
2195 evalue_mod2table(&(p
->arr
[i
]), nparam
);
2197 if (p
->type
== relation
) {
2202 evalue_copy(©
, &p
->arr
[0]);
2204 rel2table(&p
->arr
[0], 1);
2205 emul(&p
->arr
[0], &p
->arr
[1]);
2207 rel2table(©
, 0);
2208 emul(©
, &p
->arr
[2]);
2209 eadd(&p
->arr
[2], &p
->arr
[1]);
2210 free_evalue_refs(&p
->arr
[2]);
2211 free_evalue_refs(©
);
2213 free_evalue_refs(&p
->arr
[0]);
2217 } else if (p
->type
== fractional
) {
2218 Vector
*periods
= Vector_Alloc(nparam
);
2219 Vector
*val
= Vector_Alloc(nparam
);
2225 value_set_si(tmp
, 1);
2226 Vector_Set(periods
->p
, 1, nparam
);
2227 Vector_Set(val
->p
, 0, nparam
);
2228 for (ev
= &p
->arr
[0]; value_zero_p(ev
->d
); ev
= &ev
->x
.p
->arr
[0]) {
2231 assert(p
->type
== polynomial
);
2232 assert(p
->size
== 2);
2233 value_assign(periods
->p
[p
->pos
-1], p
->arr
[1].d
);
2234 value_lcm(tmp
, tmp
, p
->arr
[1].d
);
2236 value_lcm(tmp
, tmp
, ev
->d
);
2238 mod2table_r(&p
->arr
[0], periods
, tmp
, 0, val
, &EP
);
2241 evalue_set_si(&res
, 0, 1);
2242 /* Compute the polynomial using Horner's rule */
2243 for (i
=p
->size
-1;i
>1;i
--) {
2244 eadd(&p
->arr
[i
], &res
);
2247 eadd(&p
->arr
[1], &res
);
2249 free_evalue_refs(e
);
2250 free_evalue_refs(&EP
);
2255 Vector_Free(periods
);
2257 } /* evalue_mod2table */
2259 /********************************************************/
2260 /* function in domain */
2261 /* check if the parameters in list_args */
2262 /* verifies the constraints of Domain P */
2263 /********************************************************/
2264 int in_domain(Polyhedron
*P
, Value
*list_args
)
2267 Value v
; /* value of the constraint of a row when
2268 parameters are instantiated*/
2270 if (P
->Dimension
== 0)
2275 for (row
= 0; row
< P
->NbConstraints
; row
++) {
2276 Inner_Product(P
->Constraint
[row
]+1, list_args
, P
->Dimension
, &v
);
2277 value_addto(v
, v
, P
->Constraint
[row
][P
->Dimension
+1]); /*constant part*/
2278 if (value_neg_p(v
) ||
2279 (value_zero_p(P
->Constraint
[row
][0]) && value_notzero_p(v
))) {
2286 return in
|| (P
->next
&& in_domain(P
->next
, list_args
));
2289 /****************************************************/
2290 /* function compute enode */
2291 /* compute the value of enode p with parameters */
2292 /* list "list_args */
2293 /* compute the polynomial or the periodic */
2294 /****************************************************/
2296 static double compute_enode(enode
*p
, Value
*list_args
) {
2308 if (p
->type
== polynomial
) {
2310 value_assign(param
,list_args
[p
->pos
-1]);
2312 /* Compute the polynomial using Horner's rule */
2313 for (i
=p
->size
-1;i
>0;i
--) {
2314 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2315 res
*=VALUE_TO_DOUBLE(param
);
2317 res
+=compute_evalue(&p
->arr
[0],list_args
);
2319 else if (p
->type
== fractional
) {
2320 double d
= compute_evalue(&p
->arr
[0], list_args
);
2321 d
-= floor(d
+1e-10);
2323 /* Compute the polynomial using Horner's rule */
2324 for (i
=p
->size
-1;i
>1;i
--) {
2325 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2328 res
+=compute_evalue(&p
->arr
[1],list_args
);
2330 else if (p
->type
== flooring
) {
2331 double d
= compute_evalue(&p
->arr
[0], list_args
);
2334 /* Compute the polynomial using Horner's rule */
2335 for (i
=p
->size
-1;i
>1;i
--) {
2336 res
+=compute_evalue(&p
->arr
[i
],list_args
);
2339 res
+=compute_evalue(&p
->arr
[1],list_args
);
2341 else if (p
->type
== periodic
) {
2342 value_assign(m
,list_args
[p
->pos
-1]);
2344 /* Choose the right element of the periodic */
2345 value_set_si(param
,p
->size
);
2346 value_pmodulus(m
,m
,param
);
2347 res
= compute_evalue(&p
->arr
[VALUE_TO_INT(m
)],list_args
);
2349 else if (p
->type
== relation
) {
2350 if (fabs(compute_evalue(&p
->arr
[0], list_args
)) < 1e-10)
2351 res
= compute_evalue(&p
->arr
[1], list_args
);
2352 else if (p
->size
> 2)
2353 res
= compute_evalue(&p
->arr
[2], list_args
);
2355 else if (p
->type
== partition
) {
2356 int dim
= EVALUE_DOMAIN(p
->arr
[0])->Dimension
;
2357 Value
*vals
= list_args
;
2360 for (i
= 0; i
< dim
; ++i
) {
2361 value_init(vals
[i
]);
2363 value_assign(vals
[i
], list_args
[i
]);
2366 for (i
= 0; i
< p
->size
/2; ++i
)
2367 if (DomainContains(EVALUE_DOMAIN(p
->arr
[2*i
]), vals
, p
->pos
, 0, 1)) {
2368 res
= compute_evalue(&p
->arr
[2*i
+1], vals
);
2372 for (i
= 0; i
< dim
; ++i
)
2373 value_clear(vals
[i
]);
2382 } /* compute_enode */
2384 /*************************************************/
2385 /* return the value of Ehrhart Polynomial */
2386 /* It returns a double, because since it is */
2387 /* a recursive function, some intermediate value */
2388 /* might not be integral */
2389 /*************************************************/
2391 double compute_evalue(const evalue
*e
, Value
*list_args
)
2395 if (value_notzero_p(e
->d
)) {
2396 if (value_notone_p(e
->d
))
2397 res
= VALUE_TO_DOUBLE(e
->x
.n
) / VALUE_TO_DOUBLE(e
->d
);
2399 res
= VALUE_TO_DOUBLE(e
->x
.n
);
2402 res
= compute_enode(e
->x
.p
,list_args
);
2404 } /* compute_evalue */
2407 /****************************************************/
2408 /* function compute_poly : */
2409 /* Check for the good validity domain */
2410 /* return the number of point in the Polyhedron */
2411 /* in allocated memory */
2412 /* Using the Ehrhart pseudo-polynomial */
2413 /****************************************************/
2414 Value
*compute_poly(Enumeration
*en
,Value
*list_args
) {
2417 /* double d; int i; */
2419 tmp
= (Value
*) malloc (sizeof(Value
));
2420 assert(tmp
!= NULL
);
2422 value_set_si(*tmp
,0);
2425 return(tmp
); /* no ehrhart polynomial */
2426 if(en
->ValidityDomain
) {
2427 if(!en
->ValidityDomain
->Dimension
) { /* no parameters */
2428 value_set_double(*tmp
,compute_evalue(&en
->EP
,list_args
)+.25);
2433 return(tmp
); /* no Validity Domain */
2435 if(in_domain(en
->ValidityDomain
,list_args
)) {
2437 #ifdef EVAL_EHRHART_DEBUG
2438 Print_Domain(stdout
,en
->ValidityDomain
);
2439 print_evalue(stdout
,&en
->EP
);
2442 /* d = compute_evalue(&en->EP,list_args);
2444 printf("(double)%lf = %d\n", d, i ); */
2445 value_set_double(*tmp
,compute_evalue(&en
->EP
,list_args
)+.25);
2451 value_set_si(*tmp
,0);
2452 return(tmp
); /* no compatible domain with the arguments */
2453 } /* compute_poly */
2455 static evalue
*eval_polynomial(const enode
*p
, int offset
,
2456 evalue
*base
, Value
*values
)
2461 res
= evalue_zero();
2462 for (i
= p
->size
-1; i
> offset
; --i
) {
2463 c
= evalue_eval(&p
->arr
[i
], values
);
2468 c
= evalue_eval(&p
->arr
[offset
], values
);
2475 evalue
*evalue_eval(const evalue
*e
, Value
*values
)
2482 if (value_notzero_p(e
->d
)) {
2483 res
= ALLOC(evalue
);
2485 evalue_copy(res
, e
);
2488 switch (e
->x
.p
->type
) {
2490 value_init(param
.x
.n
);
2491 value_assign(param
.x
.n
, values
[e
->x
.p
->pos
-1]);
2492 value_init(param
.d
);
2493 value_set_si(param
.d
, 1);
2495 res
= eval_polynomial(e
->x
.p
, 0, ¶m
, values
);
2496 free_evalue_refs(¶m
);
2499 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2500 mpz_fdiv_r(param2
->x
.n
, param2
->x
.n
, param2
->d
);
2502 res
= eval_polynomial(e
->x
.p
, 1, param2
, values
);
2503 evalue_free(param2
);
2506 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2507 mpz_fdiv_q(param2
->x
.n
, param2
->x
.n
, param2
->d
);
2508 value_set_si(param2
->d
, 1);
2510 res
= eval_polynomial(e
->x
.p
, 1, param2
, values
);
2511 evalue_free(param2
);
2514 param2
= evalue_eval(&e
->x
.p
->arr
[0], values
);
2515 if (value_zero_p(param2
->x
.n
))
2516 res
= evalue_eval(&e
->x
.p
->arr
[1], values
);
2517 else if (e
->x
.p
->size
> 2)
2518 res
= evalue_eval(&e
->x
.p
->arr
[2], values
);
2520 res
= evalue_zero();
2521 evalue_free(param2
);
2524 assert(e
->x
.p
->pos
== EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
);
2525 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
2526 if (in_domain(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), values
)) {
2527 res
= evalue_eval(&e
->x
.p
->arr
[2*i
+1], values
);
2531 res
= evalue_zero();
2539 size_t value_size(Value v
) {
2540 return (v
[0]._mp_size
> 0 ? v
[0]._mp_size
: -v
[0]._mp_size
)
2541 * sizeof(v
[0]._mp_d
[0]);
2544 size_t domain_size(Polyhedron
*D
)
2547 size_t s
= sizeof(*D
);
2549 for (i
= 0; i
< D
->NbConstraints
; ++i
)
2550 for (j
= 0; j
< D
->Dimension
+2; ++j
)
2551 s
+= value_size(D
->Constraint
[i
][j
]);
2554 for (i = 0; i < D->NbRays; ++i)
2555 for (j = 0; j < D->Dimension+2; ++j)
2556 s += value_size(D->Ray[i][j]);
2559 return D
->next
? s
+domain_size(D
->next
) : s
;
2562 size_t enode_size(enode
*p
) {
2563 size_t s
= sizeof(*p
) - sizeof(p
->arr
[0]);
2566 if (p
->type
== partition
)
2567 for (i
= 0; i
< p
->size
/2; ++i
) {
2568 s
+= domain_size(EVALUE_DOMAIN(p
->arr
[2*i
]));
2569 s
+= evalue_size(&p
->arr
[2*i
+1]);
2572 for (i
= 0; i
< p
->size
; ++i
) {
2573 s
+= evalue_size(&p
->arr
[i
]);
2578 size_t evalue_size(evalue
*e
)
2580 size_t s
= sizeof(*e
);
2581 s
+= value_size(e
->d
);
2582 if (value_notzero_p(e
->d
))
2583 s
+= value_size(e
->x
.n
);
2585 s
+= enode_size(e
->x
.p
);
2589 static evalue
*find_second(evalue
*base
, evalue
*cst
, evalue
*e
, Value m
)
2591 evalue
*found
= NULL
;
2596 if (value_pos_p(e
->d
) || e
->x
.p
->type
!= fractional
)
2599 value_init(offset
.d
);
2600 value_init(offset
.x
.n
);
2601 poly_denom(&e
->x
.p
->arr
[0], &offset
.d
);
2602 value_lcm(offset
.d
, m
, offset
.d
);
2603 value_set_si(offset
.x
.n
, 1);
2606 evalue_copy(©
, cst
);
2609 mpz_fdiv_r(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2611 if (eequal(base
, &e
->x
.p
->arr
[0]))
2612 found
= &e
->x
.p
->arr
[0];
2614 value_set_si(offset
.x
.n
, -2);
2617 mpz_fdiv_r(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2619 if (eequal(base
, &e
->x
.p
->arr
[0]))
2622 free_evalue_refs(cst
);
2623 free_evalue_refs(&offset
);
2626 for (i
= 1; !found
&& i
< e
->x
.p
->size
; ++i
)
2627 found
= find_second(base
, cst
, &e
->x
.p
->arr
[i
], m
);
2632 static evalue
*find_relation_pair(evalue
*e
)
2635 evalue
*found
= NULL
;
2637 if (EVALUE_IS_DOMAIN(*e
) || value_pos_p(e
->d
))
2640 if (e
->x
.p
->type
== fractional
) {
2645 poly_denom(&e
->x
.p
->arr
[0], &m
);
2647 for (cst
= &e
->x
.p
->arr
[0]; value_zero_p(cst
->d
);
2648 cst
= &cst
->x
.p
->arr
[0])
2651 for (i
= 1; !found
&& i
< e
->x
.p
->size
; ++i
)
2652 found
= find_second(&e
->x
.p
->arr
[0], cst
, &e
->x
.p
->arr
[i
], m
);
2657 i
= e
->x
.p
->type
== relation
;
2658 for (; !found
&& i
< e
->x
.p
->size
; ++i
)
2659 found
= find_relation_pair(&e
->x
.p
->arr
[i
]);
2664 void evalue_mod2relation(evalue
*e
) {
2667 if (value_zero_p(e
->d
) && e
->x
.p
->type
== partition
) {
2670 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2671 evalue_mod2relation(&e
->x
.p
->arr
[2*i
+1]);
2672 if (EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
2673 value_clear(e
->x
.p
->arr
[2*i
].d
);
2674 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
2676 if (2*i
< e
->x
.p
->size
) {
2677 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
2678 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
2683 if (e
->x
.p
->size
== 0) {
2685 evalue_set_si(e
, 0, 1);
2691 while ((d
= find_relation_pair(e
)) != NULL
) {
2695 value_init(split
.d
);
2696 value_set_si(split
.d
, 0);
2697 split
.x
.p
= new_enode(relation
, 3, 0);
2698 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2699 evalue_set_si(&split
.x
.p
->arr
[2], 1, 1);
2701 ev
= &split
.x
.p
->arr
[0];
2702 value_set_si(ev
->d
, 0);
2703 ev
->x
.p
= new_enode(fractional
, 3, -1);
2704 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
2705 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
2706 evalue_copy(&ev
->x
.p
->arr
[0], d
);
2712 free_evalue_refs(&split
);
2716 static int evalue_comp(const void * a
, const void * b
)
2718 const evalue
*e1
= *(const evalue
**)a
;
2719 const evalue
*e2
= *(const evalue
**)b
;
2720 return ecmp(e1
, e2
);
2723 void evalue_combine(evalue
*e
)
2730 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
2733 NALLOC(evs
, e
->x
.p
->size
/2);
2734 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
2735 evs
[i
] = &e
->x
.p
->arr
[2*i
+1];
2736 qsort(evs
, e
->x
.p
->size
/2, sizeof(evs
[0]), evalue_comp
);
2737 p
= new_enode(partition
, e
->x
.p
->size
, e
->x
.p
->pos
);
2738 for (i
= 0, k
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2739 if (k
== 0 || ecmp(&p
->arr
[2*k
-1], evs
[i
]) != 0) {
2740 value_clear(p
->arr
[2*k
].d
);
2741 value_clear(p
->arr
[2*k
+1].d
);
2742 p
->arr
[2*k
] = *(evs
[i
]-1);
2743 p
->arr
[2*k
+1] = *(evs
[i
]);
2746 Polyhedron
*D
= EVALUE_DOMAIN(*(evs
[i
]-1));
2749 value_clear((evs
[i
]-1)->d
);
2753 L
->next
= EVALUE_DOMAIN(p
->arr
[2*k
-2]);
2754 EVALUE_SET_DOMAIN(p
->arr
[2*k
-2], D
);
2755 free_evalue_refs(evs
[i
]);
2759 for (i
= 2*k
; i
< p
->size
; ++i
)
2760 value_clear(p
->arr
[i
].d
);
2767 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2769 if (value_notzero_p(e
->x
.p
->arr
[2*i
+1].d
))
2771 H
= DomainConvex(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), 0);
2774 for (k
= 0; k
< e
->x
.p
->size
/2; ++k
) {
2775 Polyhedron
*D
, *N
, **P
;
2778 P
= &EVALUE_DOMAIN(e
->x
.p
->arr
[2*k
]);
2785 if (D
->NbEq
<= H
->NbEq
) {
2791 tmp
.x
.p
= new_enode(partition
, 2, e
->x
.p
->pos
);
2792 EVALUE_SET_DOMAIN(tmp
.x
.p
->arr
[0], Polyhedron_Copy(D
));
2793 evalue_copy(&tmp
.x
.p
->arr
[1], &e
->x
.p
->arr
[2*i
+1]);
2794 reduce_evalue(&tmp
);
2795 if (value_notzero_p(tmp
.d
) ||
2796 ecmp(&tmp
.x
.p
->arr
[1], &e
->x
.p
->arr
[2*k
+1]) != 0)
2799 D
->next
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
2800 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]) = D
;
2803 free_evalue_refs(&tmp
);
2809 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
2811 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
2813 value_clear(e
->x
.p
->arr
[2*i
].d
);
2814 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
2816 if (2*i
< e
->x
.p
->size
) {
2817 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
2818 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
2825 H
= DomainConvex(D
, 0);
2826 E
= DomainDifference(H
, D
, 0);
2828 D
= DomainDifference(H
, E
, 0);
2831 EVALUE_SET_DOMAIN(p
->arr
[2*i
], D
);
2835 /* Use smallest representative for coefficients in affine form in
2836 * argument of fractional.
2837 * Since any change will make the argument non-standard,
2838 * the containing evalue will have to be reduced again afterward.
2840 static void fractional_minimal_coefficients(enode
*p
)
2846 assert(p
->type
== fractional
);
2848 while (value_zero_p(pp
->d
)) {
2849 assert(pp
->x
.p
->type
== polynomial
);
2850 assert(pp
->x
.p
->size
== 2);
2851 assert(value_notzero_p(pp
->x
.p
->arr
[1].d
));
2852 mpz_mul_ui(twice
, pp
->x
.p
->arr
[1].x
.n
, 2);
2853 if (value_gt(twice
, pp
->x
.p
->arr
[1].d
))
2854 value_subtract(pp
->x
.p
->arr
[1].x
.n
,
2855 pp
->x
.p
->arr
[1].x
.n
, pp
->x
.p
->arr
[1].d
);
2856 pp
= &pp
->x
.p
->arr
[0];
2862 static Polyhedron
*polynomial_projection(enode
*p
, Polyhedron
*D
, Value
*d
,
2867 unsigned dim
= D
->Dimension
;
2868 Matrix
*T
= Matrix_Alloc(2, dim
+1);
2871 assert(p
->type
== fractional
|| p
->type
== flooring
);
2872 value_set_si(T
->p
[1][dim
], 1);
2873 evalue_extract_affine(&p
->arr
[0], T
->p
[0], &T
->p
[0][dim
], d
);
2874 I
= DomainImage(D
, T
, 0);
2875 H
= DomainConvex(I
, 0);
2885 static void replace_by_affine(evalue
*e
, Value offset
)
2892 value_init(inc
.x
.n
);
2893 value_set_si(inc
.d
, 1);
2894 value_oppose(inc
.x
.n
, offset
);
2895 eadd(&inc
, &p
->arr
[0]);
2896 reorder_terms_about(p
, &p
->arr
[0]); /* frees arr[0] */
2900 free_evalue_refs(&inc
);
2903 int evalue_range_reduction_in_domain(evalue
*e
, Polyhedron
*D
)
2912 if (value_notzero_p(e
->d
))
2917 if (p
->type
== relation
) {
2924 fractional_minimal_coefficients(p
->arr
[0].x
.p
);
2925 I
= polynomial_projection(p
->arr
[0].x
.p
, D
, &d
, &T
);
2926 bounded
= line_minmax(I
, &min
, &max
); /* frees I */
2927 equal
= value_eq(min
, max
);
2928 mpz_cdiv_q(min
, min
, d
);
2929 mpz_fdiv_q(max
, max
, d
);
2931 if (bounded
&& value_gt(min
, max
)) {
2937 evalue_set_si(e
, 0, 1);
2940 free_evalue_refs(&(p
->arr
[1]));
2941 free_evalue_refs(&(p
->arr
[0]));
2947 return r
? r
: evalue_range_reduction_in_domain(e
, D
);
2948 } else if (bounded
&& equal
) {
2951 free_evalue_refs(&(p
->arr
[2]));
2954 free_evalue_refs(&(p
->arr
[0]));
2960 return evalue_range_reduction_in_domain(e
, D
);
2961 } else if (bounded
&& value_eq(min
, max
)) {
2962 /* zero for a single value */
2964 Matrix
*M
= Matrix_Alloc(1, D
->Dimension
+2);
2965 Vector_Copy(T
->p
[0], M
->p
[0]+1, D
->Dimension
+1);
2966 value_multiply(min
, min
, d
);
2967 value_subtract(M
->p
[0][D
->Dimension
+1],
2968 M
->p
[0][D
->Dimension
+1], min
);
2969 E
= DomainAddConstraints(D
, M
, 0);
2975 r
= evalue_range_reduction_in_domain(&p
->arr
[1], E
);
2977 r
|= evalue_range_reduction_in_domain(&p
->arr
[2], D
);
2979 _reduce_evalue(&p
->arr
[0].x
.p
->arr
[0], 0, 1);
2987 _reduce_evalue(&p
->arr
[0].x
.p
->arr
[0], 0, 1);
2990 i
= p
->type
== relation
? 1 :
2991 p
->type
== fractional
? 1 : 0;
2992 for (; i
<p
->size
; i
++)
2993 r
|= evalue_range_reduction_in_domain(&p
->arr
[i
], D
);
2995 if (p
->type
!= fractional
) {
2996 if (r
&& p
->type
== polynomial
) {
2999 value_set_si(f
.d
, 0);
3000 f
.x
.p
= new_enode(polynomial
, 2, p
->pos
);
3001 evalue_set_si(&f
.x
.p
->arr
[0], 0, 1);
3002 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
3003 reorder_terms_about(p
, &f
);
3014 fractional_minimal_coefficients(p
);
3015 I
= polynomial_projection(p
, D
, &d
, NULL
);
3016 bounded
= line_minmax(I
, &min
, &max
); /* frees I */
3017 mpz_fdiv_q(min
, min
, d
);
3018 mpz_fdiv_q(max
, max
, d
);
3019 value_subtract(d
, max
, min
);
3021 if (bounded
&& value_eq(min
, max
)) {
3022 replace_by_affine(e
, min
);
3024 } else if (bounded
&& value_one_p(d
) && p
->size
> 3) {
3025 /* replace {g}^2 by -(g-min)^2 + (2{g}+1)*(g-min) - {g}
3026 * See pages 199-200 of PhD thesis.
3034 value_set_si(rem
.d
, 0);
3035 rem
.x
.p
= new_enode(fractional
, 3, -1);
3036 evalue_copy(&rem
.x
.p
->arr
[0], &p
->arr
[0]);
3037 value_clear(rem
.x
.p
->arr
[1].d
);
3038 value_clear(rem
.x
.p
->arr
[2].d
);
3039 rem
.x
.p
->arr
[1] = p
->arr
[1];
3040 rem
.x
.p
->arr
[2] = p
->arr
[2];
3041 for (i
= 3; i
< p
->size
; ++i
)
3042 p
->arr
[i
-2] = p
->arr
[i
];
3046 value_init(inc
.x
.n
);
3047 value_set_si(inc
.d
, 1);
3048 value_oppose(inc
.x
.n
, min
);
3051 evalue_copy(&t
, &p
->arr
[0]);
3055 value_set_si(f
.d
, 0);
3056 f
.x
.p
= new_enode(fractional
, 3, -1);
3057 evalue_copy(&f
.x
.p
->arr
[0], &p
->arr
[0]);
3058 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
3059 evalue_set_si(&f
.x
.p
->arr
[2], 2, 1);
3061 value_init(factor
.d
);
3062 evalue_set_si(&factor
, -1, 1);
3068 value_clear(f
.x
.p
->arr
[1].x
.n
);
3069 value_clear(f
.x
.p
->arr
[2].x
.n
);
3070 evalue_set_si(&f
.x
.p
->arr
[1], 0, 1);
3071 evalue_set_si(&f
.x
.p
->arr
[2], -1, 1);
3075 evalue_reorder_terms(&rem
);
3076 evalue_reorder_terms(e
);
3082 free_evalue_refs(&inc
);
3083 free_evalue_refs(&t
);
3084 free_evalue_refs(&f
);
3085 free_evalue_refs(&factor
);
3086 free_evalue_refs(&rem
);
3088 evalue_range_reduction_in_domain(e
, D
);
3092 _reduce_evalue(&p
->arr
[0], 0, 1);
3094 evalue_reorder_terms(e
);
3104 void evalue_range_reduction(evalue
*e
)
3107 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
)
3110 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3111 if (evalue_range_reduction_in_domain(&e
->x
.p
->arr
[2*i
+1],
3112 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]))) {
3113 reduce_evalue(&e
->x
.p
->arr
[2*i
+1]);
3115 if (EVALUE_IS_ZERO(e
->x
.p
->arr
[2*i
+1])) {
3116 free_evalue_refs(&e
->x
.p
->arr
[2*i
+1]);
3117 Domain_Free(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]));
3118 value_clear(e
->x
.p
->arr
[2*i
].d
);
3120 e
->x
.p
->arr
[2*i
] = e
->x
.p
->arr
[e
->x
.p
->size
];
3121 e
->x
.p
->arr
[2*i
+1] = e
->x
.p
->arr
[e
->x
.p
->size
+1];
3129 Enumeration
* partition2enumeration(evalue
*EP
)
3132 Enumeration
*en
, *res
= NULL
;
3134 if (EVALUE_IS_ZERO(*EP
)) {
3139 assert(value_zero_p(EP
->d
));
3140 assert(EP
->x
.p
->type
== partition
);
3141 assert(EP
->x
.p
->size
>= 2);
3143 for (i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
3144 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
])->Dimension
);
3145 en
= (Enumeration
*)malloc(sizeof(Enumeration
));
3148 res
->ValidityDomain
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
3149 value_clear(EP
->x
.p
->arr
[2*i
].d
);
3150 res
->EP
= EP
->x
.p
->arr
[2*i
+1];
3158 int evalue_frac2floor_in_domain3(evalue
*e
, Polyhedron
*D
, int shift
)
3167 if (value_notzero_p(e
->d
))
3172 i
= p
->type
== relation
? 1 :
3173 p
->type
== fractional
? 1 : 0;
3174 for (; i
<p
->size
; i
++)
3175 r
|= evalue_frac2floor_in_domain3(&p
->arr
[i
], D
, shift
);
3177 if (p
->type
!= fractional
) {
3178 if (r
&& p
->type
== polynomial
) {
3181 value_set_si(f
.d
, 0);
3182 f
.x
.p
= new_enode(polynomial
, 2, p
->pos
);
3183 evalue_set_si(&f
.x
.p
->arr
[0], 0, 1);
3184 evalue_set_si(&f
.x
.p
->arr
[1], 1, 1);
3185 reorder_terms_about(p
, &f
);
3195 I
= polynomial_projection(p
, D
, &d
, NULL
);
3198 Polyhedron_Print(stderr, P_VALUE_FMT, I);
3201 assert(I
->NbEq
== 0); /* Should have been reduced */
3204 for (i
= 0; i
< I
->NbConstraints
; ++i
)
3205 if (value_pos_p(I
->Constraint
[i
][1]))
3208 if (i
< I
->NbConstraints
) {
3210 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
3211 mpz_cdiv_q(min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
3212 if (value_neg_p(min
)) {
3214 mpz_fdiv_q(min
, min
, d
);
3215 value_init(offset
.d
);
3216 value_set_si(offset
.d
, 1);
3217 value_init(offset
.x
.n
);
3218 value_oppose(offset
.x
.n
, min
);
3219 eadd(&offset
, &p
->arr
[0]);
3220 free_evalue_refs(&offset
);
3230 value_set_si(fl
.d
, 0);
3231 fl
.x
.p
= new_enode(flooring
, 3, -1);
3232 evalue_set_si(&fl
.x
.p
->arr
[1], 0, 1);
3233 evalue_set_si(&fl
.x
.p
->arr
[2], -1, 1);
3234 evalue_copy(&fl
.x
.p
->arr
[0], &p
->arr
[0]);
3236 eadd(&fl
, &p
->arr
[0]);
3237 reorder_terms_about(p
, &p
->arr
[0]);
3241 free_evalue_refs(&fl
);
3246 int evalue_frac2floor_in_domain(evalue
*e
, Polyhedron
*D
)
3248 return evalue_frac2floor_in_domain3(e
, D
, 1);
3251 void evalue_frac2floor2(evalue
*e
, int shift
)
3254 if (value_notzero_p(e
->d
) || e
->x
.p
->type
!= partition
) {
3256 if (evalue_frac2floor_in_domain3(e
, NULL
, 0))
3262 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3263 if (evalue_frac2floor_in_domain3(&e
->x
.p
->arr
[2*i
+1],
3264 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), shift
))
3265 reduce_evalue(&e
->x
.p
->arr
[2*i
+1]);
3268 void evalue_frac2floor(evalue
*e
)
3270 evalue_frac2floor2(e
, 1);
3273 /* Add a new variable with lower bound 1 and upper bound specified
3274 * by row. If negative is true, then the new variable has upper
3275 * bound -1 and lower bound specified by row.
3277 static Matrix
*esum_add_constraint(int nvar
, Polyhedron
*D
, Matrix
*C
,
3278 Vector
*row
, int negative
)
3282 int nparam
= D
->Dimension
- nvar
;
3285 nr
= D
->NbConstraints
+ 2;
3286 nc
= D
->Dimension
+ 2 + 1;
3287 C
= Matrix_Alloc(nr
, nc
);
3288 for (i
= 0; i
< D
->NbConstraints
; ++i
) {
3289 Vector_Copy(D
->Constraint
[i
], C
->p
[i
], 1 + nvar
);
3290 Vector_Copy(D
->Constraint
[i
] + 1 + nvar
, C
->p
[i
] + 1 + nvar
+ 1,
3291 D
->Dimension
+ 1 - nvar
);
3296 nc
= C
->NbColumns
+ 1;
3297 C
= Matrix_Alloc(nr
, nc
);
3298 for (i
= 0; i
< oldC
->NbRows
; ++i
) {
3299 Vector_Copy(oldC
->p
[i
], C
->p
[i
], 1 + nvar
);
3300 Vector_Copy(oldC
->p
[i
] + 1 + nvar
, C
->p
[i
] + 1 + nvar
+ 1,
3301 oldC
->NbColumns
- 1 - nvar
);
3304 value_set_si(C
->p
[nr
-2][0], 1);
3306 value_set_si(C
->p
[nr
-2][1 + nvar
], -1);
3308 value_set_si(C
->p
[nr
-2][1 + nvar
], 1);
3309 value_set_si(C
->p
[nr
-2][nc
- 1], -1);
3311 Vector_Copy(row
->p
, C
->p
[nr
-1], 1 + nvar
+ 1);
3312 Vector_Copy(row
->p
+ 1 + nvar
+ 1, C
->p
[nr
-1] + C
->NbColumns
- 1 - nparam
,
3318 static void floor2frac_r(evalue
*e
, int nvar
)
3325 if (value_notzero_p(e
->d
))
3330 for (i
= type_offset(p
); i
< p
->size
; i
++)
3331 floor2frac_r(&p
->arr
[i
], nvar
);
3333 if (p
->type
!= flooring
)
3336 for (pp
= &p
->arr
[0]; value_zero_p(pp
->d
); pp
= &pp
->x
.p
->arr
[0]) {
3337 assert(pp
->x
.p
->type
== polynomial
);
3338 pp
->x
.p
->pos
-= nvar
;
3342 value_set_si(f
.d
, 0);
3343 f
.x
.p
= new_enode(fractional
, 3, -1);
3344 evalue_set_si(&f
.x
.p
->arr
[1], 0, 1);
3345 evalue_set_si(&f
.x
.p
->arr
[2], -1, 1);
3346 evalue_copy(&f
.x
.p
->arr
[0], &p
->arr
[0]);
3348 eadd(&f
, &p
->arr
[0]);
3349 reorder_terms_about(p
, &p
->arr
[0]);
3353 free_evalue_refs(&f
);
3356 /* Convert flooring back to fractional and shift position
3357 * of the parameters by nvar
3359 static void floor2frac(evalue
*e
, int nvar
)
3361 floor2frac_r(e
, nvar
);
3365 int evalue_floor2frac(evalue
*e
)
3370 if (value_notzero_p(e
->d
))
3373 if (e
->x
.p
->type
== partition
) {
3374 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3375 if (evalue_floor2frac(&e
->x
.p
->arr
[2*i
+1]))
3376 reduce_evalue(&e
->x
.p
->arr
[2*i
+1]);
3380 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
3381 r
|= evalue_floor2frac(&e
->x
.p
->arr
[i
]);
3383 if (e
->x
.p
->type
== flooring
) {
3389 evalue_reorder_terms(e
);
3394 evalue
*esum_over_domain_cst(int nvar
, Polyhedron
*D
, Matrix
*C
)
3397 int nparam
= D
->Dimension
- nvar
;
3401 D
= Constraints2Polyhedron(C
, 0);
3405 t
= barvinok_enumerate_e(D
, 0, nparam
, 0);
3407 /* Double check that D was not unbounded. */
3408 assert(!(value_pos_p(t
->d
) && value_neg_p(t
->x
.n
)));
3416 static void domain_signs(Polyhedron
*D
, int *signs
)
3420 POL_ENSURE_VERTICES(D
);
3421 for (j
= 0; j
< D
->Dimension
; ++j
) {
3423 for (k
= 0; k
< D
->NbRays
; ++k
) {
3424 signs
[j
] = value_sign(D
->Ray
[k
][1+j
]);
3431 static evalue
*esum_over_domain(evalue
*e
, int nvar
, Polyhedron
*D
,
3432 int *signs
, Matrix
*C
, unsigned MaxRays
)
3438 evalue
*factor
= NULL
;
3443 if (EVALUE_IS_ZERO(*e
))
3447 Polyhedron
*DD
= Disjoint_Domain(D
, 0, MaxRays
);
3454 res
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3457 for (Q
= DD
; Q
; Q
= DD
) {
3463 t
= esum_over_domain(e
, nvar
, Q
, signs
, C
, MaxRays
);
3476 if (value_notzero_p(e
->d
)) {
3479 t
= esum_over_domain_cst(nvar
, D
, C
);
3481 if (!EVALUE_IS_ONE(*e
))
3489 signs
= ALLOCN(int, D
->Dimension
);
3490 domain_signs(D
, signs
);
3493 switch (e
->x
.p
->type
) {
3495 evalue
*pp
= &e
->x
.p
->arr
[0];
3497 if (pp
->x
.p
->pos
> nvar
) {
3498 /* remainder is independent of the summated vars */
3504 floor2frac(&f
, nvar
);
3506 t
= esum_over_domain_cst(nvar
, D
, C
);
3510 free_evalue_refs(&f
);
3518 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3519 poly_denom(pp
, &row
->p
[1 + nvar
]);
3520 value_set_si(row
->p
[0], 1);
3521 for (pp
= &e
->x
.p
->arr
[0]; value_zero_p(pp
->d
);
3522 pp
= &pp
->x
.p
->arr
[0]) {
3524 assert(pp
->x
.p
->type
== polynomial
);
3526 if (pos
>= 1 + nvar
)
3528 value_assign(row
->p
[pos
], row
->p
[1+nvar
]);
3529 value_division(row
->p
[pos
], row
->p
[pos
], pp
->x
.p
->arr
[1].d
);
3530 value_multiply(row
->p
[pos
], row
->p
[pos
], pp
->x
.p
->arr
[1].x
.n
);
3532 value_assign(row
->p
[1 + D
->Dimension
+ 1], row
->p
[1+nvar
]);
3533 value_division(row
->p
[1 + D
->Dimension
+ 1],
3534 row
->p
[1 + D
->Dimension
+ 1],
3536 value_multiply(row
->p
[1 + D
->Dimension
+ 1],
3537 row
->p
[1 + D
->Dimension
+ 1],
3539 value_oppose(row
->p
[1 + nvar
], row
->p
[1 + nvar
]);
3543 int pos
= e
->x
.p
->pos
;
3546 factor
= ALLOC(evalue
);
3547 value_init(factor
->d
);
3548 value_set_si(factor
->d
, 0);
3549 factor
->x
.p
= new_enode(polynomial
, 2, pos
- nvar
);
3550 evalue_set_si(&factor
->x
.p
->arr
[0], 0, 1);
3551 evalue_set_si(&factor
->x
.p
->arr
[1], 1, 1);
3555 row
= Vector_Alloc(1 + D
->Dimension
+ 1 + 1);
3556 negative
= signs
[pos
-1] < 0;
3557 value_set_si(row
->p
[0], 1);
3559 value_set_si(row
->p
[pos
], -1);
3560 value_set_si(row
->p
[1 + nvar
], 1);
3562 value_set_si(row
->p
[pos
], 1);
3563 value_set_si(row
->p
[1 + nvar
], -1);
3571 offset
= type_offset(e
->x
.p
);
3573 res
= esum_over_domain(&e
->x
.p
->arr
[offset
], nvar
, D
, signs
, C
, MaxRays
);
3577 evalue_copy(&cum
, factor
);
3581 for (i
= 1; offset
+i
< e
->x
.p
->size
; ++i
) {
3585 C
= esum_add_constraint(nvar
, D
, C
, row
, negative
);
3591 Vector_Print(stderr, P_VALUE_FMT, row);
3593 Matrix_Print(stderr, P_VALUE_FMT, C);
3595 t
= esum_over_domain(&e
->x
.p
->arr
[offset
+i
], nvar
, D
, signs
, C
, MaxRays
);
3600 if (negative
&& (i
% 2))
3610 if (factor
&& offset
+i
+1 < e
->x
.p
->size
)
3617 free_evalue_refs(&cum
);
3618 evalue_free(factor
);
3632 static void Polyhedron_Insert(Polyhedron
***next
, Polyhedron
*Q
)
3642 static Polyhedron
*Polyhedron_Split_Into_Orthants(Polyhedron
*P
,
3647 Vector
*c
= Vector_Alloc(1 + P
->Dimension
+ 1);
3648 value_set_si(c
->p
[0], 1);
3650 if (P
->Dimension
== 0)
3651 return Polyhedron_Copy(P
);
3653 for (i
= 0; i
< P
->Dimension
; ++i
) {
3654 Polyhedron
*L
= NULL
;
3655 Polyhedron
**next
= &L
;
3658 for (I
= D
; I
; I
= I
->next
) {
3660 value_set_si(c
->p
[1+i
], 1);
3661 value_set_si(c
->p
[1+P
->Dimension
], 0);
3662 Q
= AddConstraints(c
->p
, 1, I
, MaxRays
);
3663 Polyhedron_Insert(&next
, Q
);
3664 value_set_si(c
->p
[1+i
], -1);
3665 value_set_si(c
->p
[1+P
->Dimension
], -1);
3666 Q
= AddConstraints(c
->p
, 1, I
, MaxRays
);
3667 Polyhedron_Insert(&next
, Q
);
3668 value_set_si(c
->p
[1+i
], 0);
3678 /* Make arguments of all floors non-negative */
3679 static void shift_floor_in_domain(evalue
*e
, Polyhedron
*D
)
3686 if (value_notzero_p(e
->d
))
3691 for (i
= type_offset(p
); i
< p
->size
; ++i
)
3692 shift_floor_in_domain(&p
->arr
[i
], D
);
3694 if (p
->type
!= flooring
)
3700 I
= polynomial_projection(p
, D
, &d
, NULL
);
3701 assert(I
->NbEq
== 0); /* Should have been reduced */
3703 for (i
= 0; i
< I
->NbConstraints
; ++i
)
3704 if (value_pos_p(I
->Constraint
[i
][1]))
3706 assert(i
< I
->NbConstraints
);
3707 if (i
< I
->NbConstraints
) {
3708 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
3709 mpz_fdiv_q(m
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
3710 if (value_neg_p(m
)) {
3711 /* replace [e] by [e-m]+m such that e-m >= 0 */
3716 value_set_si(f
.d
, 1);
3717 value_oppose(f
.x
.n
, m
);
3718 eadd(&f
, &p
->arr
[0]);
3721 value_set_si(f
.d
, 0);
3722 f
.x
.p
= new_enode(flooring
, 3, -1);
3723 value_clear(f
.x
.p
->arr
[0].d
);
3724 f
.x
.p
->arr
[0] = p
->arr
[0];
3725 evalue_set_si(&f
.x
.p
->arr
[2], 1, 1);
3726 value_set_si(f
.x
.p
->arr
[1].d
, 1);
3727 value_init(f
.x
.p
->arr
[1].x
.n
);
3728 value_assign(f
.x
.p
->arr
[1].x
.n
, m
);
3729 reorder_terms_about(p
, &f
);
3740 evalue
*box_summate(Polyhedron
*P
, evalue
*E
, unsigned nvar
, unsigned MaxRays
)
3742 evalue
*sum
= evalue_zero();
3743 Polyhedron
*D
= Polyhedron_Split_Into_Orthants(P
, MaxRays
);
3744 for (P
= D
; P
; P
= P
->next
) {
3746 evalue
*fe
= evalue_dup(E
);
3747 Polyhedron
*next
= P
->next
;
3749 reduce_evalue_in_domain(fe
, P
);
3750 evalue_frac2floor2(fe
, 0);
3751 shift_floor_in_domain(fe
, P
);
3752 t
= esum_over_domain(fe
, nvar
, P
, NULL
, NULL
, MaxRays
);
3764 /* Initial silly implementation */
3765 void eor(evalue
*e1
, evalue
*res
)
3771 evalue_set_si(&mone
, -1, 1);
3773 evalue_copy(&E
, res
);
3779 free_evalue_refs(&E
);
3780 free_evalue_refs(&mone
);
3783 /* computes denominator of polynomial evalue
3784 * d should point to a value initialized to 1
3786 void evalue_denom(const evalue
*e
, Value
*d
)
3790 if (value_notzero_p(e
->d
)) {
3791 value_lcm(*d
, *d
, e
->d
);
3794 assert(e
->x
.p
->type
== polynomial
||
3795 e
->x
.p
->type
== fractional
||
3796 e
->x
.p
->type
== flooring
);
3797 offset
= type_offset(e
->x
.p
);
3798 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3799 evalue_denom(&e
->x
.p
->arr
[i
], d
);
3802 /* Divides the evalue e by the integer n */
3803 void evalue_div(evalue
*e
, Value n
)
3807 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3810 if (value_notzero_p(e
->d
)) {
3813 value_multiply(e
->d
, e
->d
, n
);
3814 value_gcd(gc
, e
->x
.n
, e
->d
);
3815 if (value_notone_p(gc
)) {
3816 value_division(e
->d
, e
->d
, gc
);
3817 value_division(e
->x
.n
, e
->x
.n
, gc
);
3822 if (e
->x
.p
->type
== partition
) {
3823 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3824 evalue_div(&e
->x
.p
->arr
[2*i
+1], n
);
3827 offset
= type_offset(e
->x
.p
);
3828 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3829 evalue_div(&e
->x
.p
->arr
[i
], n
);
3832 /* Multiplies the evalue e by the integer n */
3833 void evalue_mul(evalue
*e
, Value n
)
3837 if (value_one_p(n
) || EVALUE_IS_ZERO(*e
))
3840 if (value_notzero_p(e
->d
)) {
3843 value_multiply(e
->x
.n
, e
->x
.n
, n
);
3844 value_gcd(gc
, e
->x
.n
, e
->d
);
3845 if (value_notone_p(gc
)) {
3846 value_division(e
->d
, e
->d
, gc
);
3847 value_division(e
->x
.n
, e
->x
.n
, gc
);
3852 if (e
->x
.p
->type
== partition
) {
3853 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3854 evalue_mul(&e
->x
.p
->arr
[2*i
+1], n
);
3857 offset
= type_offset(e
->x
.p
);
3858 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3859 evalue_mul(&e
->x
.p
->arr
[i
], n
);
3862 /* Multiplies the evalue e by the n/d */
3863 void evalue_mul_div(evalue
*e
, Value n
, Value d
)
3867 if ((value_one_p(n
) && value_one_p(d
)) || EVALUE_IS_ZERO(*e
))
3870 if (value_notzero_p(e
->d
)) {
3873 value_multiply(e
->x
.n
, e
->x
.n
, n
);
3874 value_multiply(e
->d
, e
->d
, d
);
3875 value_gcd(gc
, e
->x
.n
, e
->d
);
3876 if (value_notone_p(gc
)) {
3877 value_division(e
->d
, e
->d
, gc
);
3878 value_division(e
->x
.n
, e
->x
.n
, gc
);
3883 if (e
->x
.p
->type
== partition
) {
3884 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3885 evalue_mul_div(&e
->x
.p
->arr
[2*i
+1], n
, d
);
3888 offset
= type_offset(e
->x
.p
);
3889 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3890 evalue_mul_div(&e
->x
.p
->arr
[i
], n
, d
);
3893 void evalue_negate(evalue
*e
)
3897 if (value_notzero_p(e
->d
)) {
3898 value_oppose(e
->x
.n
, e
->x
.n
);
3901 if (e
->x
.p
->type
== partition
) {
3902 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3903 evalue_negate(&e
->x
.p
->arr
[2*i
+1]);
3906 offset
= type_offset(e
->x
.p
);
3907 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
3908 evalue_negate(&e
->x
.p
->arr
[i
]);
3911 void evalue_add_constant(evalue
*e
, const Value cst
)
3915 if (value_zero_p(e
->d
)) {
3916 if (e
->x
.p
->type
== partition
) {
3917 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
3918 evalue_add_constant(&e
->x
.p
->arr
[2*i
+1], cst
);
3921 if (e
->x
.p
->type
== relation
) {
3922 for (i
= 1; i
< e
->x
.p
->size
; ++i
)
3923 evalue_add_constant(&e
->x
.p
->arr
[i
], cst
);
3927 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)];
3928 } while (value_zero_p(e
->d
));
3930 value_addmul(e
->x
.n
, cst
, e
->d
);
3933 static void evalue_frac2polynomial_r(evalue
*e
, int *signs
, int sign
, int in_frac
)
3938 int sign_odd
= sign
;
3940 if (value_notzero_p(e
->d
)) {
3941 if (in_frac
&& sign
* value_sign(e
->x
.n
) < 0) {
3942 value_set_si(e
->x
.n
, 0);
3943 value_set_si(e
->d
, 1);
3948 if (e
->x
.p
->type
== relation
) {
3949 for (i
= e
->x
.p
->size
-1; i
>= 1; --i
)
3950 evalue_frac2polynomial_r(&e
->x
.p
->arr
[i
], signs
, sign
, in_frac
);
3954 if (e
->x
.p
->type
== polynomial
)
3955 sign_odd
*= signs
[e
->x
.p
->pos
-1];
3956 offset
= type_offset(e
->x
.p
);
3957 evalue_frac2polynomial_r(&e
->x
.p
->arr
[offset
], signs
, sign
, in_frac
);
3958 in_frac
|= e
->x
.p
->type
== fractional
;
3959 for (i
= e
->x
.p
->size
-1; i
> offset
; --i
)
3960 evalue_frac2polynomial_r(&e
->x
.p
->arr
[i
], signs
,
3961 (i
- offset
) % 2 ? sign_odd
: sign
, in_frac
);
3963 if (e
->x
.p
->type
!= fractional
)
3966 /* replace { a/m } by (m-1)/m if sign != 0
3967 * and by (m-1)/(2m) if sign == 0
3971 evalue_denom(&e
->x
.p
->arr
[0], &d
);
3972 free_evalue_refs(&e
->x
.p
->arr
[0]);
3973 value_init(e
->x
.p
->arr
[0].d
);
3974 value_init(e
->x
.p
->arr
[0].x
.n
);
3976 value_addto(e
->x
.p
->arr
[0].d
, d
, d
);
3978 value_assign(e
->x
.p
->arr
[0].d
, d
);
3979 value_decrement(e
->x
.p
->arr
[0].x
.n
, d
);
3983 reorder_terms_about(p
, &p
->arr
[0]);
3989 /* Approximate the evalue in fractional representation by a polynomial.
3990 * If sign > 0, the result is an upper bound;
3991 * if sign < 0, the result is a lower bound;
3992 * if sign = 0, the result is an intermediate approximation.
3994 void evalue_frac2polynomial(evalue
*e
, int sign
, unsigned MaxRays
)
3999 if (value_notzero_p(e
->d
))
4001 assert(e
->x
.p
->type
== partition
);
4002 /* make sure all variables in the domains have a fixed sign */
4004 evalue_split_domains_into_orthants(e
, MaxRays
);
4005 if (EVALUE_IS_ZERO(*e
))
4009 assert(e
->x
.p
->size
>= 2);
4010 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
4012 signs
= ALLOCN(int, dim
);
4015 for (i
= 0; i
< dim
; ++i
)
4017 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
4019 domain_signs(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), signs
);
4020 evalue_frac2polynomial_r(&e
->x
.p
->arr
[2*i
+1], signs
, sign
, 0);
4026 /* Split the domains of e (which is assumed to be a partition)
4027 * such that each resulting domain lies entirely in one orthant.
4029 void evalue_split_domains_into_orthants(evalue
*e
, unsigned MaxRays
)
4032 assert(value_zero_p(e
->d
));
4033 assert(e
->x
.p
->type
== partition
);
4034 assert(e
->x
.p
->size
>= 2);
4035 dim
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
;
4037 for (i
= 0; i
< dim
; ++i
) {
4040 C
= Matrix_Alloc(1, 1 + dim
+ 1);
4041 value_set_si(C
->p
[0][0], 1);
4042 value_init(split
.d
);
4043 value_set_si(split
.d
, 0);
4044 split
.x
.p
= new_enode(partition
, 4, dim
);
4045 value_set_si(C
->p
[0][1+i
], 1);
4046 C2
= Matrix_Copy(C
);
4047 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0], Constraints2Polyhedron(C2
, MaxRays
));
4049 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
4050 value_set_si(C
->p
[0][1+i
], -1);
4051 value_set_si(C
->p
[0][1+dim
], -1);
4052 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2], Constraints2Polyhedron(C
, MaxRays
));
4053 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
4055 free_evalue_refs(&split
);
4060 void evalue_extract_affine(const evalue
*e
, Value
*coeff
, Value
*cst
, Value
*d
)
4062 value_set_si(*d
, 1);
4064 for ( ; value_zero_p(e
->d
); e
= &e
->x
.p
->arr
[0]) {
4066 assert(e
->x
.p
->type
== polynomial
);
4067 assert(e
->x
.p
->size
== 2);
4068 c
= &e
->x
.p
->arr
[1];
4069 value_multiply(coeff
[e
->x
.p
->pos
-1], *d
, c
->x
.n
);
4070 value_division(coeff
[e
->x
.p
->pos
-1], coeff
[e
->x
.p
->pos
-1], c
->d
);
4072 value_multiply(*cst
, *d
, e
->x
.n
);
4073 value_division(*cst
, *cst
, e
->d
);
4076 /* returns an evalue that corresponds to
4080 static evalue
*term(int param
, Value c
, Value den
)
4082 evalue
*EP
= ALLOC(evalue
);
4084 value_set_si(EP
->d
,0);
4085 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
4086 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
4087 evalue_set_reduce(&EP
->x
.p
->arr
[1], c
, den
);
4091 evalue
*affine2evalue(Value
*coeff
, Value denom
, int nvar
)
4094 evalue
*E
= ALLOC(evalue
);
4096 evalue_set_reduce(E
, coeff
[nvar
], denom
);
4097 for (i
= 0; i
< nvar
; ++i
) {
4099 if (value_zero_p(coeff
[i
]))
4101 t
= term(i
, coeff
[i
], denom
);
4108 void evalue_substitute(evalue
*e
, evalue
**subs
)
4114 if (value_notzero_p(e
->d
))
4118 assert(p
->type
!= partition
);
4120 for (i
= 0; i
< p
->size
; ++i
)
4121 evalue_substitute(&p
->arr
[i
], subs
);
4123 if (p
->type
== relation
) {
4124 /* For relation a ? b : c, compute (a' ? 1) * b' + (a' ? 0 : 1) * c' */
4128 value_set_si(v
->d
, 0);
4129 v
->x
.p
= new_enode(relation
, 3, 0);
4130 evalue_copy(&v
->x
.p
->arr
[0], &p
->arr
[0]);
4131 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4132 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4133 emul(v
, &p
->arr
[2]);
4138 value_set_si(v
->d
, 0);
4139 v
->x
.p
= new_enode(relation
, 2, 0);
4140 value_clear(v
->x
.p
->arr
[0].d
);
4141 v
->x
.p
->arr
[0] = p
->arr
[0];
4142 evalue_set_si(&v
->x
.p
->arr
[1], 1, 1);
4143 emul(v
, &p
->arr
[1]);
4146 eadd(&p
->arr
[2], &p
->arr
[1]);
4147 free_evalue_refs(&p
->arr
[2]);
4155 if (p
->type
== polynomial
)
4160 value_set_si(v
->d
, 0);
4161 v
->x
.p
= new_enode(p
->type
, 3, -1);
4162 value_clear(v
->x
.p
->arr
[0].d
);
4163 v
->x
.p
->arr
[0] = p
->arr
[0];
4164 evalue_set_si(&v
->x
.p
->arr
[1], 0, 1);
4165 evalue_set_si(&v
->x
.p
->arr
[2], 1, 1);
4168 offset
= type_offset(p
);
4170 for (i
= p
->size
-1; i
>= offset
+1; i
--) {
4171 emul(v
, &p
->arr
[i
]);
4172 eadd(&p
->arr
[i
], &p
->arr
[i
-1]);
4173 free_evalue_refs(&(p
->arr
[i
]));
4176 if (p
->type
!= polynomial
)
4180 *e
= p
->arr
[offset
];
4184 /* evalue e is given in terms of "new" parameter; CP maps the new
4185 * parameters back to the old parameters.
4186 * Transforms e such that it refers back to the old parameters and
4187 * adds appropriate constraints to the domain.
4188 * In particular, if CP maps the new parameters onto an affine
4189 * subspace of the old parameters, then the corresponding equalities
4190 * are added to the domain.
4191 * Also, if any of the new parameters was a rational combination
4192 * of the old parameters $p' = (<a, p> + c)/m$, then modulo
4193 * constraints ${<a, p> + c)/m} = 0$ are added to ensure
4194 * the new evalue remains non-zero only for integer parameters
4195 * of the new parameters (which have been removed by the substitution).
4197 void evalue_backsubstitute(evalue
*e
, Matrix
*CP
, unsigned MaxRays
)
4204 unsigned nparam
= CP
->NbColumns
-1;
4208 if (EVALUE_IS_ZERO(*e
))
4211 assert(value_zero_p(e
->d
));
4213 assert(p
->type
== partition
);
4215 inv
= left_inverse(CP
, &eq
);
4216 subs
= ALLOCN(evalue
*, nparam
);
4217 for (i
= 0; i
< nparam
; ++i
)
4218 subs
[i
] = affine2evalue(inv
->p
[i
], inv
->p
[nparam
][inv
->NbColumns
-1],
4221 CEq
= Constraints2Polyhedron(eq
, MaxRays
);
4222 addeliminatedparams_partition(p
, inv
, CEq
, inv
->NbColumns
-1, MaxRays
);
4223 Polyhedron_Free(CEq
);
4225 for (i
= 0; i
< p
->size
/2; ++i
)
4226 evalue_substitute(&p
->arr
[2*i
+1], subs
);
4228 for (i
= 0; i
< nparam
; ++i
)
4229 evalue_free(subs
[i
]);
4233 for (i
= 0; i
< inv
->NbRows
-1; ++i
) {
4234 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &gcd
);
4235 value_gcd(gcd
, gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
4236 if (value_eq(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
4238 Vector_AntiScale(inv
->p
[i
], inv
->p
[i
], gcd
, inv
->NbColumns
);
4239 value_divexact(gcd
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1], gcd
);
4241 for (j
= 0; j
< p
->size
/2; ++j
) {
4242 evalue
*arg
= affine2evalue(inv
->p
[i
], gcd
, inv
->NbColumns
-1);
4247 value_set_si(rel
.d
, 0);
4248 rel
.x
.p
= new_enode(relation
, 2, 0);
4249 value_clear(rel
.x
.p
->arr
[1].d
);
4250 rel
.x
.p
->arr
[1] = p
->arr
[2*j
+1];
4251 ev
= &rel
.x
.p
->arr
[0];
4252 value_set_si(ev
->d
, 0);
4253 ev
->x
.p
= new_enode(fractional
, 3, -1);
4254 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
4255 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
4256 value_clear(ev
->x
.p
->arr
[0].d
);
4257 ev
->x
.p
->arr
[0] = *arg
;
4260 p
->arr
[2*j
+1] = rel
;
4271 * \sum_{i=0}^n c_i/d X^i
4273 * where d is the last element in the vector c.
4275 evalue
*evalue_polynomial(Vector
*c
, const evalue
* X
)
4277 unsigned dim
= c
->Size
-2;
4279 evalue
*EP
= ALLOC(evalue
);
4284 if (EVALUE_IS_ZERO(*X
) || dim
== 0) {
4285 evalue_set(EP
, c
->p
[0], c
->p
[dim
+1]);
4286 reduce_constant(EP
);
4290 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
4293 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
4295 for (i
= dim
-1; i
>= 0; --i
) {
4297 value_assign(EC
.x
.n
, c
->p
[i
]);
4300 free_evalue_refs(&EC
);
4304 /* Create an evalue from an array of pairs of domains and evalues. */
4305 evalue
*evalue_from_section_array(struct evalue_section
*s
, int n
)
4310 res
= ALLOC(evalue
);
4314 evalue_set_si(res
, 0, 1);
4316 value_set_si(res
->d
, 0);
4317 res
->x
.p
= new_enode(partition
, 2*n
, s
[0].D
->Dimension
);
4318 for (i
= 0; i
< n
; ++i
) {
4319 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*i
], s
[i
].D
);
4320 value_clear(res
->x
.p
->arr
[2*i
+1].d
);
4321 res
->x
.p
->arr
[2*i
+1] = *s
[i
].E
;
4328 /* shift variables (>= first, 0-based) in polynomial n up (may be negative) */
4329 void evalue_shift_variables(evalue
*e
, int first
, int n
)
4332 if (value_notzero_p(e
->d
))
4334 assert(e
->x
.p
->type
== polynomial
||
4335 e
->x
.p
->type
== flooring
||
4336 e
->x
.p
->type
== fractional
);
4337 if (e
->x
.p
->type
== polynomial
&& e
->x
.p
->pos
>= first
+1) {
4338 assert(e
->x
.p
->pos
+ n
>= 1);
4341 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
4342 evalue_shift_variables(&e
->x
.p
->arr
[i
], first
, n
);
4345 static const evalue
*outer_floor(evalue
*e
, const evalue
*outer
)
4349 if (value_notzero_p(e
->d
))
4351 switch (e
->x
.p
->type
) {
4353 if (!outer
|| evalue_level_cmp(outer
, &e
->x
.p
->arr
[0]) > 0)
4354 return &e
->x
.p
->arr
[0];
4360 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
4361 outer
= outer_floor(&e
->x
.p
->arr
[i
], outer
);
4364 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
)
4365 outer
= outer_floor(&e
->x
.p
->arr
[2*i
+1], outer
);
4372 /* Find and return outermost floor argument or NULL if e has no floors */
4373 evalue
*evalue_outer_floor(evalue
*e
)
4375 const evalue
*floor
= outer_floor(e
, NULL
);
4376 return floor
? evalue_dup(floor
): NULL
;
4379 static void evalue_set_to_zero(evalue
*e
)
4381 if (EVALUE_IS_ZERO(*e
))
4383 if (value_zero_p(e
->d
)) {
4384 free_evalue_refs(e
);
4388 value_set_si(e
->d
, 1);
4389 value_set_si(e
->x
.n
, 0);
4392 /* Replace (outer) floor with argument "floor" by variable "var" (0-based)
4393 * and drop terms not containing the floor.
4394 * Returns true if e contains the floor.
4396 int evalue_replace_floor(evalue
*e
, const evalue
*floor
, int var
)
4402 if (value_notzero_p(e
->d
))
4404 switch (e
->x
.p
->type
) {
4406 if (!eequal(floor
, &e
->x
.p
->arr
[0]))
4408 e
->x
.p
->type
= polynomial
;
4409 e
->x
.p
->pos
= 1 + var
;
4411 free_evalue_refs(&e
->x
.p
->arr
[0]);
4412 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
4413 e
->x
.p
->arr
[i
] = e
->x
.p
->arr
[i
+1];
4414 evalue_set_to_zero(&e
->x
.p
->arr
[0]);
4419 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
) {
4420 int c
= evalue_replace_floor(&e
->x
.p
->arr
[i
], floor
, var
);
4423 evalue_set_to_zero(&e
->x
.p
->arr
[i
]);
4424 if (c
&& !reorder
&& evalue_level_cmp(&e
->x
.p
->arr
[i
], e
) < 0)
4427 evalue_reduce_size(e
);
4429 evalue_reorder_terms(e
);
4437 /* Replace (outer) floor with argument "floor" by variable zero */
4438 void evalue_drop_floor(evalue
*e
, const evalue
*floor
)
4443 if (value_notzero_p(e
->d
))
4445 switch (e
->x
.p
->type
) {
4447 if (!eequal(floor
, &e
->x
.p
->arr
[0]))
4450 free_evalue_refs(&p
->arr
[0]);
4451 for (i
= 2; i
< p
->size
; ++i
)
4452 free_evalue_refs(&p
->arr
[i
]);
4460 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
4461 evalue_drop_floor(&e
->x
.p
->arr
[i
], floor
);
4462 evalue_reduce_size(e
);
4472 PROCEDURES TO COMPUTE ENUMERATION. recursive procedure, recurse for
4475 @param pos index position of current loop index (1..hdim-1)
4476 @param P loop domain
4477 @param context context values for fixed indices
4478 @param exist number of existential variables
4479 @return the number of integer points in this
4483 void count_points_e (int pos
, Polyhedron
*P
, int exist
, int nparam
,
4484 Value
*context
, Value
*res
)
4489 value_set_si(*res
, 0);
4494 count_points(pos
, P
, context
, res
);
4498 value_init(LB
); value_init(UB
); value_init(k
);
4502 if (lower_upper_bounds(pos
,P
,context
,&LB
,&UB
) !=0) {
4503 /* Problem if UB or LB is INFINITY */
4504 value_clear(LB
); value_clear(UB
); value_clear(k
);
4505 if (pos
> P
->Dimension
- nparam
- exist
)
4506 value_set_si(*res
, 1);
4508 value_set_si(*res
, -1);
4515 for (value_assign(k
,LB
); value_le(k
,UB
); value_increment(k
,k
)) {
4516 fprintf(stderr
, "(");
4517 for (i
=1; i
<pos
; i
++) {
4518 value_print(stderr
,P_VALUE_FMT
,context
[i
]);
4519 fprintf(stderr
,",");
4521 value_print(stderr
,P_VALUE_FMT
,k
);
4522 fprintf(stderr
,")\n");
4527 value_set_si(context
[pos
],0);
4528 if (value_lt(UB
,LB
)) {
4529 value_clear(LB
); value_clear(UB
); value_clear(k
);
4530 value_set_si(*res
, 0);
4535 value_set_si(*res
, 1);
4537 value_subtract(k
,UB
,LB
);
4538 value_add_int(k
,k
,1);
4539 value_assign(*res
, k
);
4541 value_clear(LB
); value_clear(UB
); value_clear(k
);
4545 /*-----------------------------------------------------------------*/
4546 /* Optimization idea */
4547 /* If inner loops are not a function of k (the current index) */
4548 /* i.e. if P->Constraint[i][pos]==0 for all P following this and */
4550 /* Then CNT = (UB-LB+1)*count_points(pos+1, P->next, context) */
4551 /* (skip the for loop) */
4552 /*-----------------------------------------------------------------*/
4555 value_set_si(*res
, 0);
4556 for (value_assign(k
,LB
);value_le(k
,UB
);value_increment(k
,k
)) {
4557 /* Insert k in context */
4558 value_assign(context
[pos
],k
);
4559 count_points_e(pos
+1, P
->next
, exist
, nparam
, context
, &c
);
4560 if(value_notmone_p(c
))
4561 value_addto(*res
, *res
, c
);
4563 value_set_si(*res
, -1);
4566 if (pos
> P
->Dimension
- nparam
- exist
&&
4573 fprintf(stderr
,"%d\n",CNT
);
4577 value_set_si(context
[pos
],0);
4578 value_clear(LB
); value_clear(UB
); value_clear(k
);
4580 } /* count_points_e */