Add implementation for the murmur3 x64_128 hash
Release / release (push) Successful in 3s

This commit is contained in:
2026-08-30 22:03:28 +01:00
parent d5578f009a
commit 55ff77c4ea
15 changed files with 980 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef HASHER_H
#define HASHER_H
#include "../../stream/stream.h"
#ifdef WP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP
typedef void (*WpHasher)(WpU8Stream *stream, void *hasher_io);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WP_PLATFORM_CPP
#endif // !HASHER_H
+105
View File
@@ -0,0 +1,105 @@
// vim:fileencoding=utf-8:foldmethod=marker
#include "murmur3.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/misc/misc_utils.h"
#define _bigConstant(x) (x##LLU)
wp_intern inline u64 fmix64(u64 k);
void wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) {
WpMur3HasherIO *io = (WpMur3HasherIO *)hasher_io;
const i32 nblocks = (bytes->count * bytes->item_size) / 16;
u64 h1 = io->seed;
u64 h2 = io->seed;
const u64 c1 = _bigConstant(0x87c37b91114253d5);
const u64 c2 = _bigConstant(0x4cf5ad432745937f);
//----------
// body
for(int i = 0; i < nblocks; ++i) {
u64 k1 = *((u64 *)wpStreamConsumeCount(u8, bytes, sizeof(u64)));
u64 k2 = *((u64 *)wpStreamConsumeCount(u8, bytes, sizeof(u64)));
k1 *= c1;
k1 = wpMiscUtilsRotl64(k1, 31);
k1 *= c2;
h1 ^= k1;
h1 = wpMiscUtilsRotl64(h1, 27);
h1 += h2;
h1 = h1 * 5 +0x52dce729;
k2 *= c2;
k2 = wpMiscUtilsRotl64(k2, 33);
k2 *= c1;
h2 ^= k2;
h2 = wpMiscUtilsRotl64(h2, 31);
h2 += h1;
h2 = h2*5+0x38495ab5;
}
//----------
// tail
const u8 *tail = wpStreamConsumeCount(u8, bytes, bytes->count - bytes->position);
u64 k1 = 0;
u64 k2 = 0;
u64 len = bytes->count * bytes->item_size;
switch(len & 15) {
case 15: k2 ^= ((u64)tail[14]) << 48;
case 14: k2 ^= ((u64)tail[13]) << 40;
case 13: k2 ^= ((u64)tail[12]) << 32;
case 12: k2 ^= ((u64)tail[11]) << 24;
case 11: k2 ^= ((u64)tail[10]) << 16;
case 10: k2 ^= ((u64)tail[ 9]) << 8;
case 9: k2 ^= ((u64)tail[ 8]) << 0;
k2 *= c2; k2 = wpMiscUtilsRotl64(k2,33); k2 *= c1; h2 ^= k2;
case 8: k1 ^= ((u64)tail[ 7]) << 56;
case 7: k1 ^= ((u64)tail[ 6]) << 48;
case 6: k1 ^= ((u64)tail[ 5]) << 40;
case 5: k1 ^= ((u64)tail[ 4]) << 32;
case 4: k1 ^= ((u64)tail[ 3]) << 24;
case 3: k1 ^= ((u64)tail[ 2]) << 16;
case 2: k1 ^= ((u64)tail[ 1]) << 8;
case 1: k1 ^= ((u64)tail[ 0]) << 0;
k1 *= c1; k1 = wpMiscUtilsRotl64(k1,31); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len; h2 ^= len;
h1 += h2;
h2 += h1;
h1 = fmix64(h1);
h2 = fmix64(h2);
h1 += h2;
h2 += h1;
io->out_hash1 = h1;
io->out_hash2 = h2;
}
wp_intern inline u64 fmix64(u64 k) {
k ^= k >> 33;
k *= _bigConstant(0xff51afd7ed558ccd);
k ^= k >> 33;
k *= _bigConstant(0xc4ceb9fe1a85ec53);
k ^= k >> 33;
return k;
}
+40
View File
@@ -0,0 +1,40 @@
// vim:fileencoding=utf-8:foldmethod=marker
/**
* An implementation of the x64 128-bit variant of the MurmurHash3 algorithm
* based on https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
*/
#ifndef MURMUR3_H
#define MURMUR3_H
#include "../../stream/stream.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h"
#ifdef WP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP
#define WP_MUR3_HASHER_MAGIC ((u64)0x57504d4841534833)
typedef struct {
u64 magic;
u64 out_hash1;
u64 out_hash2;
u32 seed;
} WpMur3HasherIO;
#ifdef WP_PLATFORM_CPP
#define wpMur3HasherIO(SEED) (WpMur3HasherIO{WP_MUR3_HASHER_MAGIC, 0, 0, SEED})
#else
#define wpMur3HasherIO(SEED) ((WpMur3HasherIO){ .magic = WP_MUR3_HASHER_MAGIC, .seed = SEED })
#endif
void wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WP_PLATFORM_CPP
#endif // !MURMUR3_H
+1
View File
@@ -6,6 +6,7 @@
#include "wapp_base.h"
#include "array/array.c"
#include "dbl_list/dbl_list.c"
#include "hash/hasher/murmur3.c"
#include "queue/queue.c"
#include "stream/stream.c"
#include "mem/allocator/mem_allocator.c"
+2
View File
@@ -5,6 +5,8 @@
#include "array/array.h"
#include "dbl_list/dbl_list.h"
#include "hash/hasher/hasher.h"
#include "hash/hasher/murmur3.h"
#include "queue/queue.h"
#include "stream/stream.h"
#include "mem/allocator/mem_allocator.h"
+4
View File
@@ -50,6 +50,10 @@ BEGIN_C_LINKAGE
#define wpMiscUtilsIsPowerOfTwo(NUM) ((NUM & (NUM - 1)) == 0)
#define wpMiscUtilsOffsetPointer(PTR, OFFSET) ((void *)((uptr)(PTR) + (OFFSET)))
wp_intern inline u64 wpMiscUtilsRotl64(u64 x, i8 rotation) {
return (x << rotation) | (x >> (64 - rotation));
}
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE