Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What happened when using -Os with gcc?

Reply
Thread Tools

What happened when using -Os with gcc?

 
 
Winter
Guest
Posts: n/a
 
      07-01-2009
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,
 
Reply With Quote
 
 
 
 
Tim Prince
Guest
Posts: n/a
 
      07-01-2009
Winter wrote:
> 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.
 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      07-02-2009
On Jul 2, 4:53*am, Winter <winter...@gmail.com> wrote:
> 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.
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      07-02-2009
In article <47afdc5a-a4f3-45b8-9ed2-> Winter <> writes:
....
> 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.
--
dik t. winter, cwi, science park 123, 1098 xg amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
Boon
Guest
Posts: n/a
 
      07-02-2009
Winter wrote:

> I ran across a strange problem in my project, and eventually I tracked
> down to the "-Os" option with gcc.


Are you using gcc 2.95?
 
Reply With Quote
 
Winter
Guest
Posts: n/a
 
      07-02-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Modding, Where to next? What happened to it? What is it r... Silverstrand Front Page News 0 10-24-2005 01:50 PM
FireFox: What happened?! Robert Firefox 7 11-15-2004 08:20 PM
Wireless Linksys Router Reset--What happened? =?Utf-8?B?TW9yZGlkbw==?= Wireless Networking 1 10-08-2004 11:28 AM
Wot has happened to FireBird Linda Leverno Firefox 1 07-10-2004 06:04 PM
Something Crazy Happened !!! nikoli Firefox 0 12-12-2003 04:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57