flatten 20260225

This commit is contained in:
Timothy Prepscius
2026-02-25 12:43:06 -05:00
commit 996151a56a
521 changed files with 46722 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#pragma once
namespace tjp::core::const_hash::simple {
constexpr value seed = 174440041;
constexpr value hasher(const u64 l)
{
return ((l << 5) + l);
}
constexpr value hash_compile_(const char *s, const char *b)
{
value v = seed;
auto *p = s;
while (p != b)
v = hasher(v) + *p++;
return v;
}
inline value hash_runtime_(const char *s, const char *b)
{
return hash_compile_(s, b);
}
} // namespace

View File

@@ -0,0 +1,50 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#pragma once
namespace tjp::core::const_hash::zltan {
//-------------------------------
// Expected Hashes
static_assert(hash_compile<u64>("a") == 14795678224343670082U);
static_assert(hash_compile<u32>("a") == 2007486030U);
static_assert(hash_compile<u16>("a") == 22182U);
static_assert(hash_compile<u8>("a") == 80U);
static_assert(hash_compile<u64>("ab") == 17973018434836123690U);
static_assert(hash_compile<u32>("ab") == 2626114336U);
static_assert(hash_compile<u16>("ab") == 46746U);
static_assert(hash_compile<u8>("ab") == 228U);
static_assert(hash_compile<u64>("abc") == 9059148220514607862U);
static_assert(hash_compile<u32>("abc") == 2779386792U);
static_assert(hash_compile<u16>("abc") == 28158U);
static_assert(hash_compile<u8>("abc") == 144U);
static_assert(hash_compile<u64>("abcd") == 11711224204720505282U);
static_assert(hash_compile<u32>("abcd") == 3972784484U);
static_assert(hash_compile<u16>("abcd") == 62618U);
static_assert(hash_compile<u8>("abcd") == 166U);
static_assert(hash_compile<u64>("abcde") == 10271438589093974638U);
static_assert(hash_compile<u32>("abcde") == 1688475508U);
static_assert(hash_compile<u16>("abcde") == 45776U);
static_assert(hash_compile<u8>("abcde") == 30U);
static_assert(hash_compile<u64>("abcdef") == 15844374392941584856U);
static_assert(hash_compile<u32>("abcdef") == 1471257416U);
static_assert(hash_compile<u16>("abcdef") == 17304U);
static_assert(hash_compile<u8>("abcdef") == 84U);
static_assert(hash_compile<u64>("abcdefg") == 1629871225258951438U);
static_assert(hash_compile<u32>("abcdefg") == 1553091536U);
static_assert(hash_compile<u16>("abcdefg") == 61246U);
static_assert(hash_compile<u8>("abcdefg") == 78U);
static_assert(hash_compile<u64>("tjp::core::variant::Variant") == 11594898570291287436U);
static_assert(hash_compile<u32>("tjp::core::variant::Variant") == 2549889722U);
static_assert(hash_compile<u16>("tjp::core::variant::Variant") == 41662U);
static_assert(hash_compile<u8>("tjp::core::variant::Variant") == 28U);
static_assert(hash_compile<u64>("tjp::core::Str") == 4181101266999643468U);
static_assert(hash_compile<u32>("tjp::core::Str") == 4083993560U);
static_assert(hash_compile<u16>("tjp::core::Str") == 55404U);
static_assert(hash_compile<u8>("tjp::core::Str") == 148U);
//-------------------------------
} // namespace

View File

