flat assembler
Message board for the users of flat assembler.

 Index > Macroinstructions > crc32 calculation using crc32 opcode
Author
CandyMan

Joined: 04 Sep 2009
Posts: 346
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan
Code:
; rdx - String, cl - Length
CalculateCRC32:
if RawMode
xor     r8d,r8d
or      eax,-1
lea     r9,[TableCRC32]
.1:     mov     r8b,[rdx]
inc     rdx
xor     r8b,al
shr     eax,8           ; Crc shr 8
xor     eax,[r8*4+r9]   ; Tab[Crc xor Byte] xor (Crc shr 8)
dec     cl
jnz     .1
not     eax             ;xor eax,-1
else
xor     eax,eax
.1:     crc32   eax,byte [rdx]
inc     rdx
dec     cl
jnz     .1
end if
ret

I want both methods to give the same results. How to use crc32 opcode? What is badly?

_________________
smaller is better
23 Mar 2017, 16:23
redsock

Joined: 09 Oct 2009
Posts: 378
Location: Australia
redsock
Since you didn't include a reference to your CRC32 table(s), the CRC32 instruction does the fixed Castagnoli polynomial (0x11EDC6F41) which is incompatible with most of the things I needed CRC32 for. See my own implementation at https://2ton.com.au/library_as_html/crc.inc.html. Also see Mark Adler's (zlib author) answer here for CRC32C http://stackoverflow.com/questions/17645167/implementing-sse-4-2s-crc32c-in-software and this discussion over at Intel: https://software.intel.com/en-us/forums/intel-isa-extensions/topic/302146

_________________
2 Ton Digital - https://2ton.com.au/
23 Mar 2017, 21:14
CandyMan

Joined: 04 Sep 2009
Posts: 346
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan
Code:

; CRC32C sample calculation for small strings using Intel CRC32 instruction and without it

format PE console
entry Start
stack 4096

CRC32X_POLYNOMIAL = 0xEDB88320  ;CRC32
CRC32C_POLYNOMIAL = 0x82F63B78  ;CRC32C

macro CRC32Ini
{
local R
virtual at 0
CRCStart::
CRC32Table rd 256
end virtual

repeat 256
R = %-1
repeat 8
R = R shr 1 xor (CRC32C_POLYNOMIAL * R and 1)
end repeat
store dword R at CRCStart:+%*4-4
end repeat
}

macro CRC32Str
{
CRC32Value = 0xFFFFFFFF
}

macro CRC32End Name
{
local P,T,NameStart
virtual at 0
NameStart::
db Name
Length = \$
end virtual

repeat Length
load T dword from CRCStart:+(P xor (CRC32Value and 0xFF))*4
CRC32Value = CRC32Value shr 8 xor T
end repeat
CRC32Value = CRC32Value xor 0xFFFFFFFF
}

macro DoCRC32 Name
{
CRC32Str
CRC32End Name
dd CRC32Value
}

CRC32Ini
DoCRC32 "This is a very long string which is used to test the CRC-32-Castagnoli function."

Str0 db "This is a very long string which is used to test the CRC-32-Castagnoli function."
StrE:

Start:
mov     ebx,Str0        ;Start
mov     ecx,StrE        ;Finit
call    CalcCRC32C      ;eax=0x20CB1E59

call    MakeCRC32Table

mov     edx,Str0        ;Start
mov     ecx,StrE        ;Finit
call    CalcCRC32T      ;eax=0x20CB1E59
ret

CalcCRC32C:
or      eax,-1
@@:
crc32   eax,byte [ebx]
inc     ebx
cmp     ebx,ecx
jnz     @B
not     eax             ;xor eax,-1
ret

CalcCRC32T:
xor     ebx,ebx
or      eax,-1
@@:     mov     bl,[edx]
inc     edx
xor     bl,al
shr     eax,8           ;Crc shr 8
xor     eax,[ebx*4+TableCRC32]  ;Tab[Crc xor Byte] xor (Crc shr 8)
cmp     edx,ecx
jnz     @B
not     eax             ;xor eax,-1
ret

MakeCRC32Table:
push    edi
lea     edi,[TableCRC32]
mov     ecx,CRC32C_POLYNOMIAL
xor     edx,edx
.NextI: mov     eax,edx
mov     dh,8
.LoopI: shr     eax,1
jnc     @F
xor     eax,ecx
@@:
dec     dh
jnz     .LoopI
stosd
inc     dl
jnz     .NextI
pop     edi
ret

align 4
TableCRC32      rd 256

_________________
smaller is better
03 May 2019, 16:09
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 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 forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum