This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
What happened when using -Os with gcc?
- From: "Tech Win" <tech_win at shaw dot ca>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Wed, 1 Jul 2009 16:38:25 -0700
- Subject: What happened when using -Os with gcc?
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.
And if I change the line
t = (width + (int)x) / 2;
to
t = (width+x)/2
And
t = (int)x * 10;
to
t = x * 10;
It'll be no problem with -Os options in gcc.
Can anybody please explain this?
My GCC version is 3.4.6.
Thanks a lot,
Win