DSPRelated.com
Forums
Online · Live + on demand
February 9-11, 2027
Price goes up Jan 1: register now to lock in today's rate
Register Now →

Fast bit-reverse on an x86?

Started by Tim Wescott ●November 2, 2010
On 11/02/2010 06:00 PM, Vladimir Vassilevsky wrote:
> > > Tim Wescott wrote: >> Doing some work on a PC. Customer says it's not real time, but real >> fast is still better than real slow. I need to bit-reverse a slew of >> bytes, so the faster the better. > > What do you do? Mirroring some bitmaps?
It's for a bunch of data that's recorded off of a 1-bit ADC -- they're changing their frequency plan and need things converted, and by the way they're changing from lsb first to msb first. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
On Tue, 02 Nov 2010 11:07:14 -0700, Tim Wescott <tim@seemywebsite.com>
wrote:

>Doing some work on a PC. Customer says it's not real time, but real >fast is still better than real slow. I need to bit-reverse a slew of >bytes, so the faster the better.
Is this a long bit string that needs to be reversed that is stored into multiple bytes ? Is the bit count a multiple of 8 or perhaps a multiple of 32 ? Swap the bytes starting from opposite ends of the byte string with byte moves and then bit swap each byte using table look up. To reduce the number of memory accesses, perform the table look up during each byte move. For bit string with multiple of 32 bits and properly aligned, load a register with 4 bytes from one end of the string, perform byte swaps within the register, use each byte separately to perform a table lookup and store 4 bytes in a single dword move into the opposite end of the string. The effects of cache is hard to predict with multilevel cache hierarchies. However, there are several articles dealing with optimizing memcpy() functions e.g. by prefetching data into cache by doing 64 bit dummy loads into double precision stack or prefetching accessing every 32th byte (one byte from each cache line). This may have an effect how cache write back/write though is performed at different cache hierarchy levels. Of course the data should be properly aligned in relative to dwords, 32 byte cache lines and for even larger data types the 4096 byte virtual memory pages.
On 11/02/2010 07:07 PM, Tim Wescott wrote:
> Doing some work on a PC. Customer says it's not real time, but real > fast is still better than real slow. I need to bit-reverse a slew of > bytes, so the faster the better.
If you need the bit reversal for a bit-reversal shuffle loop for FFT I may be able to help out with a piece of code. I've written this two years ago, and as far as I remember it performed better than the lookup-table variant. n is a power of two. data points to an array that is shuffled in place, 8 bytes per entry (aka a float complex). It's easy to modify this for a different element size by changing the movq instructions to something else. void BitReverse1 (void * data, int n) { _asm { mov eax, [n] mov edi, [data] pushad dec eax ; l--; xor ebx, ebx ; jp = 0 xor ecx, ecx ; jn = 0; bsr esi, eax ; s = bsr(l-1) theloop: bsf ebp, eax ; k = bsf (i) mov edx, esi ; x = s; sub edx, ebp ; x = s-k btc ebx, ebp ; jp ^= (1<<k) btc ecx, edx ; jn ^= (1<<x) cmp ecx, ebx jge skipShuffle movq mm0, [edi + ebx*8] movq mm1, [edi + ecx*8] movq [edi + ebx*8], mm1 movq [edi + ecx*8], mm0 skipShuffle: dec eax jnz theloop emms popad } } feel free to use this code.. I declare it as public domain. Nils Pipenbrinck
On 02/11/2010 22:45, Walter Banks wrote:
> > > Tim Wescott wrote: >> >> Doing some work on a PC. Customer says it's not real time, but real >> fast is still better than real slow. I need to bit-reverse a slew of >> bytes, so the faster the better. >> >> AFAIK the Pentium instruction set doesn't have a bit-reverse >> instruction, and there certainly isn't one available from C. My >> knee-jerk reaction to this problem is to make up a table of bit-reversed >> bytes, and do a lookup to retrieve them. >> >> Anyone know a faster way? > > > Table lookup is very fast but > this is quicker than doing it the hard way > > unsigned int > reverse(register unsigned int x) > { > x = (((x& 0xaaaaaaaa)>> 1) | ((x& 0x55555555)<< 1)); > x = (((x& 0xcccccccc)>> 2) | ((x& 0x33333333)<< 2)); > x = (((x& 0xf0f0f0f0)>> 4) | ((x& 0x0f0f0f0f)<< 4)); > x = (((x& 0xff00ff00)>> 8) | ((x& 0x00ff00ff)<< 8)); > return((x>> 16) | (x<< 16)); > > } >
There are two things that cause unexpected slowdowns on a processor like this. One is cache - as has been mentioned, table lookup may be very slow if the table is not in cache (and if it /is/ in cache, it is using up valuable cache space that other code could use). The second thing is pipelining. Code like the function above is full of pipeline stalls, since the operations are highly dependent on the results of the previous lines. It may be possible to re-arrange the code in some way to make it more pipeline-friendly.
> http://aggregate.org/MAGIC/#Bit%20Reversal > > > A number of bit reversal algorithms with source are benchmarked here > > http://stackoverflow.com/questions/746171/best-algorithm-for-bit-reversal-from-msb-lsb-to-lsb-msb-in-c > > Regards, >
On 03/11/2010 06:51, Tim Wescott wrote:
> On 11/02/2010 06:00 PM, Vladimir Vassilevsky wrote: >> >> >> Tim Wescott wrote: >>> Doing some work on a PC. Customer says it's not real time, but real >>> fast is still better than real slow. I need to bit-reverse a slew of >>> bytes, so the faster the better. >> >> What do you do? Mirroring some bitmaps? > > It's for a bunch of data that's recorded off of a 1-bit ADC -- they're > changing their frequency plan and need things converted, and by the way > they're changing from lsb first to msb first. >
Maybe it can be done in hardware. How is the data getting from the ADC into the PC in the first place? Do you have a microcontroller in the data path? If the ADC has an SPI bus, you could use an FTDI chip connected to USB, and use that to read the SPI MSB first.
On Nov 2, 2:07&#4294967295;pm, Tim Wescott <t...@seemywebsite.com> wrote:
> Doing some work on a PC. &#4294967295;Customer says it's not real time, but real > fast is still better than real slow. &#4294967295;I need to bit-reverse a slew of > bytes, so the faster the better. > > AFAIK the Pentium instruction set doesn't have a bit-reverse > instruction, and there certainly isn't one available from C. &#4294967295;My > knee-jerk reaction to this problem is to make up a table of bit-reversed > bytes, and do a lookup to retrieve them. > > Anyone know a faster way? > > -- > > Tim Wescott > Wescott Design Serviceshttp://www.wescottdesign.com > > Do you need to implement control loops in software? > "Applied Control Theory for Embedded Systems" was written for you. > See details athttp://www.wescottdesign.com/actfes/actfes.html
If you have parallel access in hardware, perhaps a "gated" bit reversing ROM may work for you.
On 3 Nov, 05:51, Tim Wescott <t...@seemywebsite.com> wrote:
> On 11/02/2010 06:00 PM, Vladimir Vassilevsky wrote: > > > > > Tim Wescott wrote: > >> Doing some work on a PC. Customer says it's not real time, but real > >> fast is still better than real slow. I need to bit-reverse a slew of > >> bytes, so the faster the better. > > > What do you do? Mirroring some bitmaps? > > It's for a bunch of data that's recorded off of a 1-bit ADC -- they're > changing their frequency plan and need things converted, and by the way > they're changing from lsb first to msb first.
On x86-32 your first idea - the table lookup - should compile to something like and eax, 0xff mov al, [bit_reverse_table + eax] That's it. These are two simple (i.e. single cycle or better) instructions. The table for this is only 256 bytes (which you should align on the line boundary of your data cache if possible). The table or the relevant parts of it will be cached and you won't see the code for dust.... I can think of some more complex schemes but none of them would be as fast even where they handle 4 or 8 bytes at a time. cc. alt.lang.asm - an x86 forum James
On Tue, 02 Nov 2010 22:51:45 -0700, Tim Wescott <tim@seemywebsite.com>
wrote:

