avr-chat
[Top][All Lists]
Advanced

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

Re: [avr-chat] Missed Optimisation ?


From: Erik Christiansen
Subject: Re: [avr-chat] Missed Optimisation ?
Date: Wed, 2 Mar 2011 01:04:44 +1100
User-agent: Mutt/1.5.17+20080114 (2008-01-14)

On Tue, Mar 01, 2011 at 07:52:55AM -0500, Graham Davies wrote:
> bob wrote:
>
>> The code is very simple (runs in Port Change interrupt):
>>
>>    if ((PINA & _BV(A2DDATA)) != 0)
>> result |= 0x80000000;
>>
>>    result >>= 1;
>
> Bob confirms (off-list) that variable 'result' is declared volatile.  My  
> opinion is that this is why the compiler must load and store all 32 bits. 
> Bob, however, disagrees.  So, maybe this is another discussion about the  
> exact meaning and effect of the volatile qualifier.

IANA "volatile" expert, but understand that "[hardware|other thread]
may change the value while this code is executing" is a legitimate view
of the compiler's perspective. In that case, the whole value must be
read and rewritten, to avoid composite values, partly software created,
and partly hardware-(or alternate thread)-written. reading and writing
only some bytes is not a reliable option with a predictable outcome.

...

> So, I think that the compiler is doing exactly what it should do.

Have to agree, 'cos the code can break if it doesn't respect a genuine
volatile.

Mind you, if "result" is genuinely volatile, then it can change between
the |= and the >>=, so we could be right shifting the other thread's
value, couldn't we?

<$.02>
Over 25 years I've always written ISRs in assembler. That has made them
easy to optimise, predictable in behaviour, and independent of compiler
"improvements" in the event of having to use another version. (Not that
in-line asm stuff, rather a proper linked-in asm file. Lots easier. ;-)
Oh, 'n don't have to argue with the compiler about what's "volatile"
either.
</$.02>

Erik

-- 
Never worry about theory as long as the machinery does what it's
supposed to do.
                                                  -- Robert A. Heinlein



reply via email to

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