210647ae66a8c85732d6e874456758a17fb5b6bf
[dpdk.git] / lib / librte_hash / rte_jhash.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_JHASH_H
35 #define _RTE_JHASH_H
36
37 /**
38  * @file
39  *
40  * jhash functions.
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <stdint.h>
48 #include <string.h>
49 #include <rte_byteorder.h>
50
51 /* jhash.h: Jenkins hash support.
52  *
53  * Copyright (C) 2006 Bob Jenkins (bob_jenkins@burtleburtle.net)
54  *
55  * http://burtleburtle.net/bob/hash/
56  *
57  * These are the credits from Bob's sources:
58  *
59  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
60  *
61  * These are functions for producing 32-bit hashes for hash table lookup.
62  * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
63  * are externally useful functions.  Routines to test the hash are included
64  * if SELF_TEST is defined.  You can use this free for any purpose.  It's in
65  * the public domain.  It has no warranty.
66  *
67  * $FreeBSD$
68  */
69
70 #define rot(x, k) (((x) << (k)) | ((x) >> (32-(k))))
71
72 /** @internal Internal function. NOTE: Arguments are modified. */
73 #define __rte_jhash_mix(a, b, c) do { \
74         a -= c; a ^= rot(c, 4); c += b; \
75         b -= a; b ^= rot(a, 6); a += c; \
76         c -= b; c ^= rot(b, 8); b += a; \
77         a -= c; a ^= rot(c, 16); c += b; \
78         b -= a; b ^= rot(a, 19); a += c; \
79         c -= b; c ^= rot(b, 4); b += a; \
80 } while (0)
81
82 #define __rte_jhash_final(a, b, c) do { \
83         c ^= b; c -= rot(b, 14); \
84         a ^= c; a -= rot(c, 11); \
85         b ^= a; b -= rot(a, 25); \
86         c ^= b; c -= rot(b, 16); \
87         a ^= c; a -= rot(c, 4);  \
88         b ^= a; b -= rot(a, 14); \
89         c ^= b; c -= rot(b, 24); \
90 } while (0)
91
92 /** The golden ratio: an arbitrary value. */
93 #define RTE_JHASH_GOLDEN_RATIO      0xdeadbeef
94
95 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
96 #define BIT_SHIFT(x, y, k) (((x) >> (k)) | ((uint64_t)(y) << (32-(k))))
97 #else
98 #define BIT_SHIFT(x, y, k) (((uint64_t)(x) << (k)) | ((y) >> (32-(k))))
99 #endif
100
101 #define LOWER8b_MASK rte_le_to_cpu_32(0xff)
102 #define LOWER16b_MASK rte_le_to_cpu_32(0xffff)
103 #define LOWER24b_MASK rte_le_to_cpu_32(0xffffff)
104
105 static inline void
106 __rte_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc,
107                 uint32_t *pb, unsigned check_align)
108 {
109         uint32_t a, b, c;
110
111         /* Set up the internal state */
112         a = b = c = RTE_JHASH_GOLDEN_RATIO + ((uint32_t)length) + *pc;
113         c += *pb;
114
115         /*
116          * Check key alignment. For x86 architecture, first case is always optimal
117          * If check_align is not set, first case will be used
118          */
119 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686) || defined(RTE_ARCH_X86_X32)
120         const uint32_t *k = key;
121         const uint32_t s = 0;
122 #else
123         const uint32_t *k = (uint32_t *)(uintptr_t)key & (uintptr_t)~3);
124         const uint32_t s = ((uintptr_t)key & 3) * CHAR_BIT;
125 #endif
126         if (!check_align || s == 0) {
127                 while (length > 12) {
128                         a += k[0];
129                         b += k[1];
130                         c += k[2];
131
132                         __rte_jhash_mix(a, b, c);
133
134                         k += 3;
135                         length -= 12;
136                 }
137
138                 switch (length) {
139                 case 12:
140                         c += k[2]; b += k[1]; a += k[0]; break;
141                 case 11:
142                         c += k[2] & LOWER24b_MASK; b += k[1]; a += k[0]; break;
143                 case 10:
144                         c += k[2] & LOWER16b_MASK; b += k[1]; a += k[0]; break;
145                 case 9:
146                         c += k[2] & LOWER8b_MASK; b += k[1]; a += k[0]; break;
147                 case 8:
148                         b += k[1]; a += k[0]; break;
149                 case 7:
150                         b += k[1] & LOWER24b_MASK; a += k[0]; break;
151                 case 6:
152                         b += k[1] & LOWER16b_MASK; a += k[0]; break;
153                 case 5:
154                         b += k[1] & LOWER8b_MASK; a += k[0]; break;
155                 case 4:
156                         a += k[0]; break;
157                 case 3:
158                         a += k[0] & LOWER24b_MASK; break;
159                 case 2:
160                         a += k[0] & LOWER16b_MASK; break;
161                 case 1:
162                         a += k[0] & LOWER8b_MASK; break;
163                 /* zero length strings require no mixing */
164                 case 0:
165                         *pc = c;
166                         *pb = b;
167                         return;
168                 };
169         } else {
170                 /* all but the last block: affect some 32 bits of (a, b, c) */
171                 while (length > 12) {
172                         a += BIT_SHIFT(k[0], k[1], s);
173                         b += BIT_SHIFT(k[1], k[2], s);
174                         c += BIT_SHIFT(k[2], k[3], s);
175                         __rte_jhash_mix(a, b, c);
176
177                         k += 3;
178                         length -= 12;
179                 }
180
181                 /* last block: affect all 32 bits of (c) */
182                 switch (length) {
183                 case 12:
184                         a += BIT_SHIFT(k[0], k[1], s);
185                         b += BIT_SHIFT(k[1], k[2], s);
186                         c += BIT_SHIFT(k[2], k[3], s);
187                         break;
188                 case 11:
189                         a += BIT_SHIFT(k[0], k[1], s);
190                         b += BIT_SHIFT(k[1], k[2], s);
191                         c += BIT_SHIFT(k[2], k[3], s) & LOWER24b_MASK;
192                         break;
193                 case 10:
194                         a += BIT_SHIFT(k[0], k[1], s);
195                         b += BIT_SHIFT(k[1], k[2], s);
196                         c += BIT_SHIFT(k[2], k[3], s) & LOWER16b_MASK;
197                         break;
198                 case 9:
199                         a += BIT_SHIFT(k[0], k[1], s);
200                         b += BIT_SHIFT(k[1], k[2], s);
201                         c += BIT_SHIFT(k[2], k[3], s) & LOWER8b_MASK;
202                         break;
203                 case 8:
204                         a += BIT_SHIFT(k[0], k[1], s);
205                         b += BIT_SHIFT(k[1], k[2], s);
206                         break;
207                 case 7:
208                         a += BIT_SHIFT(k[0], k[1], s);
209                         b += BIT_SHIFT(k[1], k[2], s) & LOWER24b_MASK;
210                         break;
211                 case 6:
212                         a += BIT_SHIFT(k[0], k[1], s);
213                         b += BIT_SHIFT(k[1], k[2], s) & LOWER16b_MASK;
214                         break;
215                 case 5:
216                         a += BIT_SHIFT(k[0], k[1], s);
217                         b += BIT_SHIFT(k[1], k[2], s) & LOWER8b_MASK;
218                         break;
219                 case 4:
220                         a += BIT_SHIFT(k[0], k[1], s);
221                         break;
222                 case 3:
223                         a += BIT_SHIFT(k[0], k[1], s) & LOWER24b_MASK;
224                         break;
225                 case 2:
226                         a += BIT_SHIFT(k[0], k[1], s) & LOWER16b_MASK;
227                         break;
228                 case 1:
229                         a += BIT_SHIFT(k[0], k[1], s) & LOWER8b_MASK;
230                         break;
231                 /* zero length strings require no mixing */
232                 case 0:
233                         *pc = c;
234                         *pb = b;
235                         return;
236                 }
237         }
238
239         __rte_jhash_final(a, b, c);
240
241         *pc = c;
242         *pb = b;
243 }
244
245 /**
246  * Same as rte_jhash, but takes two seeds and return two uint32_ts.
247  * pc and pb must be non-null, and *pc and *pb must both be initialized
248  * with seeds. If you pass in (*pb)=0, the output (*pc) will be
249  * the same as the return value from rte_jhash.
250  *
251  * @param key
252  *   Key to calculate hash of.
253  * @param length
254  *   Length of key in bytes.
255  * @param pc
256  *   IN: seed OUT: primary hash value.
257  * @param pb
258  *   IN: second seed OUT: secondary hash value.
259  */
260 static inline void
261 rte_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc, uint32_t *pb)
262 {
263         __rte_jhash_2hashes(key, length, pc, pb, 1);
264 }
265
266 /**
267  * Same as rte_jhash2, but takes two seeds and return two uint32_ts.
268  * pc and pb must be non-null, and *pc and *pb must both be initialized
269  * with seeds. If you pass in (*pb)=0, the output (*pc) will be
270  * the same as the return value from rte_jhash2.
271  *
272  * @param k
273  *   Key to calculate hash of.
274  * @param length
275  *   Length of key in units of 4 bytes.
276  * @param pc
277  *   IN: seed OUT: primary hash value.
278  * @param pb
279  *   IN: second seed OUT: secondary hash value.
280  */
281 static inline void
282 rte_jhash2_2hashes(const uint32_t *k, uint32_t length, uint32_t *pc, uint32_t *pb)
283 {
284         __rte_jhash_2hashes((const void *) k, (length << 2), pc, pb, 0);
285 }
286
287 /**
288  * The most generic version, hashes an arbitrary sequence
289  * of bytes.  No alignment or length assumptions are made about
290  * the input key.
291  *
292  * @param key
293  *   Key to calculate hash of.
294  * @param length
295  *   Length of key in bytes.
296  * @param initval
297  *   Initialising value of hash.
298  * @return
299  *   Calculated hash value.
300  */
301 static inline uint32_t
302 rte_jhash(const void *key, uint32_t length, uint32_t initval)
303 {
304         uint32_t initval2 = 0;
305
306         rte_jhash_2hashes(key, length, &initval, &initval2);
307
308         return initval;
309 }
310
311 /**
312  * A special optimized version that handles 1 or more of uint32_ts.
313  * The length parameter here is the number of uint32_ts in the key.
314  *
315  * @param k
316  *   Key to calculate hash of.
317  * @param length
318  *   Length of key in units of 4 bytes.
319  * @param initval
320  *   Initialising value of hash.
321  * @return
322  *   Calculated hash value.
323  */
324 static inline uint32_t
325 rte_jhash2(const uint32_t *k, uint32_t length, uint32_t initval)
326 {
327         uint32_t initval2 = 0;
328
329         rte_jhash2_2hashes(k, length, &initval, &initval2);
330
331         return initval;
332 }
333
334 static inline uint32_t
335 __rte_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
336 {
337         a += RTE_JHASH_GOLDEN_RATIO + initval;
338         b += RTE_JHASH_GOLDEN_RATIO + initval;
339         c += RTE_JHASH_GOLDEN_RATIO + initval;
340
341         __rte_jhash_final(a, b, c);
342
343         return c;
344 }
345
346 /**
347  * A special ultra-optimized versions that knows it is hashing exactly
348  * 3 words.
349  *
350  * @param a
351  *   First word to calcuate hash of.
352  * @param b
353  *   Second word to calcuate hash of.
354  * @param c
355  *   Third word to calcuate hash of.
356  * @param initval
357  *   Initialising value of hash.
358  * @return
359  *   Calculated hash value.
360  */
361 static inline uint32_t
362 rte_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
363 {
364         return __rte_jhash_3words(a + 12, b + 12, c + 12, initval);
365 }
366
367 /**
368  * A special ultra-optimized versions that knows it is hashing exactly
369  * 2 words.
370  *
371  * @param a
372  *   First word to calcuate hash of.
373  * @param b
374  *   Second word to calcuate hash of.
375  * @param initval
376  *   Initialising value of hash.
377  * @return
378  *   Calculated hash value.
379  */
380 static inline uint32_t
381 rte_jhash_2words(uint32_t a, uint32_t b, uint32_t initval)
382 {
383         return __rte_jhash_3words(a + 8, b + 8, 8, initval);
384 }
385
386 /**
387  * A special ultra-optimized versions that knows it is hashing exactly
388  * 1 word.
389  *
390  * @param a
391  *   Word to calcuate hash of.
392  * @param initval
393  *   Initialising value of hash.
394  * @return
395  *   Calculated hash value.
396  */
397 static inline uint32_t
398 rte_jhash_1word(uint32_t a, uint32_t initval)
399 {
400         return __rte_jhash_3words(a + 4, 4, 4, initval);
401 }
402
403 #ifdef __cplusplus
404 }
405 #endif
406
407 #endif /* _RTE_JHASH_H */