From 29a98de0e05cb04001b42a55b1ae094ab42f2ab1 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 13 Sep 2025 11:04:40 +1000 Subject: [PATCH] add alternate hash functions --- external/xxhash/xxhash.d | 10 ++++----- util.d | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/external/xxhash/xxhash.d b/external/xxhash/xxhash.d index cff5682..fa2b386 100644 --- a/external/xxhash/xxhash.d +++ b/external/xxhash/xxhash.d @@ -1052,7 +1052,7 @@ enum XXH3_INTERNALBUFFER_SIZE = 256; ///The size of the internal XXH3 buffer. * See: XXH3_INITSTATE() for stack initialization. * See: XXH32_state_s, XXH64_state_s */ -private align(64) struct XXH3_state_t +align(64) struct XXH3_state_t { align(64) XXH64_hash_t[8] acc; /** The 8 accumulators. See XXH32_state_s::v and XXH64_state_s::v */ @@ -1799,7 +1799,7 @@ XXH64_hash_t xxh3_64bits_withSecretandSeed( /* === XXH3 streaming === */ -private void XXH3_INITSTATE(XXH3_state_t* XXH3_state_ptr) +void XXH3_INITSTATE(XXH3_state_t* XXH3_state_ptr) @safe nothrow @nogc { (XXH3_state_ptr).seed = 0; @@ -1860,7 +1860,7 @@ private XXH_errorcode xxh3_64bits_reset_withSecret( } /* XXH PUBLIC API - hidden in D module */ -private XXH_errorcode xxh3_64bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed) +XXH_errorcode xxh3_64bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed) @safe pure nothrow @nogc { if (statePtr == null) @@ -2065,7 +2065,7 @@ do } /* XXH PUBLIC API - hidden in D module */ -private XXH_errorcode xxh3_64bits_update(scope XXH3_state_t* state, scope const(void)* input, size_t len) +XXH_errorcode xxh3_64bits_update(scope XXH3_state_t* state, scope const(void)* input, size_t len) @safe pure nothrow @nogc { return xxh3_update(state, cast(const(ubyte)*) input, len, @@ -2104,7 +2104,7 @@ private void xxh3_digest_long(XXH64_hash_t* acc, const XXH3_state_t* state, cons } /* XXH PUBLIC API - hidden in D module */ -private XXH64_hash_t xxh3_64bits_digest(const XXH3_state_t* state) +XXH64_hash_t xxh3_64bits_digest(const XXH3_state_t* state) @trusted pure nothrow @nogc { const ubyte* secret = (state.extSecret == null) ? &state.customSecret[0] : &state.extSecret[0]; diff --git a/util.d b/util.d index 53996e0..d1166dd 100644 --- a/util.d +++ b/util.d @@ -9,6 +9,7 @@ import includes; import std.stdio; import std.conv; import std.string; +import std.traits; import core.stdc.string : memset; import core.simd; @@ -535,6 +536,36 @@ Hash(string str) return xxh3_64bits_withSeed(str.ptr, str.length, HASH_SEED); } +pragma(inline) u64 +Hash(void* ptr_1, u64 len_1, void* ptr_2, u64 len_2) +{ + XXH3_state_t xxh; + XXH3_INITSTATE(&xxh); + xxh3_64bits_reset_withSeed(&xxh, HASH_SEED); + xxh3_64bits_update(&xxh, ptr_1, len_1); + xxh3_64bits_update(&xxh, ptr_2, len_2); + + return xxh3_64bits_digest(&xxh); +} + +pragma(inline) u64 +Hash(T, U)(T value_1, U value_2) if (isArray!(T) && isArray!(U)) +{ + return Hash(value_1.ptr, value_1.length*T.sizeof, value_2.ptr, value_2.length*U.sizeof); +} + +pragma(inline) u64 +Hash(T, U)(T value_1, U value_2) if (isArray!(T) && !isArray!(U)) +{ + return Hash(value_1.ptr, value_1.length*value_1[0].sizeof, &value_2, U.sizeof); +} + +pragma(inline) u64 +Hash(T, U)(T value_1, U value_2) if (!isArray!(T) && !isArray!(U)) +{ + return Hash(&value_1, T.sizeof, &value_2, U.sizeof); +} + pragma(inline) u64 RDTSC() { @@ -1002,6 +1033,21 @@ unittest table[100] = 100; } + { // Hash + u8[10] arr_1 = 5; + u64[10] arr_2 = 555; + u64 val_1 = 555555; + u32 val_2 = 33333; + + u64 v1 = Hash(arr_1, arr_2); + u64 v2 = Hash(arr_1, val_1); + u64 v3 = Hash(val_1, val_2); + + assert(v1 > 0); + assert(v2 > 0); + assert(v3 > 0); + } + { // Stack Stack!(u32) stack; Arena arena = CreateArena(MB(1));