OpenMPI 0.1.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

jenkins_hash.h
1 #ifndef VENDOR_HASH_JENKINS_H
2 #define VENDOR_HASH_JENKINS_H
3
4 #include <stddef.h> /* defines size_t etc */
5 #include <stdint.h> /* defines uint32_t etc */
6
7 #ifdef SELF_TEST
8 # include <stdio.h> /* defines printf for tests */
9 # include <time.h> /* defines time_t for timings in the test */
10 #endif
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16
17 #define hashsize(n) ((uint32_t)1<<(n))
18 #define hashmask(n) (hashsize(n)-1)
19
20
21 /*
22 --------------------------------------------------------------------
23 This works on all machines. To be useful, it requires
24 -- that the key be an array of uint32_t's, and
25 -- that the length be the number of uint32_t's in the key
26
27 The function hashword() is identical to hashlittle() on little-endian
28 machines, and identical to hashbig() on big-endian machines,
29 except that the length has to be measured in uint32_ts rather than in
30 bytes. hashlittle() is more complicated than hashword() only because
31 hashlittle() has to dance around fitting the key bytes into registers.
32 --------------------------------------------------------------------
33 */
34 uint32_t hashword(
35 const uint32_t *k, /* the key, an array of uint32_t values */
36 size_t length, /* the length of the key, in uint32_ts */
37 uint32_t initval); /* the previous hash, or an arbitrary value */
38
39
40 /*
41 --------------------------------------------------------------------
42 hashword2() -- same as hashword(), but take two seeds and return two
43 32-bit values. pc and pb must both be nonnull, and *pc and *pb must
44 both be initialized with seeds. If you pass in (*pb)==0, the output
45 (*pc) will be the same as the return value from hashword().
46 --------------------------------------------------------------------
47 */
48 void hashword2 (
49 const uint32_t *k, /* the key, an array of uint32_t values */
50 size_t length, /* the length of the key, in uint32_ts */
51 uint32_t *pc, /* IN: seed OUT: primary hash value */
52 uint32_t *pb); /* IN: more seed OUT: secondary hash value */
53
54
55 /*
56 -------------------------------------------------------------------------------
57 hashlittle() -- hash a variable-length key into a 32-bit value
58 k : the key (the unaligned variable-length array of bytes)
59 length : the length of the key, counting by bytes
60 initval : can be any 4-byte value
61 Returns a 32-bit value. Every bit of the key affects every bit of
62 the return value. Two keys differing by one or two bits will have
63 totally different hash values.
64
65 The best hash table sizes are powers of 2. There is no need to do
66 mod a prime (mod is sooo slow!). If you need less than 32 bits,
67 use a bitmask. For example, if you need only 10 bits, do
68 h = (h & hashmask(10));
69 In which case, the hash table should have hashsize(10) elements.
70
71 If you are hashing n strings (uint8_t **)k, do it like this:
72 for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
73
74 By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
75 code any way you wish, private, educational, or commercial. It's free.
76
77 Use for hash table lookup, or anything where one collision in 2^^32 is
78 acceptable. Do NOT use for cryptographic purposes.
79 -------------------------------------------------------------------------------
80 */
81 uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
82
83
84 /*
85 * hashlittle2: return 2 32-bit hash values
86 *
87 * This is identical to hashlittle(), except it returns two 32-bit hash
88 * values instead of just one. This is good enough for hash table
89 * lookup with 2^^64 buckets, or if you want a second hash if you're not
90 * happy with the first, or if you want a probably-unique 64-bit ID for
91 * the key. *pc is better mixed than *pb, so use *pc first. If you want
92 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
93 */
94 void hashlittle2(
95 const void *key, /* the key to hash */
96 size_t length, /* length of the key */
97 uint32_t *pc, /* IN: primary initval, OUT: primary hash */
98 uint32_t *pb); /* IN: secondary initval, OUT: secondary hash */
99
100
101 /*
102 * hashbig():
103 * This is the same as hashword() on big-endian machines. It is different
104 * from hashlittle() on all machines. hashbig() takes advantage of
105 * big-endian byte ordering.
106 */
107 uint32_t hashbig( const void *key, size_t length, uint32_t initval);
108
109
110 /*
111 * hash():
112 * compiletime multiplexer for littel- and big-endian.
113 * Note that the hash value is NOT endias agnostic.
114 */
115 static inline uint32_t hash( const void *key, size_t length, uint32_t initval)
116 {
117 #ifdef WORDS_BIGENDIAN
118 return hashbig(key, length, initval);
119 #else
120 return hashlittle(key, length, initval);
121 #endif
122 }
123
124
125 #ifdef __cplusplus
126 }
127 #endif
128
129 #endif /* VENDOR_HASH_JENKINS_H */