add copyrights, cleanup
[lttng-ust.git] / libmarkers / jhash.h
1 #ifndef _LINUX_JHASH_H
2 #define _LINUX_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 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
18 *
19 * I've modified Bob's hash to be useful in the Linux kernel, and
20 * any bugs present are surely my fault. -DaveM
21 */
22
23 #include "kernelcompat.h"
24
25 /* NOTE: Arguments are modified. */
26 #define __jhash_mix(a, b, c) \
27 { \
28 a -= b; a -= c; a ^= (c>>13); \
29 b -= c; b -= a; b ^= (a<<8); \
30 c -= a; c -= b; c ^= (b>>13); \
31 a -= b; a -= c; a ^= (c>>12); \
32 b -= c; b -= a; b ^= (a<<16); \
33 c -= a; c -= b; c ^= (b>>5); \
34 a -= b; a -= c; a ^= (c>>3); \
35 b -= c; b -= a; b ^= (a<<10); \
36 c -= a; c -= b; c ^= (b>>15); \
37 }
38
39 /* The golden ration: an arbitrary value */
40 #define JHASH_GOLDEN_RATIO 0x9e3779b9
41
42 /* The most generic version, hashes an arbitrary sequence
43 * of bytes. No alignment or length assumptions are made about
44 * the input key.
45 */
46 static inline u32 jhash(const void *key, u32 length, u32 initval)
47 {
48 u32 a, b, c, len;
49 const u8 *k = key;
50
51 len = length;
52 a = b = JHASH_GOLDEN_RATIO;
53 c = initval;
54
55 while (len >= 12) {
56 a += (k[0] +((u32)k[1]<<8) +((u32)k[2]<<16) +((u32)k[3]<<24));
57 b += (k[4] +((u32)k[5]<<8) +((u32)k[6]<<16) +((u32)k[7]<<24));
58 c += (k[8] +((u32)k[9]<<8) +((u32)k[10]<<16)+((u32)k[11]<<24));
59
60 __jhash_mix(a,b,c);
61
62 k += 12;
63 len -= 12;
64 }
65
66 c += length;
67 switch (len) {
68 case 11: c += ((u32)k[10]<<24);
69 case 10: c += ((u32)k[9]<<16);
70 case 9 : c += ((u32)k[8]<<8);
71 case 8 : b += ((u32)k[7]<<24);
72 case 7 : b += ((u32)k[6]<<16);
73 case 6 : b += ((u32)k[5]<<8);
74 case 5 : b += k[4];
75 case 4 : a += ((u32)k[3]<<24);
76 case 3 : a += ((u32)k[2]<<16);
77 case 2 : a += ((u32)k[1]<<8);
78 case 1 : a += k[0];
79 };
80
81 __jhash_mix(a,b,c);
82
83 return c;
84 }
85
86 /* A special optimized version that handles 1 or more of u32s.
87 * The length parameter here is the number of u32s in the key.
88 */
89 static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
90 {
91 u32 a, b, c, len;
92
93 a = b = JHASH_GOLDEN_RATIO;
94 c = initval;
95 len = length;
96
97 while (len >= 3) {
98 a += k[0];
99 b += k[1];
100 c += k[2];
101 __jhash_mix(a, b, c);
102 k += 3; len -= 3;
103 }
104
105 c += length * 4;
106
107 switch (len) {
108 case 2 : b += k[1];
109 case 1 : a += k[0];
110 };
111
112 __jhash_mix(a,b,c);
113
114 return c;
115 }
116
117
118 /* A special ultra-optimized versions that knows they are hashing exactly
119 * 3, 2 or 1 word(s).
120 *
121 * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
122 * done at the end is not done here.
123 */
124 static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
125 {
126 a += JHASH_GOLDEN_RATIO;
127 b += JHASH_GOLDEN_RATIO;
128 c += initval;
129
130 __jhash_mix(a, b, c);
131
132 return c;
133 }
134
135 static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
136 {
137 return jhash_3words(a, b, 0, initval);
138 }
139
140 static inline u32 jhash_1word(u32 a, u32 initval)
141 {
142 return jhash_3words(a, 0, 0, initval);
143 }
144
145 #endif /* _LINUX_JHASH_H */
This page took 0.049432 seconds and 4 git commands to generate.