add ec_node_bypass
[protos/libecoli.git] / include / ecoli_murmurhash.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 /**
6  * MurmurHash3 is a hash implementation that was written by Austin Appleby, and
7  * is placed in the public domain. The author hereby disclaims copyright to this
8  * source code.
9  */
10
11 #ifndef ECOLI_MURMURHASH_H_
12 #define ECOLI_MURMURHASH_H_
13
14 #include <stdint.h>
15
16 /** Hash rotation */
17 static inline uint32_t ec_murmurhash_rotl32(uint32_t x, int8_t r)
18 {
19         return (x << r) | (x >> (32 - r));
20 }
21
22 /** Add 32-bit to the hash */
23 static inline uint32_t ec_murmurhash3_add32(uint32_t h, uint32_t data)
24 {
25         data *= 0xcc9e2d51;
26         data = ec_murmurhash_rotl32(data, 15);
27         data *= 0x1b873593;
28         h ^= data;
29         return h;
30 }
31
32 /** Intermediate mix */
33 static inline uint32_t ec_murmurhash3_mix32(uint32_t h)
34 {
35         h = ec_murmurhash_rotl32(h,13);
36         h = h * 5 +0xe6546b64;
37         return h;
38 }
39
40 /** Final mix: force all bits of a hash block to avalanche */
41 static inline uint32_t ec_murmurhash3_fmix32(uint32_t h)
42 {
43         h ^= h >> 16;
44         h *= 0x85ebca6b;
45         h ^= h >> 13;
46         h *= 0xc2b2ae35;
47         h ^= h >> 16;
48
49         return h;
50 }
51
52 /**
53  * Calculate a 32-bit murmurhash3
54  *
55  * @param key
56  *   The key (the unaligned variable-length array of bytes).
57  * @param len
58  *   The length of the key, counting by bytes.
59  * @param seed
60  *   Can be any 4-byte value initialization value.
61  * @return
62  *   A 32-bit hash.
63  */
64 uint32_t ec_murmurhash3(const void *key, int len, uint32_t seed);
65
66 #endif /* ECOLI_MURMURHASH_H_ */