f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi/* MurmurHash3 was written by Austin Appleby, and is placed in the public
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi domain. The author hereby disclaims copyright to this source code.
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi Note - The x86 and x64 versions do _not_ produce the same results, as the
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi algorithms are optimized for their respective platforms. You can still
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi compile and run any of them on any platform, but your performance with the
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi non-native version will be less than optimal.
c9a7c4d24386315524222f3da805a693fb3cd3bfMartti Rannanjärvi Adapted for Dovecot by Aki Tuomi <aki.tuomi@dovecot.fi> 2017-11-27
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi//-----------------------------------------------------------------------------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi// Block read - if your platform needs to do endian-swapping or can only
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi// handle aligned reads, do the conversion here
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomistatic inline uint32_t getblock32(const uint32_t *p, int i)
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi return p[i];
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi//-----------------------------------------------------------------------------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi// Finalization mix - force all bits of a hash block to avalanche
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h ^= h >> 16;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h *= 0x85ebca6b;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h ^= h >> 13;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h *= 0xc2b2ae35;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h ^= h >> 16;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomivoid murmurhash3_32 (const void *key, size_t len, uint32_t seed,
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi unsigned char out[STATIC_ARRAY MURMURHASH3_32_RESULTBYTES])
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi const uint8_t *tail = (const uint8_t *)(data + nblocks*4);
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi // finalization
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi//-----------------------------------------------------------------------------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomistatic inline uint64_t getblock64(const uint64_t *p, int i)
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi return p[i];
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi k ^= k >> 33;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi k ^= k >> 33;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi k ^= k >> 33;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomivoid murmurhash3_128(const void *key, size_t len, uint32_t seed,
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi unsigned char out[STATIC_ARRAY MURMURHASH3_128_RESULTBYTES])
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi const uint8_t *tail = (const uint8_t *)(data + nblocks*16);
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi // finalization
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomivoid murmurhash3_128(const void *key, size_t len, uint32_t seed,
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi unsigned char out[STATIC_ARRAY MURMURHASH3_128_RESULTBYTES])
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi const uint8_t *tail = (const uint8_t *)(data + nblocks*16);
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
0351233834ffe10f4e64d12064680d4b01ffc138Martti Rannanjärvi /* fall through */
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi //----------
f6de86ea29e87fba001b6231d38a4c51e8a5c543Aki Tuomi // finalization