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: Joerg Wunsch
Subject: Re: [avr-gcc-list] swap bits
Date: Wed, 22 Jan 2003 11:06:20 +0100 (MET)

Klaus Rudolph <address@hidden> wrote:

> 51 cycles total without wasting any register.
> 10 words flash.
> 51 cycles is very expensive. Any better ideas here :-)
> 
> Best asm contest is open now....

Why asm? :-)

The BSD fortune database provides the following:

$ fortune -m 'reverses the bits'
%% (fortunes)
   n = ((n >>  1) & 0x55555555) | ((n <<  1) & 0xaaaaaaaa);
   n = ((n >>  2) & 0x33333333) | ((n <<  2) & 0xcccccccc);
   n = ((n >>  4) & 0x0f0f0f0f) | ((n <<  4) & 0xf0f0f0f0);
   n = ((n >>  8) & 0x00ff00ff) | ((n <<  8) & 0xff00ff00);
   n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000);

                -- C code which reverses the bits in a word.

Stripping this down to an inlined single-byte version:

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.

-- 
J"org Wunsch                                           Unix support engineer
address@hidden        http://www.interface-systems.de/~j/
avr-gcc-list at http://avr1.org



reply via email to

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