Line data Source code
1 : #ifndef HEADER_fd_src_util_fd_hash32_h 2 : #define HEADER_fd_src_util_fd_hash32_h 3 : 4 : #include "bits/fd_bits.h" 5 : 6 : #if FD_HAS_INT128 7 : 8 : static inline ulong 9 879748920 : fd_xxh3_mul128_fold64( ulong lhs, ulong rhs ) { 10 : #if defined(FD_USING_GCC) && defined(__BMI2__) 11 : ulong lo, hi; 12 : __asm__( "mulx %3,%0,%1" : "=r"(lo), "=r"(hi) : "d"(lhs), "rm"(rhs) ); 13 : return lo ^ hi; 14 : #else 15 879748920 : uint128 product = (uint128)lhs * (uint128)rhs; 16 879748920 : return (ulong)product ^ (ulong)( product>>64 ); 17 879748920 : #endif 18 879748920 : } 19 : 20 : static inline ulong 21 : fd_xxh3_mix16b( ulong i0, ulong i1, 22 : ulong s0, ulong s1, 23 879748914 : ulong seed ) { 24 879748914 : return fd_xxh3_mul128_fold64( i0 ^ (s0 + seed), i1 ^ (s1 - seed) ); 25 879748914 : } 26 : 27 : FD_FN_PURE static inline ulong 28 : fd_hash32( uchar const key[ 32 ], 29 439865390 : ulong seed ) { 30 439865390 : ulong k0 = FD_LOAD( ulong, key+ 0 ); 31 439865390 : ulong k1 = FD_LOAD( ulong, key+ 8 ); 32 439865390 : ulong k2 = FD_LOAD( ulong, key+16 ); 33 439865390 : ulong k3 = FD_LOAD( ulong, key+24 ); 34 439865390 : ulong acc = 32 * 0x9E3779B185EBCA87ULL; 35 439865390 : acc += fd_xxh3_mix16b( k0, k1, 0xbe4ba423396cfeb8UL, 0x1cad21f72c81017cUL, seed ); 36 439865390 : acc += fd_xxh3_mix16b( k2, k3, 0xdb979083e96dd4deUL, 0x1f67b3b7a4a44072UL, seed ); 37 439865390 : acc = acc ^ (acc >> 37); 38 439865390 : acc *= 0x165667919E3779F9ULL; 39 439865390 : acc = acc ^ (acc >> 32); 40 439865390 : return acc; 41 439865390 : } 42 : 43 : #else 44 : 45 : /* If the target does not support xxHash3, fallback to the 'old' key 46 : hash function. 47 : 48 : FIXME This version is vulnerable to HashDoS */ 49 : 50 : FD_FN_PURE static inline ulong 51 : fd_hash32( uchar const key[ 32 ], 52 : ulong seed ) { 53 : /* tons of ILP */ 54 : return (fd_ulong_hash( seed ^ (1UL<<0) ^ FD_LOAD( ulong, key+ 0 ) ) ^ 55 : fd_ulong_hash( seed ^ (1UL<<1) ^ FD_LOAD( ulong, key+ 8 ) ) ) ^ 56 : (fd_ulong_hash( seed ^ (1UL<<2) ^ FD_LOAD( ulong, key+16 ) ) ^ 57 : fd_ulong_hash( seed ^ (1UL<<3) ^ FD_LOAD( ulong, key+24 ) ) ); 58 : } 59 : 60 : #endif /* FD_HAS_INT128 */ 61 : 62 : #endif /* HEADER_fd_src_util_fd_hash32_h */