Xenomai API  2.6.5
jhash.h
1 /*
2  * Jenkins hash support, lifted from the Linux kernel from
3  * linux/include/linux/jhash.h.
4  *
5  * Original credits:
6  *
7  * Copyright (C) 1996 Bob Jenkins ([email protected])
8  *
9  * http://burtleburtle.net/bob/hash/
10  *
11  * These are the credits from Bob's sources:
12  *
13  * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
14  * hash(), hash2(), hash3, and mix() are externally useful functions.
15  * Routines to test the hash are included if SELF_TEST is defined.
16  * You can use this free for any purpose. It has no warranty.
17  *
18  * Copyright (C) 2003 David S. Miller ([email protected])
19  *
20  * I've modified Bob's hash to be useful in the Linux kernel, and
21  * any bugs present are surely my fault. -DaveM
22  */
23 
24 #ifndef _XENO_NUCLEUS_JHASH_H
25 #define _XENO_NUCLEUS_JHASH_H
26 
27 #include <nucleus/types.h>
28 
29 /* NOTE: Arguments are modified. */
30 #define __jhash_mix(a, b, c) \
31 { \
32  a -= b; a -= c; a ^= (c>>13); \
33  b -= c; b -= a; b ^= (a<<8); \
34  c -= a; c -= b; c ^= (b>>13); \
35  a -= b; a -= c; a ^= (c>>12); \
36  b -= c; b -= a; b ^= (a<<16); \
37  c -= a; c -= b; c ^= (b>>5); \
38  a -= b; a -= c; a ^= (c>>3); \
39  b -= c; b -= a; b ^= (a<<10); \
40  c -= a; c -= b; c ^= (b>>15); \
41 }
42 
43 /* The golden ratio: an arbitrary value */
44 #define JHASH_GOLDEN_RATIO 0x9e3779b9
45 
46 /* A special optimized version that handles 1 or more of u32s.
47  * The length parameter here is the number of u32s in the key.
48  */
49 static inline uint32_t jhash2(const uint32_t *k, uint32_t length, uint32_t initval)
50 {
51  uint32_t a, b, c, len;
52 
53  a = b = JHASH_GOLDEN_RATIO;
54  c = initval;
55  len = length;
56 
57  while (len >= 3) {
58  a += k[0];
59  b += k[1];
60  c += k[2];
61  __jhash_mix(a, b, c);
62  k += 3; len -= 3;
63  }
64 
65  c += length * 4;
66 
67  switch (len) {
68  case 2 : b += k[1];
69  case 1 : a += k[0];
70  };
71 
72  __jhash_mix(a,b,c);
73 
74  return c;
75 }
76 
77 #endif /* _XENO_NUCLEUS_JHASH_H */