4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
51 #include <rte_config.h>
53 #include <rte_byteorder.h>
55 /* jhash.h: Jenkins hash support.
57 * Copyright (C) 2006 Bob Jenkins (bob_jenkins@burtleburtle.net)
59 * http://burtleburtle.net/bob/hash/
61 * These are the credits from Bob's sources:
63 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
65 * These are functions for producing 32-bit hashes for hash table lookup.
66 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
67 * are externally useful functions. Routines to test the hash are included
68 * if SELF_TEST is defined. You can use this free for any purpose. It's in
69 * the public domain. It has no warranty.
74 #define rot(x, k) (((x) << (k)) | ((x) >> (32-(k))))
76 /** @internal Internal function. NOTE: Arguments are modified. */
77 #define __rte_jhash_mix(a, b, c) do { \
78 a -= c; a ^= rot(c, 4); c += b; \
79 b -= a; b ^= rot(a, 6); a += c; \
80 c -= b; c ^= rot(b, 8); b += a; \
81 a -= c; a ^= rot(c, 16); c += b; \
82 b -= a; b ^= rot(a, 19); a += c; \
83 c -= b; c ^= rot(b, 4); b += a; \
86 #define __rte_jhash_final(a, b, c) do { \
87 c ^= b; c -= rot(b, 14); \
88 a ^= c; a -= rot(c, 11); \
89 b ^= a; b -= rot(a, 25); \
90 c ^= b; c -= rot(b, 16); \
91 a ^= c; a -= rot(c, 4); \
92 b ^= a; b -= rot(a, 14); \
93 c ^= b; c -= rot(b, 24); \
96 /** The golden ratio: an arbitrary value. */
97 #define RTE_JHASH_GOLDEN_RATIO 0xdeadbeef
99 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
100 #define BIT_SHIFT(x, y, k) (((x) >> (k)) | ((uint64_t)(y) << (32-(k))))
102 #define BIT_SHIFT(x, y, k) (((uint64_t)(x) << (k)) | ((y) >> (32-(k))))
105 #define LOWER8b_MASK rte_le_to_cpu_32(0xff)
106 #define LOWER16b_MASK rte_le_to_cpu_32(0xffff)
107 #define LOWER24b_MASK rte_le_to_cpu_32(0xffffff)
110 __rte_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc,
111 uint32_t *pb, unsigned check_align)
115 /* Set up the internal state */
116 a = b = c = RTE_JHASH_GOLDEN_RATIO + ((uint32_t)length) + *pc;
120 * Check key alignment. For x86 architecture, first case is always optimal
121 * If check_align is not set, first case will be used
123 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686) || defined(RTE_ARCH_X86_X32)
124 const uint32_t *k = (const uint32_t *)key;
125 const uint32_t s = 0;
127 const uint32_t *k = (uint32_t *)((uintptr_t)key & (uintptr_t)~3);
128 const uint32_t s = ((uintptr_t)key & 3) * CHAR_BIT;
130 if (!check_align || s == 0) {
131 while (length > 12) {
136 __rte_jhash_mix(a, b, c);
144 c += k[2]; b += k[1]; a += k[0]; break;
146 c += k[2] & LOWER24b_MASK; b += k[1]; a += k[0]; break;
148 c += k[2] & LOWER16b_MASK; b += k[1]; a += k[0]; break;
150 c += k[2] & LOWER8b_MASK; b += k[1]; a += k[0]; break;
152 b += k[1]; a += k[0]; break;
154 b += k[1] & LOWER24b_MASK; a += k[0]; break;
156 b += k[1] & LOWER16b_MASK; a += k[0]; break;
158 b += k[1] & LOWER8b_MASK; a += k[0]; break;
162 a += k[0] & LOWER24b_MASK; break;
164 a += k[0] & LOWER16b_MASK; break;
166 a += k[0] & LOWER8b_MASK; break;
167 /* zero length strings require no mixing */
174 /* all but the last block: affect some 32 bits of (a, b, c) */
175 while (length > 12) {
176 a += BIT_SHIFT(k[0], k[1], s);
177 b += BIT_SHIFT(k[1], k[2], s);
178 c += BIT_SHIFT(k[2], k[3], s);
179 __rte_jhash_mix(a, b, c);
185 /* last block: affect all 32 bits of (c) */
188 a += BIT_SHIFT(k[0], k[1], s);
189 b += BIT_SHIFT(k[1], k[2], s);
190 c += BIT_SHIFT(k[2], k[3], s);
193 a += BIT_SHIFT(k[0], k[1], s);
194 b += BIT_SHIFT(k[1], k[2], s);
195 c += BIT_SHIFT(k[2], k[3], s) & LOWER24b_MASK;
198 a += BIT_SHIFT(k[0], k[1], s);
199 b += BIT_SHIFT(k[1], k[2], s);
200 c += BIT_SHIFT(k[2], k[3], s) & LOWER16b_MASK;
203 a += BIT_SHIFT(k[0], k[1], s);
204 b += BIT_SHIFT(k[1], k[2], s);
205 c += BIT_SHIFT(k[2], k[3], s) & LOWER8b_MASK;
208 a += BIT_SHIFT(k[0], k[1], s);
209 b += BIT_SHIFT(k[1], k[2], s);
212 a += BIT_SHIFT(k[0], k[1], s);
213 b += BIT_SHIFT(k[1], k[2], s) & LOWER24b_MASK;
216 a += BIT_SHIFT(k[0], k[1], s);
217 b += BIT_SHIFT(k[1], k[2], s) & LOWER16b_MASK;
220 a += BIT_SHIFT(k[0], k[1], s);
221 b += BIT_SHIFT(k[1], k[2], s) & LOWER8b_MASK;
224 a += BIT_SHIFT(k[0], k[1], s);
227 a += BIT_SHIFT(k[0], k[1], s) & LOWER24b_MASK;
230 a += BIT_SHIFT(k[0], k[1], s) & LOWER16b_MASK;
233 a += BIT_SHIFT(k[0], k[1], s) & LOWER8b_MASK;
235 /* zero length strings require no mixing */
243 __rte_jhash_final(a, b, c);
250 * Same as rte_jhash, but takes two seeds and return two uint32_ts.
251 * pc and pb must be non-null, and *pc and *pb must both be initialized
252 * with seeds. If you pass in (*pb)=0, the output (*pc) will be
253 * the same as the return value from rte_jhash.
256 * Key to calculate hash of.
258 * Length of key in bytes.
260 * IN: seed OUT: primary hash value.
262 * IN: second seed OUT: secondary hash value.
265 rte_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc, uint32_t *pb)
267 __rte_jhash_2hashes(key, length, pc, pb, 1);
271 * Same as rte_jhash_32b, but takes two seeds and return two uint32_ts.
272 * pc and pb must be non-null, and *pc and *pb must both be initialized
273 * with seeds. If you pass in (*pb)=0, the output (*pc) will be
274 * the same as the return value from rte_jhash_32b.
277 * Key to calculate hash of.
279 * Length of key in units of 4 bytes.
281 * IN: seed OUT: primary hash value.
283 * IN: second seed OUT: secondary hash value.
286 rte_jhash_32b_2hashes(const uint32_t *k, uint32_t length, uint32_t *pc, uint32_t *pb)
288 __rte_jhash_2hashes((const void *) k, (length << 2), pc, pb, 0);
292 * The most generic version, hashes an arbitrary sequence
293 * of bytes. No alignment or length assumptions are made about
294 * the input key. For keys not aligned to four byte boundaries
295 * or a multiple of four bytes in length, the memory region
296 * just after may be read (but not used in the computation).
297 * This may cross a page boundary.
300 * Key to calculate hash of.
302 * Length of key in bytes.
304 * Initialising value of hash.
306 * Calculated hash value.
308 static inline uint32_t
309 rte_jhash(const void *key, uint32_t length, uint32_t initval)
311 uint32_t initval2 = 0;
313 rte_jhash_2hashes(key, length, &initval, &initval2);
319 * A special optimized version that handles 1 or more of uint32_ts.
320 * The length parameter here is the number of uint32_ts in the key.
323 * Key to calculate hash of.
325 * Length of key in units of 4 bytes.
327 * Initialising value of hash.
329 * Calculated hash value.
331 static inline uint32_t
332 rte_jhash_32b(const uint32_t *k, uint32_t length, uint32_t initval)
334 uint32_t initval2 = 0;
336 rte_jhash_32b_2hashes(k, length, &initval, &initval2);
341 static inline uint32_t
342 __rte_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
344 a += RTE_JHASH_GOLDEN_RATIO + initval;
345 b += RTE_JHASH_GOLDEN_RATIO + initval;
346 c += RTE_JHASH_GOLDEN_RATIO + initval;
348 __rte_jhash_final(a, b, c);
354 * A special ultra-optimized versions that knows it is hashing exactly
358 * First word to calculate hash of.
360 * Second word to calculate hash of.
362 * Third word to calculate hash of.
364 * Initialising value of hash.
366 * Calculated hash value.
368 static inline uint32_t
369 rte_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
371 return __rte_jhash_3words(a + 12, b + 12, c + 12, initval);
375 * A special ultra-optimized versions that knows it is hashing exactly
379 * First word to calculate hash of.
381 * Second word to calculate hash of.
383 * Initialising value of hash.
385 * Calculated hash value.
387 static inline uint32_t
388 rte_jhash_2words(uint32_t a, uint32_t b, uint32_t initval)
390 return __rte_jhash_3words(a + 8, b + 8, 8, initval);
394 * A special ultra-optimized versions that knows it is hashing exactly
398 * Word to calculate hash of.
400 * Initialising value of hash.
402 * Calculated hash value.
404 static inline uint32_t
405 rte_jhash_1word(uint32_t a, uint32_t initval)
407 return __rte_jhash_3words(a + 4, 4, 4, initval);
414 #endif /* _RTE_JHASH_H */