This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [avr-gcc-list] Re: AVR byte swap optimization
- From: "Anton Erasmus" <antone at sentechsa dot com>
- To: avr-gcc-list at nongnu dot org, gcc at gcc dot gnu dot org
- Date: Mon, 18 Dec 2006 23:08:20 +0200
- Subject: Re: [avr-gcc-list] Re: AVR byte swap optimization
- References: <7f45d9390611171530g4c12f68dua3ade87a8b4147b0@mail.gmail.com>, <200611270219.56427.vda.linux@googlemail.com>, <7f45d9390612181028o21f8d1eo2fc3e01b5802c695@mail.gmail.com>
On 18 Dec 2006 at 11:28, Shaun Jackman wrote:
> On 11/26/06, Denis Vlasenko <vda.linux@googlemail.com> wrote:
> > On Saturday 18 November 2006 00:30, Shaun Jackman wrote:
> > > The following macro expands to some rather frightful code on the AVR:
> > >
> > > #define BSWAP_16(x) \
> > > ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
> >
> > Sometimes gcc is generating better code if you cast
> > values instead of masking. Try:
> >
> > ( (uint8_t)((x) >> 8) | ((uint8_t)(x)) << 8 )
>
> Your suggestion seemed like a good one and gave me some hope.
> Unfortunately, it produces identical results to my BSWAP_16 macro. I
> also tried the following:
>
> uint8_t a = x >> 8, b = x;
> return b << 8 | a;
>
> Different register allocations this time, but identical instructions.
>
Hi,
Not a macro, but the following seems to generate reasonable code.
typedef struct
{
unsigned char h;
unsigned char l;
}B2;
typedef union
{
B2 x;
unsigned short us;
}BU;
inline unsigned short byteswap(unsigned short xx)
{
BU tmp;
unsigned char tmp1;
tmp.us=xx;
tmp1=tmp.x.h;
tmp.x.h=tmp.x.l;
tmp.x.l=tmp1;
return(tmp.us);
}
volatile unsigned short gX;
void test(void)
{
gX=byteswap(gX);
}
Regards
Anton Erasmus
--
A J Erasmus