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

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

Re: [avr-gcc-list] Code generation on 3.3


From: E. Weddington
Subject: Re: [avr-gcc-list] Code generation on 3.3
Date: Wed, 22 Jan 2003 13:37:06 -0700

On 22 Jan 2003 at 11:12, Larry Barello wrote:

> I am using the latest winavr release (thanks Eric!)
Thanks for using it! (Let me know when AVRX can compile using  
WinAVR.)
 
> In debugging some code I noticed a lot of places where char's are
> being promoted to ints to do simple math and then the upper byte
> discarded.  Even unsigned chars are being promoted to ints (not
> unsigned ints!).  I know that GCC is word centric, but various other
> obvious places where this happens have been corrected in the past
> (switch on a char doesn't require word compares any more, for
> example).
> 
> For example below, shift right uses ASR, but shift left promotes to
> int, adds and then throws away the upper byte, when a simple add R, R
> would do.  Multiple shifts left are even worse since GCC shifts the
> entire word N times, rather than simply adding.
> 
> Cheers!
> 
> --- example code ----
> 
> unsigned char old;
> int LeftEncoder, RightEncoder;
> 
> void EncoderTask(void)
> {
>     unsigned char new = PINC;
>     unsigned char x = new ^ old;
>     x |= (x>>1 & 0x55) | (x<<1 & 0xAA);
>     x &= ((new>>1 ^ old) & 0x55) | ((new<<1 ^ old) & 0xAA);
>     old = new;
> 
>     // decode
> 
>     if (x & 0x40) leftEncoder ++;
>     if (c & 0x80) LeftEncoder--;
>     if (x & 0x02) RightEncoder++;
>     if (x & 0x01) RightEncoder--;
> }
> 

I got bitten by the same thing, and specifically when using bitwise 
operators. IIRC, it was either Jörg or Ted that pointed out to me 
that the promotion to int when using bitwise operators was a part of 
standard C and not just attributable to GCC. The solution would be 
then to use typecasts, even on the constants. I know that doing that, 
would make the code above probably more unreadable. :-/

Anybody, feel free to correct me if I'm wrong on this, or if there is 
a way around this without resorting to -mint8 (which is unsupported 
on avrlibc).

Eric

avr-gcc-list at http://avr1.org



reply via email to

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