Browse Source

slightly improved hash values

Darren Ranalli 21 years ago
parent
commit
798348ea12
2 changed files with 9 additions and 3 deletions
  1. 8 3
      panda/src/express/patchfile.cxx
  2. 1 0
      panda/src/express/patchfile.h

+ 8 - 3
panda/src/express/patchfile.cxx

@@ -94,11 +94,12 @@ const PN_uint32 Patchfile::_magic_number = 0xfeebfaac;
 // To version 2 on 11/2/02 to store copy offsets as relative.
 // To version 2 on 11/2/02 to store copy offsets as relative.
 const PN_uint16 Patchfile::_current_version = 2;
 const PN_uint16 Patchfile::_current_version = 2;
 
 
-const PN_uint32 Patchfile::_HASHTABLESIZE = PN_uint32(1) << 24;
+const PN_uint32 Patchfile::_HASH_BITS = 24;
+const PN_uint32 Patchfile::_HASHTABLESIZE = PN_uint32(1) << Patchfile::_HASH_BITS;
 const PN_uint32 Patchfile::_DEFAULT_FOOTPRINT_LENGTH = 9; // this produced the smallest patch file for libpanda.dll when tested, 12/20/2000
 const PN_uint32 Patchfile::_DEFAULT_FOOTPRINT_LENGTH = 9; // this produced the smallest patch file for libpanda.dll when tested, 12/20/2000
 const PN_uint32 Patchfile::_NULL_VALUE = PN_uint32(0) - 1;
 const PN_uint32 Patchfile::_NULL_VALUE = PN_uint32(0) - 1;
 const PN_uint32 Patchfile::_MAX_RUN_LENGTH = (PN_uint32(1) << 16) - 1;
 const PN_uint32 Patchfile::_MAX_RUN_LENGTH = (PN_uint32(1) << 16) - 1;
-const PN_uint32 Patchfile::_HASH_MASK = (PN_uint32(1) << 24) - 1;
+const PN_uint32 Patchfile::_HASH_MASK = (PN_uint32(1) << Patchfile::_HASH_BITS) - 1;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: Patchfile::Constructor
 //     Function: Patchfile::Constructor
@@ -543,10 +544,14 @@ calc_hash(const char *buffer) {
   for(int i = 0; i < (int)_footprint_length; i++) {
   for(int i = 0; i < (int)_footprint_length; i++) {
     // this is probably not such a good hash. to be replaced
     // this is probably not such a good hash. to be replaced
     /// --> TRIED MD5, was not worth it for the execution-time hit on 800Mhz PC
     /// --> TRIED MD5, was not worth it for the execution-time hit on 800Mhz PC
-    hash_value ^= (*buffer) << ((i % _footprint_length) * 2);
+    hash_value ^= PN_uint32(*buffer) << ((i * 2) % Patchfile::_HASH_BITS);
     buffer++;
     buffer++;
   }
   }
 
 
+  // use the bits that overflowed past the end of the hash bit range
+  // (this is intended for _HASH_BITS == 24)
+  hash_value ^= (hash_value >> Patchfile::_HASH_BITS);
+
   //cout << hash_value << " ";
   //cout << hash_value << " ";
 
 
   return hash_value & _HASH_MASK;
   return hash_value & _HASH_MASK;

+ 1 - 0
panda/src/express/patchfile.h

@@ -96,6 +96,7 @@ private:
                  PN_uint32 COPY_pos, PN_uint32 last_copy_pos,
                  PN_uint32 COPY_pos, PN_uint32 last_copy_pos,
                  PN_uint32 ADD_pos);
                  PN_uint32 ADD_pos);
 
 
+  static const PN_uint32 _HASH_BITS;
   static const PN_uint32 _HASHTABLESIZE;
   static const PN_uint32 _HASHTABLESIZE;
   static const PN_uint32 _DEFAULT_FOOTPRINT_LENGTH;
   static const PN_uint32 _DEFAULT_FOOTPRINT_LENGTH;
   static const PN_uint32 _NULL_VALUE;
   static const PN_uint32 _NULL_VALUE;