1 // SPDX-License-Identifier: LGPL-2.1+
3 * MurmurHash3 was written by Austin Appleby, and is placed in the public
4 * domain. The author hereby disclaims copyright to this source code.
9 #include "murmurhash3.h"
11 static inline u64 rotl64(u64 x, s8 r)
13 return (x << r) | (x >> (64 - r));
16 #define ROTL64(x, y) rotl64(x, y)
17 static __always_inline u64 getblock64(const u64 *p, int i)
19 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
21 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
22 return __builtin_bswap64(p[i]);
24 #error "can't figure out byte order"
28 static __always_inline void putblock64(u64 *p, int i, u64 value)
30 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
32 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
33 p[i] = __builtin_bswap64(value);
35 #error "can't figure out byte order"
39 /* Finalization mix - force all bits of a hash block to avalanche */
41 static __always_inline u64 fmix64(u64 k)
44 k *= 0xff51afd7ed558ccdLLU;
46 k *= 0xc4ceb9fe1a85ec53LLU;
52 void murmurhash3_128(const void *key, const int len, const u32 seed, void *out)
55 const int nblocks = len / 16;
60 const u64 c1 = 0x87c37b91114253d5LLU;
61 const u64 c2 = 0x4cf5ad432745937fLLU;
65 const u64 *blocks = (const u64 *)(data);
69 for (i = 0; i < nblocks; i++) {
70 u64 k1 = getblock64(blocks, i * 2 + 0);
71 u64 k2 = getblock64(blocks, i * 2 + 1);
80 h1 = h1 * 5 + 0x52dce729;
89 h2 = h2 * 5 + 0x38495ab5;
95 const u8 *tail = (const u8 *)(data + nblocks * 16);
102 k2 ^= ((u64)tail[14]) << 48;
105 k2 ^= ((u64)tail[13]) << 40;
108 k2 ^= ((u64)tail[12]) << 32;
111 k2 ^= ((u64)tail[11]) << 24;
114 k2 ^= ((u64)tail[10]) << 16;
117 k2 ^= ((u64)tail[9]) << 8;
120 k2 ^= ((u64)tail[8]) << 0;
128 k1 ^= ((u64)tail[7]) << 56;
131 k1 ^= ((u64)tail[6]) << 48;
134 k1 ^= ((u64)tail[5]) << 40;
137 k1 ^= ((u64)tail[4]) << 32;
140 k1 ^= ((u64)tail[3]) << 24;
143 k1 ^= ((u64)tail[2]) << 16;
146 k1 ^= ((u64)tail[1]) << 8;
149 k1 ^= ((u64)tail[0]) << 0;
173 putblock64((u64 *)out, 0, h1);
174 putblock64((u64 *)out, 1, h2);