Back to home page

OSCL-LXR

 
 

    


0001 #ifndef _LINUX_HASH_H
0002 #define _LINUX_HASH_H
0003 /* Fast hashing routine for ints,  longs and pointers.
0004    (C) 2002 Nadia Yvette Chambers, IBM */
0005 
0006 #include <asm/types.h>
0007 #include <linux/compiler.h>
0008 
0009 /*
0010  * The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and
0011  * fs/inode.c.  It's not actually prime any more (the previous primes
0012  * were actively bad for hashing), but the name remains.
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  * This hash multiplies the input by a large odd number and takes the
0026  * high bits.  Since multiplication propagates changes to the most
0027  * significant end only, it is essential that the high bits of the
0028  * product be used for the hash value.
0029  *
0030  * Chuck Lever verified the effectiveness of this technique:
0031  * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
0032  *
0033  * Although a random odd number will do, it turns out that the golden
0034  * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
0035  * properties.  (See Knuth vol 3, section 6.4, exercise 9.)
0036  *
0037  * These are the negative, (1 - phi) = phi**2 = (3 - sqrt(5))/2,
0038  * which is very slightly easier to multiply by and makes no
0039  * difference to the hash distribution.
0040  */
0041 #define GOLDEN_RATIO_32 0x61C88647
0042 #define GOLDEN_RATIO_64 0x61C8864680B583EBull
0043 
0044 #ifdef CONFIG_HAVE_ARCH_HASH
0045 /* This header may use the GOLDEN_RATIO_xx constants */
0046 #include <asm/hash.h>
0047 #endif
0048 
0049 /*
0050  * The _generic versions exist only so lib/test_hash.c can compare
0051  * the arch-optimized versions with the generic.
0052  *
0053  * Note that if you change these, any <asm/hash.h> that aren't updated
0054  * to match need to have their HAVE_ARCH_* define values updated so the
0055  * self-test will not false-positive.
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     /* High bits are more random, so use them. */
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     /* 64x64-bit multiply is efficient on all 64-bit processors */
0078     return val * GOLDEN_RATIO_64 >> (64 - bits);
0079 #else
0080     /* Hash 64 bits using only 32x32-bit multiply. */
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 /* This really should be called fold32_ptr; it does no hashing to speak of. */
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 /* _LINUX_HASH_H */