CNDP  22.08.0
cne_jhash.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2010-2022 Intel Corporation.
3  */
4 
5 #ifndef _CNE_JHASH_H
6 #define _CNE_JHASH_H
7 
14 #include <stdint.h>
15 #include <string.h>
16 #include <limits.h>
17 #include <endian.h>
18 
19 #include <cne_log.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /* jhash.h: Jenkins hash support.
26  *
27  * Copyright (c) 2006 Bob Jenkins (bob_jenkins@burtleburtle.net)
28  *
29  * http://burtleburtle.net/bob/hash/
30  *
31  * These are the credits from Bob's sources:
32  *
33  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
34  *
35  * These are functions for producing 32-bit hashes for hash table lookup.
36  * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
37  * are externally useful functions. Routines to test the hash are included
38  * if SELF_TEST is defined. You can use this free for any purpose. It's in
39  * the public domain. It has no warranty.
40  *
41  * $FreeBSD$
42  */
43 
44 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
45 
46 // clang-format off
48 #define __cne_jhash_mix(a, b, c) do { \
49  a -= c; a ^= rot(c, 4); c += b; \
50  b -= a; b ^= rot(a, 6); a += c; \
51  c -= b; c ^= rot(b, 8); b += a; \
52  a -= c; a ^= rot(c, 16); c += b; \
53  b -= a; b ^= rot(a, 19); a += c; \
54  c -= b; c ^= rot(b, 4); b += a; \
55 } while (0)
56 
57 #define __cne_jhash_final(a, b, c) do { \
58  c ^= b; c -= rot(b, 14); \
59  a ^= c; a -= rot(c, 11); \
60  b ^= a; b -= rot(a, 25); \
61  c ^= b; c -= rot(b, 16); \
62  a ^= c; a -= rot(c, 4); \
63  b ^= a; b -= rot(a, 14); \
64  c ^= b; c -= rot(b, 24); \
65 } while (0)
66 // clang-format on
67 
69 #define CNE_JHASH_GOLDEN_RATIO 0xdeadbeef
70 
71 #if BYTE_ORDER == LITTLE_ENDIAN
72 #define BIT_SHIFT(x, y, k) (((x) >> (k)) | ((uint64_t)(y) << (32 - (k))))
73 #else
74 #define BIT_SHIFT(x, y, k) (((uint64_t)(x) << (k)) | ((y) >> (32 - (k))))
75 #endif
76 
77 #define LOWER8b_MASK le32toh(0xff)
78 #define LOWER16b_MASK le32toh(0xffff)
79 #define LOWER24b_MASK le32toh(0xffffff)
80 
81 static inline void
82 __cne_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc, uint32_t *pb,
83  unsigned check_align)
84 {
85  uint32_t a, b, c;
86 
87  /* Set up the internal state */
88  a = b = c = CNE_JHASH_GOLDEN_RATIO + ((uint32_t)length) + *pc;
89  c += *pb;
90 
91  /*
92  * Check key alignment. For x86 architecture, first case is always optimal
93  * If check_align is not set, first case will be used
94  */
95  const uint32_t *k = (const uint32_t *)key;
96  const uint32_t s = 0;
97  if (!check_align || s == 0) {
98  while (length > 12) {
99  a += k[0];
100  b += k[1];
101  c += k[2];
102 
103  __cne_jhash_mix(a, b, c);
104 
105  k += 3;
106  length -= 12;
107  }
108  // clang-format off
109  switch (length) {
110  case 12:
111  c += k[2]; b += k[1]; a += k[0]; break;
112  case 11:
113  c += k[2] & LOWER24b_MASK; b += k[1]; a += k[0]; break;
114  case 10:
115  c += k[2] & LOWER16b_MASK; b += k[1]; a += k[0]; break;
116  case 9:
117  c += k[2] & LOWER8b_MASK; b += k[1]; a += k[0]; break;
118  case 8:
119  b += k[1]; a += k[0]; break;
120  case 7:
121  b += k[1] & LOWER24b_MASK; a += k[0]; break;
122  case 6:
123  b += k[1] & LOWER16b_MASK; a += k[0]; break;
124  case 5:
125  b += k[1] & LOWER8b_MASK; a += k[0]; break;
126  case 4:
127  a += k[0]; break;
128  case 3:
129  a += k[0] & LOWER24b_MASK; break;
130  case 2:
131  a += k[0] & LOWER16b_MASK; break;
132  case 1:
133  a += k[0] & LOWER8b_MASK; break;
134  /* zero length strings require no mixing */
135  case 0:
136  *pc = c;
137  *pb = b;
138  return;
139  };
140  // clang-format on
141  } else {
142  /* all but the last block: affect some 32 bits of (a, b, c) */
143  while (length > 12) {
144  a += BIT_SHIFT(k[0], k[1], s);
145  b += BIT_SHIFT(k[1], k[2], s);
146  c += BIT_SHIFT(k[2], k[3], s);
147  __cne_jhash_mix(a, b, c);
148 
149  k += 3;
150  length -= 12;
151  }
152 
153  /* last block: affect all 32 bits of (c) */
154  switch (length) {
155  case 12:
156  a += BIT_SHIFT(k[0], k[1], s);
157  b += BIT_SHIFT(k[1], k[2], s);
158  c += BIT_SHIFT(k[2], k[3], s);
159  break;
160  case 11:
161  a += BIT_SHIFT(k[0], k[1], s);
162  b += BIT_SHIFT(k[1], k[2], s);
163  c += BIT_SHIFT(k[2], k[3], s) & LOWER24b_MASK;
164  break;
165  case 10:
166  a += BIT_SHIFT(k[0], k[1], s);
167  b += BIT_SHIFT(k[1], k[2], s);
168  c += BIT_SHIFT(k[2], k[3], s) & LOWER16b_MASK;
169  break;
170  case 9:
171  a += BIT_SHIFT(k[0], k[1], s);
172  b += BIT_SHIFT(k[1], k[2], s);
173  c += BIT_SHIFT(k[2], k[3], s) & LOWER8b_MASK;
174  break;
175  case 8:
176  a += BIT_SHIFT(k[0], k[1], s);
177  b += BIT_SHIFT(k[1], k[2], s);
178  break;
179  case 7:
180  a += BIT_SHIFT(k[0], k[1], s);
181  b += BIT_SHIFT(k[1], k[2], s) & LOWER24b_MASK;
182  break;
183  case 6:
184  a += BIT_SHIFT(k[0], k[1], s);
185  b += BIT_SHIFT(k[1], k[2], s) & LOWER16b_MASK;
186  break;
187  case 5:
188  a += BIT_SHIFT(k[0], k[1], s);
189  b += BIT_SHIFT(k[1], k[2], s) & LOWER8b_MASK;
190  break;
191  case 4:
192  a += BIT_SHIFT(k[0], k[1], s);
193  break;
194  case 3:
195  a += BIT_SHIFT(k[0], k[1], s) & LOWER24b_MASK;
196  break;
197  case 2:
198  a += BIT_SHIFT(k[0], k[1], s) & LOWER16b_MASK;
199  break;
200  case 1:
201  a += BIT_SHIFT(k[0], k[1], s) & LOWER8b_MASK;
202  break;
203  /* zero length strings require no mixing */
204  case 0:
205  *pc = c;
206  *pb = b;
207  return;
208  }
209  }
210 
211  __cne_jhash_final(a, b, c);
212 
213  *pc = c;
214  *pb = b;
215 }
216 
232 static inline void
233 cne_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc, uint32_t *pb)
234 {
235  __cne_jhash_2hashes(key, length, pc, pb, 1);
236 }
237 
253 static inline void
254 cne_jhash_32b_2hashes(const uint32_t *k, uint32_t length, uint32_t *pc, uint32_t *pb)
255 {
256  __cne_jhash_2hashes((const void *)k, (length << 2), pc, pb, 0);
257 }
258 
276 static inline uint32_t
277 cne_jhash(const void *key, uint32_t length, uint32_t initval)
278 {
279  uint32_t initval2 = 0;
280 
281  cne_jhash_2hashes(key, length, &initval, &initval2);
282 
283  return initval;
284 }
285 
299 static inline uint32_t
300 cne_jhash_32b(const uint32_t *k, uint32_t length, uint32_t initval)
301 {
302  uint32_t initval2 = 0;
303 
304  cne_jhash_32b_2hashes(k, length, &initval, &initval2);
305 
306  return initval;
307 }
308 
309 static inline uint32_t
310 __cne_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
311 {
312  a += CNE_JHASH_GOLDEN_RATIO + initval;
313  b += CNE_JHASH_GOLDEN_RATIO + initval;
314  c += CNE_JHASH_GOLDEN_RATIO + initval;
315 
316  __cne_jhash_final(a, b, c);
317 
318  return c;
319 }
320 
336 static inline uint32_t
337 cne_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
338 {
339  return __cne_jhash_3words(a + 12, b + 12, c + 12, initval);
340 }
341 
355 static inline uint32_t
356 cne_jhash_2words(uint32_t a, uint32_t b, uint32_t initval)
357 {
358  return __cne_jhash_3words(a + 8, b + 8, 8, initval);
359 }
360 
372 static inline uint32_t
373 cne_jhash_1word(uint32_t a, uint32_t initval)
374 {
375  return __cne_jhash_3words(a + 4, 4, 4, initval);
376 }
377 
378 #ifdef __cplusplus
379 }
380 #endif
381 
382 #endif /* _CNE_JHASH_H */
static uint32_t cne_jhash_1word(uint32_t a, uint32_t initval)
Definition: cne_jhash.h:373
#define CNE_JHASH_GOLDEN_RATIO
Definition: cne_jhash.h:69
static uint32_t cne_jhash(const void *key, uint32_t length, uint32_t initval)
Definition: cne_jhash.h:277
static uint32_t cne_jhash_3words(uint32_t a, uint32_t b, uint32_t c, uint32_t initval)
Definition: cne_jhash.h:337
static uint32_t cne_jhash_32b(const uint32_t *k, uint32_t length, uint32_t initval)
Definition: cne_jhash.h:300
static void cne_jhash_32b_2hashes(const uint32_t *k, uint32_t length, uint32_t *pc, uint32_t *pb)
Definition: cne_jhash.h:254
static uint32_t cne_jhash_2words(uint32_t a, uint32_t b, uint32_t initval)
Definition: cne_jhash.h:356
static void cne_jhash_2hashes(const void *key, uint32_t length, uint32_t *pc, uint32_t *pb)
Definition: cne_jhash.h:233