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

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

[avr-gcc-list] Code generation on 3.3


From: Larry Barello
Subject: [avr-gcc-list] Code generation on 3.3
Date: Wed, 22 Jan 2003 11:12:29 -0800

I am using the latest winavr release (thanks Eric!)

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--;
}

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



reply via email to

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