Add jhash (Jenkins hash) and use that for file names
authorJens Axboe <axboe@kernel.dk>
Thu, 6 Oct 2011 10:37:10 +0000 (12:37 +0200)
committerJens Axboe <axboe@kernel.dk>
Thu, 6 Oct 2011 10:37:10 +0000 (12:37 +0200)
Signed-off-by: Jens Axboe <axboe@kernel.dk>
filehash.c
hash.h

index 1df7db0c2f2d754fd4ae7bbc674ac408d7ea7848..1bb1eb237606096a8d0bbec6b0ec583f34bafc00 100644 (file)
@@ -3,7 +3,7 @@
 
 #include "fio.h"
 #include "flist.h"
 
 #include "fio.h"
 #include "flist.h"
-#include "crc/crc16.h"
+#include "hash.h"
 
 #define HASH_BUCKETS   512
 #define HASH_MASK      (HASH_BUCKETS - 1)
 
 #define HASH_BUCKETS   512
 #define HASH_MASK      (HASH_BUCKETS - 1)
@@ -15,7 +15,7 @@ static struct fio_mutex *hash_lock;
 
 static unsigned short hash(const char *name)
 {
 
 static unsigned short hash(const char *name)
 {
-       return crc16((const unsigned char *) name, strlen(name)) & HASH_MASK;
+       return jhash(name, strlen(name), 0) & HASH_MASK;
 }
 
 void remove_file_hash(struct fio_file *f)
 }
 
 void remove_file_hash(struct fio_file *f)
diff --git a/hash.h b/hash.h
index 73f961b73ac77975bd7a86eb8c7b49d995e05408..0c3cdda77be642842337f6fe949da7c24581e661 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -1,6 +1,7 @@
 #ifndef _LINUX_HASH_H
 #define _LINUX_HASH_H
 
 #ifndef _LINUX_HASH_H
 #define _LINUX_HASH_H
 
+#include <inttypes.h>
 #include "arch/arch.h"
 
 /* Fast hashing routine for a long.
 #include "arch/arch.h"
 
 /* Fast hashing routine for a long.
@@ -59,4 +60,80 @@ static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
 {
        return hash_long((unsigned long)ptr, bits);
 }
 {
        return hash_long((unsigned long)ptr, bits);
 }
+
+/*
+ * Bob Jenkins jhash
+ */
+
+#define JHASH_INITVAL  GOLDEN_RATIO_PRIME
+
+static inline uint32_t rol32(uint32_t word, uint32_t shift)
+{
+       return (word << shift) | (word >> (32 - shift));
+}
+
+/* __jhash_mix -- mix 3 32-bit values reversibly. */
+#define __jhash_mix(a, b, c)                   \
+{                                              \
+       a -= c;  a ^= rol32(c, 4);  c += b;     \
+       b -= a;  b ^= rol32(a, 6);  a += c;     \
+       c -= b;  c ^= rol32(b, 8);  b += a;     \
+       a -= c;  a ^= rol32(c, 16); c += b;     \
+       b -= a;  b ^= rol32(a, 19); a += c;     \
+       c -= b;  c ^= rol32(b, 4);  b += a;     \
+}
+
+/* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
+#define __jhash_final(a, b, c)                 \
+{                                              \
+       c ^= b; c -= rol32(b, 14);              \
+       a ^= c; a -= rol32(c, 11);              \
+       b ^= a; b -= rol32(a, 25);              \
+       c ^= b; c -= rol32(b, 16);              \
+       a ^= c; a -= rol32(c, 4);               \
+       b ^= a; b -= rol32(a, 14);              \
+       c ^= b; c -= rol32(b, 24);              \
+}
+
+static inline uint32_t jhash(const void *key, uint32_t length, uint32_t initval)
+{
+       const uint8_t *k = key;
+       uint32_t a, b, c;
+
+       /* Set up the internal state */
+       a = b = c = JHASH_INITVAL + length + initval;
+
+       /* All but the last block: affect some 32 bits of (a,b,c) */
+       while (length > 12) {
+               a += *k;
+               b += *(k + 4);
+               c += *(k + 8);
+               __jhash_mix(a, b, c);
+               length -= 12;
+               k += 12;
+       }
+
+       /* Last block: affect all 32 bits of (c) */
+       /* All the case statements fall through */
+       switch (length) {
+       case 12: c += (uint32_t) k[11] << 24;
+       case 11: c += (uint32_t) k[10] << 16;
+       case 10: c += (uint32_t) k[9] << 8;
+       case 9:  c += k[8];
+       case 8:  b += (uint32_t) k[7] << 24;
+       case 7:  b += (uint32_t) k[6] << 16;
+       case 6:  b += (uint32_t) k[5] << 8;
+       case 5:  b += k[4];
+       case 4:  a += (uint32_t) k[3] << 24;
+       case 3:  a += (uint32_t) k[2] << 16;
+       case 2:  a += (uint32_t) k[1] << 8;
+       case 1:  a += k[0];
+                __jhash_final(a, b, c);
+       case 0: /* Nothing left to add */
+               break;
+       }
+
+       return c;
+}
+
 #endif /* _LINUX_HASH_H */
 #endif /* _LINUX_HASH_H */