@@ -0,0 +1,123 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#pragma once
#include <stddef.h>
namespace tjp::core::const_hash::zltan {
using u32 = uint32_t;
using u64 = uint64_t;
constexpr u64 seed = 0x026f23a;
constexpr
u64 mix(const u64 h_) {
u64 h = h_;
h ^= h >> 23;
h *= 0x2127599bf4325c37ULL;
h ^= h >> 47;
return h;
}
constexpr
u64 fasthash64_compiletime(const char *p, const size_t len, const u64 seed)
{
const u64 m = 0x880355f21e6d1965ULL;
u64 h = seed ^ (len * m);
u64 v = 0;
for (size_t i=1; i<=len; ++i)
{
v <<= 8;
v ^= (u64)*p++;
if ((i % 8) == 0)
{
h ^= mix(v);
h *= m;
v = 0;
}
}
if ((len % 8) != 0)
{
h ^= mix(v);
h *= m;
}
return mix(h);
}
constexpr
u64 fasthash64_compiletime_chunk(const char *p, const size_t len, const u64 seed)
{
const u64 m = 0x880355f21e6d1965ULL;
u64 h = seed ^ (len * m);
u64 v = 0;
const size_t len_8 = len / 8;
for (size_t i=0; i<len_8; ++i)
{
v ^= (u64)(*p++) << 56;
v ^= (u64)(*p++) << 48;
v ^= (u64)(*p++) << 40;
v ^= (u64)(*p++) << 32;
v ^= (u64)(*p++) << 24;
v ^= (u64)(*p++) << 16;
v ^= (u64)(*p++) << 8;
v ^= (u64)(*p++);
h ^= mix(v);
h *= m;
v = 0;
}
switch (len & 7) {
case 7: v ^= (u64)(*p++) << 48;
case 6: v ^= (u64)(*p++) << 40;
case 5: v ^= (u64)(*p++) << 32;
case 4: v ^= (u64)(*p++) << 24;
case 3: v ^= (u64)(*p++) << 16;
case 2: v ^= (u64)(*p++) << 8;
case 1: v ^= (u64)(*p++);
h ^= mix(v);
h *= m;
}
return mix(h);
}
constexpr
size_t hash_strlen(const char *b, const char *e)
{
return e-b;
}
constexpr
size_t hash_strlen_(const std::string_view &v)
{
return hash_strlen(v.begin(), v.end());
}
static_assert(hash_strlen_(std::string_view("abc")) == 3);
constexpr
u64 hash_compile_(const char *b, const char *e)
{
return fasthash64_compiletime_chunk(b, hash_strlen(b, e), seed);
}
inline
u64 hash_runtime_(const char *b, const char *e)
{
return fasthash64_compiletime_chunk(b, hash_strlen(b, e), seed);
}
} // namespace

View File

@@ -0,0 +1,32 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#include "Hash.hpp"
#include "../debug/Debug.h"
#include "../containers/Map.h"
#include "../containers/Tuple.h"
#include "../string/String.h"
#include <assert.h>
namespace tjp::core::const_hash {
#ifdef _DEBUG
static Map<Tuple<size_t,u64>, String> hashes;
void hash_collision_test(size_t width, u64 value, const std::string_view &s)
{
auto &v = hashes[{width,value}];
if (v.empty())
v = s;
assert(v == s);
}
#endif
} // namespace

View File

@@ -0,0 +1,19 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#pragma once
#include "../types/Definitions.h"
namespace tjp::core::const_hash {
using value = u64;
} // namespace
namespace tjp::core {
using hash_t = const_hash::value;
} ;

View File

@@ -0,0 +1,252 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#pragma once
#include "Hash.h"
#include <string_view>
#include "../debug/Debug.h"
#include "Hash+simple.hpp"
#include "Hash+zltan.hpp"
namespace tjp::core::const_hash {
namespace detail = zltan;
using value = u64;
constexpr
value version = 0;
template<typename T>
constexpr
T hash_label(const T l)
{
constexpr auto mask = ~T(1);
return (l & mask) | T(version);
}
template<typename T>
constexpr
value hash_version(const T h)
{
return (h & T(1));
}
constexpr
size_t hash_strlen(const char *s)
{
size_t i=0;
while(s[i])
++i;
return i;
}
constexpr
size_t hash_strlen(const std::string_view &s)
{
return s.size();
}
static_assert(hash_strlen("abc") == 3);
constexpr
value hash_compile_(const char *s, const char *e)
{
return detail::hash_compile_(s, e);
}
constexpr
value hash_compile_(const char *s)
{
return hash_compile_(s, s + hash_strlen(s));
}
inline
value hash_runtime_(const char *s, const char *e)
{
return detail::hash_runtime_(s, e);
}
inline
value hash_runtime_(const char *s)
{
return hash_runtime_(s, s + hash_strlen(s));
}
// ----------
template<typename T>
constexpr
value hash_runtime_(const T &v)
{
auto *begin = v.data();
auto *end = begin + v.size();
return hash_runtime_((const char *)begin, (const char *)end);
}
static_assert(hash_strlen(std::string_view("abc")) == 3);
constexpr
value hash_compile_(const std::string_view &s)
{
return hash_compile_(s.begin(), s.end());
}
inline
value hash_runtime_(const std::string_view &s)
{
return hash_runtime_(s.begin(), s.end());
}
// ---------------
template<typename T>
constexpr
T hash_transform(value v)
{
return (T)v;
}
template<>
constexpr
u64 hash_transform(value v)
{
return v;
}
template<>
constexpr
u32 hash_transform(value h)
{
auto r = h - (h >> 32);
return (u32)r;
}
template<>
constexpr
u16 hash_transform(value h64)
{
auto h = hash_transform<u32>(h64);
auto r = h - (h >> 16);
return (u16)r;
}
template<>
constexpr
u8 hash_transform(value h64)
{
auto h = hash_transform<u16>(h64);
auto r = h - (h >> 8);
return (u8)r;
}
template<typename T>
constexpr
T hash_max(T v, T m)
{
return (m > 0) ? (v % m) : 0;
}
template<typename T>
constexpr T hash_compile(const char *s)
{
return hash_label(hash_transform<T>(hash_compile_(s)));
}
template<typename T>
constexpr T hash_compile(const std::string_view &s)
{
return hash_label(hash_transform<T>(hash_compile_(s)));
}
template<typename T, typename ... Args>
constexpr T hash_compile(const T max, Args&& ... args)
{
return
hash_label(
hash_max(
hash_transform<T>(hash_compile_(std::forward<Args>(args)...)),
max
)
);
}
// ----
static_assert(hash_compile<u64>("abcdefghijk") != 0);
static_assert(hash_compile<u32>("abcdefghijk") != 0);
static_assert(hash_compile<u16>("abcdefghijk") != 0);
static_assert(hash_compile<u8>("abcdefghijk") != 0);
// -----
template<typename T>
T hash_runtime(const char *str)
{
return hash_label(hash_transform<T>(hash_runtime_(std::string_view(str))));
}
template<typename T>
T hash_runtime(const std::string_view &s)
{
return hash_label(hash_transform<T>(hash_runtime_(s)));
}
template<typename T, typename U>
T hash_runtime(const U &v)
{
return hash_label(hash_transform<T>(hash_runtime_(v)));
}
template<typename T, typename ... Args>
constexpr T hash_runtime(const T max, Args&& ... args)
{
return
hash_label(
hash_max(
hash_transform<T>(hash_runtime_(std::forward<Args>(args)...)),
max
)
);
}
#ifdef _DEBUG
void hash_collision_test(size_t width, u64 value, const std::string_view &s);
template<typename T>
void hash_collision_test(T value, const std::string_view &s)
{
hash_collision_test(sizeof(T), value, s);
}
#else
template<typename T>
void hash_collision_test(T value, const std::string_view &s)
{
}
#endif
// ---------
} // namespace
#include "Hash+zltan+expected.inl"
namespace tjp::core {
using const_hash::hash_compile;
using const_hash::hash_runtime;
using const_hash::hash_version;
using const_hash::hash_collision_test;
} ;

