api documentation for ec_parse
[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  * @defgroup murmurhash Murmurhash
7  * @{
8  *
9  * @brief Hash calculation using murmurhash algorithm
10  *
11  * MurmurHash3 is a hash implementation that was written by Austin Appleby, and
12  * is placed in the public domain. The author hereby disclaims copyright to this
13  * source code.
14  */
15
16 #ifndef ECOLI_MURMURHASH_H_
17 #define ECOLI_MURMURHASH_H_
18
19 #include <stdint.h>
20
21 /** Hash rotation */
22 static inline uint32_t ec_murmurhash_rotl32(uint32_t x, int8_t r)
23 {
24         return (x << r) | (x >> (32 - r));
25 }
26
27 /** Add 32-bit to the hash */
28 static inline uint32_t ec_murmurhash3_add32(uint32_t h, uint32_t data)
29 {
30         data *= 0xcc9e2d51;
31         data = ec_murmurhash_rotl32(data, 15);
32         data *= 0x1b873593;
33         h ^= data;
34         return h;
35 }
36
37 /** Intermediate mix */
38 static inline uint32_t ec_murmurhash3_mix32(uint32_t h)
39 {
40         h = ec_murmurhash_rotl32(h,13);
41         h = h * 5 +0xe6546b64;
42         return h;
43 }
44
45 /** Final mix: force all bits of a hash block to avalanche */
46 static inline uint32_t ec_murmurhash3_fmix32(uint32_t h)
47 {
48         h ^= h >> 16;
49         h *= 0x85ebca6b;
50         h ^= h >> 13;
51         h *= 0xc2b2ae35;
52         h ^= h >> 16;
53
54         return h;
55 }
56
57 /**
58  * Calculate a 32-bit murmurhash3
59  *
60  * @param key
61  *   The key (the unaligned variable-length array of bytes).
62  * @param len
63  *   The length of the key, counting by bytes.
64  * @param seed
65  *   Can be any 4-byte value initialization value.
66  * @return
67  *   A 32-bit hash.
68  */
69 uint32_t ec_murmurhash3(const void *key, int len, uint32_t seed);
70
71 #endif /* ECOLI_MURMURHASH_H_ */
72
73 /** @} */