>On 11/02/2010 06:00 PM, Vladimir Vassilevsky wrote: >> >> >> Tim Wescott wrote: >>> Doing some work on a PC. Customer says it's not real time, but real >>> fast is still better than real slow. I need to bit-reverse a slew of >>> bytes, so the faster the better. >> >> What do you do? Mirroring some bitmaps? > >It's for a bunch of data that's recorded off of a 1-bit ADC -- they're >changing their frequency plan and need things converted, and by the way >they're changing from lsb first to msb first.
If your requirements are not worse than that and assuming 12-16 bit ADC, just use 4-64 KiW look up table. Prefetching the LUT might help in some cases, but in general I do not expect any significant improvement. With 16 bit ADCs, you could experiment with using a single 256 byte LUT and two translations/sample, however, it is hard to predict, if this is useful or not for a particular cache architecture.
On Tue, 2 Nov 2010 18:19:55 +0000 (UTC), Grant Edwards
<invalid@invalid.invalid> wrote:

>On 2010-11-02, Tim Wescott <tim@seemywebsite.com> wrote: > >> Doing some work on a PC. Customer says it's not real time, but real >> fast is still better than real slow. I need to bit-reverse a slew of >> bytes, so the faster the better. >> >> AFAIK the Pentium instruction set doesn't have a bit-reverse >> instruction, and there certainly isn't one available from C. My >> knee-jerk reaction to this problem is to make up a table of >> bit-reversed bytes, and do a lookup to retrieve them. > >That's how I've always done it. > >> Anyone know a faster way? > >If there's no bit-reverse instruction, I can't really imagine anything >faster than doing a lookup in a 256-byte table. > >I do know somebody (a HW engineer) who once wired up two I/O ports so >that he could do a bit-reversal by writing to one port and reading >from the other. However, on most machines that's still not any faster >than a lookup table (and probably not something you can easily do on a >PC).
Hello Grant, Ha ha. I like that "hard-wired" idea. Neat. [-Rick-]
Not knowing anything past 586, I'd do it something like:

mov eax,[table] ;aligned on 256 bytes
mov ecx,[src]
mov edx,[dst]
dec ebx ;count

loop:
mov al,[ecx+ebx]
mov al,[eax]
mov [edx+ebx],al
dec ebx
jns loop