libcruft-util/hash/murmur/murmur1.cpp

49 lines
1.4 KiB
C++
Raw Normal View History

2015-05-20 14:47:36 +10:00
/*
* 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 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "murmur1.hpp"
#include "common.hpp"
#include "../../debug.hpp"
2018-05-03 18:32:08 +10:00
#include "../../endian.hpp"
2015-05-20 14:47:36 +10:00
using util::hash::murmur1;
2015-05-20 14:47:36 +10:00
///////////////////////////////////////////////////////////////////////////////
2015-05-20 14:47:36 +10:00
uint32_t
murmur1::operator() (util::view<const uint8_t*> data) const noexcept
2015-05-20 14:47:36 +10:00
{
static const uint32_t m = 0xc6a4a793;
uint32_t h = m_seed ^ ((data.size () & 0xffffffff) * m);
2015-05-20 14:47:36 +10:00
// mix the body
2018-05-03 18:32:08 +10:00
auto cursor = data.begin ();
for (auto last = data.end () - data.size () % sizeof (uint32_t); cursor < last; cursor += 4)
h = mix (h, util::readhe<uint32_t> (cursor));
2015-05-20 14:47:36 +10:00
// mix the tail
if (data.size () % sizeof (uint32_t))
2018-05-03 18:32:08 +10:00
h = mix (h, murmur::tail<uint32_t> (cursor, data.size ()));
2015-05-20 14:47:36 +10:00
// finalise
h *= m; h ^= h >> 10;
h *= m; h ^= h >> 17;
return h;
}