1 /* 2 * Copyright (c) 2022, Meta 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #include <stddef.h> 8 #include <stdint.h> 9 murmur_32_scramble(uint32_t k)10static inline uint32_t murmur_32_scramble(uint32_t k) 11 { 12 k *= 0xcc9e2d51; 13 k = (k << 15) | (k >> 17); 14 k *= 0x1b873593; 15 16 return k; 17 } 18 sys_hash32_murmur3(const char * str,size_t n)19uint32_t sys_hash32_murmur3(const char *str, size_t n) 20 { 21 uint32_t k; 22 /* seed of 0 */ 23 uint32_t h = 0; 24 const size_t len = n; 25 26 for (; n >= sizeof(uint32_t); n -= sizeof(uint32_t), str += sizeof(uint32_t)) { 27 k = *(const uint32_t *)str; 28 h ^= murmur_32_scramble(k); 29 h = (h << 13) | (h >> 19); 30 h = h * 5 + 0xe6546b64; 31 } 32 33 for (k = 0; n != 0; --n, ++str) { 34 k <<= 8; 35 k |= *str; 36 } 37 38 h ^= murmur_32_scramble(k); 39 40 h ^= len; 41 h ^= h >> 16; 42 h *= 0x85ebca6b; 43 h ^= h >> 13; 44 h *= 0xc2b2ae35; 45 h ^= h >> 16; 46 47 return h; 48 } 49