xref: /openbsd-src/sys/dev/pci/drm/include/linux/hash.h (revision 7f4dd37977dc50fdbac8c09deb3ed9ed9b8d0c87)
1 /* Public domain. */
2 
3 #ifndef _LINUX_HASH_H
4 #define _LINUX_HASH_H
5 
6 #include <sys/types.h>
7 
8 /* 2^32 * ((sqrt(5) - 1) / 2) from Knuth */
9 #define GOLDEN_RATIO_32 0x9e3779b9
10 
11 static inline uint32_t
hash_32(uint32_t val,unsigned int bits)12 hash_32(uint32_t val, unsigned int bits)
13 {
14 	return (val * GOLDEN_RATIO_32) >> (32 - bits);
15 }
16 
17 /* 2^64 * ((sqrt(5) - 1) / 2) from Knuth */
18 #define GOLDEN_RATIO_64 0x9e3779b97f4a7c16ULL
19 
20 static inline uint32_t
hash_64(uint64_t val,unsigned int bits)21 hash_64(uint64_t val, unsigned int bits)
22 {
23 	return (val * GOLDEN_RATIO_64) >> (64 - bits);
24 }
25 
26 #ifdef __LP64__
27 #define hash_long(val, bits) hash_64(val, bits)
28 #else
29 #define hash_long(val, bits) hash_32(val, bits)
30 #endif
31 
32 #endif
33