# PureBasic Forum

 It is currently Wed Feb 26, 2020 8:42 pm

 All times are UTC + 1 hour

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Configurable CRC procedure (1 - 32 bits)Posted: Sat Aug 24, 2019 3:34 pm
 PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3601
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

 Post subject: Re: Configurable CRC procedure (1 - 32 bits)Posted: Sat Aug 24, 2019 7:16 pm

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

_________________
Not a destination

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 2 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: mk-soft and 9 guests

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

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite