This source file includes following definitions.
- jhash
- jhash2
- __jhash_nwords
- jhash_3words
- jhash_2words
- jhash_1word
1 #ifndef _LINUX_JHASH_H
2 #define _LINUX_JHASH_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #include <linux/bitops.h>
27 #include <linux/unaligned/packed_struct.h>
28
29
30 #define jhash_size(n) ((u32)1<<(n))
31
32 #define jhash_mask(n) (jhash_size(n)-1)
33
34
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
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
58 #define JHASH_INITVAL 0xdeadbeef
59
60
61
62
63
64
65
66
67
68
69
70 static inline u32 jhash(const void *key, u32 length, u32 initval)
71 {
72 u32 a, b, c;
73 const u8 *k = key;
74
75
76 a = b = c = JHASH_INITVAL + length + initval;
77
78
79 while (length > 12) {
80 a += __get_unaligned_cpu32(k);
81 b += __get_unaligned_cpu32(k + 4);
82 c += __get_unaligned_cpu32(k + 8);
83 __jhash_mix(a, b, c);
84 length -= 12;
85 k += 12;
86 }
87
88 switch (length) {
89 case 12: c += (u32)k[11]<<24;
90 case 11: c += (u32)k[10]<<16;
91 case 10: c += (u32)k[9]<<8;
92 case 9: c += k[8];
93 case 8: b += (u32)k[7]<<24;
94 case 7: b += (u32)k[6]<<16;
95 case 6: b += (u32)k[5]<<8;
96 case 5: b += k[4];
97 case 4: a += (u32)k[3]<<24;
98 case 3: a += (u32)k[2]<<16;
99 case 2: a += (u32)k[1]<<8;
100 case 1: a += k[0];
101 __jhash_final(a, b, c);
102 case 0:
103 break;
104 }
105
106 return c;
107 }
108
109
110
111
112
113
114
115
116 static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
117 {
118 u32 a, b, c;
119
120
121 a = b = c = JHASH_INITVAL + (length<<2) + initval;
122
123
124 while (length > 3) {
125 a += k[0];
126 b += k[1];
127 c += k[2];
128 __jhash_mix(a, b, c);
129 length -= 3;
130 k += 3;
131 }
132
133
134 switch (length) {
135 case 3: c += k[2];
136 case 2: b += k[1];
137 case 1: a += k[0];
138 __jhash_final(a, b, c);
139 case 0:
140 break;
141 }
142
143 return c;
144 }
145
146
147
148 static inline u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
149 {
150 a += initval;
151 b += initval;
152 c += initval;
153
154 __jhash_final(a, b, c);
155
156 return c;
157 }
158
159 static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
160 {
161 return __jhash_nwords(a, b, c, initval + JHASH_INITVAL + (3 << 2));
162 }
163
164 static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
165 {
166 return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
167 }
168
169 static inline u32 jhash_1word(u32 a, u32 initval)
170 {
171 return __jhash_nwords(a, 0, 0, initval + JHASH_INITVAL + (1 << 2));
172 }
173
174 #endif