Skip to content

Commit

Permalink
crc32c 2x/4x speeup by using SSE4.2+pclmulqdq opcodes
Browse files Browse the repository at this point in the history
- defined in SynCrypto.pas, not in SynCommons.pas, to avoid .o/.obj dependencies
  • Loading branch information
Arnaud Bouchez committed Aug 21, 2017
1 parent e488bfa commit 7bf5951
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
71 changes: 71 additions & 0 deletions SynCrypto.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3608,6 +3608,9 @@ function SingleTest(const s: RawByteString; TDig: TSHA1Digest): boolean;
{$define AES_PASCAL} // Delphi XE2/XE3 do not have the AES-NI opcodes :(
{$endif}
{$define AESPASCAL_OR_CPU64}
{$ifndef BSD}
{$define CRC32C_X64} // external crc32_iscsi_01 for win64/lin64
{$endif}
{$else}
{$define USEAESNI}
{$define USEAESNI32}
Expand Down Expand Up @@ -12525,10 +12528,78 @@ function ToCaption(res: TJWTResult): string;

{$endif NOVARIANTS}

{$ifdef CRC32C_X64}
{ ISCSI CRC 32 Implementation with crc32 and pclmulqdq Instruction
Copyright(c) 2011-2015 Intel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICESLOSS OF USE,
DATA, OR PROFITSOR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. }

{$ifdef FPC}
{$ifdef MSWINDOWS}
{$L fpc-win64\crc32c64.o}
{$else}
{$L fpc-linux64/crc32c64.o}
{$endif}
{$else}
{$L crc32c64.obj}
{$endif}
// defined in SynCrypto.pas, not in SynCommons.pas, to avoid .o/.obj dependencies

function crc32_iscsi_01(buf: PAnsiChar; len: PtrUInt; crc: cardinal): cardinal; external;

function crc32c_sse42_aesni(crc: cardinal; buf: PAnsiChar; len: cardinal): cardinal;
{$ifdef MSWINDOWS}
asm
mov rax, rcx
test rdx, rdx
jz @none
not eax
mov rcx, rdx
mov rdx, r8
mov r8, rax
call crc32_iscsi_01
not eax
@none:
{$else}
begin
if buf=nil then
result := crc else
result := not crc32_iscsi_01(buf,len,not crc);
{$endif}
end;

{$endif CRC32C_X64}

initialization
ComputeAesStaticTables;
{$ifdef USEPADLOCK}
PadlockInit;
{$endif}
{$ifdef CRC32C_X64}
if (cfSSE42 in CpuFeatures) and (cfAesNi in CpuFeatures) then
crc32c := @crc32c_sse42_aesni; // use SSE4.2+pclmulqdq instructions
{$endif}
assert(sizeof(TMD5Buf)=sizeof(TMD5Digest));
assert(sizeof(TAESContext)=AESContextSize);
Expand Down
6 changes: 4 additions & 2 deletions SynSelfTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3034,8 +3034,8 @@ procedure Test(hash: THasher; const name: string);
for i := 0 to High(crc) do
with crc[i] do
Check(hash(0,pointer(s),length(s))=crc);
fRunConsole := format('%s %s %s %s/s',[fRunConsole,name,Timer.Stop,
KB(Timer.PerSec(totallen))]);
Timer.ComputeTime;
fRunConsole := format('%s %s %s/s',[fRunConsole,name,KB(Timer.PerSec(totallen))]);
end;
procedure test16(const text: RawUTF8; expected: cardinal);
begin
Expand Down Expand Up @@ -3118,6 +3118,8 @@ procedure test16(const text: RawUTF8; expected: cardinal);
{$ifdef CPUINTEL}
if cfSSE42 in CpuFeatures then
Test(crc32csse42,'sse42');
if (cfSSE42 in CpuFeatures) and (cfAesNi in CpuFeatures) then
Test(crc32c,'sse42+aesni'); // use SSE4.2+pclmulqdq instructions
{$endif}
exit; // code below is speed informative only, without any test
Timer.Start;
Expand Down
2 changes: 1 addition & 1 deletion SynopseCommit.inc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
'1.18.3799'
'1.18.3800'
Binary file added crc32c64.obj
Binary file not shown.

0 comments on commit 7bf5951

Please sign in to comment.