I could have sworn I posted this once, but I guess I didn't! Odd!
Anyway! This code is pretty much the fastest, and although there may be a few faster asm variants out there (they are mostly identical) they give diminishing returns, and they may need certain CPU features. This is definitely one for your "toolbox".
This is 1035% (10.35x) faster vs the loop above.
And this is 211% (2.11x) faster vs the lookup table above.
Test was done using a normal executable (no debugging, aka Shift+F5 was used in the PB IDE).
Each procedure was looped 100 000 000 times to test cacheability/assemblyline performance.
438ms total (for the procedure in this post).
4533ms total (for the loop code in the original post of this thread)
925ms total (for the lookup code in the post further up)
PS! For more performance simply inline the contents of the procedure into you main code loop (that handles the data you are processing). Replace "ProcedureReturn" with "!MOV [p.v_value],eax" in that case.
Code: Select all
Procedure.l ReverseBits(value.l)
!MOV eax,[p.v_value]
!MOV edx,eax ;Make a copy of the the data.
!SHR eax,1 ;Move the even bits to odd positions.
!AND edx,0x55555555 ;Isolate the odd bits by clearing even bits.
!AND eax,0x55555555 ;Isolate the even bits (in odd positions now).
!SHL edx,1 ;Move the odd bits to the even positions.
!OR eax,edx ;Merge the bits and complete the swap.
!MOV edx,eax ;Make a copy of the odd numbered bit pairs.
!SHR eax,2 ;Move the even bit pairs to the odd positions.
!AND edx,0x33333333 ;Isolate the odd pairs by clearing even pairs.
!AND eax,0x33333333 ;Isolate the even pairs (in odd positions now).
!SHL edx,2 ;Move the odd pairs to the even positions.
!OR eax,edx ;Merge the bits and complete the swap.
!MOV edx,eax ;Make a copy of the odd numbered nibbles.
!SHR eax,4 ;Move the even nibbles to the odd position.
!AND edx,0x0f0f0f0f ;Isolate the odd nibbles.
!AND eax,0x0f0f0f0f ;Isolate the even nibbles (in odd position now).
!SHL edx,4 ;Move the odd pairs to the even positions.
!OR eax,edx ;Merge the bits and complete the Swap.
!BSWAP eax ;Swap the bytes and words.
ProcedureReturn
EndProcedure