murmur2: make mixing constexpr and inline

This commit is contained in:
Danny Robson 2016-07-01 17:01:21 +10:00
parent 2bf3e3c431
commit e1c115c71a
3 changed files with 85 additions and 60 deletions

View File

@ -21,61 +21,6 @@
#include "common.hpp"
///////////////////////////////////////////////////////////////////////////////
template <typename T> struct constants { };
template <>
struct constants<uint32_t> {
static const uint32_t m = 0x5bd1e995;
static const int32_t r = 24;
};
template <>
struct constants<uint64_t> {
static const uint64_t m = 0xc6a4a7935bd1e995;
static const int64_t r = 47;
};
/////////////////////////////////////////////////////////////////////
uint32_t
util::hash::murmur2::mix (uint32_t h, uint32_t k)
{
static const uint32_t m = constants<uint32_t>::m;
static const uint32_t r = constants<uint32_t>::r;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
return h;
}
//-----------------------------------------------------------------------------
// 64 bit murmur2 mixing function. Note the last two lines are swapped
// compared with 32 bit murmur2_mix. It's not clear if this is deliberate
// in the canonical implementation, so we just leave it to help compatibility.
uint64_t
util::hash::murmur2::mix (uint64_t h, uint64_t k)
{
static const uint64_t m = constants<uint64_t>::m;
static const uint64_t r = constants<uint64_t>::r;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
return h;
}
///////////////////////////////////////////////////////////////////////////////
uint32_t
util::hash::murmur2::hash_32 (const void *restrict key,
@ -85,7 +30,7 @@ util::hash::murmur2::hash_32 (const void *restrict key,
CHECK (key);
// setup
static const auto m = constants<uint32_t>::m;
constexpr auto m = detail::constants<uint32_t>::m;
uint32_t h = seed ^ uint32_t (len);
// body
@ -116,8 +61,8 @@ util::hash::murmur2::hash_64 (const void *restrict key,
uint64_t seed)
{
// setup
const auto m = constants<uint64_t>::m;
const auto r = constants<uint64_t>::r;
constexpr auto m = detail::constants<uint64_t>::m;
constexpr auto r = detail::constants<uint64_t>::r;
uint64_t h = seed ^ (len * m);

View File

@ -24,12 +24,14 @@
// variants is deliberately not provided. You can damn well align your data or
// fix the algorithm.
namespace util { namespace hash { namespace murmur2 {
uint32_t mix (uint32_t, uint32_t);
uint64_t mix (uint64_t, uint64_t);
constexpr uint32_t mix (uint32_t, uint32_t);
constexpr uint64_t mix (uint64_t, uint64_t);
uint32_t hash_32 (const void *restrict data, size_t len, uint32_t seed);
uint64_t hash_64 (const void *restrict data, size_t len, uint64_t seed);
} } }
#include "./murmur2.ipp"
#endif

78
hash/murmur/murmur2.ipp Normal file
View File

@ -0,0 +1,78 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#if defined(__UTIL_HASH_MURMUR_MURMUR2_IPP)
#error
#endif
#define __UTIL_HASH_MURMUR_MURMUR2_IPP
///////////////////////////////////////////////////////////////////////////////
namespace util { namespace hash { namespace murmur2 { namespace detail {
template <typename T> struct constants;
template <>
struct constants<uint32_t> {
static const uint32_t m = 0x5bd1e995;
static const int32_t r = 24;
};
template <>
struct constants<uint64_t> {
static const uint64_t m = 0xc6a4a7935bd1e995;
static const int64_t r = 47;
};
} } } }
/////////////////////////////////////////////////////////////////////
constexpr uint32_t
util::hash::murmur2::mix (uint32_t h, uint32_t k)
{
constexpr uint32_t m = detail::constants<uint32_t>::m;
constexpr uint32_t r = detail::constants<uint32_t>::r;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
return h;
}
//-----------------------------------------------------------------------------
// 64 bit murmur2 mixing function. Note the last two lines are swapped
// compared with 32 bit murmur2_mix. It's not clear if this is deliberate
// in the canonical implementation, so we just leave it to help compatibility.
constexpr uint64_t
util::hash::murmur2::mix (uint64_t h, uint64_t k)
{
constexpr uint64_t m = detail::constants<uint64_t>::m;
constexpr uint64_t r = detail::constants<uint64_t>::r;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
return h;
}