2015-05-28 12:17:25 +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 "fasthash.hpp"
|
|
|
|
|
2018-05-03 18:32:08 +10:00
|
|
|
#include "../endian.hpp"
|
|
|
|
#include "../debug.hpp"
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
using util::hash::fasthash;
|
2015-05-28 12:17:25 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-13 13:48:58 +11:00
|
|
|
template <>
|
2015-05-28 12:17:25 +10:00
|
|
|
uint64_t
|
2018-01-13 13:48:58 +11:00
|
|
|
fasthash<uint64_t>::operator() (uint64_t seed, const util::view<const uint8_t*> data) const
|
2015-05-28 12:17:25 +10:00
|
|
|
{
|
|
|
|
static const uint64_t m = 0x880355f21e6d1965;
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
uint64_t result = seed ^ (data.size () * m);
|
2015-05-28 12:17:25 +10:00
|
|
|
|
2018-05-03 18:32:08 +10:00
|
|
|
auto cursor = data.begin ();
|
|
|
|
for (auto last = data.end () - data.size () % 8; cursor < last; cursor += 8) {
|
|
|
|
result ^= mix (readle<uint64_t> (cursor));
|
2015-05-28 12:17:25 +10:00
|
|
|
result *= m;
|
|
|
|
}
|
|
|
|
|
2018-05-03 18:32:08 +10:00
|
|
|
size_t remain = data.size () % 8;
|
2015-05-28 12:17:25 +10:00
|
|
|
if (remain) {
|
|
|
|
uint64_t accum = 0;
|
|
|
|
for (size_t i = 0; i < remain; ++i)
|
2018-05-03 18:32:08 +10:00
|
|
|
accum ^= uint64_t {cursor[i]} << i * 8;
|
2015-05-28 12:17:25 +10:00
|
|
|
|
|
|
|
result ^= mix (accum);
|
|
|
|
result *= m;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mix (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-01-13 13:48:58 +11:00
|
|
|
template <>
|
2015-05-28 12:17:25 +10:00
|
|
|
uint32_t
|
2018-01-13 13:48:58 +11:00
|
|
|
fasthash<uint32_t>::operator() (uint64_t seed, const util::view<const uint8_t*> data) const
|
2015-05-28 12:17:25 +10:00
|
|
|
{
|
2018-01-13 13:48:58 +11:00
|
|
|
auto h = fasthash<uint64_t> {} (seed, data);
|
2016-05-12 17:39:33 +10:00
|
|
|
return (h & 0xffffffff) - (h >> 32);
|
2015-05-28 12:17:25 +10:00
|
|
|
}
|
|
|
|
|