]> git.proxmox.com Git - mirror_frr.git/blob - lib/jhash.c
*: fix GCC 7 switch/case fallthrough warnings
[mirror_frr.git] / lib / jhash.c
1 /* jhash.h: Jenkins hash support.
2 *
3 * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
4 *
5 * http://burtleburtle.net/bob/hash/
6 *
7 * These are the credits from Bob's sources:
8 *
9 * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
10 * hash(), hash2(), hash3, and mix() are externally useful functions.
11 * Routines to test the hash are included if SELF_TEST is defined.
12 * You can use this free for any purpose. It has no warranty.
13 *
14 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
15 *
16 * I've modified Bob's hash to be useful in the Linux kernel, and
17 * any bugs present are surely my fault. -DaveM
18 */
19
20 #include "zebra.h"
21 #include "jhash.h"
22
23 /* The golden ration: an arbitrary value */
24 #define JHASH_GOLDEN_RATIO 0x9e3779b9
25
26 /* NOTE: Arguments are modified. */
27 #define __jhash_mix(a, b, c) \
28 { \
29 a -= b; a -= c; a ^= (c>>13); \
30 b -= c; b -= a; b ^= (a<<8); \
31 c -= a; c -= b; c ^= (b>>13); \
32 a -= b; a -= c; a ^= (c>>12); \
33 b -= c; b -= a; b ^= (a<<16); \
34 c -= a; c -= b; c ^= (b>>5); \
35 a -= b; a -= c; a ^= (c>>3); \
36 b -= c; b -= a; b ^= (a<<10); \
37 c -= a; c -= b; c ^= (b>>15); \
38 }
39
40 /* The most generic version, hashes an arbitrary sequence
41 * of bytes. No alignment or length assumptions are made about
42 * the input key.
43 */
44 u_int32_t
45 jhash (const void *key, u_int32_t length, u_int32_t initval)
46 {
47 u_int32_t a, b, c, len;
48 const u_int8_t *k = key;
49
50 len = length;
51 a = b = JHASH_GOLDEN_RATIO;
52 c = initval;
53
54 while (len >= 12)
55 {
56 a +=
57 (k[0] + ((u_int32_t) k[1] << 8) + ((u_int32_t) k[2] << 16) +
58 ((u_int32_t) k[3] << 24));
59 b +=
60 (k[4] + ((u_int32_t) k[5] << 8) + ((u_int32_t) k[6] << 16) +
61 ((u_int32_t) k[7] << 24));
62 c +=
63 (k[8] + ((u_int32_t) k[9] << 8) + ((u_int32_t) k[10] << 16) +
64 ((u_int32_t) k[11] << 24));
65
66 __jhash_mix (a, b, c);
67
68 k += 12;
69 len -= 12;
70 }
71
72 c += length;
73 switch (len)
74 {
75 case 11:
76 c += ((u_int32_t) k[10] << 24);
77 /* fallthru */
78 case 10:
79 c += ((u_int32_t) k[9] << 16);
80 /* fallthru */
81 case 9:
82 c += ((u_int32_t) k[8] << 8);
83 /* fallthru */
84 case 8:
85 b += ((u_int32_t) k[7] << 24);
86 /* fallthru */
87 case 7:
88 b += ((u_int32_t) k[6] << 16);
89 /* fallthru */
90 case 6:
91 b += ((u_int32_t) k[5] << 8);
92 /* fallthru */
93 case 5:
94 b += k[4];
95 /* fallthru */
96 case 4:
97 a += ((u_int32_t) k[3] << 24);
98 /* fallthru */
99 case 3:
100 a += ((u_int32_t) k[2] << 16);
101 /* fallthru */
102 case 2:
103 a += ((u_int32_t) k[1] << 8);
104 /* fallthru */
105 case 1:
106 a += k[0];
107 };
108
109 __jhash_mix (a, b, c);
110
111 return c;
112 }
113
114 /* A special optimized version that handles 1 or more of u_int32_ts.
115 * The length parameter here is the number of u_int32_ts in the key.
116 */
117 u_int32_t
118 jhash2 (const u_int32_t *k, u_int32_t length, u_int32_t initval)
119 {
120 u_int32_t a, b, c, len;
121
122 a = b = JHASH_GOLDEN_RATIO;
123 c = initval;
124 len = length;
125
126 while (len >= 3)
127 {
128 a += k[0];
129 b += k[1];
130 c += k[2];
131 __jhash_mix (a, b, c);
132 k += 3;
133 len -= 3;
134 }
135
136 c += length * 4;
137
138 switch (len)
139 {
140 case 2:
141 b += k[1];
142 /* fallthru */
143 case 1:
144 a += k[0];
145 };
146
147 __jhash_mix (a, b, c);
148
149 return c;
150 }
151
152
153 /* A special ultra-optimized versions that knows they are hashing exactly
154 * 3, 2 or 1 word(s).
155 *
156 * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
157 * done at the end is not done here.
158 */
159 u_int32_t
160 jhash_3words (u_int32_t a, u_int32_t b, u_int32_t c, u_int32_t initval)
161 {
162 a += JHASH_GOLDEN_RATIO;
163 b += JHASH_GOLDEN_RATIO;
164 c += initval;
165
166 __jhash_mix (a, b, c);
167
168 return c;
169 }
170
171 u_int32_t
172 jhash_2words (u_int32_t a, u_int32_t b, u_int32_t initval)
173 {
174 return jhash_3words (a, b, 0, initval);
175 }
176
177 u_int32_t
178 jhash_1word (u_int32_t a, u_int32_t initval)
179 {
180 return jhash_3words (a, 0, 0, initval);
181 }