1 | /* |
---|---|
2 | * Copyright Andrey Semashev 2016. |
3 | * Distributed under the Boost Software License, Version 1.0. |
4 | * (See accompanying file LICENSE_1_0.txt or copy at |
5 | * http://www.boost.org/LICENSE_1_0.txt) |
6 | */ |
7 | /*! |
8 | * \file murmur3.hpp |
9 | * \author Andrey Semashev |
10 | * \date 16.01.2016 |
11 | * |
12 | * \brief This header is the Boost.Log library implementation, see the library documentation |
13 | * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. |
14 | * |
15 | * This file implements MurmurHash3 hash function implementation. See https://en.wikipedia.org/wiki/MurmurHash. |
16 | */ |
17 | |
18 | #ifndef BOOST_LOG_MURMUR3_HPP_INCLUDED_ |
19 | #define BOOST_LOG_MURMUR3_HPP_INCLUDED_ |
20 | |
21 | #include <boost/log/detail/config.hpp> |
22 | #include <boost/cstdint.hpp> |
23 | #include <boost/log/detail/header.hpp> |
24 | |
25 | namespace boost { |
26 | |
27 | BOOST_LOG_OPEN_NAMESPACE |
28 | |
29 | namespace aux { |
30 | |
31 | //! 32-bit MurmurHash3 algorithm implementation (https://en.wikipedia.org/wiki/MurmurHash) |
32 | class murmur3_32 |
33 | { |
34 | private: |
35 | uint32_t m_state; |
36 | uint32_t m_len; |
37 | |
38 | static BOOST_CONSTEXPR_OR_CONST uint32_t c1 = 0xcc9e2d51; |
39 | static BOOST_CONSTEXPR_OR_CONST uint32_t c2 = 0x1b873593; |
40 | static BOOST_CONSTEXPR_OR_CONST uint32_t r1 = 15; |
41 | static BOOST_CONSTEXPR_OR_CONST uint32_t r2 = 13; |
42 | static BOOST_CONSTEXPR_OR_CONST uint32_t m = 5; |
43 | static BOOST_CONSTEXPR_OR_CONST uint32_t n = 0xe6546b64; |
44 | |
45 | public: |
46 | explicit BOOST_CONSTEXPR murmur3_32(uint32_t seed) BOOST_NOEXCEPT : m_state(seed), m_len(0u) |
47 | { |
48 | } |
49 | |
50 | //! Mixing stage of the 32-bit MurmurHash3 algorithm |
51 | void mix(uint32_t value) BOOST_NOEXCEPT |
52 | { |
53 | value *= c1; |
54 | value = (value << r1) | (value >> (32u - r1)); |
55 | value *= c2; |
56 | |
57 | uint32_t h = m_state ^ value; |
58 | m_state = ((h << r2) | (h >> (32u - r2))) * m + n; |
59 | m_len += 4u; |
60 | } |
61 | |
62 | //! Finalization stage of the 32-bit MurmurHash3 algorithm |
63 | uint32_t finalize() BOOST_NOEXCEPT |
64 | { |
65 | uint32_t h = m_state ^ m_len; |
66 | h ^= h >> 16u; |
67 | h *= 0x85ebca6bu; |
68 | h ^= h >> 13u; |
69 | h *= 0xc2b2ae35u; |
70 | h ^= h >> 16u; |
71 | m_state = h; |
72 | return h; |
73 | } |
74 | }; |
75 | |
76 | } // namespace aux |
77 | |
78 | BOOST_LOG_CLOSE_NAMESPACE // namespace log |
79 | |
80 | } // namespace boost |
81 | |
82 | #include <boost/log/detail/footer.hpp> |
83 | |
84 | #endif // BOOST_LOG_MURMUR3_HPP_INCLUDED_ |
85 |