diff options
Diffstat (limited to 'src/common/hash.cpp')
| -rw-r--r-- | src/common/hash.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
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 | |||
| 13 | namespace 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 | |||
| 21 | static FORCE_INLINE u32 getblock32(const u32* p, int i) { | ||
| 22 | return p[i]; | ||
| 23 | } | ||
| 24 | |||
| 25 | static 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 | |||
| 31 | static 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 | |||
| 41 | static 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 | ||
| 54 | void 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 | ||