Subversion Repositories mdb

Rev

Rev 27 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
27 andreas 1
/*
2
 * -------------------------------------------------------------------------------
3
 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
4
 *
5
 * These are functions for producing 32-bit hashes for hash table lookup.
6
 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
7
 * are externally useful functions.  Routines to test the hash are included
8
 * if SELF_TEST is defined.  You can use this free for any purpose.  It's in
9
 * the public domain.  It has no warranty.
10
 *
11
 * You probably want to use hashlittle().  hashlittle() and hashbig()
12
 * hash byte arrays.  hashlittle() is is faster than hashbig() on
13
 * little-endian machines.  Intel and AMD are little-endian machines.
14
 * On second thought, you probably want hashlittle2(), which is identical to
15
 * hashlittle() except it returns two 32-bit hashes for the price of one.
16
 * You could implement hashbig2() if you wanted but I haven't bothered here.
17
 *
18
 * If you want to find a hash of, say, exactly 7 integers, do
19
 *  a = i1;  b = i2;  c = i3;
20
 *  mix(a,b,c);
21
 *  a += i4; b += i5; c += i6;
22
 *  mix(a,b,c);
23
 *  a += i7;
24
 *  final(a,b,c);
25
 * then use c as the hash value.  If you have a variable length array of
26
 * 4-byte integers to hash, use hashword().  If you have a byte array (like
27
 * a character string), use hashlittle().  If you have several byte arrays, or
28
 * a mix of things, see the comments above hashlittle().
29
 *
30
 * Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
31
 * then mix those integers.  This is fast (you can do a lot more thorough
32
 * mixing with 12*3 instructions on 3 integers than you can with 3 instructions
33
 * on 1 byte), but shoehorning those bytes into integers efficiently is messy.
34
 * -------------------------------------------------------------------------------
35
 */
36
 
37
#ifndef __LOOKUP3_H__
38
#define __LOOKUP3_H__
39
 
55 andreas 40
#ifndef __APPLE__
27 andreas 41
#ifndef uint32_t
42
	typedef unsigned long uint32_t;
43
#endif
55 andreas 44
#endif
27 andreas 45
#ifndef uint8_t
46
	typedef unsigned char uint8_t;
47
#endif
48
 
49
/*
50
 * --------------------------------------------------------------------
51
 * This works on all machines.  To be useful, it requires
52
 * -- that the key be an array of uint32_t's, and
53
 * -- that the length be the number of uint32_t's in the key
54
 *
55
 * The function hashword() is identical to hashlittle() on little-endian
56
 * machines, and identical to hashbig() on big-endian machines,
57
 * except that the length has to be measured in uint32_ts rather than in
58
 * bytes.  hashlittle() is more complicated than hashword() only because
59
 * hashlittle() has to dance around fitting the key bytes into registers.
60
 * --------------------------------------------------------------------
61
 */
62
uint32_t hashword(
63
	const uint32_t *k,                   /* the key, an array of uint32_t values */
64
	size_t          length,               /* the length of the key, in uint32_ts */
65
	uint32_t        initval);         /* the previous hash, or an arbitrary value */
66
 
67
/*
68
 * --------------------------------------------------------------------
69
 * hashword2() -- same as hashword(), but take two seeds and return two
70
 * 32-bit values.  pc and pb must both be nonnull, and *pc and *pb must
71
 * both be initialized with seeds.  If you pass in (*pb)==0, the output
72
 * (*pc) will be the same as the return value from hashword().
73
 * --------------------------------------------------------------------
74
 */
75
void hashword2 (
76
	const uint32_t *k,                   /* the key, an array of uint32_t values */
77
	size_t          length,               /* the length of the key, in uint32_ts */
78
	uint32_t       *pc,                      /* IN: seed OUT: primary hash value */
79
	uint32_t       *pb);               /* IN: more seed OUT: secondary hash value */
80
 
81
/*
82
 * -------------------------------------------------------------------------------
83
 * hashlittle() -- hash a variable-length key into a 32-bit value
84
 *  k       : the key (the unaligned variable-length array of bytes)
85
 *  length  : the length of the key, counting by bytes
86
 *  initval : can be any 4-byte value
87
 * Returns a 32-bit value.  Every bit of the key affects every bit of
88
 * the return value.  Two keys differing by one or two bits will have
89
 * totally different hash values.
90
 *
91
 * The best hash table sizes are powers of 2.  There is no need to do
92
 * mod a prime (mod is sooo slow!).  If you need less than 32 bits,
93
 * use a bitmask.  For example, if you need only 10 bits, do
94
 *  h = (h & hashmask(10));
95
 * In which case, the hash table should have hashsize(10) elements.
96
 *
97
 * If you are hashing n strings (uint8_t **)k, do it like this:
98
 *  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
99
 *
100
 * By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
101
 * code any way you wish, private, educational, or commercial.  It's free.
102
 *
103
 * Use for hash table lookup, or anything where one collision in 2^^32 is
104
 * acceptable.  Do NOT use for cryptographic purposes.
105
 * -------------------------------------------------------------------------------
106
 */
107
 
108
uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
109
 
110
/*
111
 * hashlittle2: return 2 32-bit hash values
112
 *
113
 * This is identical to hashlittle(), except it returns two 32-bit hash
114
 * values instead of just one.  This is good enough for hash table
115
 * lookup with 2^^64 buckets, or if you want a second hash if you're not
116
 * happy with the first, or if you want a probably-unique 64-bit ID for
117
 * the key.  *pc is better mixed than *pb, so use *pc first.  If you want
118
 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
119
 */
120
void hashlittle2(
121
	const void *key,       /* the key to hash */
122
	size_t      length,    /* length of the key */
123
	uint32_t   *pc,        /* IN: primary initval, OUT: primary hash */
124
	uint32_t   *pb);       /* IN: secondary initval, OUT: secondary hash */
125
 
126
/*
127
 * hashbig():
128
 * This is the same as hashword() on big-endian machines.  It is different
129
 * from hashlittle() on all machines.  hashbig() takes advantage of
130
 * big-endian byte ordering.
131
 */
132
uint32_t hashbig( const void *key, size_t length, uint32_t initval);
133
 
134
 
135
#endif