DPDK  19.05.0
rte_jhash.h
Go to the documentation of this file.
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 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #include <stdint.h>
48 #include <string.h>
49 #include <limits.h>
50 
51 #include <rte_config.h>
52 #include <rte_log.h>
53 #include <rte_byteorder.h>
54 
55 /* jhash.h: Jenkins hash support.
56  *
57  * Copyright (C) 2006 Bob Jenkins (bob_jenkins@burtleburtle.net)
58  *
59  * http://burtleburtle.net/bob/hash/
60  *
61  * These are the credits from Bob's sources:
62  *
63  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
64  *
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.
70  *
71  * $FreeBSD$
72  */
73 
74 #define rot(x, k) (((x) << (k)) | ((x) >> (32-(k))))
75 
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; \
84 } while (0)
85 
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); \
94 } while (0)
95 
97 #define RTE_JHASH_GOLDEN_RATIO 0xdeadbeef
98 
99 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
100 #define BIT_SHIFT(x, y, k) (((x) >> (k)) | ((uint64_t)(y) << (32-(k))))
101 #else
102 #define BIT_SHIFT(x, y, k) (((uint64_t)(x) << (k)) | ((y) >> (32-(k))))
103 #endif
104 
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)
108 
109 static inline void
110 __rte_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc,
111  uint32_t *pb, unsigned check_align)
112 {
113  uint32_t a, b, c;
114 
115  /* Set up the internal state */
116  a = b = c = RTE_JHASH_GOLDEN_RATIO + ((uint32_t)length) + *pc;
117  c += *pb;
118 
119  /*
120  * Check key alignment. For x86 architecture, first case is always optimal
121  * If check_align is not set, first case will be used
122  */
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;
126 #else
127  const uint32_t *k = (uint32_t *)((uintptr_t)key & (uintptr_t)~3);
128  const uint32_t s = ((uintptr_t)key & 3) * CHAR_BIT;
129 #endif
130  if (!check_align || s == 0) {
131  while (length > 12) {
132  a += k[0];
133  b += k[1];
134  c += k[2];
135 
136  __rte_jhash_mix(a, b, c);
137 
138  k += 3;
139  length -= 12;
140  }
141 
142  switch (length) {
143  case 12:
144  c += k[2]; b += k[1]; a += k[0]; break;
145  case 11:
146  c += k[2] & LOWER24b_MASK; b += k[1]; a += k[0]; break;
147  case 10:
148  c += k[2] & LOWER16b_MASK; b += k[1]; a += k[0]; break;
149  case 9:
150  c += k[2] & LOWER8b_MASK; b += k[1]; a += k[0]; break;
151  case 8:
152  b += k[1]; a += k[0]; break;
153  case 7:
154  b += k[1] & LOWER24b_MASK; a += k[0]; break;
155  case 6:
156  b += k[1] & LOWER16b_MASK; a += k[0]; break;
157  case 5:
158  b += k[1] & LOWER8b_MASK; a += k[0]; break;
159  case 4:
160  a += k[0]; break;
161  case 3:
162  a += k[0] & LOWER24b_MASK; break;
163  case 2:
164  a += k[0] & LOWER16b_MASK; break;
165  case 1:
166  a += k[0] & LOWER8b_MASK; break;
167  /* zero length strings require no mixing */
168  case 0:
169  *pc = c;
170  *pb = b;
171  return;
172  };
173  } else {
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);
180 
181  k += 3;
182  length -= 12;
183  }
184 
185  /* last block: affect all 32 bits of (c) */
186  switch (length) {
187  case 12:
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);
191  break;
192  case 11:
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;
196  break;
197  case 10:
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;
201  break;
202  case 9:
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;
206  break;
207  case 8:
208  a += BIT_SHIFT(k[0], k[1], s);
209  b += BIT_SHIFT(k[1], k[2], s);
210  break;
211  case 7:
212  a += BIT_SHIFT(k[0], k[1], s);
213  b += BIT_SHIFT(k[1], k[2], s) & LOWER24b_MASK;
214  break;
215  case 6:
216  a += BIT_SHIFT(k[0], k[1], s);
217  b += BIT_SHIFT(k[1], k[2], s) & LOWER16b_MASK;
218  break;
219  case 5:
220  a += BIT_SHIFT(k[0], k[1], s);
221  b += BIT_SHIFT(k[1], k[2], s) & LOWER8b_MASK;
222  break;
223  case 4:
224  a += BIT_SHIFT(k[0], k[1], s);
225  break;
226  case 3:
227  a += BIT_SHIFT(k[0], k[1], s) & LOWER24b_MASK;
228  break;
229  case 2:
230  a += BIT_SHIFT(k[0], k[1], s) & LOWER16b_MASK;
231  break;
232  case 1:
233  a += BIT_SHIFT(k[0], k[1], s) & LOWER8b_MASK;
234  break;
235  /* zero length strings require no mixing */
236  case 0:
237  *pc = c;
238  *pb = b;
239  return;
240  }
241  }
242 
243  __rte_jhash_final(a, b, c);
244 
245  *pc = c;
246  *pb = b;
247 }
248 
264 static inline void
265 rte_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc, uint32_t *pb)
266 {
267  __rte_jhash_2hashes(key, length, pc, pb, 1);
268 }
269 
285 static inline void
286 rte_jhash_32b_2hashes(const uint32_t *k, uint32_t length, uint32_t *pc, uint32_t *pb)
287 {
288  __rte_jhash_2hashes((const void *) k, (length << 2), pc, pb, 0);
289 }
290 
308 static inline uint32_t
309 rte_jhash(const void *key, uint32_t length, uint32_t initval)
310 {
311  uint32_t initval2 = 0;
312 
313  rte_jhash_2hashes(key, length, &initval, &initval2);
314 
315  return initval;
316 }
317 
331 static inline uint32_t
332 rte_jhash_32b(const uint32_t *k, uint32_t length, uint32_t initval)
333 {
334  uint32_t initval2 = 0;
335 
336  rte_jhash_32b_2hashes(k, length, &initval, &initval2);
337 
338  return initval;
339 }
340 
341 static inline uint32_t
342 __rte_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
343 {
344  a += RTE_JHASH_GOLDEN_RATIO + initval;
345  b += RTE_JHASH_GOLDEN_RATIO + initval;
346  c += RTE_JHASH_GOLDEN_RATIO + initval;
347 
348  __rte_jhash_final(a, b, c);
349 
350  return c;
351 }
352 
368 static inline uint32_t
369 rte_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
370 {
371  return __rte_jhash_3words(a + 12, b + 12, c + 12, initval);
372 }
373 
387 static inline uint32_t
388 rte_jhash_2words(uint32_t a, uint32_t b, uint32_t initval)
389 {
390  return __rte_jhash_3words(a + 8, b + 8, 8, initval);
391 }
392 
404 static inline uint32_t
405 rte_jhash_1word(uint32_t a, uint32_t initval)
406 {
407  return __rte_jhash_3words(a + 4, 4, 4, initval);
408 }
409 
410 #ifdef __cplusplus
411 }
412 #endif
413 
414 #endif /* _RTE_JHASH_H */