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: David Kelly
Subject: Re: [avr-gcc-list] Tip: handling volatile operands
Date: Thu, 10 Jan 2008 13:43:43 -0600
User-agent: Mutt/1.4.2.3i

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 ) )
{
}

-- 
David Kelly N4HHE, address@hidden
========================================================================
Whom computers would destroy, they must first drive mad.




reply via email to

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