hash.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #if defined(_MSC_VER)
  5. #include <stdlib.h>
  6. #endif
  7. #include "common_funcs.h"
  8. #include "common_types.h"
  9. #include "hash.h"
  10. namespace Common {
  11. // MurmurHash3 was written by Austin Appleby, and is placed in the public
  12. // domain. The author hereby disclaims copyright to this source code.
  13. // Block read - if your platform needs to do endian-swapping or can only handle aligned reads, do
  14. // the conversion here
  15. static FORCE_INLINE u64 getblock64(const u64* p, int i) {
  16. return p[i];
  17. }
  18. // Finalization mix - force all bits of a hash block to avalanche
  19. static FORCE_INLINE u64 fmix64(u64 k) {
  20. k ^= k >> 33;
  21. k *= 0xff51afd7ed558ccdllu;
  22. k ^= k >> 33;
  23. k *= 0xc4ceb9fe1a85ec53llu;
  24. k ^= k >> 33;
  25. return k;
  26. }
  27. // This is the 128-bit variant of the MurmurHash3 hash function that is targetted for 64-bit
  28. // platforms (MurmurHash3_x64_128). It was taken from:
  29. // https://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
  30. void MurmurHash3_128(const void* key, int len, u32 seed, void* out) {
  31. const u8 * data = (const u8*)key;
  32. const int nblocks = len / 16;
  33. u64 h1 = seed;
  34. u64 h2 = seed;
  35. const u64 c1 = 0x87c37b91114253d5llu;
  36. const u64 c2 = 0x4cf5ad432745937fllu;
  37. // Body
  38. const u64 * blocks = (const u64 *)(data);
  39. for (int i = 0; i < nblocks; i++) {
  40. u64 k1 = getblock64(blocks,i*2+0);
  41. u64 k2 = getblock64(blocks,i*2+1);
  42. k1 *= c1; k1 = _rotl64(k1,31); k1 *= c2; h1 ^= k1;
  43. h1 = _rotl64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
  44. k2 *= c2; k2 = _rotl64(k2,33); k2 *= c1; h2 ^= k2;
  45. h2 = _rotl64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
  46. }
  47. // Tail
  48. const u8 * tail = (const u8*)(data + nblocks*16);
  49. u64 k1 = 0;
  50. u64 k2 = 0;
  51. switch (len & 15) {
  52. case 15: k2 ^= ((u64)tail[14]) << 48;
  53. case 14: k2 ^= ((u64)tail[13]) << 40;
  54. case 13: k2 ^= ((u64)tail[12]) << 32;
  55. case 12: k2 ^= ((u64)tail[11]) << 24;
  56. case 11: k2 ^= ((u64)tail[10]) << 16;
  57. case 10: k2 ^= ((u64)tail[ 9]) << 8;
  58. case 9: k2 ^= ((u64)tail[ 8]) << 0;
  59. k2 *= c2; k2 = _rotl64(k2,33); k2 *= c1; h2 ^= k2;
  60. case 8: k1 ^= ((u64)tail[ 7]) << 56;
  61. case 7: k1 ^= ((u64)tail[ 6]) << 48;
  62. case 6: k1 ^= ((u64)tail[ 5]) << 40;
  63. case 5: k1 ^= ((u64)tail[ 4]) << 32;
  64. case 4: k1 ^= ((u64)tail[ 3]) << 24;
  65. case 3: k1 ^= ((u64)tail[ 2]) << 16;
  66. case 2: k1 ^= ((u64)tail[ 1]) << 8;
  67. case 1: k1 ^= ((u64)tail[ 0]) << 0;
  68. k1 *= c1; k1 = _rotl64(k1,31); k1 *= c2; h1 ^= k1;
  69. };
  70. // Finalization
  71. h1 ^= len; h2 ^= len;
  72. h1 += h2;
  73. h2 += h1;
  74. h1 = fmix64(h1);
  75. h2 = fmix64(h2);
  76. h1 += h2;
  77. h2 += h1;
  78. ((u64*)out)[0] = h1;
  79. ((u64*)out)[1] = h2;
  80. }
  81. } // namespace Common