From d67e2f78b77c0739caa8c6a915685aa8961a519c Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 21 Jul 2015 22:54:38 -0400 Subject: [PATCH] Common: Added MurmurHash3 hash function for general-purpose use. --- src/common/CMakeLists.txt | 4 +- src/common/cpu_detect_generic.cpp | 4 +- src/common/cpu_detect_x86.cpp | 1 + src/common/hash.cpp | 126 ++++++++++++++++++++++++++++++ src/common/hash.h | 25 ++++++ src/video_core/shader/shader.cpp | 2 +- 6 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 src/common/hash.cpp create mode 100644 src/common/hash.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 600193858..af387c6c8 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -6,6 +6,7 @@ set(SRCS break_points.cpp emu_window.cpp file_util.cpp + hash.cpp key_map.cpp logging/filter.cpp logging/text_formatter.cpp @@ -35,6 +36,7 @@ set(HEADERS debug_interface.h emu_window.h file_util.h + hash.h key_map.h linear_disk_cache.h logging/text_formatter.h @@ -59,7 +61,7 @@ set(HEADERS vector_math.h ) -if(_M_X86) +if(_M_X86_64) set(SRCS ${SRCS} cpu_detect_x86.cpp x64_emitter.cpp) diff --git a/src/common/cpu_detect_generic.cpp b/src/common/cpu_detect_generic.cpp index b99c22af8..ccec324d9 100644 --- a/src/common/cpu_detect_generic.cpp +++ b/src/common/cpu_detect_generic.cpp @@ -3,12 +3,14 @@ // Refer to the license.txt file included. #include "cpu_detect.h" +#include "hash.h" namespace Common { CPUInfo cpu_info; -CPUInfo::CPUInfo() { } +CPUInfo::CPUInfo() { +} std::string CPUInfo::Summarize() { return "Generic"; diff --git a/src/common/cpu_detect_x86.cpp b/src/common/cpu_detect_x86.cpp index 2dff69b94..0bcff726d 100644 --- a/src/common/cpu_detect_x86.cpp +++ b/src/common/cpu_detect_x86.cpp @@ -7,6 +7,7 @@ #include "common_types.h" #include "cpu_detect.h" +#include "hash.h" #ifndef _WIN32 diff --git a/src/common/hash.cpp b/src/common/hash.cpp new file mode 100644 index 000000000..413e9c6f1 --- /dev/null +++ b/src/common/hash.cpp @@ -0,0 +1,126 @@ +// Copyright 2015 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#if defined(_MSC_VER) +#include +#endif + +#include "common_funcs.h" +#include "common_types.h" +#include "hash.h" + +namespace Common { + +// MurmurHash3 was written by Austin Appleby, and is placed in the public +// domain. The author hereby disclaims copyright to this source code. + +// Block read - if your platform needs to do endian-swapping or can only handle aligned reads, do +// the conversion here + +static FORCE_INLINE u32 getblock32(const u32* p, int i) { + return p[i]; +} + +static FORCE_INLINE u64 getblock64(const u64* p, int i) { + return p[i]; +} + +// Finalization mix - force all bits of a hash block to avalanche + +static FORCE_INLINE u32 fmix32(u32 h) { + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + + return h; +} + +static FORCE_INLINE u64 fmix64(u64 k) { + k ^= k >> 33; + k *= 0xff51afd7ed558ccdllu; + k ^= k >> 33; + k *= 0xc4ceb9fe1a85ec53llu; + k ^= k >> 33; + + return k; +} + +// This is the 128-bit variant of the MurmurHash3 hash function that is targetted for 64-bit +// platforms (MurmurHash3_x64_128). It was taken from: +// https://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp +void MurmurHash3_128(const void* key, int len, u32 seed, void* out) { + const u8 * data = (const u8*)key; + const int nblocks = len / 16; + + u64 h1 = seed; + u64 h2 = seed; + + const u64 c1 = 0x87c37b91114253d5llu; + const u64 c2 = 0x4cf5ad432745937fllu; + + // Body + + const u64 * blocks = (const u64 *)(data); + + for (int i = 0; i < nblocks; i++) { + u64 k1 = getblock64(blocks,i*2+0); + u64 k2 = getblock64(blocks,i*2+1); + + k1 *= c1; k1 = _rotl64(k1,31); k1 *= c2; h1 ^= k1; + + h1 = _rotl64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; + + k2 *= c2; k2 = _rotl64(k2,33); k2 *= c1; h2 ^= k2; + + h2 = _rotl64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; + } + + // Tail + + const u8 * tail = (const u8*)(data + nblocks*16); + + u64 k1 = 0; + u64 k2 = 0; + + 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 = _rotl64(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 = _rotl64(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; + + ((u64*)out)[0] = h1; + ((u64*)out)[1] = h2; +} + +} // namespace Common diff --git a/src/common/hash.h b/src/common/hash.h new file mode 100644 index 000000000..a3850be68 --- /dev/null +++ b/src/common/hash.h @@ -0,0 +1,25 @@ +// Copyright 2015 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +namespace Common { + +void MurmurHash3_128(const void* key, int len, u32 seed, void* out); + +/** + * Computes a 64-bit hash over the specified block of data + * @param data Block of data to compute hash over + * @param len Length of data (in bytes) to compute hash over + * @returns 64-bit hash value that was computed over the data block + */ +static inline u64 ComputeHash64(const void* data, int len) { + u64 res[2]; + MurmurHash3_128(data, len, 0, res); + return res[0]; +} + +} // namespace Common diff --git a/src/video_core/shader/shader.cpp b/src/video_core/shader/shader.cpp index e397e8e03..13e22cb53 100644 --- a/src/video_core/shader/shader.cpp +++ b/src/video_core/shader/shader.cpp @@ -16,7 +16,7 @@ namespace Pica { namespace Shader { void Setup(UnitState& state) { - // TODO(bunnei): This will be used by the JIT in a subsequent commit + // TODO(bunnei): This will be used by the JIT in a subsequent patch } static Common::Profiling::TimingCategory shader_category("Vertex Shader");