Agreed. But my example was purposely "if", as "while" would indeed indicate that its volatile qualities were required.

The point I was trying to make (poorly) was that io (and other variables) that may universally declared volatile, may in fact have values that need to be used in a "non-volatile" fashion.

You have same problem with unrollable operations such as:

if (ioport == 1)
else if (ioport == 2)
else if (ioport == 99)

So, copy to temporary seems a more general solution - if "non volatile" access is required.

(I suppose casts would work but that might be a risky general assumption for all compilers)


David Kelly wrote:
On Thu, Jan 10, 2008 at 02:24:46PM -0500, [EMAIL PROTECTED] wrote:
I have come across a few instances where very inefficient c code is
created by volatile operands - often accidentally and thru no fault of
the compiler(s)

...

For example:

 while (ioport != 0)
{

}

...

If ioport were a "normal" variable, there would be one read and one
test!

So, if you don't want the absolute latest value of a volatile,  use a
temporary!

unsigned char a = ioport;

if ((a& 1) ||  (a& 2) || (a& 0x0c))
{
}

Will produce much better code!

You changed horses in the middle of the stream. Replace "if" above with
"while" that you started with and your non-volatile may loop forever
based on one read.

The solution is to roll your conditions into one test:

while( ioport & ( 1 | 2 | 0x0c ) )
{
}



_______________________________________________
AVR-GCC-list mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Reply via email to