add alternate hash functions

This commit is contained in:
Matthew 2025-09-13 11:04:40 +10:00
parent f31cdfbedf
commit 29a98de0e0
2 changed files with 51 additions and 5 deletions

View File

@ -1052,7 +1052,7 @@ enum XXH3_INTERNALBUFFER_SIZE = 256; ///The size of the internal XXH3 buffer.
* See: XXH3_INITSTATE() for stack initialization. * See: XXH3_INITSTATE() for stack initialization.
* See: XXH32_state_s, XXH64_state_s * 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; align(64) XXH64_hash_t[8] acc;
/** The 8 accumulators. See XXH32_state_s::v and XXH64_state_s::v */ /** 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 === */ /* === XXH3 streaming === */
private void XXH3_INITSTATE(XXH3_state_t* XXH3_state_ptr) void XXH3_INITSTATE(XXH3_state_t* XXH3_state_ptr)
@safe nothrow @nogc @safe nothrow @nogc
{ {
(XXH3_state_ptr).seed = 0; (XXH3_state_ptr).seed = 0;
@ -1860,7 +1860,7 @@ private XXH_errorcode xxh3_64bits_reset_withSecret(
} }
/* XXH PUBLIC API - hidden in D module */ /* 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 @safe pure nothrow @nogc
{ {
if (statePtr == null) if (statePtr == null)
@ -2065,7 +2065,7 @@ do
} }
/* XXH PUBLIC API - hidden in D module */ /* 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 @safe pure nothrow @nogc
{ {
return xxh3_update(state, cast(const(ubyte)*) input, len, 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 */ /* 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 @trusted pure nothrow @nogc
{ {
const ubyte* secret = (state.extSecret == null) ? &state.customSecret[0] : &state.extSecret[0]; const ubyte* secret = (state.extSecret == null) ? &state.customSecret[0] : &state.extSecret[0];

46
util.d
View File

@ -9,6 +9,7 @@ import includes;
import std.stdio; import std.stdio;
import std.conv; import std.conv;
import std.string; import std.string;
import std.traits;
import core.stdc.string : memset; import core.stdc.string : memset;
import core.simd; import core.simd;
@ -535,6 +536,36 @@ Hash(string str)
return xxh3_64bits_withSeed(str.ptr, str.length, HASH_SEED); 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 pragma(inline) u64
RDTSC() RDTSC()
{ {
@ -1002,6 +1033,21 @@ unittest
table[100] = 100; 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
Stack!(u32) stack; Stack!(u32) stack;
Arena arena = CreateArena(MB(1)); Arena arena = CreateArena(MB(1));