public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* RE: [ECOS] Optimizations and bad code
@ 2001-07-23  9:47 Doug Fraser
  2001-07-23 12:36 ` Jonathan Larmour
  0 siblings, 1 reply; 8+ messages in thread
From: Doug Fraser @ 2001-07-23  9:47 UTC (permalink / raw)
  To: 'Gary Thomas', rob.wj.jansen; +Cc: ecos-discuss, tadams

Yup, it is ugly code, but because 'i' is used in the context
of an argument to a function, the optimizer must honor the
scope of the variable within the for() loop. The value of
'i' after each func() call had better be three greater than
before. Its value within the array subscipts is definitely
undefined. That the arm-elf compiler only increments 'i' by
one for a particular optimizer setting is a defect, and badly
formed source code is no excuse for erroneous object code.
If that held true, where would the obsfucated C contest go?

As an additional exercise to the arm compiler, I would try this...

	int i, j;

	i = 0;
	for(j = 0; j < 100; j++)
	{
		i++, i++, i++;     /* a perfectly legit 'comma' operator */
		printf("i %d, j %d\n", i, j);
	}

while j is incremented by one on each print, i must increment by three......

Doug

> -----Original Message-----
> From: Gary Thomas [ mailto:gthomas@redhat.com ]
> Sent: Monday, July 23, 2001 9:32 AM
> To: rob.wj.jansen@philips.com
> Cc: ecos-discuss@sourceware.cygnus.com; tadams@extremeeng.com
> Subject: Re: [ECOS] Optimizations and bad code
> 
> 
> 
> On 23-Jul-2001 rob.wj.jansen@philips.com wrote:
> > 
> > Compiler optimizations can be a real pain, especially if 
> you have some funny code.
> > Look at the following example:
> > 
> > {
> >     int i, y;
> > 
> >    i = 0;
> > 
> >     for(y=0; y<176; y++)
> >     {
> >         func(x, y, (a[i++] + (a[i++]<<1) + a[i++])>>2);
> >     }
> > }
> > 
> > This is real ugly code, extracted from a non working progam.
> > It was meant to combine three values from an RGB buffer 
> (containing a 24 bit image)
> > into one 8 bit gray scale value. This was no serious 
> programming but just a fast hack
> > to get something on the LCD.
> > 
> > According to the C standard, the arguments to the + 
> operator may be evaluated in any
> > order, making it unpredictable to tell if the R, G or B 
> value will get the <<1. But I guess that
> > the "i" should always be three more that it started of with 
> after going through the loop once.
> > 
> > The amizing part however is that with the arm-elf-gcc 
> compiler, it depends if optimizations
> > are on "-O2" or off "-O0". Without optimizations i is 
> incremented with 1 (!) after going through
> > the loop once. All other variants (including different 
> optimization settings for the
> > i686-pc-linux-gnu
> > and native linux compiler) have i incremented by 3.
> > 
> > Although I admit that one should never write code like 
> this, the results are amazing.
> > It just shows that "working in my private enviroment" does 
> not mean it is correct code.
> 
> This code is simply frightening!!
> 
> Firstly, in the line
>          func(x, y, (a[i++] + (a[i++]<<1) + a[i++])>>2);
> the order of the i++ is not guaranteed at all, so the results 
> could vary
> widely from platform/architecture to platform.
> 
> Also, since the value of 'i' is not used after the for{} 
> loop, or even within it except
> as an index, there is no reason at all for the compiler to 
> ever keep it around or make
> the updated value available.
> 
> The worst part of it all is that even "This was no serious 
> programming but just a fast hack",
> writing this sort of code can become habitual.  Once in that 
> "mold", the programmer will
> write similar code for "serious" use and then wonder why it 
> doesn't work :-(
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread
[parent not found: <E2D27064CD59574F88D05AEF5728396D4453FD@PH01SRV02.photuris.com>]
* Re: [ECOS] Optimizations and bad code
@ 2001-07-24  1:07 rob.wj.jansen
  0 siblings, 0 replies; 8+ messages in thread
From: rob.wj.jansen @ 2001-07-24  1:07 UTC (permalink / raw)
  To: ecos-discuss

Hi,

I completely agree with Gary,

> This code is simply frightening!!

so the example I made obviously was not typed in exactly like this, this was the
simple version of the macro infested source code I started off with.
The a[i++] was part of a macro (like getc() is a macro in a number of stdio.h files)
where it fetches a byte from a buffer.

Well, they warned me about this kind of stuff with macros ...
That's why I'm so glad that insure++ is available for Linux ;-)

Regards,

     Rob Jansen

Software Engineer
Competence Center Platforms
BU Mobile Communications
Meijhorst 60-10, 6537 KT Nijmegen, The Netherlands
Tel: +31-24-353-6329
Fax: +31-24-353-3613
mailto:Rob.WJ.Jansen@philips.com



^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [ECOS] Optimizations and bad code
@ 2001-07-23  0:58 rob.wj.jansen
  2001-07-23  6:32 ` Gary Thomas
  0 siblings, 1 reply; 8+ messages in thread
