Line data Source code
1 : #ifndef _LINUX_JHASH_H
2 : #define _LINUX_JHASH_H
3 :
4 : /* jhash.h: Jenkins hash support.
5 : *
6 : * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net)
7 : *
8 : * https://burtleburtle.net/bob/hash/
9 : *
10 : * These are the credits from Bob's sources:
11 : *
12 : * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
13 : *
14 : * These are functions for producing 32-bit hashes for hash table lookup.
15 : * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
16 : * are externally useful functions. Routines to test the hash are included
17 : * if SELF_TEST is defined. You can use this free for any purpose. It's in
18 : * the public domain. It has no warranty.
19 : *
20 : * Copyright (C) 2009-2010 Jozsef Kadlecsik (kadlec@netfilter.org)
21 : *
22 : * I've modified Bob's hash to be useful in the Linux kernel, and
23 : * any bugs present are my fault.
24 : * Jozsef
25 : */
26 : #include <linux/bitops.h>
27 : #include <linux/unaligned/packed_struct.h>
28 :
29 : /* Best hash sizes are of power of two */
30 : #define jhash_size(n) ((u32)1<<(n))
31 : /* Mask the hash value, i.e (value & jhash_mask(n)) instead of (value % n) */
32 : #define jhash_mask(n) (jhash_size(n)-1)
33 :
34 : /* __jhash_mix -- mix 3 32-bit values reversibly. */
35 : #define __jhash_mix(a, b, c) \
36 : { \
37 : a -= c; a ^= rol32(c, 4); c += b; \
38 : b -= a; b ^= rol32(a, 6); a += c; \
39 : c -= b; c ^= rol32(b, 8); b += a; \
40 : a -= c; a ^= rol32(c, 16); c += b; \
41 : b -= a; b ^= rol32(a, 19); a += c; \
42 : c -= b; c ^= rol32(b, 4); b += a; \
43 : }
44 :
45 : /* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
46 : #define __jhash_final(a, b, c) \
47 : { \
48 : c ^= b; c -= rol32(b, 14); \
49 : a ^= c; a -= rol32(c, 11); \
50 : b ^= a; b -= rol32(a, 25); \
51 : c ^= b; c -= rol32(b, 16); \
52 : a ^= c; a -= rol32(c, 4); \
53 : b ^= a; b -= rol32(a, 14); \
54 : c ^= b; c -= rol32(b, 24); \
55 : }
56 :
57 : /* An arbitrary initial parameter */
58 : #define JHASH_INITVAL 0xdeadbeef
59 :
60 : /* jhash - hash an arbitrary key
61 : * @k: sequence of bytes as key
62 : * @length: the length of the key
63 : * @initval: the previous hash, or an arbitray value
64 : *
65 : * The generic version, hashes an arbitrary sequence of bytes.
66 : * No alignment or length assumptions are made about the input key.
67 : *
68 : * Returns the hash value of the key. The result depends on endianness.
69 : */
70 5300 : static inline u32 jhash(const void *key, u32 length, u32 initval)
71 : {
72 5300 : u32 a, b, c;
73 5300 : const u8 *k = key;
74 :
75 : /* Set up the internal state */
76 5300 : a = b = c = JHASH_INITVAL + length + initval;
77 :
78 : /* All but the last block: affect some 32 bits of (a,b,c) */
79 51375 : while (length > 12) {
80 46075 : a += __get_unaligned_cpu32(k);
81 46075 : b += __get_unaligned_cpu32(k + 4);
82 46075 : c += __get_unaligned_cpu32(k + 8);
83 46075 : __jhash_mix(a, b, c);
84 46075 : length -= 12;
85 46075 : k += 12;
86 : }
87 : /* Last block: affect all 32 bits of (c) */
88 5300 : switch (length) {
89 1804 : case 12: c += (u32)k[11]<<24; fallthrough;
90 1817 : case 11: c += (u32)k[10]<<16; fallthrough;
91 1823 : case 10: c += (u32)k[9]<<8; fallthrough;
92 1836 : case 9: c += k[8]; fallthrough;
93 3499 : case 8: b += (u32)k[7]<<24; fallthrough;
94 3510 : case 7: b += (u32)k[6]<<16; fallthrough;
95 3521 : case 6: b += (u32)k[5]<<8; fallthrough;
96 3531 : case 5: b += k[4]; fallthrough;
97 5237 : case 4: a += (u32)k[3]<<24; fallthrough;
98 5244 : case 3: a += (u32)k[2]<<16; fallthrough;
99 5258 : case 2: a += (u32)k[1]<<8; fallthrough;
100 5271 : case 1: a += k[0];
101 5271 : __jhash_final(a, b, c);
102 5271 : break;
103 : case 0: /* Nothing left to add */
104 : break;
105 : }
106 :
107 5300 : return c;
108 : }
109 :
110 : /* jhash2 - hash an array of u32's
111 : * @k: the key which must be an array of u32's
112 : * @length: the number of u32's in the key
113 : * @initval: the previous hash, or an arbitray value
114 : *
115 : * Returns the hash value of the key.
116 : */
117 5336653 : static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
118 : {
119 5336653 : u32 a, b, c;
120 :
121 : /* Set up the internal state */
122 5336653 : a = b = c = JHASH_INITVAL + (length<<2) + initval;
123 :
124 : /* Handle most of the key */
125 56837415 : while (length > 3) {
126 51500762 : a += k[0];
127 51500762 : b += k[1];
128 51500762 : c += k[2];
129 51500762 : __jhash_mix(a, b, c);
130 51500762 : length -= 3;
131 51500762 : k += 3;
132 : }
133 :
134 : /* Handle the last 3 u32's */
135 5336653 : switch (length) {
136 1646330 : case 3: c += k[2]; fallthrough;
137 3165408 : case 2: b += k[1]; fallthrough;
138 5336653 : case 1: a += k[0];
139 5336653 : __jhash_final(a, b, c);
140 5336653 : break;
141 : case 0: /* Nothing left to add */
142 : break;
143 : }
144 :
145 5336653 : return c;
146 : }
147 :
148 :
149 : /* __jhash_nwords - hash exactly 3, 2 or 1 word(s) */
150 564 : static inline u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
151 : {
152 564 : a += initval;
153 564 : b += initval;
154 564 : c += initval;
155 :
156 564 : __jhash_final(a, b, c);
157 :
158 564 : return c;
159 : }
160 :
161 450 : static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
162 : {
163 450 : return __jhash_nwords(a, b, c, initval + JHASH_INITVAL + (3 << 2));
164 : }
165 :
166 : static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
167 : {
168 : return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
169 : }
170 :
171 114 : static inline u32 jhash_1word(u32 a, u32 initval)
172 : {
173 114 : return __jhash_nwords(a, 0, 0, initval + JHASH_INITVAL + (1 << 2));
174 : }
175 :
176 : #endif /* _LINUX_JHASH_H */
|