Hash.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Config.h"
  25. #include "Assert.h"
  26. #include "Types.h"
  27. #include "StringUtils.h"
  28. namespace crown
  29. {
  30. namespace hash
  31. {
  32. //-----------------------------------------------------------------------------
  33. /// MurmurHash2, by Austin Appleby
  34. ///
  35. /// @note
  36. /// This code makes a few assumptions about how your machine behaves
  37. ///
  38. /// 1. We can read a 4-byte value from any address without crashing
  39. /// 2. sizeof(int) == 4
  40. ///
  41. /// And it has a few limitations -
  42. ///
  43. /// 1. It will not work incrementally.
  44. /// 2. It will not produce the same results on little-endian and big-endian
  45. /// machines.
  46. inline uint32_t murmur2_32(const void* key, size_t len, uint32_t seed = 0)
  47. {
  48. CE_ASSERT_NOT_NULL(key);
  49. // 'm' and 'r' are mixing constants generated offline.
  50. // They're not really 'magic', they just happen to work well.
  51. const unsigned int m = 0x5bd1e995;
  52. const int r = 24;
  53. // Initialize the hash to a 'random' value
  54. unsigned int h = seed ^ len;
  55. // Mix 4 bytes at a time into the hash
  56. const unsigned char * data = (const unsigned char *)key;
  57. while(len >= 4)
  58. {
  59. unsigned int k = *(unsigned int *)data;
  60. k *= m;
  61. k ^= k >> r;
  62. k *= m;
  63. h *= m;
  64. h ^= k;
  65. data += 4;
  66. len -= 4;
  67. }
  68. // Handle the last few bytes of the input array
  69. switch(len)
  70. {
  71. case 3: h ^= data[2] << 16;
  72. case 2: h ^= data[1] << 8;
  73. case 1: h ^= data[0];
  74. h *= m;
  75. };
  76. // Do a few final mixes of the hash to ensure the last few
  77. // bytes are well-incorporated.
  78. h ^= h >> 13;
  79. h *= m;
  80. h ^= h >> 15;
  81. return h;
  82. }
  83. //-----------------------------------------------------------------------------
  84. inline uint64_t murmur2_64(const void* key, size_t len, unsigned int seed = 0)
  85. {
  86. CE_ASSERT_NOT_NULL(key);
  87. const unsigned int m = 0x5bd1e995;
  88. const int r = 24;
  89. unsigned int h1 = seed ^ len;
  90. unsigned int h2 = 0;
  91. const unsigned int * data = (const unsigned int *)key;
  92. while(len >= 8)
  93. {
  94. unsigned int k1 = *data++;
  95. k1 *= m; k1 ^= k1 >> r; k1 *= m;
  96. h1 *= m; h1 ^= k1;
  97. len -= 4;
  98. unsigned int k2 = *data++;
  99. k2 *= m; k2 ^= k2 >> r; k2 *= m;
  100. h2 *= m; h2 ^= k2;
  101. len -= 4;
  102. }
  103. if(len >= 4)
  104. {
  105. unsigned int k1 = *data++;
  106. k1 *= m; k1 ^= k1 >> r; k1 *= m;
  107. h1 *= m; h1 ^= k1;
  108. len -= 4;
  109. }
  110. switch(len)
  111. {
  112. case 3: h2 ^= ((unsigned char*)data)[2] << 16;
  113. case 2: h2 ^= ((unsigned char*)data)[1] << 8;
  114. case 1: h2 ^= ((unsigned char*)data)[0];
  115. h2 *= m;
  116. };
  117. h1 ^= h2 >> 18; h1 *= m;
  118. h2 ^= h1 >> 22; h2 *= m;
  119. h1 ^= h2 >> 17; h1 *= m;
  120. h2 ^= h1 >> 19; h2 *= m;
  121. uint64_t h = h1;
  122. h = (h << 32) | h2;
  123. return h;
  124. }
  125. #ifdef CROWN_DEBUG
  126. inline uint32_t HASH32(const char *s, uint32_t value)
  127. {
  128. CE_ASSERT(murmur2_32(s, string::strlen(s), 0) == value, "Hash mismatch");
  129. return value;
  130. }
  131. inline uint64_t HASH64(const char* s, uint64_t value)
  132. {
  133. CE_ASSERT(murmur2_64(s, string::strlen(s), 0) == value, "Hash mismatch");
  134. return value;
  135. }
  136. #else
  137. #define HASH32(s, v) (v)
  138. #define HASH64(s, v) (v)
  139. #endif
  140. } // namespace hash
  141. } // namespace crown