W
Winter
Hi there,
I ran across a strange problem in my project, and eventually I tracked
down to the "-Os" option with gcc. Please take a look at the following
code:
==================================================
static float test_cell()
{
int i;
float sum =0;
for ( i= 0; i < 100; i ++ )
sum += i;
return sum;
}
void strange_test(const char *name)
{
int i;
float t;
printf("================= %s ===============\n", name);
for ( i = 0; i< 3; i++ ) {
int head = !i;
float x = test_cell();
float width = 100.0;
if ( head ) {
t = (width + (int)x) / 2;
printf("1 --- %d\n", head);
}
else {
t = (int)x * 10;
printf("0 --- %d\n", head);
}
}
printf("t=%.2f\n", t);
}
The normal output should be:
==============ZZZZZZZZZZZZZZZZZ================
1 --- 1
0 --- 0
0 --- 0
t=49500.00
But if I use -Os when compiling, the result is:
================= LAST ===============
1 --- 1
1 --- 0
1 --- 0
t=2525.00
Seems it's not branching!
When I use -O0, -O1, -O2, or -O3 the output is no problem. But if I
use -Os then get the wrong result.
Can anybody please explain this?
Thanks a lot,
I ran across a strange problem in my project, and eventually I tracked
down to the "-Os" option with gcc. Please take a look at the following
code:
==================================================
static float test_cell()
{
int i;
float sum =0;
for ( i= 0; i < 100; i ++ )
sum += i;
return sum;
}
void strange_test(const char *name)
{
int i;
float t;
printf("================= %s ===============\n", name);
for ( i = 0; i< 3; i++ ) {
int head = !i;
float x = test_cell();
float width = 100.0;
if ( head ) {
t = (width + (int)x) / 2;
printf("1 --- %d\n", head);
}
else {
t = (int)x * 10;
printf("0 --- %d\n", head);
}
}
printf("t=%.2f\n", t);
}
The normal output should be:
==============ZZZZZZZZZZZZZZZZZ================
1 --- 1
0 --- 0
0 --- 0
t=49500.00
But if I use -Os when compiling, the result is:
================= LAST ===============
1 --- 1
1 --- 0
1 --- 0
t=2525.00
Seems it's not branching!
When I use -O0, -O1, -O2, or -O3 the output is no problem. But if I
use -Os then get the wrong result.
Can anybody please explain this?
Thanks a lot,