This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch, libgfortran] Better format hash function


Hi,

the attached patch replaces the existing (too?) simple string hash
function with an almost equally simple but supposedly pretty well
working one by D.J. Bernstein.

Regtested on x86_64-unknown-linux-gnu, Ok for trunk?

-- 
Janne Blomqvist

Attachment: ChangeLog
Description: Binary data

diff --git a/libgfortran/io/format.c b/libgfortran/io/format.c
index 2c116d6..886b524 100644
--- a/libgfortran/io/format.c
+++ b/libgfortran/io/format.c
@@ -141,14 +141,15 @@ uint32_t format_hash (st_parameter_dt *dtp)
 {
   char *key;
   gfc_charlen_type key_len;
-  uint32_t hash = 0;
+  uint32_t hash = 5381;
   gfc_charlen_type i;
 
-  /* Hash the format string. Super simple, but what the heck!  */
+  /* Hash the format string. Uses the DJB algorithm, which is simple
+     but is supposed to work well.  */
   key = dtp->format;
   key_len = dtp->format_len;
   for (i = 0; i < key_len; i++)
-    hash ^= key[i];
+    hash = ((hash << 5) + hash) + key[i]; /* hash = hash * 33 + key[i]  */
   hash &= (FORMAT_HASH_SIZE - 1);
   return hash;
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]