hash.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/common_funcs.h"
  8. #include "common/common_types.h"
  9. #include "common/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, size_t 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 targeted 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, size_t len, u32 seed, void* out) {
  31. const u8* data = (const u8*)key;
  32. const size_t 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 (size_t i = 0; i < nblocks; i++) {
  40. u64 k1 = getblock64(blocks, i * 2 + 0);
  41. u64 k2 = getblock64(blocks, i * 2 + 1);
  42. k1 *= c1;
  43. k1 = _rotl64(k1, 31);
  44. k1 *= c2;
  45. h1 ^= k1;
  46. h1 = _rotl64(h1, 27);
  47. h1 += h2;
  48. h1 = h1 * 5 + 0x52dce729;
  49. k2 *= c2;
  50. k2 = _rotl64(k2, 33);
  51. k2 *= c1;
  52. h2 ^= k2;
  53. h2 = _rotl64(h2, 31);
  54. h2 += h1;
  55. h2 = h2 * 5 + 0x38495ab5;
  56. }
  57. // Tail
  58. const u8* tail = (const u8*)(data + nblocks * 16);
  59. u64 k1 = 0;
  60. u64 k2 = 0;
  61. switch (len & 15) {
  62. case 15:
  63. k2 ^= ((u64)tail[14]) << 48;
  64. case 14:
  65. k2 ^= ((u64)tail[13]) << 40;
  66. case 13:
  67. k2 ^= ((u64)tail[12]) << 32;
  68. case 12:
  69. k2 ^= ((u64)tail[11]) << 24;
  70. case 11:
  71. k2 ^= ((u64)tail[10]) << 16;
  72. case 10:
  73. k2 ^= ((u64)tail[9]) << 8;
  74. case 9:
  75. k2 ^= ((u64)tail[8]) << 0;
  76. k2 *= c2;
  77. k2 = _rotl64(k2, 33);
  78. k2 *= c1;
  79. h2 ^= k2;
  80. case 8:
  81. k1 ^= ((u64)tail[7]) << 56;
  82. case 7:
  83. k1 ^= ((u64)tail[6]) << 48;
  84. case 6:
  85. k1 ^= ((u64)tail[5]) << 40;
  86. case 5:
  87. k1 ^= ((u64)tail[4]) << 32;
  88. case 4:
  89. k1 ^= ((u64)tail[3]) << 24;
  90. case 3:
  91. k1 ^= ((u64)tail[2]) << 16;
  92. case 2:
  93. k1 ^= ((u64)tail[1]) << 8;
  94. case 1:
  95. k1 ^= ((u64)tail[0]) << 0;
  96. k1 *= c1;
  97. k1 = _rotl64(k1, 31);
  98. k1 *= c2;
  99. h1 ^= k1;
  100. };
  101. // Finalization
  102. h1 ^= len;
  103. h2 ^= len;
  104. h1 += h2;
  105. h2 += h1;
  106. h1 = fmix64(h1);
  107. h2 = fmix64(h2);
  108. h1 += h2;
  109. h2 += h1;
  110. ((u64*)out)[0] = h1;
  111. ((u64*)out)[1] = h2;
  112. }
  113. } // namespace Common