From: rob.wj.jansen @ 2001-07-23  0:58 UTC (permalink / raw)
  To: tadams; +Cc: ecos-discuss

Compiler optimizations can be a real pain, especially if you have some funny code.
Look at the following example:

{
    int i, y;

   i = 0;

    for(y=0; y<176; y++)
    {
        func(x, y, (a[i++] + (a[i++]<<1) + a[i++])>>2);
    }
}

This is real ugly code, extracted from a non working progam.
It was meant to combine three values from an RGB buffer (containing a 24 bit image)
into one 8 bit gray scale value. This was no serious programming but just a fast hack
to get something on the LCD.

According to the C standard, the arguments to the + operator may be evaluated in any
order, making it unpredictable to tell if the R, G or B value will get the <<1. But I guess that
the "i" should always be three more that it started of with after going through the loop once.

The amizing part however is that with the arm-elf-gcc compiler, it depends if optimizations
are on "-O2" or off "-O0". Without optimizations i is incremented with 1 (!) after going through
the loop once. All other variants (including different optimization settings for the i686-pc-linux-gnu
and native linux compiler) have i incremented by 3.

Although I admit that one should never write code like this, the results are amazing.
It just shows that "working in my private enviroment" does not mean it is correct code.

Regards,

     Rob Jansen

Software Engineer
Competence Center Platforms
BU Mobile Communications
Meijhorst 60-10, 6537 KT Nijmegen, The Netherlands
Tel: +31-24-353-6329
Fax: +31-24-353-3613
mailto:Rob.WJ.Jansen@philips.com



^ permalink raw reply	[flat|nested] 8+ messages in thread
* [ECOS] Optimizations and bad code
@ 2001-07-20 10:31 Trenton D. Adams
  0 siblings, 0 replies; 8+ messages in thread
From: Trenton D. Adams @ 2001-07-20 10:31 UTC (permalink / raw)
  To: 'eCos Discussion'

I was thinking.  Maybe my PCMCIA controller driver isn't working
properly because the compiler optimizations are screwing it up.  Is this
possible?  If so, how do I turn them off just for edb7xxx_pcmcia.c?

I tried turning them off completely, and an eCos application will not
run whatsoever.  And yes, I did do a Build|Clean first.

Trenton D. Adams
Embedded Developer
Windows Developer
Extreme Engineering Ltd.
Calgary Alberta.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2001-07-24  6:23 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-23  9:47 [ECOS] Optimizations and bad code Doug Fraser
2001-07-23 12:36 ` Jonathan Larmour
     [not found] <E2D27064CD59574F88D05AEF5728396D4453FD@PH01SRV02.photuris.com>
2001-07-24  6:23 ` Jonathan Larmour
  -- strict thread matches above, loose matches on Subject: below --
2001-07-24  1:07 rob.wj.jansen
2001-07-23  0:58 rob.wj.jansen
2001-07-23  6:32 ` Gary Thomas
2001-07-23  9:31   ` Trenton D. Adams
2001-07-20 10:31 Trenton D. Adams

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).