libcruft-util/cruft/util/hash/murmur/murmur2.cpp

105 lines
2.8 KiB
C++
Raw Normal View History

2015-05-20 14:47:36 +10:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2015-05-20 14:47:36 +10:00
* limitations under the License.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "murmur2.hpp"
#include "common.hpp"
2021-04-12 16:56:06 +10:00
#include "../../debug/panic.hpp"
using cruft::hash::murmur2;
2015-05-20 14:47:36 +10:00
///////////////////////////////////////////////////////////////////////////////
static uint32_t
hash_32 (const void *restrict key,
size_t len,
uint32_t seed)
2015-05-20 14:47:36 +10:00
{
// setup
constexpr auto m = cruft::hash::detail::murmur2::constants<uint32_t>::m;
2017-01-05 15:06:49 +11:00
uint32_t h = seed ^ (len & 0xffffffff);
2015-05-20 14:47:36 +10:00
// body
auto cursor = reinterpret_cast<const uint32_t*> (key);
auto last = cursor + len / sizeof (uint32_t);
for (; cursor < last; ++cursor)
h = cruft::hash::murmur2<uint32_t>::mix (h, *cursor);
2015-05-20 14:47:36 +10:00
// tail
if (len % sizeof (uint32_t)) {
h ^= cruft::hash::murmur::tail<uint32_t> (reinterpret_cast<const uint8_t*> (cursor), len);
2015-05-20 14:47:36 +10:00
h *= m;
}
// finalise
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
//-----------------------------------------------------------------------------
static uint64_t
hash_64 (const void *restrict key,
size_t len,
uint64_t seed)
2015-05-20 14:47:36 +10:00
{
// setup
constexpr auto m = cruft::hash::detail::murmur2::constants<uint64_t>::m;
constexpr auto r = cruft::hash::detail::murmur2::constants<uint64_t>::r;
2015-05-20 14:47:36 +10:00
uint64_t h = seed ^ (len * m);
// body
auto cursor = reinterpret_cast<const uint64_t*> (key);
auto last = cursor + len / sizeof (uint64_t);
for (; cursor < last; ++cursor)
h = cruft::hash::murmur2<uint64_t>::mix (h, *cursor);
2015-05-20 14:47:36 +10:00
// tail
if (len % sizeof (uint64_t)) {
h ^= cruft::hash::murmur::tail<uint64_t> (reinterpret_cast<const uint8_t*> (cursor), len);
2015-05-20 14:47:36 +10:00
h *= m;
}
// finalise
h ^= h >> r;
h *= m;
h ^= h >> r;
return h;
}
///////////////////////////////////////////////////////////////////////////////
template <typename DigestT>
typename murmur2<DigestT>::digest_t
murmur2<DigestT>::operator() (cruft::view<const uint8_t*> data) const noexcept
{
static_assert (std::is_same_v<DigestT,uint32_t> || std::is_same_v<DigestT,uint64_t>);
2019-01-03 15:48:34 +11:00
if constexpr (std::is_same_v<DigestT,uint32_t>) {
return hash_32 (data.data (), data.size (), m_seed);
2019-01-03 15:48:34 +11:00
}
2019-01-03 15:48:34 +11:00
if constexpr (std::is_same_v<DigestT,uint64_t>) {
return hash_64 (data.data (), data.size (), m_seed);
2019-01-03 15:48:34 +11:00
}
unreachable ();
}
///////////////////////////////////////////////////////////////////////////////
template class cruft::hash::murmur2<uint32_t>;
template class cruft::hash::murmur2<uint64_t>;