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

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

Re: [avr-gcc-list] Strange issues with 16-bit values


From: David Kelly
Subject: Re: [avr-gcc-list] Strange issues with 16-bit values
Date: Tue, 11 Nov 2008 12:16:27 -0600
User-agent: Mutt/1.4.2.3i

On Tue, Nov 11, 2008 at 12:30:10PM -0500, Brian Neltner wrote:
> 
> I compiled with -Wall and minus a few unused variables there are no
> warnings. Although I'm not sure how it could cause the problem, I am
> curious why it would be doing 32 bit calculations in the polling. I do
> this:
> 
> while(TCNT3<(uint16_t)benc_period*224UL/255);
> 
> where benc_period is a #define, so it should be cast as a 16-bit
> unsigned integer. Do I need to take manual action to tell it to do
> 16-bit comparison?

You use 224UL in your expression and wonder why the comparison is
promoted to 32 bits? The comparison is always the size of the biggest
and your cast only sticks to the first item.

If you want to force it down to a 16 bit comparison you need to force
everything down to 16 bits:

while(TCNT3<(uint16_t)( benc_period*224UL/255) );

But I think it would be best written like this to enforce the desired
evaluation order:

while( TCNT3 < (uint16_t)( (benc_period*224UL) /255) )
        ;

The above because I would be concerned the compiler might rearrange the
order and precalculate 224UL/255. Always better safe than sorry.

On empty while() loops I like to put the semicolon on a separate line
so that the emptiness stands out.

-- 
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]