View File

@@ -0,0 +1,85 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#include <tjp/core/const_hash/Hash.hpp>
#include <tjp/core/testing/catch.hpp>
#include <tjp/core/containers/Function.h>
#include <tjp/core/containers/Vector.h>
#include <tjp/core/string/String.h>
#include <tjp/core/log/Log.h>
#include <tjp/core/log/LogOf.h>
#include <tjp/core/debug/debug_break.h>
#include <iostream>
namespace tjp::core::const_hash {
namespace {
constexpr char X[] = "hello there";
SCENARIO("core::const_hash")
{
GIVEN("compile vs runtime")
{
constexpr auto hc = hash_compile<value>(X);
auto hr = hash_runtime<value>(X);
REQUIRE(hc == hr);
}
GIVEN("switch")
{
auto h = hash_runtime<value>("there");
switch (h)
{
case hash_compile<value>("hello"):
REQUIRE(false);
break;
case hash_compile<value>("there"):
REQUIRE(true);
break;
} ;
}
GIVEN("label")
{
bool correct = true;
Vector<int> v { 0 };
for (auto i=0; i<32768; ++i)
{
v[0] = i;
auto h = hash_runtime<value>(v);
correct = correct && (hash_version(h) == version);
}
REQUIRE(correct);
}
GIVEN("max")
{
bool correct = true;
Vector<int> v { 0 };
for (auto i=1; i<32768; ++i)
{
v[0] = i;
value maxValue = i;
auto h = hash_runtime<value>(maxValue, v);
correct = correct && (hash_version(h) == version);
correct = correct && (h < maxValue);
if (!correct)
debug_break();
}
REQUIRE(correct);
}
}
} // namespace
} // namespace

View File

@@ -0,0 +1,60 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#include <tjp/core/const_hash/Hash.hpp>
#include <tjp/core/testing/catch.hpp>
#include <tjp/core/containers/Function.h>
#include <tjp/core/containers/Vector.h>
#include <tjp/core/string/String.h>
#include <tjp/core/log/Log.h>
#include <tjp/core/log/LogOf.h>
#include <tjp/core/debug/debug_break.h>
#include <iostream>
namespace tjp::core::const_hash {
namespace {
SCENARIO("core::const_hash::expected")
{
GIVEN("expected")
{
std::ostringstream oss;
auto produceHash = [&](auto *s) {
auto hu64 = hash_runtime<u64>(s);
auto hu32 = hash_runtime<u32>(s);
auto hu16 = hash_runtime<u16>(s);
auto hu8 = hash_runtime<u8>(s);
oss << "static_assert(hash_compile<u64>(\"" << s << "\") == " << hu64 << "U);\n";
oss << "static_assert(hash_compile<u32>(\"" << s << "\") == " << hu32 << "U);\n";
oss << "static_assert(hash_compile<u16>(\"" << s << "\") == " << hu16 << "U);\n";
oss << "static_assert(hash_compile<u8>(\"" << s << "\") == " << (int)hu8 << "U);\n";
};
oss << "//-------------------------------\n";
oss << "// Expected Hashes\n";
produceHash("a");
produceHash("ab");
produceHash("abc");
produceHash("abcd");
produceHash("abcde");
produceHash("abcdef");
produceHash("abcdefg");
produceHash("tjp::core::variant::Variant");
produceHash("tjp::core::Str");
oss << "//-------------------------------\n";
std::cout << oss.str() << std::endl;
REQUIRE(true);
}
}
} // namespace
} // namespace

