From 07c98630038ba2b83ec4e6702ca22cf41fe6f723 Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 1 Jun 2020 00:19:02 +0000 Subject: [PATCH] Core: Mem git-svn-id: svn://ultimatepp.org/upp/trunk@14523 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Mem.cpp | 36 ++++++++++++++++++++++++++++++++++++ uppsrc/Core/Mem.h | 36 +----------------------------------- uppsrc/Core/Ops.h | 4 ++++ 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/uppsrc/Core/Mem.cpp b/uppsrc/Core/Mem.cpp index 676bfa3fc..3f0123ef4 100644 --- a/uppsrc/Core/Mem.cpp +++ b/uppsrc/Core/Mem.cpp @@ -85,4 +85,40 @@ bool memeq64(const void *p, const void *q, size_t len) { return inline_memeq64_a #endif +#ifdef CPU_UNALIGNED + +NOUBSAN // CPU supports unaligned memory access +hash_t memhash(const void *ptr, size_t count) +{ + unsigned hash = 1234567890U; + + const unsigned *ds = (unsigned *)ptr; + const unsigned *de = ds + (count >> 2); + while(ds < de) + hash = ((hash << 5) - hash) ^ *ds++; + + const byte *s = (byte *)ds; + const byte *e = s + (count & 3); + while(s < e) + hash = ((hash << 5) - hash) ^ *s++; + + return hash; +} + +#else + +hash_t memhash(const void *ptr, size_t count) +{ + unsigned hash = 1234567890U; + + const byte *s = (byte *)ptr; + const byte *e = s + count; + while(s < e) + hash = ((hash << 5) - hash) ^ *s++; + + return hash; +} + +#endif + }; \ No newline at end of file diff --git a/uppsrc/Core/Mem.h b/uppsrc/Core/Mem.h index 6d9742442..f3c352f2c 100644 --- a/uppsrc/Core/Mem.h +++ b/uppsrc/Core/Mem.h @@ -615,38 +615,4 @@ int inline_memcmp_aligned(const char *a, const char *b, size_t len) } #endif -#ifdef CPU_UNALIGNED - -NOUBSAN // CPU supports unaligned memory access -inline hash_t memhash(const void *ptr, size_t count) -{ - unsigned hash = 1234567890U; - - const unsigned *ds = (unsigned *)ptr; - const unsigned *de = ds + (count >> 2); - while(ds < de) - hash = ((hash << 5) - hash) ^ *ds++; - - const byte *s = (byte *)ds; - const byte *e = s + (count & 3); - while(s < e) - hash = ((hash << 5) - hash) ^ *s++; - - return hash; -} - -#else - -inline hash_t memhash(const void *ptr, size_t count) -{ - unsigned hash = 1234567890U; - - const byte *s = (byte *)ptr; - const byte *e = s + count; - while(s < e) - hash = ((hash << 5) - hash) ^ *s++; - - return hash; -} - -#endif +hash_t memhash(const void *ptr, size_t count); diff --git a/uppsrc/Core/Ops.h b/uppsrc/Core/Ops.h index 4ac0aa0cc..97a19f663 100644 --- a/uppsrc/Core/Ops.h +++ b/uppsrc/Core/Ops.h @@ -158,6 +158,10 @@ inline dword FoldHash(dword h) return SwapEndian32(0xa3613c16 * h); } +#define HASH32_CONST1 0xf7c21089 +#define HASH32_CONST2 0xc85abc8d +#define HASH32_CONST3 0x8642b0fe + #define HASH64_CONST1 I64(0xf7c21089bee7c0a5) #define HASH64_CONST2 I64(0xc85abc8da7534a4d) #define HASH64_CONST3 I64(0x8642b0fe3e86671b)