FreeBSD kernel CXGB device code
jhash.h
Go to the documentation of this file.
1#ifndef _JHASH_H
2#define _JHASH_H
3
4/* jhash.h: Jenkins hash support.
5 *
6 * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
7 *
8 * http://burtleburtle.net/bob/hash/
9 *
10 * These are the credits from Bob's sources:
11 *
12 * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
13 * hash(), hash2(), hash3, and mix() are externally useful functions.
14 * Routines to test the hash are included if SELF_TEST is defined.
15 * You can use this free for any purpose. It has no warranty.
16 *
17 * $FreeBSD$
18 */
19
20/* NOTE: Arguments are modified. */
21#define __jhash_mix(a, b, c) \
22{ \
23 a -= b; a -= c; a ^= (c>>13); \
24 b -= c; b -= a; b ^= (a<<8); \
25 c -= a; c -= b; c ^= (b>>13); \
26 a -= b; a -= c; a ^= (c>>12); \
27 b -= c; b -= a; b ^= (a<<16); \
28 c -= a; c -= b; c ^= (b>>5); \
29 a -= b; a -= c; a ^= (c>>3); \
30 b -= c; b -= a; b ^= (a<<10); \
31 c -= a; c -= b; c ^= (b>>15); \
32}
33
34/* The golden ration: an arbitrary value */
35#define JHASH_GOLDEN_RATIO 0x9e3779b9
36
37/* The most generic version, hashes an arbitrary sequence
38 * of bytes. No alignment or length assumptions are made about
39 * the input key.
40 */
41static inline u32 jhash(const void *key, u32 length, u32 initval)
42{
43 u32 a, b, c, len;
44 const u8 *k = key;
45
46 len = length;
47 a = b = JHASH_GOLDEN_RATIO;
48 c = initval;
49
50 while (len >= 12) {
51 a += (k[0] +((u32)k[1]<<8) +((u32)k[2]<<16) +((u32)k[3]<<24));
52 b += (k[4] +((u32)k[5]<<8) +((u32)k[6]<<16) +((u32)k[7]<<24));
53 c += (k[8] +((u32)k[9]<<8) +((u32)k[10]<<16)+((u32)k[11]<<24));
54
55 __jhash_mix(a,b,c);
56
57 k += 12;
58 len -= 12;
59 }
60
61 c += length;
62 switch (len) {
63 case 11: c += ((u32)k[10]<<24);
64 case 10: c += ((u32)k[9]<<16);
65 case 9 : c += ((u32)k[8]<<8);
66 case 8 : b += ((u32)k[7]<<24);
67 case 7 : b += ((u32)k[6]<<16);
68 case 6 : b += ((u32)k[5]<<8);
69 case 5 : b += k[4];
70 case 4 : a += ((u32)k[3]<<24);
71 case 3 : a += ((u32)k[2]<<16);
72 case 2 : a += ((u32)k[1]<<8);
73 case 1 : a += k[0];
74 };
75
76 __jhash_mix(a,b,c);
77
78 return c;
79}
80
81/* A special optimized version that handles 1 or more of u32s.
82 * The length parameter here is the number of u32s in the key.
83 */
84static inline u32 jhash2(u32 *k, u32 length, u32 initval)
85{
86 u32 a, b, c, len;
87
88 a = b = JHASH_GOLDEN_RATIO;
89 c = initval;
90 len = length;
91
92 while (len >= 3) {
93 a += k[0];
94 b += k[1];
95 c += k[2];
96 __jhash_mix(a, b, c);
97 k += 3; len -= 3;
98 }
99
100 c += length * 4;
101
102 switch (len) {
103 case 2 : b += k[1];
104 case 1 : a += k[0];
105 };
106
107 __jhash_mix(a,b,c);
108
109 return c;
110}
111
112
113/* A special ultra-optimized versions that knows they are hashing exactly
114 * 3, 2 or 1 word(s).
115 *
116 * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
117 * done at the end is not done here.
118 */
119static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
120{
123 c += initval;
124
125 __jhash_mix(a, b, c);
126
127 return c;
128}
129
130static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
131{
132 return jhash_3words(a, b, 0, initval);
133}
134
135static inline u32 jhash_1word(u32 a, u32 initval)
136{
137 return jhash_3words(a, 0, 0, initval);
138}
139
140#endif /* _JHASH_H */
uint8_t u8
Definition: cxgb_osdep.h:200
uint32_t u32
Definition: cxgb_osdep.h:202
static u32 jhash_2words(u32 a, u32 b, u32 initval)
Definition: jhash.h:130
#define JHASH_GOLDEN_RATIO
Definition: jhash.h:35
static u32 jhash(const void *key, u32 length, u32 initval)
Definition: jhash.h:41
static u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
Definition: jhash.h:119
static u32 jhash2(u32 *k, u32 length, u32 initval)
Definition: jhash.h:84
#define __jhash_mix(a, b, c)
Definition: jhash.h:21
static u32 jhash_1word(u32 a, u32 initval)
Definition: jhash.h:135