View File

@@ -0,0 +1,44 @@
project(SMHasher)
cmake_minimum_required(VERSION 2.4)
set(CMAKE_BUILD_TYPE Release)
add_library(
SMHasherSupport
AvalancheTest.cpp
Bitslice.cpp
Bitvec.cpp
CityTest.cpp
City.cpp
crc.cpp
DifferentialTest.cpp
Hashes.cpp
KeysetTest.cpp
lookup3.cpp
md5.cpp
MurmurHash1.cpp
MurmurHash2.cpp
MurmurHash3.cpp
ZLTanP.cpp
Platform.cpp
Random.cpp
sha1.cpp
SpeedTest.cpp
Spooky.cpp
SpookyTest.cpp
Stats.cpp
SuperFastHash.cpp
Types.cpp
PMurHash.c
)
add_executable(
SMHasher
main.cpp
)
target_link_libraries(
SMHasher
SMHasherSupport
)

View File

@@ -0,0 +1,94 @@
//-----------------------------------------------------------------------------
// Platform-specific functions and macros
#pragma once
#include <ctime>
#include <cstdint>
void SetAffinity ( int cpu );
//-----------------------------------------------------------------------------
// Microsoft Visual Studio
#if defined(_MSC_VER)
#define FORCE_INLINE __forceinline
#define NEVER_INLINE __declspec(noinline)
#include <stdlib.h>
#include <math.h> // Has to be included before intrin.h or VC complains about 'ceil'
#include <intrin.h> // for __rdtsc
#include "pstdint.h"
#define ROTL32(x,y) _rotl(x,y)
#define ROTL64(x,y) _rotl64(x,y)
#define ROTR32(x,y) _rotr(x,y)
#define ROTR64(x,y) _rotr64(x,y)
#pragma warning(disable : 4127) // "conditional expression is constant" in the if()s for avalanchetest
#pragma warning(disable : 4100)
#pragma warning(disable : 4702)
#define BIG_CONSTANT(x) (x)
// RDTSC == Read Time Stamp Counter
#define rdtsc() __rdtsc()
//-----------------------------------------------------------------------------
// Other compilers
#else // defined(_MSC_VER)
#include <stdint.h>
#define FORCE_INLINE inline __attribute__((always_inline))
#define NEVER_INLINE __attribute__((noinline))
inline uint32_t rotl32 ( uint32_t x, int8_t r )
{
return (x << r) | (x >> (32 - r));
}
inline uint64_t rotl64 ( uint64_t x, int8_t r )
{
return (x << r) | (x >> (64 - r));
}
inline uint32_t rotr32 ( uint32_t x, int8_t r )
{
return (x >> r) | (x << (32 - r));
}
inline uint64_t rotr64 ( uint64_t x, int8_t r )
{
return (x >> r) | (x << (64 - r));
}
#define ROTL32(x,y) rotl32(x,y)
#define ROTL64(x,y) rotl64(x,y)
#define ROTR32(x,y) rotr32(x,y)
#define ROTR64(x,y) rotr64(x,y)
#define BIG_CONSTANT(x) (x##LLU)
inline
uint64_t get_time_ns() {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return static_cast<uint64_t>(ts.tv_sec) * 1000000000 + static_cast<uint64_t>(ts.tv_nsec);
}
inline uint64_t rdtsc()
{
return get_time_ns();
}
#include <strings.h>
#define _stricmp strcasecmp
#endif // !defined(_MSC_VER)
//-----------------------------------------------------------------------------

View File

@@ -0,0 +1,17 @@
// get rid of constexpr for that shmasher
#define constexpr
#include "ZLTanP.h"
#include "ZLTanP.hpp"
uint64_t ZLTanPHash64 ( const void * key, int len, uint64_t seed )
{
return tjp::core::const_hash::zltan::fasthash64_compiletime((char *)key, len, seed);
}
uint64_t ZLTanPHashChunk64 ( const void * key, int len, uint64_t seed )
{
return tjp::core::const_hash::zltan::fasthash64_compiletime_chunk((char *)key, len, seed);
}

