You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mum.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* Copyright (c) 2016 Vladimir Makarov <vmakarov@gcc.gnu.org>
  2. Permission is hereby granted, free of charge, to any person
  3. obtaining a copy of this software and associated documentation
  4. files (the "Software"), to deal in the Software without
  5. restriction, including without limitation the rights to use, copy,
  6. modify, merge, publish, distribute, sublicense, and/or sell copies
  7. of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be
  10. included in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  15. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. /* This file implements MUM (MUltiply and Mix) hashing. We randomize
  21. input data by 64x64-bit multiplication and mixing hi- and low-parts
  22. of the multiplication result by using an addition and then mix it
  23. into the current state. We use prime numbers randomly generated
  24. with the equal probability of their bit values for the
  25. multiplication. When all primes are used once, the state is
  26. randomized and the same prime numbers are used again for data
  27. randomization.
  28. The MUM hashing passes all SMHasher tests. Pseudo Random Number
  29. Generator based on MUM also passes NIST Statistical Test Suite for
  30. Random and Pseudorandom Number Generators for Cryptographic
  31. Applications (version 2.2.1) with 1000 bitstreams each containing
  32. 1M bits. MUM hashing is also faster Spooky64 and City64 on small
  33. strings (at least up to 512-bit) on Haswell and Power7. The MUM bulk
  34. speed (speed on very long data) is bigger than Spooky and City on
  35. Power7. On Haswell the bulk speed is bigger than Spooky one and
  36. close to City speed. */
  37. #ifndef __MUM_HASH__
  38. #define __MUM_HASH__
  39. #include <stddef.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <limits.h>
  43. #ifdef _MSC_VER
  44. typedef unsigned __int16 uint16_t;
  45. typedef unsigned __int32 uint32_t;
  46. typedef unsigned __int64 uint64_t;
  47. #else
  48. #include <stdint.h>
  49. #endif
  50. /* Macro saying to use 128-bit integers implemented by GCC for some
  51. targets. */
  52. #ifndef _MUM_USE_INT128
  53. /* In GCC uint128_t is defined if HOST_BITS_PER_WIDE_INT >= 64.
  54. HOST_WIDE_INT is long if HOST_BITS_PER_LONG > HOST_BITS_PER_INT,
  55. otherwise int. */
  56. #ifdef __SIZEOF_INT128__
  57. #define _MUM_USE_INT128 1
  58. #else
  59. #define _MUM_USE_INT128 0
  60. #endif
  61. #endif
  62. #if defined(__GNUC__) && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9) || (__GNUC__ > 4))
  63. #define _MUM_FRESH_GCC
  64. #endif
  65. #if !defined(__llvm__) && defined(_MUM_FRESH_GCC)
  66. #define _MUM_ATTRIBUTE_UNUSED __attribute__((unused))
  67. #define _MUM_OPTIMIZE(opts) __attribute__((__optimize__ (opts)))
  68. #define _MUM_TARGET(opts) __attribute__((__target__ (opts)))
  69. #define _MUM_INLINE __attribute__((always_inline))
  70. #else
  71. #define _MUM_ATTRIBUTE_UNUSED
  72. #define _MUM_OPTIMIZE(opts)
  73. #define _MUM_TARGET(opts)
  74. #define _MUM_INLINE
  75. #endif
  76. /* Here are different primes randomly generated with the equal
  77. probability of their bit values. They are used to randomize input
  78. values. */
  79. static uint64_t _mum_hash_step_prime = 0x2e0bb864e9ea7df5ULL;
  80. static uint64_t _mum_key_step_prime = 0xcdb32970830fcaa1ULL;
  81. static uint64_t _mum_block_start_prime = 0xc42b5e2e6480b23bULL;
  82. static uint64_t _mum_unroll_prime = 0x7b51ec3d22f7096fULL;
  83. static uint64_t _mum_tail_prime = 0xaf47d47c99b1461bULL;
  84. static uint64_t _mum_finish_prime1 = 0xa9a7ae7ceff79f3fULL;
  85. static uint64_t _mum_finish_prime2 = 0xaf47d47c99b1461bULL;
  86. static uint64_t _mum_primes [] = {
  87. 0X9ebdcae10d981691, 0X32b9b9b97a27ac7d, 0X29b5584d83d35bbd, 0X4b04e0e61401255f,
  88. 0X25e8f7b1f1c9d027, 0X80d4c8c000f3e881, 0Xbd1255431904b9dd, 0X8a3bd4485eee6d81,
  89. 0X3bc721b2aad05197, 0X71b1a19b907d6e33, 0X525e6c1084a8534b, 0X9e4c2cd340c1299f,
  90. 0Xde3add92e94caa37, 0X7e14eadb1f65311d, 0X3f5aa40f89812853, 0X33b15a3b587d15c9,
  91. };
  92. /* Multiply 64-bit V and P and return sum of high and low parts of the
  93. result. */
  94. static inline uint64_t _MUM_INLINE
  95. _mum (uint64_t v, uint64_t p) {
  96. uint64_t hi, lo;
  97. #if _MUM_USE_INT128
  98. #if defined(__aarch64__)
  99. /* AARCH64 needs 2 insns to calculate 128-bit result of the
  100. multiplication. If we use a generic code we actually call a
  101. function doing 128x128->128 bit multiplication. The function is
  102. very slow. */
  103. lo = v * p;
  104. __asm__ ("umulh %0, %1, %2" : "=r" (hi) : "r" (v), "r" (p));
  105. #else
  106. __uint128_t r = (__uint128_t) v * (__uint128_t) p;
  107. hi = (uint64_t) (r >> 64);
  108. lo = (uint64_t) r;
  109. #endif
  110. #else
  111. /* Implementation of 64x64->128-bit multiplication by four 32x32->64
  112. bit multiplication. */
  113. uint64_t hv = v >> 32, hp = p >> 32;
  114. uint64_t lv = (uint32_t) v, lp = (uint32_t) p;
  115. uint64_t rh = hv * hp;
  116. uint64_t rm_0 = hv * lp;
  117. uint64_t rm_1 = hp * lv;
  118. uint64_t rl = lv * lp;
  119. uint64_t t, carry = 0;
  120. /* We could ignore a carry bit here if we did not care about the
  121. same hash for 32-bit and 64-bit targets. */
  122. t = rl + (rm_0 << 32);
  123. #ifdef MUM_TARGET_INDEPENDENT_HASH
  124. carry = t < rl;
  125. #endif
  126. lo = t + (rm_1 << 32);
  127. #ifdef MUM_TARGET_INDEPENDENT_HASH
  128. carry += lo < t;
  129. #endif
  130. hi = rh + (rm_0 >> 32) + (rm_1 >> 32) + carry;
  131. #endif
  132. /* We could use XOR here too but, for some reasons, on Haswell and
  133. Power7 using an addition improves hashing performance by 10% for
  134. small strings. */
  135. return hi + lo;
  136. }
  137. #if defined(_MSC_VER)
  138. #define _mum_bswap_32(x) _byteswap_uint32_t (x)
  139. #define _mum_bswap_64(x) _byteswap_uint64_t (x)
  140. #elif defined(__APPLE__)
  141. #include <libkern/OSByteOrder.h>
  142. #define _mum_bswap_32(x) OSSwapInt32 (x)
  143. #define _mum_bswap_64(x) OSSwapInt64 (x)
  144. #elif defined(__GNUC__)
  145. #define _mum_bswap32(x) __builtin_bswap32 (x)
  146. #define _mum_bswap64(x) __builtin_bswap64 (x)
  147. #else
  148. #include <byteswap.h>
  149. #define _mum_bswap32(x) bswap32 (x)
  150. #define _mum_bswap64(x) bswap64 (x)
  151. #endif
  152. static inline uint64_t _MUM_INLINE
  153. _mum_le (uint64_t v) {
  154. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || !defined(MUM_TARGET_INDEPENDENT_HASH)
  155. return v;
  156. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  157. return _mum_bswap64 (v);
  158. #else
  159. #error "Unknown endianness"
  160. #endif
  161. }
  162. static inline uint32_t _MUM_INLINE
  163. _mum_le32 (uint32_t v) {
  164. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || !defined(MUM_TARGET_INDEPENDENT_HASH)
  165. return v;
  166. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  167. return _mum_bswap32 (v);
  168. #else
  169. #error "Unknown endianness"
  170. #endif
  171. }
  172. /* Macro defining how many times the most nested loop in
  173. _mum_hash_aligned will be unrolled by the compiler (although it can
  174. make an own decision:). Use only a constant here to help a
  175. compiler to unroll a major loop.
  176. The macro value affects the result hash for strings > 128 bit. The
  177. unroll factor greatly affects the hashing speed. We prefer the
  178. speed. */
  179. #ifndef _MUM_UNROLL_FACTOR_POWER
  180. #if defined(__PPC64__) && !defined(MUM_TARGET_INDEPENDENT_HASH)
  181. #define _MUM_UNROLL_FACTOR_POWER 3
  182. #elif defined(__aarch64__) && !defined(MUM_TARGET_INDEPENDENT_HASH)
  183. #define _MUM_UNROLL_FACTOR_POWER 4
  184. #else
  185. #define _MUM_UNROLL_FACTOR_POWER 2
  186. #endif
  187. #endif
  188. #if _MUM_UNROLL_FACTOR_POWER < 1
  189. #error "too small unroll factor"
  190. #elif _MUM_UNROLL_FACTOR_POWER > 4
  191. #error "We have not enough primes for such unroll factor"
  192. #endif
  193. #define _MUM_UNROLL_FACTOR (1 << _MUM_UNROLL_FACTOR_POWER)
  194. static inline uint64_t _MUM_OPTIMIZE("unroll-loops") _MUM_INLINE
  195. _mum_hash_aligned (uint64_t start, const void *key, size_t len) {
  196. uint64_t result = start;
  197. const unsigned char *str = (const unsigned char *) key;
  198. uint64_t u64;
  199. int i;
  200. size_t n;
  201. result = _mum (result, _mum_block_start_prime);
  202. while (len > _MUM_UNROLL_FACTOR * sizeof (uint64_t)) {
  203. /* This loop could be vectorized when we have vector insns for
  204. 64x64->128-bit multiplication. AVX2 currently only have a
  205. vector insn for 4 32x32->64-bit multiplication. */
  206. for (i = 0; i < _MUM_UNROLL_FACTOR; i++)
  207. result ^= _mum (_mum_le (((uint64_t *) str)[i]), _mum_primes[i]);
  208. len -= _MUM_UNROLL_FACTOR * sizeof (uint64_t);
  209. str += _MUM_UNROLL_FACTOR * sizeof (uint64_t);
  210. /* We will use the same prime numbers on the next iterations --
  211. randomize the state. */
  212. result = _mum (result, _mum_unroll_prime);
  213. }
  214. n = len / sizeof (uint64_t);
  215. for (i = 0; i < (int)n; i++)
  216. result ^= _mum (_mum_le (((uint64_t *) str)[i]), _mum_primes[i]);
  217. len -= n * sizeof (uint64_t); str += n * sizeof (uint64_t);
  218. switch (len) {
  219. case 7:
  220. u64 = _mum_le32 (*(uint32_t *) str);
  221. u64 |= (uint64_t) str[4] << 32;
  222. u64 |= (uint64_t) str[5] << 40;
  223. u64 |= (uint64_t) str[6] << 48;
  224. return result ^ _mum (u64, _mum_tail_prime);
  225. case 6:
  226. u64 = _mum_le32 (*(uint32_t *) str);
  227. u64 |= (uint64_t) str[4] << 32;
  228. u64 |= (uint64_t) str[5] << 40;
  229. return result ^ _mum (u64, _mum_tail_prime);
  230. case 5:
  231. u64 = _mum_le32 (*(uint32_t *) str);
  232. u64 |= (uint64_t) str[4] << 32;
  233. return result ^ _mum (u64, _mum_tail_prime);
  234. case 4:
  235. u64 = _mum_le32 (*(uint32_t *) str);
  236. return result ^ _mum (u64, _mum_tail_prime);
  237. case 3:
  238. u64 = str[0];
  239. u64 |= (uint64_t) str[1] << 8;
  240. u64 |= (uint64_t) str[2] << 16;
  241. return result ^ _mum (u64, _mum_tail_prime);
  242. case 2:
  243. u64 = str[0];
  244. u64 |= (uint64_t) str[1] << 8;
  245. return result ^ _mum (u64, _mum_tail_prime);
  246. case 1:
  247. u64 = str[0];
  248. return result ^ _mum (u64, _mum_tail_prime);
  249. }
  250. return result;
  251. }
  252. /* Final randomization of H. */
  253. static inline uint64_t
  254. _mum_final (uint64_t h) {
  255. h ^= _mum (h, _mum_finish_prime1);
  256. h ^= _mum (h, _mum_finish_prime2);
  257. return h;
  258. }
  259. #ifndef _MUM_UNALIGNED_ACCESS
  260. #if defined(__x86_64__) || defined(__i386__) || defined(__PPC64__) \
  261. || defined(__s390__) || defined(__m32c__) || defined(cris) \
  262. || defined(__CR16__) || defined(__vax__) || defined(__m68k__) \
  263. || defined(__aarch64__)
  264. #define _MUM_UNALIGNED_ACCESS 1
  265. #else
  266. #define _MUM_UNALIGNED_ACCESS 0
  267. #endif
  268. #endif
  269. /* When we need an aligned access to data being hashed we move part of
  270. the unaligned data to an aligned block of given size and then
  271. process it, repeating processing the data by the block. */
  272. #ifndef _MUM_BLOCK_LEN
  273. #define _MUM_BLOCK_LEN 1024
  274. #endif
  275. #if _MUM_BLOCK_LEN < 8
  276. #error "too small block length"
  277. #endif
  278. static inline uint64_t _MUM_INLINE
  279. _mum_hash_default (const void *key, size_t len, uint64_t seed) {
  280. uint64_t result;
  281. const unsigned char *str = (const unsigned char *) key;
  282. size_t block_len;
  283. uint64_t buf[_MUM_BLOCK_LEN / sizeof (uint64_t)];
  284. result = seed + len;
  285. if (_MUM_UNALIGNED_ACCESS || ((size_t) str & 0x7) == 0)
  286. result = _mum_hash_aligned (result, key, len);
  287. else {
  288. while (len != 0) {
  289. block_len = len < _MUM_BLOCK_LEN ? len : _MUM_BLOCK_LEN;
  290. memmove (buf, str, block_len);
  291. result = _mum_hash_aligned (result, buf, block_len);
  292. len -= block_len;
  293. str += block_len;
  294. }
  295. }
  296. return _mum_final (result);
  297. }
  298. static inline uint64_t _MUM_INLINE
  299. _mum_next_factor (void) {
  300. uint64_t start = 0;
  301. int i;
  302. for (i = 0; i < 8; i++)
  303. start = (start << 8) | rand() % 256;
  304. return start;
  305. }
  306. /* ++++++++++++++++++++++++++ Interface functions: +++++++++++++++++++ */
  307. /* Set random multiplicators depending on SEED. */
  308. static inline void
  309. mum_hash_randomize (uint64_t seed) {
  310. int i;
  311. srand (seed);
  312. _mum_hash_step_prime = _mum_next_factor ();
  313. _mum_key_step_prime = _mum_next_factor ();
  314. _mum_finish_prime1 = _mum_next_factor ();
  315. _mum_finish_prime2 = _mum_next_factor ();
  316. _mum_block_start_prime = _mum_next_factor ();
  317. _mum_unroll_prime = _mum_next_factor ();
  318. _mum_tail_prime = _mum_next_factor ();
  319. for (i = 0; i < (int)(sizeof (_mum_primes) / sizeof (uint64_t)); i++)
  320. _mum_primes[i] = _mum_next_factor ();
  321. }
  322. /* Start hashing data with SEED. Return the state. */
  323. static inline uint64_t
  324. mum_hash_init (uint64_t seed) {
  325. return seed;
  326. }
  327. /* Process data KEY with the state H and return the updated state. */
  328. static inline uint64_t
  329. mum_hash_step (uint64_t h, uint64_t key)
  330. {
  331. return _mum (h, _mum_hash_step_prime) ^ _mum (key, _mum_key_step_prime);
  332. }
  333. /* Return the result of hashing using the current state H. */
  334. static inline uint64_t
  335. mum_hash_finish (uint64_t h) {
  336. return _mum_final (h);
  337. }
  338. /* Fast hashing of KEY with SEED. The hash is always the same for the
  339. same key on any target. */
  340. static inline size_t
  341. mum_hash64 (uint64_t key, uint64_t seed) {
  342. return mum_hash_finish (mum_hash_step (mum_hash_init (seed), key));
  343. }
  344. /* Hash data KEY of length LEN and SEED. The hash depends on the
  345. target endianness and the unroll factor. */
  346. static inline uint64_t _MUM_INLINE
  347. mum_hash (const void *key, size_t len, uint64_t seed) {
  348. return _mum_hash_default (key, len, seed);
  349. }
  350. #endif