avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Tip: handling volatile operands


From: Andrew Hutchinson
Subject: Re: [avr-gcc-list] Tip: handling volatile operands
Date: Thu, 10 Jan 2008 20:21:26 -0500
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)

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, address@hidden 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 ) )
{
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]