View File

@@ -0,0 +1,46 @@
//-----------------------------------------------------------------------------
// MurmurHash2 was written by Austin Appleby, and is placed in the public
// domain. The author hereby disclaims copyright to this source code.
#ifndef _ZLTanP_H_
#define _ZLTanP_H_
//-----------------------------------------------------------------------------
// Platform-specific functions and macros
// Microsoft Visual Studio
#if defined(_MSC_VER) && (_MSC_VER < 1600)
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned __int64 uint64_t;
// Other compilers
#else // defined(_MSC_VER)
#include <stdint.h>
#endif // !defined(_MSC_VER)
//-----------------------------------------------------------------------------
uint64_t ZLTanPHash64 ( const void * key, int len, uint64_t seed );
uint64_t ZLTanPHashChunk64 ( const void * key, int len, uint64_t seed );
inline void ZLTanPHash64_test ( const void * key, int len, uint32_t seed, void * out )
{
*(uint64_t*)out = ZLTanPHash64(key,len,seed);
}
inline void ZLTanPHashChunk64_test ( const void * key, int len, uint32_t seed, void * out )
{
*(uint64_t*)out = ZLTanPHashChunk64(key,len,seed);
}
//-----------------------------------------------------------------------------
#endif // _ZLTanP_H_

View File

@@ -0,0 +1 @@
../Hash+zltan.hpp

View File

