about summary refs log tree commit homepage
path: root/ext/tdb/fnv.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-26 02:27:17 +0000
committerEric Wong <normalperson@yhbt.net>2010-12-01 09:45:32 +0000
commit58938c980f38a4581b4a0e8a780fffe7ac95bc93 (patch)
treeece822520cf8cbabe23f4559361afaf8346aa729 /ext/tdb/fnv.c
downloadruby-tdb-58938c980f38a4581b4a0e8a780fffe7ac95bc93.tar.gz
Diffstat (limited to 'ext/tdb/fnv.c')
-rw-r--r--ext/tdb/fnv.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/ext/tdb/fnv.c b/ext/tdb/fnv.c
new file mode 100644
index 0000000..769a3d7
--- /dev/null
+++ b/ext/tdb/fnv.c
@@ -0,0 +1,28 @@
+#include "rbtdb.h"
+
+#define FNV1A_32A_INIT (unsigned int)0x811c9dc5
+#define FNV_32_PRIME (unsigned int)0x01000193
+
+unsigned int rbtdb_fnv1a(TDB_DATA * data)
+{
+        unsigned char *bp = data->dptr;
+        unsigned char *be = bp + data->dsize;
+        unsigned int h = FNV1A_32A_INIT;
+
+        /* FNV-1a hash each octet in the buffer */
+        while (bp < be) {
+
+                /* xor the bottom with the current octet */
+                h ^= (unsigned)*bp++;
+
+                /* multiply by the 32 bit FNV magic prime mod 2^32 */
+#if defined(NO_FNV_GCC_OPTIMIZATION)
+                h *= FNV_32_PRIME;
+#else
+                h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
+#endif
+        }
+
+        /* return our new hash value */
+        return h;
+}