Explorar o código

minor refactoring in crc

Ugochukwu Mmaduekwe %!s(int64=6) %!d(string=hai) anos
pai
achega
8f033110cf

+ 8 - 1
src/libraries/hashlib4pascal/HlpCRC32.pas

@@ -8,6 +8,7 @@ uses
 
   HlpHashLibTypes,
   HlpHash,
+  HlpIHash,
   HlpICRC,
   HlpIHashResult,
   HlpIHashInfo,
@@ -41,6 +42,7 @@ type
     procedure TransformBytes(const a_data: THashLibByteArray;
       a_index, a_length: Int32); override;
     function TransformFinal(): IHashResult; override;
+    function Clone(): IHash; override;
 
   end;
 
@@ -62,6 +64,11 @@ implementation
 
 { TCRC32 }
 
+function TCRC32.Clone(): IHash;
+begin
+  Result := FCRCAlgorithm.Clone();
+end;
+
 constructor TCRC32.Create(_poly, _Init: UInt64; _refIn, _refOut: Boolean;
   _XorOut, _check: UInt64; const _Names: THashLibStringArray);
 begin
@@ -83,7 +90,7 @@ end;
 
 function TCRC32.TransformFinal: IHashResult;
 begin
-  result := FCRCAlgorithm.TransformFinal();
+  Result := FCRCAlgorithm.TransformFinal();
 end;
 
 { TCRC32_PKZIP }

+ 23 - 12
src/libraries/hashlib4pascal/HlpCRC32Fast.pas

@@ -40,7 +40,8 @@ type
   strict private
 
   const
-    CRC32_PKZIP_Polynomial = UInt32($EDB88320); // Polynomial Reversed
+    // Polynomial Reversed
+    CRC32_PKZIP_Polynomial = UInt32($EDB88320);
     class var
 
       FCRC32_PKZIP_Table: THashLibUInt32Array;
@@ -94,14 +95,23 @@ begin
       LKIdx := 0;
       while LKIdx < System.Pred(9) do
       begin
-        if (LRes and 1) = 1 then
-        begin
+        { *
+          // branched variant
+          if (LRes and 1) = 1 then
+          begin
           LRes := APolynomial xor (LRes shr 1)
-        end
-        else
-        begin
+          end
+          else
+          begin
           LRes := LRes shr 1;
-        end;
+          end;
+          * }
+        { *
+          // branchless variant
+          LRes := (LRes shr 1) xor (LRes and 1) * APolynomial;
+          * }
+        // faster branchless variant
+        LRes := (LRes shr 1) xor (-Int32(LRes and 1) and APolynomial);
         Result[(LJIdx * 256) + LIdx] := LRes;
         System.Inc(LKIdx);
       end;
@@ -115,7 +125,7 @@ var
   LCRC, LA, LB, LC, LD: UInt32;
   LCRCTable: THashLibUInt32Array;
 begin
-  LCRC := not FCurrentCRC;
+  LCRC := not FCurrentCRC; // LCRC := System.High(UInt32) xor FCurrentCRC;
   LCRCTable := ACRCTable;
   while ALength >= 16 do
   begin
@@ -135,9 +145,10 @@ begin
       [(9 * 256) + AData[AIndex + 6]] xor LCRCTable
       [(8 * 256) + AData[AIndex + 7]];
 
-    LD := LCRCTable[(15 * 256) + (Byte(LCRC) xor AData[AIndex])] xor LCRCTable
-      [(14 * 256) + (Byte(LCRC shr 8) xor AData[AIndex + 1])] xor LCRCTable
-      [(13 * 256) + (Byte(LCRC shr 16) xor AData[AIndex + 2])] xor LCRCTable
+    LD := LCRCTable[(15 * 256) + ((LCRC and $FF) xor AData[AIndex])
+      ] xor LCRCTable[(14 * 256) + (((LCRC shr 8) and $FF) xor AData[AIndex + 1]
+      )] xor LCRCTable[(13 * 256) + (((LCRC shr 16) and $FF) xor AData
+      [AIndex + 2])] xor LCRCTable
       [(12 * 256) + ((LCRC shr 24) xor AData[AIndex + 3])];
 
     LCRC := LD xor LC xor LB xor LA;
@@ -153,7 +164,7 @@ begin
     System.Dec(ALength);
   end;
 
-  FCurrentCRC := not LCRC;
+  FCurrentCRC := not LCRC; // FCurrentCRC := LCRC xor System.High(UInt32);
 end;
 
 constructor TCRC32Fast.Create();

+ 10 - 3
src/libraries/hashlib4pascal/HlpCRC64.pas

@@ -7,6 +7,7 @@ interface
 uses
   HlpHashLibTypes,
   HlpHash,
+  HlpIHash,
   HlpICRC,
   HlpIHashResult,
   HlpIHashInfo,
@@ -38,10 +39,11 @@ type
     procedure TransformBytes(const a_data: THashLibByteArray;
       a_index, a_length: Int32); override;
     function TransformFinal(): IHashResult; override;
+    function Clone(): IHash; override;
 
   end;
 
-  TCRC64_ECMA = class sealed(TCRC64)
+  TCRC64_ECMA_182 = class sealed(TCRC64)
 
   public
     constructor Create();
@@ -52,6 +54,11 @@ implementation
 
 { TCRC64 }
 
+function TCRC64.Clone(): IHash;
+begin
+  result := FCRCAlgorithm.Clone();
+end;
+
 constructor TCRC64.Create(_poly, _Init: UInt64; _refIn, _refOut: Boolean;
   _XorOut, _check: UInt64; const _Names: THashLibStringArray);
 begin
@@ -76,9 +83,9 @@ begin
   result := FCRCAlgorithm.TransformFinal();
 end;
 
-{ TCRC64_ECMA }
+{ TCRC64_ECMA_182 }
 
-constructor TCRC64_ECMA.Create;
+constructor TCRC64_ECMA_182.Create;
 begin
   Inherited Create(TCRC64Polynomials.ECMA_182, $0000000000000000, false, false,
     $0000000000000000, $6C40DF5F0B497347,

+ 3 - 3
src/libraries/hashlib4pascal/HlpHashFactory.pas

@@ -150,7 +150,7 @@ type
       /// ECMA-182, polynomial = $42F0E1EBA9EA3693
       /// </summary>
       /// <returns></returns>
-      class function CreateCRC64_ECMA(): IHash; static;
+      class function CreateCRC64_ECMA_182(): IHash; static;
 
       class function CreateAdler32: IHash; static;
     end;
@@ -497,9 +497,9 @@ begin
     _check, _Names);
 end;
 
-class function THashFactory.TChecksum.CreateCRC64_ECMA: IHash;
+class function THashFactory.TChecksum.CreateCRC64_ECMA_182: IHash;
 begin
-  Result := TCRC64_ECMA.Create();
+  Result := TCRC64_ECMA_182.Create();
 end;
 
 class function THashFactory.TChecksum.CreateAdler32: IHash;