[Pascal] 纯文本查看 复制代码
program fastcrc32c;
{$mode objfpc}
{$AsmMode Intel}
uses
SysUtils;
const
bufsize = 1024*1024*1024; // 1GiB
function crc32_1(crc:DWord; buf:Pointer; len:PtrInt):DWord;assembler;nostackframe;
asm
@start:
xor rax, rax
mov eax, ecx
xor eax, $FFFFFFFF
@check:
cmp r8, 0
jle @out
@loop:
cmp r8, 8
jl @loop2
mov rcx, [rdx]
crc32 rax, rcx // 一次处理8字节
add rdx, 8
sub r8, 8
jmp @loop
@loop2:
cmp r8, 0
je @out
mov cl, [rdx]
crc32 eax, cl
inc rdx
dec r8
jmp @loop2
@out:
xor eax, $FFFFFFFF
end;
function crc32_2(crc:DWord; buf:Pointer; len:PtrInt):DWord;assembler;nostackframe;
asm
@start:
xor rax, rax
mov eax, ecx
xor eax, $FFFFFFFF
@check:
cmp r8, 0
jle @out
@loop:
cmp r8, 4
jl @loop2
mov ecx, [rdx]
crc32 eax, ecx // 一次处理4字节
add rdx, 4
sub r8, 4
jmp @loop
@loop2:
cmp r8, 0
je @out
mov cl, [rdx]
crc32 eax, cl
inc rdx
dec r8
jmp @loop2
@out:
xor eax, $FFFFFFFF
end;
var
buf:Pointer;
t:QWord;
c:DWord;
begin
GetMem(buf,bufsize);
FillChar(buf^,bufsize,1); // 相当于用memset置1
t:=GetTickCount64;
c:=crc32_1($ffffffff,buf,bufsize);
t:=GetTickCount64-t;
Writeln('Result: ',c,', ',t,' ms');
t:=GetTickCount64;
c:=crc32_2($ffffffff,buf,bufsize);
t:=GetTickCount64-t;
Writeln('Result: ',c,', ',t,' ms');
Freemem(buf);
Readln;
end.