flat assembler
Message board for the users of flat assembler.

flat assembler > Macroinstructions > crc32 calculation using crc32 opcode

Author
Thread Post new topic Reply to topic
CandyMan



Joined: 04 Sep 2009
Posts: 294
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
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   ; TabCrc 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
Post 23 Mar 2017, 16:23
View user's profile Send private message Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 310
Location: Australia
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/
Post 23 Mar 2017, 21:14
View user's profile Send private message Reply with quote
CandyMan



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

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

format PE console
entry Start
stack 4096

section '.flat' readable writable

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 P byte from NameStart%-1
    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  ;TabCrc 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
Post 03 May 2019, 16:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


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


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.