2016-11-17 18:35:06 +11: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 2016 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "xxhash.hpp"
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "../bitwise.hpp"
|
|
|
|
#include "../debug.hpp"
|
|
|
|
#include "../endian.hpp"
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-09 16:28:25 +11:00
|
|
|
#include <cstring>
|
|
|
|
|
2016-12-21 21:08:26 +11:00
|
|
|
using util::hash::xxhash;
|
2016-11-17 18:35:06 +11:00
|
|
|
|
|
|
|
|
2016-12-21 21:08:26 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
static
|
|
|
|
T
|
|
|
|
read_le (const void *ptr)
|
2016-11-17 18:35:06 +11:00
|
|
|
{
|
2016-12-21 21:08:26 +11:00
|
|
|
return *static_cast<const T*> (ptr);
|
2016-11-17 18:35:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
struct constants {
|
|
|
|
static const T prime[5];
|
|
|
|
static const T round_rotl;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-12-21 21:08:26 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2016-11-17 18:35:06 +11:00
|
|
|
template <>
|
|
|
|
const uint32_t
|
|
|
|
constants<uint32_t>::prime[5] = {
|
|
|
|
2654435761u,
|
|
|
|
2246822519u,
|
|
|
|
3266489917u,
|
|
|
|
668265263u,
|
|
|
|
374761393u,
|
|
|
|
};
|
|
|
|
|
2016-12-21 21:08:26 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-11-17 18:35:06 +11:00
|
|
|
template <>
|
|
|
|
const uint32_t
|
|
|
|
constants<uint32_t>::round_rotl = 13;
|
|
|
|
|
|
|
|
|
2016-12-21 21:08:26 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2016-11-17 18:35:06 +11:00
|
|
|
template <>
|
|
|
|
const uint64_t constants<uint64_t>::prime[5] = {
|
|
|
|
11400714785074694791u,
|
|
|
|
14029467366897019727u,
|
|
|
|
1609587929392839161u,
|
|
|
|
9650029242287828579u,
|
|
|
|
2870177450012600261u,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-12-21 21:08:26 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2016-11-17 18:35:06 +11:00
|
|
|
template <>
|
|
|
|
const uint64_t
|
|
|
|
constants<uint64_t>::round_rotl = 31;
|
|
|
|
|
|
|
|
|
2016-12-21 21:08:26 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-11-17 18:35:06 +11:00
|
|
|
template <typename T>
|
2018-01-13 13:48:58 +11:00
|
|
|
static
|
|
|
|
T
|
|
|
|
round (T seed, T input)
|
|
|
|
{
|
|
|
|
seed += input * constants<T>::prime[1];
|
|
|
|
seed = util::rotatel (seed, constants<T>::round_rotl);
|
|
|
|
seed *= constants<T>::prime[0];
|
|
|
|
|
|
|
|
return seed;
|
|
|
|
}
|
2016-11-17 18:35:06 +11:00
|
|
|
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-11-17 18:35:06 +11:00
|
|
|
template <typename T>
|
|
|
|
xxhash<T>::xxhash (uint32_t _seed):
|
|
|
|
m_seed (_seed)
|
2018-01-13 13:48:58 +11:00
|
|
|
{ ; }
|
2016-11-17 18:35:06 +11:00
|
|
|
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-11-17 18:35:06 +11:00
|
|
|
template <typename T>
|
2018-01-13 13:48:58 +11:00
|
|
|
typename xxhash<T>::digest_t
|
|
|
|
xxhash<T>::operator() (const util::view<const uint8_t*> data)
|
2016-11-17 18:35:06 +11:00
|
|
|
{
|
2018-01-13 13:48:58 +11:00
|
|
|
struct {
|
|
|
|
uint32_t total_len_32;
|
|
|
|
uint32_t large_len;
|
|
|
|
|
|
|
|
T v1, v2, v3, v4;
|
|
|
|
uint32_t mem32[4];
|
|
|
|
uint32_t memsize;
|
|
|
|
uint32_t reserved;
|
|
|
|
|
|
|
|
//uint64_t length;
|
|
|
|
//T v[4];
|
|
|
|
//T mem[4];
|
|
|
|
//unsigned memsize;
|
|
|
|
} m_state;
|
|
|
|
|
|
|
|
/* RESET */
|
2016-11-17 18:35:06 +11:00
|
|
|
memset (&m_state, 0, sizeof (m_state));
|
|
|
|
|
|
|
|
m_state.v1 = m_seed + constants<T>::prime[0] + constants<T>::prime[1];
|
|
|
|
m_state.v2 = m_seed + constants<T>::prime[1];
|
|
|
|
m_state.v3 = m_seed;
|
|
|
|
m_state.v4 = m_seed - constants<T>::prime[0];
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
/* UPDATE */
|
|
|
|
do {
|
|
|
|
auto first = data.begin ();
|
|
|
|
auto last = data.end ();
|
|
|
|
if (first == last)
|
|
|
|
break;
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
CHECK (first);
|
|
|
|
CHECK (last);
|
|
|
|
CHECK_LE (first, last);
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
//auto endian = XXH_littleEndian;
|
|
|
|
size_t len = last - first;
|
|
|
|
auto input = (const void*)first;
|
2016-12-21 21:08:26 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
auto p = reinterpret_cast<const uint8_t*> (input);
|
|
|
|
auto const bEnd = p + len;
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
constexpr auto CHUNK = 4 * sizeof (T);
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
m_state.total_len_32 += (unsigned)len;
|
|
|
|
m_state.large_len |= (len >= CHUNK) | (m_state.total_len_32 >= CHUNK);
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
if (m_state.memsize + len < CHUNK) { /* fill in tmp buffer */
|
|
|
|
memcpy ((uint8_t*)(m_state.mem32) + m_state.memsize, input, len);
|
|
|
|
m_state.memsize += (unsigned)len;
|
|
|
|
break;
|
2016-11-17 18:35:06 +11:00
|
|
|
}
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
if (m_state.memsize) { /* some data left from previous update */
|
|
|
|
memcpy ((uint8_t*)(m_state.mem32) + m_state.memsize, input, CHUNK - m_state.memsize);
|
|
|
|
{ const uint32_t* p32 = m_state.mem32;
|
|
|
|
m_state.v1 = round<T> (m_state.v1, ltoh (*p32)); p32++;
|
|
|
|
m_state.v2 = round<T> (m_state.v2, ltoh (*p32)); p32++;
|
|
|
|
m_state.v3 = round<T> (m_state.v3, ltoh (*p32)); p32++;
|
|
|
|
m_state.v4 = round<T> (m_state.v4, ltoh (*p32)); p32++;
|
|
|
|
}
|
|
|
|
p += CHUNK - m_state.memsize;
|
|
|
|
m_state.memsize = 0;
|
|
|
|
}
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
if (p <= bEnd - CHUNK * sizeof (T)) {
|
|
|
|
const uint8_t* const limit = bEnd - 4 * sizeof (T);
|
|
|
|
T v1 = m_state.v1;
|
|
|
|
T v2 = m_state.v2;
|
|
|
|
T v3 = m_state.v3;
|
|
|
|
T v4 = m_state.v4;
|
|
|
|
|
|
|
|
do {
|
|
|
|
v1 = round<T> (v1, read_le<T> (p)); p += sizeof (T);
|
|
|
|
v2 = round<T> (v2, read_le<T> (p)); p += sizeof (T);
|
|
|
|
v3 = round<T> (v3, read_le<T> (p)); p += sizeof (T);
|
|
|
|
v4 = round<T> (v4, read_le<T> (p)); p += sizeof (T);
|
|
|
|
} while (p <= limit);
|
|
|
|
|
|
|
|
m_state.v1 = v1;
|
|
|
|
m_state.v2 = v2;
|
|
|
|
m_state.v3 = v3;
|
|
|
|
m_state.v4 = v4;
|
|
|
|
}
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
if (p < bEnd) {
|
|
|
|
memcpy (m_state.mem32, p, (size_t)(bEnd-p));
|
|
|
|
m_state.memsize = (unsigned)(bEnd-p);
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
/* DIGEST */
|
|
|
|
{
|
|
|
|
auto p = reinterpret_cast<const uint8_t*> (m_state.mem32);
|
|
|
|
auto last = p + m_state.memsize;
|
|
|
|
|
|
|
|
T h;
|
|
|
|
|
|
|
|
if (m_state.large_len) {
|
|
|
|
h = rotatel (m_state.v1, T{ 1}) +
|
|
|
|
rotatel (m_state.v2, T{ 7}) +
|
|
|
|
rotatel (m_state.v3, T{12}) +
|
|
|
|
rotatel (m_state.v4, T{18});
|
|
|
|
} else {
|
|
|
|
h = m_state.v3 /* == seed */ + constants<T>::prime[4];
|
|
|
|
}
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
h += m_state.total_len_32;
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
while (p + sizeof (T) <= last) {
|
|
|
|
h += read_le<T> (p) * constants<T>::prime[2];
|
|
|
|
h = rotatel (h, 17) * constants<T>::prime[3];
|
|
|
|
p += 4;
|
|
|
|
}
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
while (p < last) {
|
|
|
|
h += (*p) * constants<T>::prime[4];
|
|
|
|
h = rotatel (h, 11) * constants<T>::prime[0];
|
|
|
|
p++;
|
|
|
|
}
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
h ^= h >> 15; h *= constants<T>::prime[1];
|
|
|
|
h ^= h >> 13; h *= constants<T>::prime[2];
|
|
|
|
h ^= h >> 16;
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
return h;
|
2016-11-17 18:35:06 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 21:08:26 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-11-17 18:35:06 +11:00
|
|
|
template class util::hash::xxhash<uint32_t>;
|
|
|
|
template class util::hash::xxhash<uint64_t>;
|