It is currently Wed Nov 20, 2019 8:34 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Configurable CRC procedure (1 - 32 bits)
PostPosted: Sat Aug 24, 2019 3:34 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3519
Location: Netherlands
After workin on a CRC module ( viewtopic.php?f=12&t=73462 ),
I realized that I could do most of it in a single procedure without the use of asm.
It's not as fast and doesn't support CRC with a width above 32 bits but seems to work fine.
In it's current configuration, the result is the same as the PB builtin CRC32 algorithm.

Some other often used configurations ...
CRC-16/CCITT: Width=16, Poly=$1021, Init=$ffff, RefIn=#False, RefOut=#False, XOrOut=0
CRC-16/MODBUS: Width=16, Poly=$8005, Init=$ffff, RefIn=#True, RefOut=#True, XOrOut=0

For more of them, see http://reveng.sourceforge.net/crc-catalogue/

Code:
Procedure.l CRC(*Buffer, Size)
 
  Static.l Width  = 32
  Static.l Poly   = $04c11db7
  Static.l Init   = -1
  Static.l RefIn  = #True
  Static.l RefOut = #True
  Static.l XOrOut = -1
 
  Static.l M
  Static Dim T.l(1023)
  Protected.l i,j,C,R,*B1.Ascii,*B4.Long
 
  ; Calculate CRC lookup table
  If Not M
    M=~((-2)<<(Width-1))
    If RefIn
      R=0: For i=1 To Width: R|((Poly>>(i-1))&1)<<(Width-i): Next
    Else
      R=Poly<<(32-Width)
    EndIf
    For i=0 To 255
      If RefIn
        C=i: For j=0 To 7: C=((C>>1)&$7fffffff)!((-(C&1))&R): Next
      Else
        C=i<<24: For j=0 To 7: C=(C<<1)!((C>>31)&R): Next
        C=(C<<24)|(((C>>8)&255)<<16)|(((C>>16)&255)<<8)|((C>>24)&255)
      EndIf
      T(i)=C
    Next
    For i=0 To 255
      C=T(i): For j=1 To 3: C=((C>>8)&$ffffff)!T(C&255): T((j<<8)|i)=C: Next
    Next   
  EndIf
 
  ; 4 byte loop
  C=Init&M: *B4=*Buffer
  While Size >= 4
    C!*B4\l: *B4+4: Size-4
    C=T(C&255+768)!T(((C>>8)&255)+512)!T(((C>>16)&255)+256)!T(((C>>24)&255))
  Wend
 
  ; 1 byte loop
  *B1=*B4
  While Size
    C=((C>>8)&$ffffff)!T((C!*B1\a)&255)
    *B1+1: Size-1
  Wend
 
  ; Finalize and output CRC
  If Not RefIn
    C=((C<<24)|(((C>>8)&255)<<16)|(((C>>16)&255)<<8)|((C>>24)&255))>>(32-Width)
  EndIf 
  If RefOut<>RefIn
    R=0: For i=1 To Width: R|((C>>(i-1))&1)<<(Width-i): Next: C=R
  EndIf
  ProcedureReturn (C!XOrOut)&M 
   
EndProcedure

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Configurable CRC procedure (1 - 32 bits)
PostPosted: Sat Aug 24, 2019 7:16 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4528
Location: Lyon - France
That works to on the same machine :wink:
I obtain "378246432" all the time
Code:
*Mem = AllocateMemory(1234567, #PB_Memory_NoClear)
RandomSeed(0)
RandomData(*Mem, 1234567)
Debug CRC(*Mem, 1234567)
FreeMemory(*Mem)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye