"James Harris" <james.harris.1@googlemail.com> wrote in message news:f79c61e2-acf7-45a3-b27e-a61827d5029e@g25g2000yqn.googlegroups.com...> 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 >Your first idea should be to use assembly. Implementing the routine in C will likely be worse than assembly, whether it's shift based or table lookup. Most C compilers don't effectively use the x86 instruction set for such situations, and/or the C routine uses more shifts etc. than assembly would.> 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.... >There's an x86 instruction for the byte table lookup. It's called XLAT. Although, MOV is probably faster on modern x86's, due to x86 instruction pipelining.> 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. >For 4 or 8 bytes, using the x86 BSWAP instruction will reverse the byte order, so you can then use a table lookup for the bytes. Of course, one issue with the XLAT instruction is moving each byte into AL to do the lookup. You'll probably need a few intermediate registers and additional reordering instructions to place each byte into AL, e.g., XCHG AH,AL and SHR EAX,16, etc. However, this should be as simple as a hex display routine. Even without using XLAT, say using MOV, you'd still need to access 32-bit or 64-bit value as bytes to use a lookup table. x86 only allows the lower 16-bits of a register, as two byte registers, to be accessed directly as bytes. This is only for some of the original registers, and not the new registers. So, some shifts, rotates, BSWAPs or XCHGs are needed. Of course, you can do it entirely with shifts (SHR/SHL/SAL), as you would in C. I think that's probably slower, however. Rotates (ROR/ROL/RCL/RCR) could be used also, with similar speed. If you decide to use shifts, shifts and masks with a couple registers can be optimized to handle multiple bytes at once, like this hex routine handles multiple nybbles at once using two registers: http://groups.google.com/group/comp.lang.asm.x86/msg/7571b4b71a5d21f7 Of course, whatever you come up with, you may want to test for speed against other solutions. It seems Nils' posted MMX solution in the original c.a.e. thread. An MMX solution could be faster. Rod Pemberton
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 ●November 2, 2010
Reply by ●November 4, 20102010-11-04
Reply by ●November 4, 20102010-11-04
James Harris copied to ALA: and I have no access to comp.dsp, comp.arch.embedded ...>> 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.My first thought about a 1-bit ADC was: "must be a digital device" :) Ok, serial input ADC are a bit cheaper than parallel ones especially if unbuffered. MSB first does make sense to overcome settling issues. Fast buffered flash-ADC ask for more U$s, so I think we see one more time a merchants decision for a cost-saving change which usually end up with workarounds and unsatisfied customers.> 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....Yes, if it's an 8-bit ADC here, but when I look at audio-recorder, voltage/temperature-sensors and graphic aquisition ADC, there are 12...24 bits to convert. It would work with the byte table as well, even some more lines are required then.> 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.Right, unfortunately we haven't got bit reversal functions on x86 except SHL-SHR for every single bit, which is somehow slow, but I know that LIFO-hardware and wired converters exists since digital electronic joined in. Modern CPU hardware could have parallel bit reversion within one cycle by adding a gate and a few wires. And if I'd had a problem like reported here, I'd add/modify/replace a few hardware-parts :)> cc. alt.lang.asm - an x86 forum__ wolfgang (ALA)
Reply by ●November 4, 20102010-11-04
In article <iatru8$jgo$1@speranza.aioe.org>, do_not_have@notreplytome.cmm says...> "James Harris" <james.harris.1@googlemail.com> wrote in message > news:f79c61e2-acf7-45a3-b27e-a61827d5029e@g25g2000yqn.googlegroups.com... > > 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 > > > > Your first idea should be to use assembly. Implementing the routine in C > will likely be worse than assembly, whether it's shift based or table > lookup. Most C compilers don't effectively use the x86 instruction set for > such situations, and/or the C routine uses more shifts etc. than assembly > would. > > > 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.... > > > > There's an x86 instruction for the byte table lookup. It's called XLAT. > Although, MOV is probably faster on modern x86's, due to x86 instruction > pipelining. > > > 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. > > > > For 4 or 8 bytes, using the x86 BSWAP instruction will reverse the byte > order, so you can then use a table lookup for the bytes. Of course, one > issue with the XLAT instruction is moving each byte into AL to do the > lookup. You'll probably need a few intermediate registers and additional > reordering instructions to place each byte into AL, e.g., XCHG AH,AL and > SHR EAX,16, etc. However, this should be as simple as a hex display > routine. Even without using XLAT, say using MOV, you'd still need to access > 32-bit or 64-bit value as bytes to use a lookup table. x86 only allows the > lower 16-bits of a register, as two byte registers, to be accessed directly > as bytes. This is only for some of the original registers, and not the new > registers. So, some shifts, rotates, BSWAPs or XCHGs are needed. Of > course, you can do it entirely with shifts (SHR/SHL/SAL), as you would in C. > I think that's probably slower, however. Rotates (ROR/ROL/RCL/RCR) could be > used also, with similar speed. If you decide to use shifts, shifts and > masks with a couple registers can be optimized to handle multiple bytes at > once, like this hex routine handles multiple nybbles at once using two > registers: > http://groups.google.com/group/comp.lang.asm.x86/msg/7571b4b71a5d21f7 > > Of course, whatever you come up with, you may want to test for speed against > other solutions. It seems Nils' posted MMX solution in the original c.a.e. > thread. An MMX solution could be faster. > >Does the incoming data have to be available on an immediate byte-for- byte basis? If not, would it be better to accumulate some number of bytes (16 to 512, say), then bit reverse the buffer with a tight loop and lookup table. This might reduce cache-miss problems during the bit reversal. If the bytes are arriving with strict timing intervals, you could set things up so that input bytes are stuffed into one buffer, while bytes are fetched from another for processing. That would result in a timing delay, but no jitter. If you're recording the data to a file, just bit-reverse the buffer prior to the fwrite(). I must say that worrying about "fast" on systems with large caches and an unknown number of interrupts to interefere with cache management is a matter of theoretical curiosity for me. I have no real experience with assembly-language programming on X86 CPUs with on-chip or off-chip cache. Mark Borgerson
Reply by ●November 4, 20102010-11-04
On Nov 4, 2:17�pm, "Harold Aptroot" <harold.aptr...@gmail.com> wrote:> "James Harris" <james.harri...@googlemail.com> wrote in message > > news:f79c61e2-acf7-45a3-b27e-a61827d5029e@g25g2000yqn.googlegroups.com... > > > > > 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 > > Is one of those schemes "2 pshufb's and some magic"?No. It would be interesting to see a solution based on pshufb - if you are offering. I was thinking of things like A 16-entry table (instead of a 256-entry one) used twice Paired shift lefts and shift rights Swapping half-bytes then bit pairs then bits Methods in http://www-graphics.stanford.edu/~seander/bithacks.html MMX/SSE instructions The simple table lookup still seems best - in so many ways.> If not, how would it compare to the "regular" version?Not sure I understand the question. Just a word on caches that others have objected to: 1. Size - Even the original Pentium processor had an 8k data cache. Modern machines have much more caching. A 256-byte lookup table would not normally be a problem. 2. Populating the cache - Also not a problem. If a cache-line's worth of the lookup table is used only once it adds less than a microsecond to the run time of the program. If it's used repeatedly then subsequent reads come from cache. James
Reply by ●November 4, 20102010-11-04
James Harris said:>> 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> Is one of those schemes "2 pshufb's and some magic"?|No. It would be interesting to see a solution based on pshufb - if you |are offering. | |I was thinking of things like | | A 16-entry table (instead of a 256-entry one) used twice | Paired shift lefts and shift rights | Swapping half-bytes then bit pairs then bits | Methods in http://www-graphics.stanford.edu/~seander/bithacks.html | MMX/SSE instructions | |The simple table lookup still seems best - in so many ways. Yes, I can confirm this from practice. A faster method can only be a hardware-solution.> If not, how would it compare to the "regular" version?|Not sure I understand the question. yeah, what's "regular" today ? perhaps 'not SSE3/4a/5b/6c..' ? :) I'll interprete it as: 'without SSE/3DNow! at all' A regular loop "SHL-SHR per bit" variant would be at least four times slower for single byte conversion and about 12 times slower for 32-bit. |Just a word on caches that others have objected to: | |1. Size - Even the original Pentium processor had an 8k data cache. |Modern machines have much more caching. A 256-byte lookup table would |not normally be a problem. | |2. Populating the cache - Also not a problem. If a cache-line's worth |of the lookup table is used only once it adds less than a microsecond |to the run time of the program. If it's used repeatedly then |subsequent reads come from cache. Exact, except (you're right: it's less than a micro-second) I see just a few "nano"-seconds for four cache-burst-reads (64 byte junks) on a 3GHz CPU with 1600 MHz RAM. Each of my four cores got 6 MB L3 cache, 512KB L2 and 2*64K data/code on L1, so a 64K LUT may fit well into. __ wolfgang
Reply by ●November 4, 20102010-11-04
On Nov 4, 9:16�pm, "wolfgang kern" <nowh...@never.at> wrote:> James Harris said:...> |2. Populating the cache - Also not a problem. If a cache-line's worth > |of the lookup table is used only once it adds less than a microsecond > |to the run time of the program. If it's used repeatedly then > |subsequent reads come from cache. > > Exact, except (you're right: it's less than a micro-second) > I see just a few "nano"-seconds for four cache-burst-reads > (64 byte junks) on a 3GHz CPU with 1600 MHz RAM. > Each of my four cores got 6 MB L3 cache, 512KB L2 and 2*64K data/code > on L1, so a 64K LUT may fit well into.Yes, I was being a bit vague on uncached DRAM timing due to ignorance. I was thinking of a 60 ns value but being unsure of what all the detailed DRAM timings were for I figured "less than a microsecond" would cover it. With a 32-byte data cache line size I guess the entire 256-byte table (eight cache lines) could be loaded in about a microsecond - which makes the point even more forcefully. James
Reply by ●November 4, 20102010-11-04
"James Harris" <james.harris.1@googlemail.com> wrote in message news:4bd687e7-e63f-4738-b88d-fced8d0cc82c@z19g2000yqb.googlegroups.com...> On Nov 4, 2:17 pm, "Harold Aptroot" <harold.aptr...@gmail.com> wrote:> > "James Harris" <james.harri...@googlemail.com> wrote in message > > news:f79c61e2-acf7-45a3-b27e-a61827d5029e@g25g2000yqn.googlegroups.com...> > > 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> > Is one of those schemes "2 pshufb's and some magic"?> No. It would be interesting to see a solution based on pshufb - if you > are offering.> I was thinking of things like> A 16-entry table (instead of a 256-entry one) used twice > Paired shift lefts and shift rights > Swapping half-bytes then bit pairs then bits > Methods in http://www-graphics.stanford.edu/~seander/bithacks.html > MMX/SSE instructionsYes, like (Warning! untested code follows): movdqa xmm1, magic1 ; Assume 16 bytes to be reversed are in xmm0 pand xmm1, xmm0 ; xmm1 has high nibbles pxor xmm0, xmm1 ; xmm0 has low nibbles psrld xmm1, 4 ; Shift high nibbles to low position movdqa xmm2, magic2 ; High reversed LUT pshufb xmm2, xmm0 ; xmm2 has high nibbles of reversed bytes movdqa xmm0, magic3 ; Low reversed LUT pshufb xmm0, xmm1 ; xmm0 has low nibbles of reversed bytes por xmm0, xmm2 ; Now xmm0 has the 16 reversed bytes .data .align 16 magic1 db 0f0h,0f0h,0f0h,0f0h,0f0h,0f0h,0f0h,0f0h db 0f0h,0f0h,0f0h,0f0h,0f0h,0f0h,0f0h,0f0h magic2 db 0h, 80h, 40h,0c0h, 20h,0a0h, 60h,0e0h db 10h, 90h, 50h,0d0h, 30h,0b0h, 70h,0f0h magic3 db 0h, 8h, 4h, 0ch, 2h, 0ah, 6h, 0eh db 1h, 9h, 5h, 0dh, 3h, 0bh, 7h, 0fh So that seems to me to be the fastest possibility. Shifting and masking is about all you can do if you don't have the mega-lookup instruction PSHUFB available: movdqa xmm1, magic1 ; Assume 16 bytes to be reversed are in xmm0 pand xmm1, xmm0 pxor xmm0, xmm1 psrld xmm1, 4 pslld xmm0, 4 por xmm0, xmm1 ; Groups of 4 bits have been swappedmovdqa xmm1, magic2 movdqa xmm1, magic2 pand xmm1, xmm0 pxor xmm0, xmm1 psrld xmm1, 2 pslld xmm0, 2 por xmm0, xmm1 ; Groups of 2 bits have been swappedmovdqa xmm1, magic2 movdqa xmm1, magic3 pand xmm1, xmm0 pxor xmm0, xmm1 psrld xmm1, 1 paddd xmm0, xmm0 por xmm0, xmm1 ; Now xmm0 has the 16 reversed bytes .data .align 16 magic1 db 16 dup(0f0h) magic2 db 16 dup(0cch) magic3 db 16 dup(0aah)> The simple table lookup still seems best - in so many ways.> > If not, how would it compare to the "regular" version?> Not sure I understand the question.> Just a word on caches that others have objected to:> 1. Size - Even the original Pentium processor had an 8k data cache. > Modern machines have much more caching. A 256-byte lookup table would > not normally be a problem.> 2. Populating the cache - Also not a problem. If a cache-line's worth > of the lookup table is used only once it adds less than a microsecond > to the run time of the program. If it's used repeatedly then > subsequent reads come from cache.If you want to be competitive with the above algorithms you are going to need an LUT capable of converting two bytes simultaneously. that would be 2**16*2 = 131072 bytes and may start to grow outside of L1 cache. Also if the data starts out in noncacheable memory, you would want to load it 16 bytes at a time if possible and then unpack it into byte or word registers to perform the lookups. It all depends on how the data gets into the processor. For example, there is a really cool algorithm for bit reversal of indices for FFTs but that isn't reversing data at all, just generating the bit-reversals for a set of numbers in sequence generating each pair exactly once. -- write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, & 6.0134700243160014d-154/),(/'x'/)); end
Reply by ●November 4, 20102010-11-04
"James Van Buskirk" <not_valid@comcast.net> wrote in message news:iav9jm$t4s$1@news.eternal-september.org...> "James Harris" <james.harris.1@googlemail.com> wrote in message > news:4bd687e7-e63f-4738-b88d-fced8d0cc82c@z19g2000yqb.googlegroups.com... > > [...] > > MMX/SSE instructions > > Yes, like (Warning! untested code follows): > > movdqa xmm1, magic1 ; Assume 16 bytes to be reversed are in xmm0 > pand xmm1, xmm0 ; xmm1 has high nibbles > pxor xmm0, xmm1 ; xmm0 has low nibbles > psrld xmm1, 4 ; Shift high nibbles to low position > movdqa xmm2, magic2 ; High reversed LUT > pshufb xmm2, xmm0 ; xmm2 has high nibbles of reversed bytes > movdqa xmm0, magic3 ; Low reversed LUT > pshufb xmm0, xmm1 ; xmm0 has low nibbles of reversed bytes > por xmm0, xmm2 ; Now xmm0 has the 16 reversed bytes > > .data > .align 16 > magic1 db 0f0h,0f0h,0f0h,0f0h,0f0h,0f0h,0f0h,0f0h > db 0f0h,0f0h,0f0h,0f0h,0f0h,0f0h,0f0h,0f0h > magic2 db 0h, 80h, 40h,0c0h, 20h,0a0h, 60h,0e0h > db 10h, 90h, 50h,0d0h, 30h,0b0h, 70h,0f0h > > magic3 db 0h, 8h, 4h, 0ch, 2h, 0ah, 6h, 0eh > db 1h, 9h, 5h, 0dh, 3h, 0bh, 7h, 0fh > > So that seems to me to be the fastest possibility. >Do you mean fastest for MMX/SSE? Or, do you mean fastest x86 solution overall?> > The simple table lookup still seems best - in so many ways. > > > > If not, how would it compare to the "regular" version? > > > Not sure I understand the question. > > > Just a word on caches that others have objected to: > > > 1. Size - Even the original Pentium processor had an 8k data cache. > > Modern machines have much more caching. A 256-byte lookup table would > > not normally be a problem. > > > 2. Populating the cache - Also not a problem. If a cache-line's worth > > of the lookup table is used only once it adds less than a microsecond > > to the run time of the program. If it's used repeatedly then > > subsequent reads come from cache. > > If you want to be competitive with the above algorithms you are > going to need an LUT capable of converting two bytes simultaneously. >Why? I don't understand why you'd need to convert two byte simultaneously to outperform that routine. Let's take your routine above. You used 18 instructions. The latencies are listed as 2 for each instruction used. They're all "DirectPath Double" at 1.5/cycle. That's 2*18/1.5=24 cycles. If we add a xmm register load and store at the start and end of the routine, that's 24+(5/1.5)= 27.3 cycles for 16 bytes, or 1.7 cycles per byte. Now, let's take a routine loop for 32-bits using bswap, xchg, and mov's and unroll it four times: (untested pseudo-code x86) (repeat this four times for 16 bytes) mov dword (load eax) mov byte (ah) mov byte (al) bswap dword (eax) mov byte (ah) mov byte (al) mov dword (store eax) They're all DirectPath Single at 3/cycle. So, that's ((7*1)/3)*4=9.3 cycles for 16 bytes, or 0.58 cycles per byte. ISTM, that standard x86 should be three times faster than your SSE2 code... Have I made a mistake somewhere? Doesn't that perform without converting two bytes simultaneously? Rod Pemberton
Reply by ●November 5, 20102010-11-05
On 5.11.10 2:33 , Rod Pemberton wrote:> Now, let's take a routine loop for 32-bits using bswap, xchg, and mov's and > unroll it four times: > > (untested pseudo-code x86) > (repeat this four times for 16 bytes) > > mov dword (load eax) > mov byte (ah) > mov byte (al) > bswap dword (eax) > mov byte (ah) > mov byte (al) > mov dword (store eax) > > They're all DirectPath Single at 3/cycle. So, that's ((7*1)/3)*4=9.3 cycles > for 16 bytes, or 0.58 cycles per byte. > > ISTM, that standard x86 should be three times faster than your SSE2 code... > > Have I made a mistake somewhere? Doesn't that perform without converting > two bytes simultaneously? > > > Rod PembertonAs it is, the pseudo-code reorders the bytes but it leaves the bit order inside the bytes intact. At least, you need to add a bit-reverse table lookup for handling each byte. -- Tauno Voipio tauno voipio (at) iki fi
Reply by ●November 5, 20102010-11-05
"Tauno Voipio" <tauno.voipio@notused.fi.invalid> wrote in message news:ib0dqc$qgl$2@news.eternal-september.org...> On 5.11.10 2:33 , Rod Pemberton wrote: >> Now, let's take a routine loop for 32-bits using bswap, xchg, and mov's >> and >> unroll it four times:>> (untested pseudo-code x86) >> (repeat this four times for 16 bytes)>> mov dword (load eax) >> mov byte (ah) >> mov byte (al) >> bswap dword (eax) >> mov byte (ah) >> mov byte (al) >> mov dword (store eax)>> They're all DirectPath Single at 3/cycle. So, that's ((7*1)/3)*4=9.3 >> cycles >> for 16 bytes, or 0.58 cycles per byte.>> ISTM, that standard x86 should be three times faster than your SSE2 >> code...>> Have I made a mistake somewhere? Doesn't that perform without converting >> two bytes simultaneously?> As it is, the pseudo-code reorders the bytes but it leaves the > bit order inside the bytes intact. At least, you need to add > a bit-reverse table lookup for handling each byte.I think that the pseudocode: mov byte(al) is intended to be the super-slow XLAT instruction, and the: mov byte(ah) is intented to be the nonexistent XLATH instruction. Then: bswap dword (eax) is intended to cause a partial register stall via the instruction: bswap eax So he would need another bswap instruction (and another PRS) to get the bytes back in order. Overall, perhaps an order of magnitude slower than the SSE2 code I posted, once he gets working code. Get the partial register stalls and other issues worked out and still significantly slower. -- write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, & 6.0134700243160014d-154/),(/'x'/)); end







