0001 #ifndef _LINUX_HASH_H
0002 #define _LINUX_HASH_H
0003
0004
0005
0006 #include <asm/types.h>
0007 #include <linux/compiler.h>
0008
0009
0010
0011
0012
0013
0014 #if BITS_PER_LONG == 32
0015 #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
0016 #define hash_long(val, bits) hash_32(val, bits)
0017 #elif BITS_PER_LONG == 64
0018 #define hash_long(val, bits) hash_64(val, bits)
0019 #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
0020 #else
0021 #error Wordsize not 32 or 64
0022 #endif
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 #define GOLDEN_RATIO_32 0x61C88647
0042 #define GOLDEN_RATIO_64 0x61C8864680B583EBull
0043
0044 #ifdef CONFIG_HAVE_ARCH_HASH
0045
0046 #include <asm/hash.h>
0047 #endif
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 #ifndef HAVE_ARCH__HASH_32
0058 #define __hash_32 __hash_32_generic
0059 #endif
0060 static inline u32 __hash_32_generic(u32 val)
0061 {
0062 return val * GOLDEN_RATIO_32;
0063 }
0064
0065 static inline u32 hash_32(u32 val, unsigned int bits)
0066 {
0067
0068 return __hash_32(val) >> (32 - bits);
0069 }
0070
0071 #ifndef HAVE_ARCH_HASH_64
0072 #define hash_64 hash_64_generic
0073 #endif
0074 static __always_inline u32 hash_64_generic(u64 val, unsigned int bits)
0075 {
0076 #if BITS_PER_LONG == 64
0077
0078 return val * GOLDEN_RATIO_64 >> (64 - bits);
0079 #else
0080
0081 return hash_32((u32)val ^ __hash_32(val >> 32), bits);
0082 #endif
0083 }
0084
0085 static inline u32 hash_ptr(const void *ptr, unsigned int bits)
0086 {
0087 return hash_long((unsigned long)ptr, bits);
0088 }
0089
0090
0091 static inline u32 hash32_ptr(const void *ptr)
0092 {
0093 unsigned long val = (unsigned long)ptr;
0094
0095 #if BITS_PER_LONG == 64
0096 val ^= (val >> 32);
0097 #endif
0098 return (u32)val;
0099 }
0100
0101 #endif