hash: add xxhash implementation

This commit is contained in:
Danny Robson 2016-11-17 18:35:06 +11:00
parent 5b44b8f3e0
commit cb32823248
5 changed files with 1278 additions and 0 deletions

View File

@ -160,6 +160,8 @@ UTIL_FILES = \
hash/sha2.hpp \ hash/sha2.hpp \
hash/wang.hpp \ hash/wang.hpp \
hash/wang.ipp \ hash/wang.ipp \
hash/xxhash.cpp \
hash/xxhash.hpp \
introspection.cpp \ introspection.cpp \
introspection.hpp \ introspection.hpp \
io.cpp \ io.cpp \
@ -425,6 +427,7 @@ TEST_BIN = \
test/hash/ripemd \ test/hash/ripemd \
test/hash/sha1 \ test/hash/sha1 \
test/hash/sha2 \ test/hash/sha2 \
test/hash/xxhash \
test/hton \ test/hton \
test/introspection \ test/introspection \
test/json_types \ test/json_types \

View File

@ -36,6 +36,8 @@ namespace util { namespace hash {
class fletcher; class fletcher;
class crc32; class crc32;
class xxhash;
// hash adapters // hash adapters
class PBKDF1; class PBKDF1;
class PBKDF2; class PBKDF2;

1152
hash/xxhash.cpp Normal file

File diff suppressed because it is too large Load Diff

63
hash/xxhash.hpp Normal file
View File

@ -0,0 +1,63 @@
/*
* 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>
*/
#ifndef CRUFT_UTIL_HASH_XXHASH_HPP
#define CRUFT_UTIL_HASH_XXHASH_HPP
#include <cstdint>
#include <type_traits>
namespace util::hash {
template <typename T>
class xxhash {
public:
static_assert (std::is_same<T,uint32_t>::value || std::is_same<T,uint64_t>::value);
using digest_t = T;
xxhash (void);
xxhash (uint32_t seed);
void update (const uint8_t *restrict first, const uint8_t *restrict last);
void finish (void);
digest_t digest (void) const;
void reset (void);
private:
uint32_t m_seed;
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;
};
using xxhash32 = xxhash<uint32_t>;
using xxhash64 = xxhash<uint64_t>;
}
#endif

58
test/hash/xxhash.cpp Normal file
View File

@ -0,0 +1,58 @@
/*
* 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>
*/
#include "tap.hpp"
#include "hash/simple.hpp"
#include "hash/xxhash.hpp"
int
main (int, char **)
{
util::TAP::logger tap;
static const struct {
uint32_t hash32;
uint64_t hash64;
unsigned seed;
std::string data;
std::string msg;
} TESTS[] = {
{ 0x02CC5D05, 0xef46db3751d8e999, 0, "", "empty string, 0 seed" },
{ 0x0b2cb792, 0xd5afba1336a3be4b, 1, "", "empty string, 1 seed" },
{ 0x550d7456, 0xd24ec4f1a98c6e5b, 0, "a", "single a, 0 seed" },
{ 0xf514706f, 0xdec2bc81c3cd46c6, 1, "a", "single a, 1 seed" },
{ 0x32d153ff, 0x44bc2cf5ad770999, 0, "abc", "abc, 0 seed" },
{ 0xaa3da8ff, 0xbea9ca8199328908, 1, "abc", "abc, 1 seed" },
{ 0x54ca7e46, 0x892a0760a6343391, 0x1234,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+",
"long alphabet" }
};
for (const auto &t: TESTS) {
auto first = t.data.data ();
auto last = first + t.data.size ();
auto digest32 = util::hash::simple<util::hash::xxhash32> (first, last, t.seed);
//auto digest64 = util::hash::simple<util::hash::xxhash64> (first, last, t.seed);
tap.expect_eq (digest32, t.hash32, "xxhash32 %s", t.msg);
//tap.expect_eq (digest64, t.hash64, "xxhash64 %s", t.msg);
}
return tap.status ();
}