summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_murmur_hash.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/nxt_murmur_hash.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/nxt_murmur_hash.c b/src/nxt_murmur_hash.c
new file mode 100644
index 00000000..e9adabfa
--- /dev/null
+++ b/src/nxt_murmur_hash.c
@@ -0,0 +1,84 @@
+
+/*
+ * The code is based on the code by Austin Appleby,
+ * released to the public domain.
+ */
+
+#include <nxt_main.h>
+
+
+uint32_t
+nxt_murmur_hash2(const void *data, size_t len)
+{
+ uint32_t h, k;
+ const u_char *p;
+ const uint32_t m = 0x5bd1e995;
+
+ p = data;
+ h = 0 ^ (uint32_t) len;
+
+ while (len >= 4) {
+ k = p[0];
+ k |= p[1] << 8;
+ k |= p[2] << 16;
+ k |= p[3] << 24;
+
+ k *= m;
+ k ^= k >> 24;
+ k *= m;
+
+ h *= m;
+ h ^= k;
+
+ p += 4;
+ len -= 4;
+ }
+
+ switch (len) {
+ case 3:
+ h ^= p[2] << 16;
+ case 2:
+ h ^= p[1] << 8;
+ case 1:
+ h ^= p[0];
+ h *= m;
+ }
+
+ h ^= h >> 13;
+ h *= m;
+ h ^= h >> 15;
+
+ return h;
+}
+
+
+/* The MurmurHash2 for fixed 4 byte length. */
+
+uint32_t
+nxt_murmur_hash2_uint32(const void *data)
+{
+ uint32_t h, k;
+ const u_char *p;
+ const uint32_t m = 0x5bd1e995;
+
+ p = data;
+
+ k = p[0];
+ k |= p[1] << 8;
+ k |= p[2] << 16;
+ k |= p[3] << 24;
+
+ k *= m;
+ k ^= k >> 24;
+ k *= m;
+
+ h = 0 ^ 4;
+ h *= m;
+ h ^= k;
+
+ h ^= h >> 13;
+ h *= m;
+ h ^= h >> 15;
+
+ return h;
+}