How to Optimize another CRC routine.

Everything else that doesn't fall into one of the other PB categories.
swhite
Addict
Addict
Posts: 808
Joined: Thu May 21, 2009 6:56 pm

How to Optimize another CRC routine.

Post by swhite »

Code: Select all

Procedure.a GetLRC(tcTxt.s)
   ;
   ; Assumes the text does not include the STX and ETX Characters
   ;
   Define lnLRC.i,ln,*ln1,lnLen.i
   lnLen=Len(tcTxt)-1
   *ln1=@tcTxt
   lnLRC=PeekA(*ln1)
   For ln=1 To lnLen
      lnLRC = lnLRC ! PeekA(*ln1+ln)
   Next
   If lnLRC=0
      lnLRC=255
   EndIf
   ProcedureReturn lnLRC
EndProcedure
The above code is used to produce a checksum for data sent to and from particular equipment we use. I received a lot for very good and appreciated help regarding my need for a CRC16CCITT algorithm so I wondered if someone might know how to optimize this in assembler.

Thanks,
Simon
Simon White
dCipher Computing
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to Optimize another CRC routine.

Post by wilbert »

Assuming you compile your application in ASCII mode, you can do it like this

Code: Select all

Procedure.a GetLRC(tcTxt.s)
  !xor eax, eax
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov rdx, [p.v_tcTxt]
    !getlrc0:
    !mov cl, [rdx]
    !inc rdx
  CompilerElse
    !mov edx, [p.v_tcTxt]
    !getlrc0:
    !mov cl, [edx]
    !inc edx
  CompilerEndIf
  !xor al, cl
  !and cl, cl
  !jnz getlrc0
  !and al, al
  !jnz getlrc1
  !mov al, 0xff
  !getlrc1:
  ProcedureReturn
EndProcedure

Debug GetLRC("hello")
Passing a pointer instead of a string might be faster.

Code: Select all

Procedure.a GetLRC(*tcTxt)
  !xor eax, eax
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov rdx, [p.p_tcTxt]
    !getlrc0:
    !mov cl, [rdx]
    !inc rdx
  CompilerElse
    !mov edx, [p.p_tcTxt]
    !getlrc0:
    !mov cl, [edx]
    !inc edx
  CompilerEndIf
  !xor al, cl
  !and cl, cl
  !jnz getlrc0
  !and al, al
  !jnz getlrc1
  !mov al, 0xff
  !getlrc1:
  ProcedureReturn
EndProcedure

Debug GetLRC(@"hello")
Windows (x64)
Raspberry Pi OS (Arm64)
swhite
Addict
Addict
Posts: 808
Joined: Thu May 21, 2009 6:56 pm

Re: How to Optimize another CRC routine.

Post by swhite »

I will give it a try thank-you.

Simon
Simon White
dCipher Computing
Post Reply