hash.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #ifndef JEMALLOC_INTERNAL_HASH_H
  2. #define JEMALLOC_INTERNAL_HASH_H
  3. #include "jemalloc/internal/assert.h"
  4. /*
  5. * The following hash function is based on MurmurHash3, placed into the public
  6. * domain by Austin Appleby. See https://github.com/aappleby/smhasher for
  7. * details.
  8. */
  9. /******************************************************************************/
  10. /* Internal implementation. */
  11. static inline uint32_t
  12. hash_rotl_32(uint32_t x, int8_t r) {
  13. return ((x << r) | (x >> (32 - r)));
  14. }
  15. static inline uint64_t
  16. hash_rotl_64(uint64_t x, int8_t r) {
  17. return ((x << r) | (x >> (64 - r)));
  18. }
  19. static inline uint32_t
  20. hash_get_block_32(const uint32_t *p, int i) {
  21. /* Handle unaligned read. */
  22. if (unlikely((uintptr_t)p & (sizeof(uint32_t)-1)) != 0) {
  23. uint32_t ret;
  24. memcpy(&ret, (uint8_t *)(p + i), sizeof(uint32_t));
  25. return ret;
  26. }
  27. return p[i];
  28. }
  29. static inline uint64_t
  30. hash_get_block_64(const uint64_t *p, int i) {
  31. /* Handle unaligned read. */
  32. if (unlikely((uintptr_t)p & (sizeof(uint64_t)-1)) != 0) {
  33. uint64_t ret;
  34. memcpy(&ret, (uint8_t *)(p + i), sizeof(uint64_t));
  35. return ret;
  36. }
  37. return p[i];
  38. }
  39. static inline uint32_t
  40. hash_fmix_32(uint32_t h) {
  41. h ^= h >> 16;
  42. h *= 0x85ebca6b;
  43. h ^= h >> 13;
  44. h *= 0xc2b2ae35;
  45. h ^= h >> 16;
  46. return h;
  47. }
  48. static inline uint64_t
  49. hash_fmix_64(uint64_t k) {
  50. k ^= k >> 33;
  51. k *= KQU(0xff51afd7ed558ccd);
  52. k ^= k >> 33;
  53. k *= KQU(0xc4ceb9fe1a85ec53);
  54. k ^= k >> 33;
  55. return k;
  56. }
  57. static inline uint32_t
  58. hash_x86_32(const void *key, int len, uint32_t seed) {
  59. const uint8_t *data = (const uint8_t *) key;
  60. const int nblocks = len / 4;
  61. uint32_t h1 = seed;
  62. const uint32_t c1 = 0xcc9e2d51;
  63. const uint32_t c2 = 0x1b873593;
  64. /* body */
  65. {
  66. const uint32_t *blocks = (const uint32_t *) (data + nblocks*4);
  67. int i;
  68. for (i = -nblocks; i; i++) {
  69. uint32_t k1 = hash_get_block_32(blocks, i);
  70. k1 *= c1;
  71. k1 = hash_rotl_32(k1, 15);
  72. k1 *= c2;
  73. h1 ^= k1;
  74. h1 = hash_rotl_32(h1, 13);
  75. h1 = h1*5 + 0xe6546b64;
  76. }
  77. }
  78. /* tail */
  79. {
  80. const uint8_t *tail = (const uint8_t *) (data + nblocks*4);
  81. uint32_t k1 = 0;
  82. switch (len & 3) {
  83. case 3: k1 ^= tail[2] << 16; JEMALLOC_FALLTHROUGH;
  84. case 2: k1 ^= tail[1] << 8; JEMALLOC_FALLTHROUGH;
  85. case 1: k1 ^= tail[0]; k1 *= c1; k1 = hash_rotl_32(k1, 15);
  86. k1 *= c2; h1 ^= k1;
  87. }
  88. }
  89. /* finalization */
  90. h1 ^= len;
  91. h1 = hash_fmix_32(h1);
  92. return h1;
  93. }
  94. static inline void
  95. hash_x86_128(const void *key, const int len, uint32_t seed,
  96. uint64_t r_out[2]) {
  97. const uint8_t * data = (const uint8_t *) key;
  98. const int nblocks = len / 16;
  99. uint32_t h1 = seed;
  100. uint32_t h2 = seed;
  101. uint32_t h3 = seed;
  102. uint32_t h4 = seed;
  103. const uint32_t c1 = 0x239b961b;
  104. const uint32_t c2 = 0xab0e9789;
  105. const uint32_t c3 = 0x38b34ae5;
  106. const uint32_t c4 = 0xa1e38b93;
  107. /* body */
  108. {
  109. const uint32_t *blocks = (const uint32_t *) (data + nblocks*16);
  110. int i;
  111. for (i = -nblocks; i; i++) {
  112. uint32_t k1 = hash_get_block_32(blocks, i*4 + 0);
  113. uint32_t k2 = hash_get_block_32(blocks, i*4 + 1);
  114. uint32_t k3 = hash_get_block_32(blocks, i*4 + 2);
  115. uint32_t k4 = hash_get_block_32(blocks, i*4 + 3);
  116. k1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;
  117. h1 = hash_rotl_32(h1, 19); h1 += h2;
  118. h1 = h1*5 + 0x561ccd1b;
  119. k2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;
  120. h2 = hash_rotl_32(h2, 17); h2 += h3;
  121. h2 = h2*5 + 0x0bcaa747;
  122. k3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;
  123. h3 = hash_rotl_32(h3, 15); h3 += h4;
  124. h3 = h3*5 + 0x96cd1c35;
  125. k4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;
  126. h4 = hash_rotl_32(h4, 13); h4 += h1;
  127. h4 = h4*5 + 0x32ac3b17;
  128. }
  129. }
  130. /* tail */
  131. {
  132. const uint8_t *tail = (const uint8_t *) (data + nblocks*16);
  133. uint32_t k1 = 0;
  134. uint32_t k2 = 0;
  135. uint32_t k3 = 0;
  136. uint32_t k4 = 0;
  137. switch (len & 15) {
  138. case 15: k4 ^= tail[14] << 16; JEMALLOC_FALLTHROUGH;
  139. case 14: k4 ^= tail[13] << 8; JEMALLOC_FALLTHROUGH;
  140. case 13: k4 ^= tail[12] << 0;
  141. k4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;
  142. JEMALLOC_FALLTHROUGH;
  143. case 12: k3 ^= (uint32_t) tail[11] << 24; JEMALLOC_FALLTHROUGH;
  144. case 11: k3 ^= tail[10] << 16; JEMALLOC_FALLTHROUGH;
  145. case 10: k3 ^= tail[ 9] << 8; JEMALLOC_FALLTHROUGH;
  146. case 9: k3 ^= tail[ 8] << 0;
  147. k3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;
  148. JEMALLOC_FALLTHROUGH;
  149. case 8: k2 ^= (uint32_t) tail[ 7] << 24; JEMALLOC_FALLTHROUGH;
  150. case 7: k2 ^= tail[ 6] << 16; JEMALLOC_FALLTHROUGH;
  151. case 6: k2 ^= tail[ 5] << 8; JEMALLOC_FALLTHROUGH;
  152. case 5: k2 ^= tail[ 4] << 0;
  153. k2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;
  154. JEMALLOC_FALLTHROUGH;
  155. case 4: k1 ^= (uint32_t) tail[ 3] << 24; JEMALLOC_FALLTHROUGH;
  156. case 3: k1 ^= tail[ 2] << 16; JEMALLOC_FALLTHROUGH;
  157. case 2: k1 ^= tail[ 1] << 8; JEMALLOC_FALLTHROUGH;
  158. case 1: k1 ^= tail[ 0] << 0;
  159. k1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;
  160. break;
  161. }
  162. }
  163. /* finalization */
  164. h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
  165. h1 += h2; h1 += h3; h1 += h4;
  166. h2 += h1; h3 += h1; h4 += h1;
  167. h1 = hash_fmix_32(h1);
  168. h2 = hash_fmix_32(h2);
  169. h3 = hash_fmix_32(h3);
  170. h4 = hash_fmix_32(h4);
  171. h1 += h2; h1 += h3; h1 += h4;
  172. h2 += h1; h3 += h1; h4 += h1;
  173. r_out[0] = (((uint64_t) h2) << 32) | h1;
  174. r_out[1] = (((uint64_t) h4) << 32) | h3;
  175. }
  176. static inline void
  177. hash_x64_128(const void *key, const int len, const uint32_t seed,
  178. uint64_t r_out[2]) {
  179. const uint8_t *data = (const uint8_t *) key;
  180. const int nblocks = len / 16;
  181. uint64_t h1 = seed;
  182. uint64_t h2 = seed;
  183. const uint64_t c1 = KQU(0x87c37b91114253d5);
  184. const uint64_t c2 = KQU(0x4cf5ad432745937f);
  185. /* body */
  186. {
  187. const uint64_t *blocks = (const uint64_t *) (data);
  188. int i;
  189. for (i = 0; i < nblocks; i++) {
  190. uint64_t k1 = hash_get_block_64(blocks, i*2 + 0);
  191. uint64_t k2 = hash_get_block_64(blocks, i*2 + 1);
  192. k1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;
  193. h1 = hash_rotl_64(h1, 27); h1 += h2;
  194. h1 = h1*5 + 0x52dce729;
  195. k2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;
  196. h2 = hash_rotl_64(h2, 31); h2 += h1;
  197. h2 = h2*5 + 0x38495ab5;
  198. }
  199. }
  200. /* tail */
  201. {
  202. const uint8_t *tail = (const uint8_t*)(data + nblocks*16);
  203. uint64_t k1 = 0;
  204. uint64_t k2 = 0;
  205. switch (len & 15) {
  206. case 15: k2 ^= ((uint64_t)(tail[14])) << 48; JEMALLOC_FALLTHROUGH;
  207. case 14: k2 ^= ((uint64_t)(tail[13])) << 40; JEMALLOC_FALLTHROUGH;
  208. case 13: k2 ^= ((uint64_t)(tail[12])) << 32; JEMALLOC_FALLTHROUGH;
  209. case 12: k2 ^= ((uint64_t)(tail[11])) << 24; JEMALLOC_FALLTHROUGH;
  210. case 11: k2 ^= ((uint64_t)(tail[10])) << 16; JEMALLOC_FALLTHROUGH;
  211. case 10: k2 ^= ((uint64_t)(tail[ 9])) << 8; JEMALLOC_FALLTHROUGH;
  212. case 9: k2 ^= ((uint64_t)(tail[ 8])) << 0;
  213. k2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;
  214. JEMALLOC_FALLTHROUGH;
  215. case 8: k1 ^= ((uint64_t)(tail[ 7])) << 56; JEMALLOC_FALLTHROUGH;
  216. case 7: k1 ^= ((uint64_t)(tail[ 6])) << 48; JEMALLOC_FALLTHROUGH;
  217. case 6: k1 ^= ((uint64_t)(tail[ 5])) << 40; JEMALLOC_FALLTHROUGH;
  218. case 5: k1 ^= ((uint64_t)(tail[ 4])) << 32; JEMALLOC_FALLTHROUGH;
  219. case 4: k1 ^= ((uint64_t)(tail[ 3])) << 24; JEMALLOC_FALLTHROUGH;
  220. case 3: k1 ^= ((uint64_t)(tail[ 2])) << 16; JEMALLOC_FALLTHROUGH;
  221. case 2: k1 ^= ((uint64_t)(tail[ 1])) << 8; JEMALLOC_FALLTHROUGH;
  222. case 1: k1 ^= ((uint64_t)(tail[ 0])) << 0;
  223. k1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;
  224. break;
  225. }
  226. }
  227. /* finalization */
  228. h1 ^= len; h2 ^= len;
  229. h1 += h2;
  230. h2 += h1;
  231. h1 = hash_fmix_64(h1);
  232. h2 = hash_fmix_64(h2);
  233. h1 += h2;
  234. h2 += h1;
  235. r_out[0] = h1;
  236. r_out[1] = h2;
  237. }
  238. /******************************************************************************/
  239. /* API. */
  240. static inline void
  241. hash(const void *key, size_t len, const uint32_t seed, size_t r_hash[2]) {
  242. assert(len <= INT_MAX); /* Unfortunate implementation limitation. */
  243. #if (LG_SIZEOF_PTR == 3 && !defined(JEMALLOC_BIG_ENDIAN))
  244. hash_x64_128(key, (int)len, seed, (uint64_t *)r_hash);
  245. #else
  246. {
  247. uint64_t hashes[2];
  248. hash_x86_128(key, (int)len, seed, hashes);
  249. r_hash[0] = (size_t)hashes[0];
  250. r_hash[1] = (size_t)hashes[1];
  251. }
  252. #endif
  253. }
  254. #endif /* JEMALLOC_INTERNAL_HASH_H */