summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar bunnei2015-07-21 22:54:38 -0400
committerGravatar bunnei2015-08-15 17:33:46 -0400
commitd67e2f78b77c0739caa8c6a915685aa8961a519c (patch)
tree6973754fc51ceb01efecd8618150084fd0608487
parentCommon: Ported over boilerplate x86 JIT code from Dolphin/PPSSPP. (diff)
downloadyuzu-d67e2f78b77c0739caa8c6a915685aa8961a519c.tar.gz
yuzu-d67e2f78b77c0739caa8c6a915685aa8961a519c.tar.xz
yuzu-d67e2f78b77c0739caa8c6a915685aa8961a519c.zip
Common: Added MurmurHash3 hash function for general-purpose use.
-rw-r--r--src/common/CMakeLists.txt4
-rw-r--r--src/common/cpu_detect_generic.cpp4
-rw-r--r--src/common/cpu_detect_x86.cpp1
-rw-r--r--src/common/hash.cpp126
-rw-r--r--src/common/hash.h25
-rw-r--r--src/video_core/shader/shader.cpp2
6 files changed, 159 insertions, 3 deletions
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
6 break_points.cpp 6 break_points.cpp
7 emu_window.cpp 7 emu_window.cpp
8 file_util.cpp 8 file_util.cpp
9 hash.cpp
9 key_map.cpp 10 key_map.cpp
10 logging/filter.cpp 11 logging/filter.cpp
11 logging/text_formatter.cpp 12 logging/text_formatter.cpp
@@ -35,6 +36,7 @@ set(HEADERS
35 debug_interface.h 36 debug_interface.h
36 emu_window.h 37 emu_window.h
37 file_util.h 38 file_util.h
39 hash.h
38 key_map.h 40 key_map.h
39 linear_disk_cache.h 41 linear_disk_cache.h
40 logging/text_formatter.h 42 logging/text_formatter.h
@@ -59,7 +61,7 @@ set(HEADERS
59 vector_math.h 61 vector_math.h
60 ) 62 )
61 63
62if(_M_X86) 64if(_M_X86_64)
63 set(SRCS ${SRCS} 65 set(SRCS ${SRCS}
64 cpu_detect_x86.cpp 66 cpu_detect_x86.cpp
65 x64_emitter.cpp) 67 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 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "cpu_detect.h" 5#include "cpu_detect.h"
6#include "hash.h"
6 7
7namespace Common { 8namespace Common {
8 9
9CPUInfo cpu_info; 10CPUInfo cpu_info;
10 11
11CPUInfo::CPUInfo() { } 12CPUInfo::CPUInfo() {
13}
12 14
13std::string CPUInfo::Summarize() { 15std::string CPUInfo::Summarize() {
14 return "Generic"; 16 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 @@
7 7
8#include "common_types.h" 8#include "common_types.h"
9#include "cpu_detect.h" 9#include "cpu_detect.h"
10#include "hash.h"
10 11
11#ifndef _WIN32 12#ifndef _WIN32
12 13
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 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#if defined(_MSC_VER)
6#include <stdlib.h>
7#endif
8
9#include "common_funcs.h"
10#include "common_types.h"
11#include "hash.h"
12
13namespace Common {
14
15// MurmurHash3 was written by Austin Appleby, and is placed in the public
16// domain. The author hereby disclaims copyright to this source code.
17
18// Block read - if your platform needs to do endian-swapping or can only handle aligned reads, do
19// the conversion here
20
21static FORCE_INLINE u32 getblock32(const u32* p, int i) {
22 return p[i];
23}
24
25static FORCE_INLINE u64 getblock64(const u64* p, int i) {
26 return p[i];
27}
28
29// Finalization mix - force all bits of a hash block to avalanche
30
31static FORCE_INLINE u32 fmix32(u32 h) {
32 h ^= h >> 16;
33 h *= 0x85ebca6b;
34 h ^= h >> 13;
35 h *= 0xc2b2ae35;
36 h ^= h >> 16;
37
38 return h;
39}
40
41static FORCE_INLINE u64 fmix64(u64 k) {
42 k ^= k >> 33;
43 k *= 0xff51afd7ed558ccdllu;
44 k ^= k >> 33;
45 k *= 0xc4ceb9fe1a85ec53llu;
46 k ^= k >> 33;
47
48 return k;
49}
50
51// This is the 128-bit variant of the MurmurHash3 hash function that is targetted for 64-bit
52// platforms (MurmurHash3_x64_128). It was taken from:
53// https://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
54void MurmurHash3_128(const void* key, int len, u32 seed, void* out) {
55 const u8 * data = (const u8*)key;
56 const int nblocks = len / 16;
57
58 u64 h1 = seed;
59 u64 h2 = seed;
60
61 const u64 c1 = 0x87c37b91114253d5llu;
62 const u64 c2 = 0x4cf5ad432745937fllu;
63
64 // Body
65
66 const u64 * blocks = (const u64 *)(data);
67
68 for (int i = 0; i < nblocks; i++) {
69 u64 k1 = getblock64(blocks,i*2+0);
70 u64 k2 = getblock64(blocks,i*2+1);
71
72 k1 *= c1; k1 = _rotl64(k1,31); k1 *= c2; h1 ^= k1;
73
74 h1 = _rotl64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
75
76 k2 *= c2; k2 = _rotl64(k2,33); k2 *= c1; h2 ^= k2;
77
78 h2 = _rotl64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
79 }
80
81 // Tail
82
83 const u8 * tail = (const u8*)(data + nblocks*16);
84
85 u64 k1 = 0;
86 u64 k2 = 0;
87
88 switch (len & 15) {
89 case 15: k2 ^= ((u64)tail[14]) << 48;
90 case 14: k2 ^= ((u64)tail[13]) << 40;
91 case 13: k2 ^= ((u64)tail[12]) << 32;
92 case 12: k2 ^= ((u64)tail[11]) << 24;
93 case 11: k2 ^= ((u64)tail[10]) << 16;
94 case 10: k2 ^= ((u64)tail[ 9]) << 8;
95 case 9: k2 ^= ((u64)tail[ 8]) << 0;
96 k2 *= c2; k2 = _rotl64(k2,33); k2 *= c1; h2 ^= k2;
97
98 case 8: k1 ^= ((u64)tail[ 7]) << 56;
99 case 7: k1 ^= ((u64)tail[ 6]) << 48;
100 case 6: k1 ^= ((u64)tail[ 5]) << 40;
101 case 5: k1 ^= ((u64)tail[ 4]) << 32;
102 case 4: k1 ^= ((u64)tail[ 3]) << 24;
103 case 3: k1 ^= ((u64)tail[ 2]) << 16;
104 case 2: k1 ^= ((u64)tail[ 1]) << 8;
105 case 1: k1 ^= ((u64)tail[ 0]) << 0;
106 k1 *= c1; k1 = _rotl64(k1,31); k1 *= c2; h1 ^= k1;
107 };
108
109 // Finalization
110
111 h1 ^= len; h2 ^= len;
112
113 h1 += h2;
114 h2 += h1;
115
116 h1 = fmix64(h1);
117 h2 = fmix64(h2);
118
119 h1 += h2;
120 h2 += h1;
121
122 ((u64*)out)[0] = h1;
123 ((u64*)out)[1] = h2;
124}
125
126} // 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 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/common_types.h"
8
9namespace Common {
10
11void MurmurHash3_128(const void* key, int len, u32 seed, void* out);
12
13/**
14 * Computes a 64-bit hash over the specified block of data
15 * @param data Block of data to compute hash over
16 * @param len Length of data (in bytes) to compute hash over
17 * @returns 64-bit hash value that was computed over the data block
18 */
19static inline u64 ComputeHash64(const void* data, int len) {
20 u64 res[2];
21 MurmurHash3_128(data, len, 0, res);
22 return res[0];
23}
24
25} // 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 {
16namespace Shader { 16namespace Shader {
17 17
18void Setup(UnitState& state) { 18void Setup(UnitState& state) {
19 // TODO(bunnei): This will be used by the JIT in a subsequent commit 19 // TODO(bunnei): This will be used by the JIT in a subsequent patch
20} 20}
21 21
22static Common::Profiling::TimingCategory shader_category("Vertex Shader"); 22static Common::Profiling::TimingCategory shader_category("Vertex Shader");