Changeset 2667

Show
Ignore:
Timestamp:
11/06/08 22:03:36 (2 months ago)
Author:
maxr
Message:

ada: optimize crc32 calculation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tendra/src/producers/ada/libgela/gela-hash-crc-b32.adb

    r2639 r2667  
    1 with Ada.Unchecked_Conversion; 
    2  
    31package body Gela.Hash.CRC.b32 is 
    42 
     
    4846      3020668471, 3272380065, 1510334235, 755167117); 
    4947 
    50    type Byte_Array is 
    51      array (Natural range <>) of Interfaces.Unsigned_8; 
     48   subtype Byte is CRC32 range 0 .. 255; 
     49 
     50   procedure Update_Hash 
     51     (This  : in out Hasher; 
     52      Value : in     Byte); 
     53   pragma Inline (Update_Hash); 
     54 
     55   ------------ 
     56   -- Update -- 
     57   ------------ 
    5258 
    5359   procedure Update 
    5460     (This  : in out Hasher; 
    55       Value : in     Byte_Array); 
    56  
    57    ------------ 
    58    -- Update -- 
    59    ------------ 
    60  
    61    procedure Update 
    62      (This  : in out Hasher; 
    63       Value : in     String) 
    64    is 
    65       subtype C_Array is 
    66         Byte_Array (1 .. Value'Size / Interfaces.Unsigned_8'Size); 
    67  
    68       function To_Array is 
    69         new Ada.Unchecked_Conversion (String, C_Array); 
    70    begin 
    71       Update (This, To_Array (Value)); 
     61      Value : in     String) is 
     62   begin 
     63      This.Length := This.Length + Value'Length; 
     64 
     65      if This.Length > Maximum_Length then 
     66         raise Maximum_Length_Error; 
     67      end if; 
     68 
     69      for J in Value'Range loop 
     70         Update_Hash (This, Character'Pos (Value (J))); 
     71      end loop; 
    7272   end Update; 
    7373 
     
    7878   procedure Wide_Update 
    7979     (This  : in out Hasher; 
    80       Value : in     Wide_String) 
    81    is 
    82       subtype C_Array is 
    83         Byte_Array (1 .. Value'Size / Interfaces.Unsigned_8'Size); 
    84  
    85       function To_Array is 
    86         new Ada.Unchecked_Conversion (Wide_String, C_Array); 
    87    begin 
    88       Update (This, To_Array (Value)); 
     80      Value : in     Wide_String) is 
     81   begin 
     82      This.Length := This.Length + 2 * Value'Length; 
     83 
     84      if This.Length > Maximum_Length then 
     85         raise Maximum_Length_Error; 
     86      end if; 
     87 
     88      for J in Value'Range loop 
     89         Update_Hash (This, Wide_Character'Pos (Value (J)) and 16#FF#); 
     90         Update_Hash (This, Shift_Right (Wide_Character'Pos (Value (J)), 8)); 
     91      end loop; 
    8992   end Wide_Update; 
    9093 
     
    97100      Value : in     Wide_Wide_String) 
    98101   is 
    99       subtype C_Array is 
    100         Byte_Array (1 .. Value'Size / Interfaces.Unsigned_8'Size); 
    101  
    102       function To_Array is 
    103         new Ada.Unchecked_Conversion (Wide_Wide_String, C_Array); 
    104    begin 
    105       Update (This, To_Array (Value)); 
     102      subtype W is Wide_Wide_Character; 
     103   begin 
     104      This.Length := This.Length + 4 * Value'Length; 
     105 
     106      if This.Length > Maximum_Length then 
     107         raise Maximum_Length_Error; 
     108      end if; 
     109 
     110      for J in Value'Range loop 
     111         Update_Hash (This,              W'Pos (Value (J))      and 16#FF#); 
     112         Update_Hash (This, Shift_Right (W'Pos (Value (J)),  8) and 16#FF#); 
     113         Update_Hash (This, Shift_Right (W'Pos (Value (J)), 16) and 16#FF#); 
     114         Update_Hash (This, Shift_Right (W'Pos (Value (J)), 24)); 
     115      end loop; 
    106116   end Wide_Wide_Update; 
    107117 
     
    112122   procedure Update 
    113123     (This  : in out Hasher; 
    114       Value : in     Ada.Streams.Stream_Element_Array) 
    115    is 
    116       subtype C_Array is 
    117         Byte_Array (1 .. Value'Size / Interfaces.Unsigned_8'Size); 
    118  
    119       function To_Array is 
    120         new Ada.Unchecked_Conversion 
    121           (Ada.Streams.Stream_Element_Array, C_Array); 
    122    begin 
    123       Update (This, To_Array (Value)); 
     124      Value : in     Ada.Streams.Stream_Element_Array) is 
     125   begin 
     126      This.Length := This.Length + Value'Length; 
     127 
     128      if This.Length > Maximum_Length then 
     129         raise Maximum_Length_Error; 
     130      end if; 
     131 
     132      for J in Value'Range loop 
     133         Update_Hash (This, CRC32 (Value (J))); 
     134      end loop; 
    124135   end Update; 
    125136 
    126    -- Calculate -- 
    127    function Calculate 
    128      (Value : in String) 
    129       return CRC32 
    130    is 
     137   --------------- 
     138   -- Calculate -- 
     139   --------------- 
     140 
     141   function Calculate (Value : in String) return CRC32 is 
    131142      H : Hasher; 
    132143   begin 
     
    135146   end Calculate; 
    136147 
     148   -------------------- 
    137149   -- Wide_Calculate -- 
    138    function Wide_Calculate 
    139      (Value : in Wide_String) 
    140       return CRC32 
    141    is 
     150   -------------------- 
     151 
     152   function Wide_Calculate (Value : in Wide_String) return CRC32 is 
    142153      H : Hasher; 
    143154   begin 
     
    146157   end Wide_Calculate; 
    147158 
    148    -- Calculate -- 
    149    function Wide_Wide_Calculate 
    150      (Value : in Wide_Wide_String) 
    151       return CRC32 
    152    is 
     159   --------------- 
     160   -- Calculate -- 
     161   --------------- 
     162 
     163   function Wide_Wide_Calculate (Value : in Wide_Wide_String) return CRC32 is 
    153164      H : Hasher; 
    154165   begin 
     
    157168   end Wide_Wide_Calculate; 
    158169 
    159    -- Calculate -- 
     170   --------------- 
     171   -- Calculate -- 
     172   --------------- 
     173 
    160174   function Calculate 
    161175     (Value : in Ada.Streams.Stream_Element_Array) 
     
    168182   end Calculate; 
    169183 
     184   ------------- 
    170185   -- To_Hash -- 
    171    function To_Hash 
    172      (T : in CRC32) 
    173       return Hash_Type 
    174    is 
     186   ------------- 
     187 
     188   function To_Hash (T : in CRC32) return Hash_Type is 
    175189   begin 
    176190      return Hash_Type (T); 
    177191   end To_Hash; 
    178192 
    179    -- Calculate -- 
    180    function Calculate 
    181      (Value : in String) 
    182       return Hash_Type 
    183    is 
     193   --------------- 
     194   -- Calculate -- 
     195   --------------- 
     196 
     197   function Calculate (Value : in String) return Hash_Type is 
    184198   begin 
    185199      return To_Hash (Calculate (Value)); 
    186200   end Calculate; 
    187201 
     202   -------------------- 
    188203   -- Wide_Calculate -- 
    189    function Wide_Calculate 
    190      (Value : in Wide_String) 
    191       return Hash_Type 
    192    is 
     204   -------------------- 
     205 
     206   function Wide_Calculate (Value : in Wide_String) return Hash_Type is 
    193207   begin 
    194208      return To_Hash (Wide_Calculate (Value)); 
    195209   end Wide_Calculate; 
    196210 
    197    -- Calculate -- 
     211   --------------- 
     212   -- Calculate -- 
     213   --------------- 
     214 
    198215   function Wide_Wide_Calculate 
    199216     (Value : in Wide_Wide_String) 
     
    204221   end Wide_Wide_Calculate; 
    205222 
    206    -- Calculate -- 
     223   --------------- 
     224   -- Calculate -- 
     225   --------------- 
     226 
    207227   function Calculate 
    208228     (Value : in Ada.Streams.Stream_Element_Array) 
     
    217237   ------------ 
    218238 
    219    procedure Update 
    220      (This  : in out Hasher; 
    221       Value : in     Byte_Array) 
    222    is 
    223       use Interfaces; 
    224  
    225       Reg : CRC32 := This.Cm_Reg; 
    226    begin 
    227       This.Length := This.Length + Value'Length; 
    228  
    229       if This.Length > Maximum_Length then 
    230          raise Maximum_Length_Error; 
    231       end if; 
    232  
    233       for Index in Value'Range loop 
    234          Reg := Shift_Right (Reg, 8) xor 
    235            Keys (CRC32 (Value (Index)) xor (Reg and 16#0000_00FF#)); 
    236       end loop; 
    237  
    238       This.Cm_Reg := Reg; 
    239    end Update; 
     239   procedure Update_Hash 
     240     (This  : in out Hasher; 
     241      Value : in     Byte) 
     242   is 
     243   begin 
     244      This.Cm_Reg := Shift_Right (This.Cm_Reg, 8) xor 
     245        Keys (Value xor (This.Cm_Reg and 16#0000_00FF#)); 
     246   end Update_Hash; 
    240247 
    241248   ------------ 
     
    243250   ------------ 
    244251 
    245    function Result 
    246      (This : in Hasher) 
    247       return CRC32 
    248    is 
     252   function Result (This : in Hasher) return CRC32 is 
    249253   begin 
    250254      return This.Cm_Reg xor 16#FFFFFFFF#;