hash_base.h (1370B)
1 /* Copyright 2025 Google Inc. All Rights Reserved. 2 3 Distributed under MIT license. 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5 */ 6 7 /* Basic common hash functions / constants. */ 8 9 #ifndef THIRD_PARTY_BROTLI_ENC_HASH_BASE_H_ 10 #define THIRD_PARTY_BROTLI_ENC_HASH_BASE_H_ 11 12 #include "../common/platform.h" 13 14 /* kHashMul32 multiplier has these properties: 15 * The multiplier must be odd. Otherwise we may lose the highest bit. 16 * No long streaks of ones or zeros. 17 * There is no effort to ensure that it is a prime, the oddity is enough 18 for this use. 19 * The number has been tuned heuristically against compression benchmarks. */ 20 static const uint32_t kHashMul32 = 0x1E35A7BD; 21 static const uint64_t kHashMul64 = 22 BROTLI_MAKE_UINT64_T(0x1FE35A7Bu, 0xD3579BD3u); 23 24 static BROTLI_INLINE uint32_t Hash14(const uint8_t* data) { 25 uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; 26 /* The higher bits contain more mixture from the multiplication, 27 so we take our results from there. */ 28 return h >> (32 - 14); 29 } 30 31 static BROTLI_INLINE uint32_t Hash15(const uint8_t* data) { 32 uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32; 33 /* The higher bits contain more mixture from the multiplication, 34 so we take our results from there. */ 35 return h >> (32 - 15); 36 } 37 38 #endif // THIRD_PARTY_BROTLI_ENC_HASH_BASE_H_