On 24/10/2012 09:38, mind entropy wrote:
> On Wed, Oct 24, 2012 at 12:36 PM, Eric Decker <cire...@gmail.com>
> wrote:
>
>>
>>
>> On Tue, Oct 23, 2012 at 11:14 PM, mind entropy
>> <mindentr...@gmail.com>wrote:
>>
>>> Hi,
>>>
>>> I was reading the mspgcc manual (
>>>
>>> http://www.eecs.harvard.edu/~konrad/projects/motetrack/mspgcc-manual-20031127.pdf
>>>
>>>
)
>>> and on Pg 39 its written as
>>>
>>
>> I believe there is a more up to date version but I'm not usre where
>> it lives.  Perhaps Peter can point to it?
>>
>>
>>>
>>> "If you execute while ((long) a & 0x80000l); the program will
>>> hang, unless ’a’ is declared volatile. So, do it!"
>>>
>> With optimization a will be put into a register (actually two
>> registers since it is a long) and loaded once.
>>
>> If a is declared as a volatile, that tells the toolchain that a can
>> change out from underneath any assumptions made about contents.
>> This forces the compiler to reload a into a register each
>> iteration.
>>
>
> Its loaded once during the start of the iteration right?
> Theoretically if there is no change from outside is there a point in
> making it volatile?  i.e. if that loop block is the only thing in the
> loop?
>

If "a" is not volatile, the compiler will generate code that is like this:

long localCopyOfA = a;
if (localCopyOfA & 0x800001) {
        while (true) ;
}

If that is what you meant to write, then it's fine to have "a" 
non-volatile.  But I /really/ doubt that this is what you want.

The loop is completely useless unless "a" is modified from outside in 
some way - from an interrupt function (the typical case), via hardware 
(maybe "a" is a register, or perhaps modified by DMA), from another RTOS 
thread, or even via a debugger (such constructs can sometimes be useful 
in testing and debugging).  In all these cases, you have to let the 
compiler know that "a" may be modified externally - and you do that by 
declaring it "volatile".

> If it applies to long then it should apply to double and float also
> right?

It applies to /all/ variables, of all sizes.

If the variable is bigger than the native size (16-bit in this case), 
then "volatile" alone is not enough - you need to do extra work to make 
the access atomic.

>
>>
>>> I did not get it. Why should it hang?
>>>

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to