@@ -0,0 +1,19 @@
git clone https://github.com/aappleby/smhasher
and then copy all of these files into the src
do the standard
cd smhasher/src
ln -fs ~/Projects/crown/Core_Zero/core/const_hash/smhasher/* .
mkdir build
cd build
cmake ../src
make
the Platform.h is changed to measure time correctly on the mac
run with
./SMHasher ZLTanHash64
./SMHasher ZLTanHashChunk64

View File

@@ -0,0 +1,603 @@
#include "Platform.h"
#include "Hashes.h"
#include "KeysetTest.h"
#include "SpeedTest.h"
#include "AvalancheTest.h"
#include "DifferentialTest.h"
#include "PMurHash.h"
#include "ZLTanP.h"
#include <stdio.h>
#include <time.h>
//-----------------------------------------------------------------------------
// Configuration. TODO - move these to command-line flags
bool g_testAll = false;
bool g_testSanity = false;
bool g_testSpeed = false;
bool g_testDiff = false;
bool g_testDiffDist = false;
bool g_testAvalanche = false;
bool g_testBIC = false;
bool g_testCyclic = false;
bool g_testTwoBytes = false;
bool g_testSparse = false;
bool g_testPermutation = false;
bool g_testWindow = false;
bool g_testText = false;
bool g_testZeroes = false;
bool g_testSeed = false;
//-----------------------------------------------------------------------------
// This is the list of all hashes that SMHasher can test.
struct HashInfo
{
pfHash hash;
int hashbits;
uint32_t verification;
const char * name;
const char * desc;
};
HashInfo g_hashes[] =
{
{ DoNothingHash, 32, 0x00000000, "donothing32", "Do-Nothing function (only valid for measuring call overhead)" },
{ DoNothingHash, 64, 0x00000000, "donothing64", "Do-Nothing function (only valid for measuring call overhead)" },
{ DoNothingHash, 128, 0x00000000, "donothing128", "Do-Nothing function (only valid for measuring call overhead)" },
{ crc32, 32, 0x3719DB20, "crc32", "CRC-32" },
{ md5_32, 32, 0xC10C356B, "md5_32a", "MD5, first 32 bits of result" },
{ sha1_32a, 32, 0xF9376EA7, "sha1_32a", "SHA1, first 32 bits of result" },
{ FNV, 32, 0xE3CBBE91, "FNV", "Fowler-Noll-Vo hash, 32-bit" },
{ Bernstein, 32, 0xBDB4B640, "bernstein", "Bernstein, 32-bit" },
{ lookup3_test, 32, 0x3D83917A, "lookup3", "Bob Jenkins' lookup3" },
{ SuperFastHash, 32, 0x980ACD1D, "superfast", "Paul Hsieh's SuperFastHash" },
{ MurmurOAAT_test, 32, 0x5363BD98, "MurmurOAAT", "Murmur one-at-a-time" },
{ Crap8_test, 32, 0x743E97A1, "Crap8", "Crap8" },
{ CityHash64_test, 64, 0x25A20825, "City64", "Google CityHash64WithSeed" },
{ CityHash128_test, 128, 0x6531F54E, "City128", "Google CityHash128WithSeed" },
{ SpookyHash32_test, 32, 0x3F798BBB, "Spooky32", "Bob Jenkins' SpookyHash, 32-bit result" },
{ SpookyHash64_test, 64, 0xA7F955F1, "Spooky64", "Bob Jenkins' SpookyHash, 64-bit result" },
{ SpookyHash128_test, 128, 0x8D263080, "Spooky128", "Bob Jenkins' SpookyHash, 128-bit result" },
// MurmurHash2
{ MurmurHash2_test, 32, 0x27864C1E, "Murmur2", "MurmurHash2 for x86, 32-bit" },
{ MurmurHash2A_test, 32, 0x7FBD4396, "Murmur2A", "MurmurHash2A for x86, 32-bit" },
{ MurmurHash64A_test, 64, 0x1F0D3804, "Murmur2B", "MurmurHash2 for x64, 64-bit" },
{ MurmurHash64B_test, 64, 0xDD537C05, "Murmur2C", "MurmurHash2 for x86, 64-bit" },
// MurmurHash3
{ MurmurHash3_x86_32, 32, 0xB0F57EE3, "Murmur3A", "MurmurHash3 for x86, 32-bit" },
{ MurmurHash3_x86_128, 128, 0xB3ECE62A, "Murmur3C", "MurmurHash3 for x86, 128-bit" },
{ MurmurHash3_x64_128, 128, 0x6384BA69, "Murmur3F", "MurmurHash3 for x64, 128-bit" },
{ PMurHash32_test, 32, 0xB0F57EE3, "PMurHash32", "Shane Day's portable-ized MurmurHash3 for x86, 32-bit." },
// CompiletimeFastHash
{ ZLTanPHash64_test, 64, 0x61B121EC, "ZLTanHash64", "ZLTan ZLTan ZLTan" },
{ ZLTanPHashChunk64_test, 64, 0x61B121EC, "ZLTanHashChunk64", "ZLTan ZLTan ZLTan" },
};
HashInfo * findHash ( const char * name )
{
for(size_t i = 0; i < sizeof(g_hashes) / sizeof(HashInfo); i++)
{
if(_stricmp(name,g_hashes[i].name) == 0) return &g_hashes[i];
}
return NULL;
}
//-----------------------------------------------------------------------------
// Self-test on startup - verify that all installed hashes work correctly.
void SelfTest ( void )
{
bool pass = true;
for(size_t i = 0; i < sizeof(g_hashes) / sizeof(HashInfo); i++)
{
HashInfo * info = & g_hashes[i];
pass &= VerificationTest(info->hash,info->hashbits,info->verification,false);
}
if(!pass)
{
printf("Self-test FAILED!\n");
for(size_t i = 0; i < sizeof(g_hashes) / sizeof(HashInfo); i++)
{
HashInfo * info = & g_hashes[i];
printf("%16s - ",info->name);
pass &= VerificationTest(info->hash,info->hashbits,info->verification,true);
}
exit(1);
}
}
//----------------------------------------------------------------------------
template < typename hashtype >
void test ( hashfunc<hashtype> hash, HashInfo * info )
{
const int hashbits = sizeof(hashtype) * 8;
printf("-------------------------------------------------------------------------------\n");
printf("--- Testing %s (%s)\n\n",info->name,info->desc);
//-----------------------------------------------------------------------------
// Sanity tests
if(g_testSanity || g_testAll)
{
printf("[[[ Sanity Tests ]]]\n\n");
VerificationTest(hash,hashbits,info->verification,true);
SanityTest(hash,hashbits);
AppendedZeroesTest(hash,hashbits);
printf("\n");
}
//-----------------------------------------------------------------------------
// Speed tests
if(g_testSpeed || g_testAll)
{
printf("[[[ Speed Tests ]]]\n\n");
BulkSpeedTest(info->hash,info->verification);
printf("\n");
for(int i = 1; i < 32; i++)
{
double cycles;
TinySpeedTest(hashfunc<hashtype>(info->hash),sizeof(hashtype),i,info->verification,true,cycles);
}
printf("\n");
}
//-----------------------------------------------------------------------------
// Differential tests
if(g_testDiff || g_testAll)
{
printf("[[[ Differential Tests ]]]\n\n");
bool result = true;
bool dumpCollisions = false;
result &= DiffTest< Blob<64>, hashtype >(hash,5,1000,dumpCollisions);
result &= DiffTest< Blob<128>, hashtype >(hash,4,1000,dumpCollisions);
result &= DiffTest< Blob<256>, hashtype >(hash,3,1000,dumpCollisions);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Differential-distribution tests
if(g_testDiffDist /*|| g_testAll*/)
{
printf("[[[ Differential Distribution Tests ]]]\n\n");
bool result = true;
result &= DiffDistTest2<uint64_t,hashtype>(hash);
printf("\n");
}
//-----------------------------------------------------------------------------
// Avalanche tests
if(g_testAvalanche || g_testAll)
{
printf("[[[ Avalanche Tests ]]]\n\n");
bool result = true;
result &= AvalancheTest< Blob< 32>, hashtype > (hash,300000);
result &= AvalancheTest< Blob< 40>, hashtype > (hash,300000);
result &= AvalancheTest< Blob< 48>, hashtype > (hash,300000);
result &= AvalancheTest< Blob< 56>, hashtype > (hash,300000);
result &= AvalancheTest< Blob< 64>, hashtype > (hash,300000);
result &= AvalancheTest< Blob< 72>, hashtype > (hash,300000);
result &= AvalancheTest< Blob< 80>, hashtype > (hash,300000);
result &= AvalancheTest< Blob< 88>, hashtype > (hash,300000);
result &= AvalancheTest< Blob< 96>, hashtype > (hash,300000);
result &= AvalancheTest< Blob<104>, hashtype > (hash,300000);
result &= AvalancheTest< Blob<112>, hashtype > (hash,300000);
result &= AvalancheTest< Blob<120>, hashtype > (hash,300000);
result &= AvalancheTest< Blob<128>, hashtype > (hash,300000);
result &= AvalancheTest< Blob<136>, hashtype > (hash,300000);
result &= AvalancheTest< Blob<144>, hashtype > (hash,300000);
result &= AvalancheTest< Blob<152>, hashtype > (hash,300000);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Bit Independence Criteria. Interesting, but doesn't tell us much about
// collision or distribution.
if(g_testBIC)
{
printf("[[[ Bit Independence Criteria ]]]\n\n");
bool result = true;
//result &= BicTest<uint64_t,hashtype>(hash,2000000);
BicTest3<Blob<88>,hashtype>(hash,2000000);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Keyset 'Cyclic' - keys of the form "abcdabcdabcd..."
if(g_testCyclic || g_testAll)
{
printf("[[[ Keyset 'Cyclic' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+0,8,10000000,drawDiagram);
result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+1,8,10000000,drawDiagram);
result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+2,8,10000000,drawDiagram);
result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+3,8,10000000,drawDiagram);
result &= CyclicKeyTest<hashtype>(hash,sizeof(hashtype)+4,8,10000000,drawDiagram);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Keyset 'TwoBytes' - all keys up to N bytes containing two non-zero bytes
// This generates some huge keysets, 128-bit tests will take ~1.3 gigs of RAM.
if(g_testTwoBytes || g_testAll)
{
printf("[[[ Keyset 'TwoBytes' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
for(int i = 4; i <= 20; i += 4)
{
result &= TwoBytesTest2<hashtype>(hash,i,drawDiagram);
}
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Keyset 'Sparse' - keys with all bits 0 except a few
if(g_testSparse || g_testAll)
{
printf("[[[ Keyset 'Sparse' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
result &= SparseKeyTest< 32,hashtype>(hash,6,true,true,true,drawDiagram);
result &= SparseKeyTest< 40,hashtype>(hash,6,true,true,true,drawDiagram);
result &= SparseKeyTest< 48,hashtype>(hash,5,true,true,true,drawDiagram);
result &= SparseKeyTest< 56,hashtype>(hash,5,true,true,true,drawDiagram);
result &= SparseKeyTest< 64,hashtype>(hash,5,true,true,true,drawDiagram);
result &= SparseKeyTest< 96,hashtype>(hash,4,true,true,true,drawDiagram);
result &= SparseKeyTest< 256,hashtype>(hash,3,true,true,true,drawDiagram);
result &= SparseKeyTest<2048,hashtype>(hash,2,true,true,true,drawDiagram);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Keyset 'Permutation' - all possible combinations of a set of blocks
if(g_testPermutation || g_testAll)
{
{
// This one breaks lookup3, surprisingly
printf("[[[ Keyset 'Combination Lowbits' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
uint32_t blocks[] =
{
0x00000000,
0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
};
result &= CombinationKeyTest<hashtype>(hash,8,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
{
printf("[[[ Keyset 'Combination Highbits' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
uint32_t blocks[] =
{
0x00000000,
0x20000000, 0x40000000, 0x60000000, 0x80000000, 0xA0000000, 0xC0000000, 0xE0000000
};
result &= CombinationKeyTest<hashtype>(hash,8,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
{
printf("[[[ Keyset 'Combination 0x8000000' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
uint32_t blocks[] =
{
0x00000000,
0x80000000,
};
result &= CombinationKeyTest<hashtype>(hash,20,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
{
printf("[[[ Keyset 'Combination 0x0000001' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
uint32_t blocks[] =
{
0x00000000,
0x00000001,
};
result &= CombinationKeyTest<hashtype>(hash,20,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
{
printf("[[[ Keyset 'Combination Hi-Lo' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
uint32_t blocks[] =
{
0x00000000,
0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
0x80000000, 0x40000000, 0xC0000000, 0x20000000, 0xA0000000, 0x60000000, 0xE0000000
};
result &= CombinationKeyTest<hashtype>(hash,6,blocks,sizeof(blocks) / sizeof(uint32_t),true,true,drawDiagram);
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
}
//-----------------------------------------------------------------------------
// Keyset 'Window'
// Skip distribution test for these - they're too easy to distribute well,
// and it generates a _lot_ of testing
if(g_testWindow || g_testAll)
{
printf("[[[ Keyset 'Window' Tests ]]]\n\n");
bool result = true;
bool testCollision = true;
bool testDistribution = false;
bool drawDiagram = false;
result &= WindowedKeyTest< Blob<hashbits*2>, hashtype > ( hash, 20, testCollision, testDistribution, drawDiagram );
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Keyset 'Text'
if(g_testText || g_testAll)
{
printf("[[[ Keyset 'Text' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
const char * alnum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
result &= TextKeyTest( hash, "Foo", alnum,4, "Bar", drawDiagram );
result &= TextKeyTest( hash, "FooBar", alnum,4, "", drawDiagram );
result &= TextKeyTest( hash, "", alnum,4, "FooBar", drawDiagram );
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Keyset 'Zeroes'
if(g_testZeroes || g_testAll)
{
printf("[[[ Keyset 'Zeroes' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
result &= ZeroKeyTest<hashtype>( hash, drawDiagram );
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
//-----------------------------------------------------------------------------
// Keyset 'Seed'
if(g_testSeed || g_testAll)
{
printf("[[[ Keyset 'Seed' Tests ]]]\n\n");
bool result = true;
bool drawDiagram = false;
result &= SeedTest<hashtype>( hash, 1000000, drawDiagram );
if(!result) printf("*********FAIL*********\n");
printf("\n");
}
}
//-----------------------------------------------------------------------------
uint32_t g_inputVCode = 1;
uint32_t g_outputVCode = 1;
uint32_t g_resultVCode = 1;
HashInfo * g_hashUnderTest = NULL;
void VerifyHash ( const void * key, int len, uint32_t seed, void * out )
{
g_inputVCode = MurmurOAAT(key,len,g_inputVCode);
g_inputVCode = MurmurOAAT(&seed,sizeof(uint32_t),g_inputVCode);
g_hashUnderTest->hash(key,len,seed,out);
g_outputVCode = MurmurOAAT(out,g_hashUnderTest->hashbits/8,g_outputVCode);
}
//-----------------------------------------------------------------------------
void testHash ( const char * name )
{
HashInfo * pInfo = findHash(name);
if(pInfo == NULL)
{
printf("Invalid hash '%s' specified\n",name);
return;
}
else
{
g_hashUnderTest = pInfo;
if(pInfo->hashbits == 32)
{
test<uint32_t>( VerifyHash, pInfo );
}
else if(pInfo->hashbits == 64)
{
test<uint64_t>( pInfo->hash, pInfo );
}
else if(pInfo->hashbits == 128)
{
test<uint128_t>( pInfo->hash, pInfo );
}
else if(pInfo->hashbits == 256)
{
test<uint256_t>( pInfo->hash, pInfo );
}
else
{
printf("Invalid hash bit width %d for hash '%s'",pInfo->hashbits,pInfo->name);
}
}
}
//-----------------------------------------------------------------------------
int main ( int argc, char ** argv )
{
const char * hashToTest = "murmur3a";
if(argc < 2)
{
printf("(No test hash given on command line, testing Murmur3_x86_32.)\n");
}
else
{
hashToTest = argv[1];
}
// Code runs on the 3rd CPU by default
SetAffinity((1 << 2));
SelfTest();
int timeBegin = clock();
g_testAll = true;
//g_testSanity = true;
//g_testSpeed = true;
//g_testAvalanche = true;
//g_testBIC = true;
//g_testCyclic = true;
//g_testTwoBytes = true;
//g_testDiff = true;
//g_testDiffDist = true;
//g_testSparse = true;
//g_testPermutation = true;
//g_testWindow = true;
//g_testZeroes = true;
testHash(hashToTest);
//----------
int timeEnd = clock();
printf("\n");
printf("Input vcode 0x%08x, Output vcode 0x%08x, Result vcode 0x%08x\n",g_inputVCode,g_outputVCode,g_resultVCode);
printf("Verification value is 0x%08x - Testing took %f seconds\n",g_verify,double(timeEnd-timeBegin)/double(CLOCKS_PER_SEC));
printf("-------------------------------------------------------------------------------\n");
return 0;
}