Added string hashing function
authorSebastian Hack <hack@ipd.info.uni-karlsruhe.de>
Thu, 21 Jun 2007 16:47:46 +0000 (16:47 +0000)
committerSebastian Hack <hack@ipd.info.uni-karlsruhe.de>
Thu, 21 Jun 2007 16:47:46 +0000 (16:47 +0000)
[r14705]

include/libfirm/adt/hashptr.h

index 043ba5b..03dd108 100644 (file)
 #define FIRM_ADT_HASHPTR_H
 
 #include "firm_config.h"
+#include "compiler.h"
 
 #define _FIRM_FNV_OFFSET_BASIS 2166136261U
 #define _FIRM_FNV_FNV_PRIME 16777619U
 
 /* Computing x * _FIRM_FNV_FNV_PRIME */
-#define _FIRM_FNV_TIMES_PRIME(x) \
-  (((x) << 24) + ((x) << 8) + ((x) << 7) + ((x) << 4) + ((x) << 1) + 1)
+#define _FIRM_FNV_TIMES_PRIME(x) ((x) * _FIRM_FNV_FNV_PRIME)
 
 static INLINE unsigned firm_fnv_hash(const unsigned char *data, unsigned bytes)
 {
@@ -48,6 +48,19 @@ static INLINE unsigned firm_fnv_hash(const unsigned char *data, unsigned bytes)
        return hash;
 }
 
+static INLINE unsigned firm_fnv_hash_str(const char *data)
+{
+       unsigned i;
+       unsigned hash = _FIRM_FNV_OFFSET_BASIS;
+
+       for(i = 0; data[i] != '\0'; ++i) {
+               hash = _FIRM_FNV_TIMES_PRIME(hash);
+               hash ^= data[i];
+       }
+
+       return hash;
+}
+
 /**
  * hash a pointer value: Pointer addresses are mostly aligned to 4
  * or 8 bytes. So we remove the lowest 3 bits