bit swap in byte


Author Message
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1154
Posted: 01:27pm 27 Jul 2022      

Just comparison purposes (This is a recompilation of the CMM2 version):
The BitOrderReverse CSUB for PicoMite takes 21us for a byte (8 bit) and 31us (23-34us) for 64 bit integers on a PicoMite at 378MHz (V5.07.05b15).
Execution time is not constant and varies somewhat with the number of ones.


Teraterm output:
BitOrderReverse Demo

Input:
10111100101111001011110010111100101111001011110010111100101111
Output for 8 Bits:
11110100


Time for 10000 loops:
CSUBs     Empty loops|  1x CSUB
250ms            45ms|   21us


Basic-source:
'File BitOrderReverse.bas written 10-09-2020 14:03:13
' v1.01 by twofingers@tbs (thanks to chris@TBS)
'
'a CSUB to reverse the order of bits for PicoMite
'Usage:
'CSUB BitOrderReverse(nInput, nBits, RetValue)

Dim integer a, b=0, nBits=8,l=10000

'   1234567890123456789012345678901234567890123456789012345678901234
'            1         2         3         4         5         6
a=&b0010111100101111001011110010111100101111001011110010111100101111

Print "BitOrderReverse Demo":Print

Print "Input:"
Print Bin$(a,nBits)

Timer =0
For i = 1 To l
BitOrderReverse(a,nBits,b)
Next i
t0=Timer


Timer =0
For i = 1 To l

Next i
t2=Timer

Print "Output for"nBits" Bits:"
Print Bin$(b,nBits)

Print :Print
Print "Time for"l " loops:"
Print "CSUBs","  Empty loops|","1x CSUB"
Print Cint(t0)"ms",,Cint(t2)"ms|",Cint((t0-t2)/l*1000)"us";

End

'File bitreverse.bas written 27-07-2022 17:49:23 v1.44 (optimized: -ofast)
CSub BitOrderReverse
00000000
2300B5F0 46DE4657 4645464E 00104684 B5E02200 60436002 684A680B B085469A
DD672A00 681B4663 46992400 685B4663 46982500 91022301 21209003 00274249
D43D1866 40B10019 001E468B 40A64649 400E4640 40014659 D037430E 37014652
22201BD7 18BA4252 0019D43E 91014091 40BA001A 92009803 68476806 9A019900
41571876 60476006 26019902 2700680A 20024692 2100684A 416F1936 414D1824
DC2B4295 4661D031 00346809 46614689 003D6849 21204688 00274249 D5C11866
1B0E2120 40F10019 E7BE468B 27002601 21002002 416F1936 414D1824 DC0D4295
0034D016 E7A8003D 00192220 40D11BD2 E7BD9101 D1012A00 D1932B00 BCF0B005
46B246BB 46A046A9 4554BDF0 E7F5D9CB D9E64554 46C0E7F2
End CSUB


C-source:
#include "PicoCFunctions.h"

void borev(long long *orig, long long *nbits, long long *reversed)
{
 int i = 1;
 *reversed = 0;
 unsigned long long one_bit = 1;

 for(;i<=*nbits;i++){
   if ((*orig & (one_bit<<(i-1))) > 0)
   *reversed += one_bit<<(*nbits-i);
 }
}


For infos about CSUBs on PicoMite:
https://www.thebackshed.com/forum/ViewTopic.php?TID=14126

Regards
Michael
Edited 2022-07-28 03:04 by twofingers