What happened when using -Os with gcc?

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,
 
T

Tim Prince

Winter said:
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,
This question is probably more suited to the gcc-help mailing list,
which you can subscribe at gcc.gnu.org. When you post there, give at
least your gcc -v screen output so people know something about your
versions.
 
O

Old Wolf

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:
        printf("================= %s ===============\n", name);

The normal output should be:
==============ZZZZZZZZZZZZZZZZZ================

But if I use -Os when compiling, the result is:
================= LAST ===============

It's weird that -Os changes "ZZZ..." into "LAST".

Please post a *complete* program that shows the
problem, there may be an error in code that you
did not show.
 
D

Dik T. Winter

> 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:

No problem with gcc 4.3.2.
 
W

Winter

Thanks for all replies!

As Tim suggested, I post the question in gcc.gnu.org. And it turns out
a bug in GCC 3.4.6. (Sorry i forgot to mention my GCC is 3.4.6)

4.3.2 should be no problem.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top