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

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

Re: [avr-gcc-list] swap bits


From: Marek Michalkiewicz
Subject: Re: [avr-gcc-list] swap bits
Date: Wed, 22 Jan 2003 15:17:40 +0100 (CET)

> inline static unsigned char
> swapbits(unsigned char n)
> {
>    n = ((n >>  1) & 0x55) | ((n <<  1) & 0xaa);
>    n = ((n >>  2) & 0x33) | ((n <<  2) & 0xcc);
>    n = ((n >>  4) & 0x0f) | ((n <<  4) & 0xf0);
>    return n;
> }
> 
> ...i can count 20 instructions, i. e. 20 words of flash and 20 cycles.

Nice algorithm.  Still, the last operation is simply "swap r24",
but GCC is not yet smart enough to see this...

#define r_data r24
#define r_temp r25

        mov r_temp,r_data       ; temp=HGFEDCBA
        lsr r_temp              ; temp=0HGFEDCB
        andi r_temp,0x55        ; temp=0H0F0D0B
        lsl r_data              ; data=GFEDCBA0
        andi r_data,0xaa        ; data=G0E0C0A0
        or r_data,r_temp        ; data=GHEFCDAB
        mov r_temp,r_data       ; temp=GHEFCDAB
        lsr r_temp              ; temp=0GHEFCDA
        lsr r_temp              ; temp=00GHEFCD
        andi r_temp,0x33        ; temp=00GH00CD
        lsl r_data              ; data=HEFCDAB0
        lsl r_data              ; data=EFCDAB00
        andi r_data,0xcc        ; data=EF00AB00
        or r_data,r_temp        ; data=EFGHABCD
        swap r_data             ; data=ABCDEFGH

15 words, 15 cycles - better than rol/ror brute force :)

Marek

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



reply via email to

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