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

40 lines
1.0 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
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "murmur1.hpp"
#include "common.hpp"
2018-05-03 18:32:08 +10:00
#include "../../endian.hpp"
2015-05-20 14:47:36 +10:00
using cruft::hash::murmur1;
2015-05-20 14:47:36 +10:00
///////////////////////////////////////////////////////////////////////////////
2015-05-20 14:47:36 +10:00
uint32_t
murmur1::operator() (cruft::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, cruft::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;
}