Subversion Repositories mdb

Rev

Rev 21 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 Rev 22
Line 1... Line 1...
1
/*
1
/*
2
 * -------------------------------------------------------------------------------
2
 * -------------------------------------------------------------------------------
3
 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
3
 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
4
 * 
4
 *
5
 * These are functions for producing 32-bit hashes for hash table lookup.
5
 * These are functions for producing 32-bit hashes for hash table lookup.
6
 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() 
6
 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
7
 * are externally useful functions.  Routines to test the hash are included 
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
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.
9
 * the public domain.  It has no warranty.
10
 * 
10
 *
11
 * You probably want to use hashlittle().  hashlittle() and hashbig()
11
 * You probably want to use hashlittle().  hashlittle() and hashbig()
12
 * hash byte arrays.  hashlittle() is is faster than hashbig() on
12
 * hash byte arrays.  hashlittle() is is faster than hashbig() on
13
 * little-endian machines.  Intel and AMD are little-endian machines.
13
 * little-endian machines.  Intel and AMD are little-endian machines.
14
 * On second thought, you probably want hashlittle2(), which is identical to
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.  
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.
16
 * You could implement hashbig2() if you wanted but I haven't bothered here.
17
 * 
17
 *
18
 * If you want to find a hash of, say, exactly 7 integers, do
18
 * If you want to find a hash of, say, exactly 7 integers, do
19
 *  a = i1;  b = i2;  c = i3;
19
 *  a = i1;  b = i2;  c = i3;
20
 *  mix(a,b,c);
20
 *  mix(a,b,c);
21
 *  a += i4; b += i5; c += i6;
21
 *  a += i4; b += i5; c += i6;
22
 *  mix(a,b,c);
22
 *  mix(a,b,c);
23
 *  a += i7;
23
 *  a += i7;
24
 *  final(a,b,c);
24
 *  final(a,b,c);
25
 * then use c as the hash value.  If you have a variable length array of
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
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
27
 * a character string), use hashlittle().  If you have several byte arrays, or
28
 * a mix of things, see the comments above hashlittle().  
28
 * a mix of things, see the comments above hashlittle().
29
 * 
29
 *
30
 * Why is this so big?  I read 12 bytes at a time into 3 4-byte integers, 
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
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
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.
33
 * on 1 byte), but shoehorning those bytes into integers efficiently is messy.
34
 * -------------------------------------------------------------------------------
34
 * -------------------------------------------------------------------------------
35
 */
35
 */
Line 68... Line 68...
68
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
68
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
69
 
69
 
70
/*
70
/*
71
* -------------------------------------------------------------------------------
71
* -------------------------------------------------------------------------------
72
* mix -- mix 3 32-bit values reversibly.
72
* mix -- mix 3 32-bit values reversibly.
73
* 
73
*
74
* This is reversible, so any information in (a,b,c) before mix() is
74
* This is reversible, so any information in (a,b,c) before mix() is
75
* still in (a,b,c) after mix().
75
* still in (a,b,c) after mix().
76
* 
76
*
77
* If four pairs of (a,b,c) inputs are run through mix(), or through
77
* If four pairs of (a,b,c) inputs are run through mix(), or through
78
* mix() in reverse, there are at least 32 bits of the output that
78
* mix() in reverse, there are at least 32 bits of the output that
79
* are sometimes the same for one pair and different for another pair.
79
* are sometimes the same for one pair and different for another pair.
80
* This was tested for:
80
* This was tested for:
81
* pairs that differed by one bit, by two bits, in any combination
81
* pairs that differed by one bit, by two bits, in any combination
Line 83... Line 83...
83
*  (a,b,c).
83
*  (a,b,c).
84
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
84
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
85
*  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
85
*  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
86
*  is commonly produced by subtraction) look like a single 1-bit
86
*  is commonly produced by subtraction) look like a single 1-bit
87
*  difference.
87
*  difference.
88
* the base values were pseudorandom, all zero but one bit set, or 
88
* the base values were pseudorandom, all zero but one bit set, or
89
*  all zero plus a counter that starts at zero.
89
*  all zero plus a counter that starts at zero.
90
* 
90
*
91
* Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
91
* Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
92
* satisfy this are
92
* satisfy this are
93
*    4  6  8 16 19  4
93
*    4  6  8 16 19  4
94
*    9 15  3 18 27 15
94
*    9 15  3 18 27 15
95
*   14  9  3  7 17  3
95
*   14  9  3  7 17  3
96
* Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
96
* Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
97
* for "differ" defined as + with a one-bit base and a two-bit delta.  I
97
* for "differ" defined as + with a one-bit base and a two-bit delta.  I
98
* used http://burtleburtle.net/bob/hash/avalanche.html to choose 
98
* used http://burtleburtle.net/bob/hash/avalanche.html to choose
99
* the operations, constants, and arrangements of the variables.
99
* the operations, constants, and arrangements of the variables.
100
* 
100
*
101
* This does not achieve avalanche.  There are input bits of (a,b,c)
101
* This does not achieve avalanche.  There are input bits of (a,b,c)
102
* that fail to affect some output bits of (a,b,c), especially of a.  The
102
* that fail to affect some output bits of (a,b,c), especially of a.  The
103
* most thoroughly mixed value is c, but it doesn't really even achieve
103
* most thoroughly mixed value is c, but it doesn't really even achieve
104
* avalanche in c.
104
* avalanche in c.
105
* 
105
*
106
* This allows some parallelism.  Read-after-writes are good at doubling
106
* This allows some parallelism.  Read-after-writes are good at doubling
107
* the number of bits affected, so the goal of mixing pulls in the opposite
107
* the number of bits affected, so the goal of mixing pulls in the opposite
108
* direction as the goal of parallelism.  I did what I could.  Rotates
108
* direction as the goal of parallelism.  I did what I could.  Rotates
109
* seem to cost as much as shifts on every machine I could lay my hands
109
* seem to cost as much as shifts on every machine I could lay my hands
110
* on, and rotates are much kinder to the top and bottom bits, so I used
110
* on, and rotates are much kinder to the top and bottom bits, so I used
111
* rotates.
111
* rotates.
112
* -------------------------------------------------------------------------------
112
* -------------------------------------------------------------------------------
113
*/
113
*/
114
#define mix(a,b,c) \
114
#define mix(a,b,c) \
115
{ \
115
	{ \
116
 a -= c;  a ^= rot(c, 4);  c += b; \
116
		a -= c;  a ^= rot(c, 4);  c += b; \
117
 b -= a;  b ^= rot(a, 6);  a += c; \
117
		b -= a;  b ^= rot(a, 6);  a += c; \
118
 c -= b;  c ^= rot(b, 8);  b += a; \
118
		c -= b;  c ^= rot(b, 8);  b += a; \
119
 a -= c;  a ^= rot(c,16);  c += b; \
119
		a -= c;  a ^= rot(c,16);  c += b; \
120
 b -= a;  b ^= rot(a,19);  a += c; \
120
		b -= a;  b ^= rot(a,19);  a += c; \
121
 c -= b;  c ^= rot(b, 4);  b += a; \
121
		c -= b;  c ^= rot(b, 4);  b += a; \
122
}
122
	}
123
 
123
 
124
/*
124
/*
125
* -------------------------------------------------------------------------------
125
* -------------------------------------------------------------------------------
126
* final -- final mixing of 3 32-bit values (a,b,c) into c
126
* final -- final mixing of 3 32-bit values (a,b,c) into c
127
* 
127
*
128
* Pairs of (a,b,c) values differing in only a few bits will usually
128
* Pairs of (a,b,c) values differing in only a few bits will usually
129
* produce values of c that look totally different.  This was tested for
129
* produce values of c that look totally different.  This was tested for
130
* pairs that differed by one bit, by two bits, in any combination
130
* pairs that differed by one bit, by two bits, in any combination
131
*  of top bits of (a,b,c), or in any combination of bottom bits of
131
*  of top bits of (a,b,c), or in any combination of bottom bits of
132
*  (a,b,c).
132
*  (a,b,c).
133
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
133
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
134
*  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
134
*  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
135
*  is commonly produced by subtraction) look like a single 1-bit
135
*  is commonly produced by subtraction) look like a single 1-bit
136
*  difference.
136
*  difference.
137
* the base values were pseudorandom, all zero but one bit set, or 
137
* the base values were pseudorandom, all zero but one bit set, or
138
*  all zero plus a counter that starts at zero.
138
*  all zero plus a counter that starts at zero.
139
* 
139
*
140
* These constants passed:
140
* These constants passed:
141
* 14 11 25 16 4 14 24
141
* 14 11 25 16 4 14 24
142
* 12 14 25 16 4 14 24
142
* 12 14 25 16 4 14 24
143
* and these came close:
143
* and these came close:
144
*  4  8 15 26 3 22 24
144
*  4  8 15 26 3 22 24
145
* 10  8 15 26 3 22 24
145
* 10  8 15 26 3 22 24
146
* 11  8 15 26 3 22 24
146
* 11  8 15 26 3 22 24
147
* -------------------------------------------------------------------------------
147
* -------------------------------------------------------------------------------
148
*/
148
*/
149
#define final(a,b,c) \
149
#define final(a,b,c) \
150
{ \
150
	{ \
151
 c ^= b; c -= rot(b,14); \
151
		c ^= b; c -= rot(b,14); \
152
 a ^= c; a -= rot(c,11); \
152
		a ^= c; a -= rot(c,11); \
153
 b ^= a; b -= rot(a,25); \
153
		b ^= a; b -= rot(a,25); \
154
 c ^= b; c -= rot(b,16); \
154
		c ^= b; c -= rot(b,16); \
155
 a ^= c; a -= rot(c,4);  \
155
		a ^= c; a -= rot(c,4);  \
156
 b ^= a; b -= rot(a,14); \
156
		b ^= a; b -= rot(a,14); \
157
 c ^= b; c -= rot(b,24); \
157
		c ^= b; c -= rot(b,24); \
158
}
158
	}
159
 
159
 
160
/*
160
/*
161
 * --------------------------------------------------------------------
161
 * --------------------------------------------------------------------
162
 * This works on all machines.  To be useful, it requires
162
 * This works on all machines.  To be useful, it requires
163
 * -- that the key be an array of uint32_t's, and
163
 * -- that the key be an array of uint32_t's, and
164
 * -- that the length be the number of uint32_t's in the key
164
 * -- that the length be the number of uint32_t's in the key
165
 * 
165
 *
166
 * The function hashword() is identical to hashlittle() on little-endian
166
 * The function hashword() is identical to hashlittle() on little-endian
167
 * machines, and identical to hashbig() on big-endian machines,
167
 * machines, and identical to hashbig() on big-endian machines,
168
 * except that the length has to be measured in uint32_ts rather than in
168
 * except that the length has to be measured in uint32_ts rather than in
169
 * bytes.  hashlittle() is more complicated than hashword() only because
169
 * bytes.  hashlittle() is more complicated than hashword() only because
170
 * hashlittle() has to dance around fitting the key bytes into registers.
170
 * hashlittle() has to dance around fitting the key bytes into registers.
171
 * --------------------------------------------------------------------
171
 * --------------------------------------------------------------------
172
 */
172
 */
173
uint32_t hashword(
173
uint32_t hashword(
174
	const uint32_t *k,                   /* the key, an array of uint32_t values */
174
    const uint32_t *k,                   /* the key, an array of uint32_t values */
175
	size_t          length,               /* the length of the key, in uint32_ts */
175
    size_t          length,               /* the length of the key, in uint32_ts */
176
	uint32_t        initval)         /* the previous hash, or an arbitrary value */
176
    uint32_t        initval)         /* the previous hash, or an arbitrary value */
177
{
177
{
178
	uint32_t a,b,c;
178
	uint32_t a, b, c;
179
	 
179
 
180
	/* Set up the internal state */
180
	/* Set up the internal state */
181
	a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
181
	a = b = c = 0xdeadbeef + (((uint32_t)length) << 2) + initval;
182
	 
182
 
183
	/*------------------------------------------------- handle most of the key */
183
	/*------------------------------------------------- handle most of the key */
184
	while (length > 3)
184
	while (length > 3)
185
	{
185
	{
186
		a += k[0];
186
		a += k[0];
187
		b += k[1];
187
		b += k[1];
188
		c += k[2];
188
		c += k[2];
189
		mix(a,b,c);
189
		mix(a, b, c);
190
		length -= 3;
190
		length -= 3;
191
		k += 3;
191
		k += 3;
192
	}
192
	}
193
 
193
 
194
	/*------------------------------------------- handle the last 3 uint32_t's */
194
	/*------------------------------------------- handle the last 3 uint32_t's */
195
	switch(length)                     /* all the case statements fall through */
195
	switch (length)                    /* all the case statements fall through */
196
	{ 
196
	{
197
		case 3 : c+=k[2];
197
		case 3 :
-
 
198
			c += k[2];
-
 
199
 
198
		case 2 : b+=k[1];
200
		case 2 :
-
 
201
			b += k[1];
-
 
202
 
-
 
203
		case 1 :
199
		case 1 : a+=k[0];
204
			a += k[0];
200
				 final(a,b,c);
205
			final(a, b, c);
-
 
206
 
201
		case 0:     /* case 0: nothing left to add */
207
		case 0:     /* case 0: nothing left to add */
202
		break;
208
			break;
203
	}
209
	}
-
 
210
 
204
	/*------------------------------------------------------ report the result */
211
	/*------------------------------------------------------ report the result */
205
	return c;
212
	return c;
206
}
213
}
207
 
214
 
208
/*
215
/*
209
 * --------------------------------------------------------------------
216
 * --------------------------------------------------------------------
210
 * hashword2() -- same as hashword(), but take two seeds and return two
217
 * hashword2() -- same as hashword(), but take two seeds and return two
211
 * 32-bit values.  pc and pb must both be nonnull, and *pc and *pb must
218
 * 32-bit values.  pc and pb must both be nonnull, and *pc and *pb must
212
 * both be initialized with seeds.  If you pass in (*pb)==0, the output 
219
 * both be initialized with seeds.  If you pass in (*pb)==0, the output
213
 * (*pc) will be the same as the return value from hashword().
220
 * (*pc) will be the same as the return value from hashword().
214
 * --------------------------------------------------------------------
221
 * --------------------------------------------------------------------
215
 */
222
 */
216
void hashword2 (
223
void hashword2 (
217
	const uint32_t *k,                   /* the key, an array of uint32_t values */
224
    const uint32_t *k,                   /* the key, an array of uint32_t values */
218
	size_t          length,               /* the length of the key, in uint32_ts */
225
    size_t          length,               /* the length of the key, in uint32_ts */
219
	uint32_t       *pc,                      /* IN: seed OUT: primary hash value */
226
    uint32_t       *pc,                      /* IN: seed OUT: primary hash value */
220
	uint32_t       *pb)               /* IN: more seed OUT: secondary hash value */
227
    uint32_t       *pb)               /* IN: more seed OUT: secondary hash value */
221
{
228
{
222
	uint32_t a,b,c;
229
	uint32_t a, b, c;
223
 
230
 
224
	/* Set up the internal state */
231
	/* Set up the internal state */
225
	a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
232
	a = b = c = 0xdeadbeef + ((uint32_t)(length << 2)) + *pc;
226
	c += *pb;
233
	c += *pb;
227
 
234
 
228
	/*------------------------------------------------- handle most of the key */
235
	/*------------------------------------------------- handle most of the key */
229
	while (length > 3)
236
	while (length > 3)
230
	{
237
	{
231
		a += k[0];
238
		a += k[0];
232
		b += k[1];
239
		b += k[1];
233
		c += k[2];
240
		c += k[2];
234
		mix(a,b,c);
241
		mix(a, b, c);
235
		length -= 3;
242
		length -= 3;
236
		k += 3;
243
		k += 3;
237
	}
244
	}
238
	 
245
 
239
	/*------------------------------------------- handle the last 3 uint32_t's */
246
	/*------------------------------------------- handle the last 3 uint32_t's */
240
	switch(length)                     /* all the case statements fall through */
247
	switch (length)                    /* all the case statements fall through */
241
	{ 
248
	{
-
 
249
		case 3:
242
		case 3: c+=k[2];
250
			c += k[2];
-
 
251
 
-
 
252
		case 2:
243
		case 2: b+=k[1];
253
			b += k[1];
-
 
254
 
-
 
255
		case 1:
244
		case 1: a+=k[0];
256
			a += k[0];
245
				final(a,b,c);
257
			final(a, b, c);
-
 
258
 
246
		case 0:     /* case 0: nothing left to add */
259
		case 0:     /* case 0: nothing left to add */
247
			break;
260
			break;
248
	}
261
	}
-
 
262
 
249
	/*------------------------------------------------------ report the result */
263
	/*------------------------------------------------------ report the result */
250
 
264
 
251
	*pc=c;
265
	*pc = c;
252
	*pb=b;
266
	*pb = b;
253
}
267
}
254
 
268
 
255
/*
269
/*
256
 * -------------------------------------------------------------------------------
270
 * -------------------------------------------------------------------------------
257
 * hashlittle() -- hash a variable-length key into a 32-bit value
271
 * hashlittle() -- hash a variable-length key into a 32-bit value
Line 259... Line 273...
259
 *  length  : the length of the key, counting by bytes
273
 *  length  : the length of the key, counting by bytes
260
 *  initval : can be any 4-byte value
274
 *  initval : can be any 4-byte value
261
 * Returns a 32-bit value.  Every bit of the key affects every bit of
275
 * Returns a 32-bit value.  Every bit of the key affects every bit of
262
 * the return value.  Two keys differing by one or two bits will have
276
 * the return value.  Two keys differing by one or two bits will have
263
 * totally different hash values.
277
 * totally different hash values.
264
 * 
278
 *
265
 * The best hash table sizes are powers of 2.  There is no need to do
279
 * The best hash table sizes are powers of 2.  There is no need to do
266
 * mod a prime (mod is sooo slow!).  If you need less than 32 bits,
280
 * mod a prime (mod is sooo slow!).  If you need less than 32 bits,
267
 * use a bitmask.  For example, if you need only 10 bits, do
281
 * use a bitmask.  For example, if you need only 10 bits, do
268
 *  h = (h & hashmask(10));
282
 *  h = (h & hashmask(10));
269
 * In which case, the hash table should have hashsize(10) elements.
283
 * In which case, the hash table should have hashsize(10) elements.
270
 * 
284
 *
271
 * If you are hashing n strings (uint8_t **)k, do it like this:
285
 * If you are hashing n strings (uint8_t **)k, do it like this:
272
 *  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
286
 *  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
273
 * 
287
 *
274
 * By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
288
 * By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
275
 * code any way you wish, private, educational, or commercial.  It's free.
289
 * code any way you wish, private, educational, or commercial.  It's free.
276
 * 
290
 *
277
 * Use for hash table lookup, or anything where one collision in 2^^32 is
291
 * Use for hash table lookup, or anything where one collision in 2^^32 is
278
 * acceptable.  Do NOT use for cryptographic purposes.
292
 * acceptable.  Do NOT use for cryptographic purposes.
279
 * -------------------------------------------------------------------------------
293
 * -------------------------------------------------------------------------------
280
 */
294
 */
281
 
295
 
282
uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
296
uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
283
{
297
{
284
	uint32_t a,b,c;                                          /* internal state */
298
	uint32_t a, b, c;                                        /* internal state */
-
 
299
	union
-
 
300
	{
-
 
301
		const void *ptr;
-
 
302
		size_t i;
285
	union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
303
	} u;     /* needed for Mac Powerbook G4 */
286
 
304
 
287
     /* Set up the internal state */
305
	/* Set up the internal state */
288
	 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
306
	a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
289
	 u.ptr = key;
307
	u.ptr = key;
290
 
308
 
291
	if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0))
309
	if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0))
292
	{
310
	{
293
		const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
311
		const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
294
		const uint8_t  *k8;
312
		const uint8_t  *k8;
Line 297... Line 315...
297
		while (length > 12)
315
		while (length > 12)
298
		{
316
		{
299
			a += k[0];
317
			a += k[0];
300
			b += k[1];
318
			b += k[1];
301
			c += k[2];
319
			c += k[2];
302
			mix(a,b,c);
320
			mix(a, b, c);
303
			length -= 12;
321
			length -= 12;
304
			k += 3;
322
			k += 3;
305
		}
323
		}
306
 
324
 
307
		/*----------------------------- handle the last (probably partial) block */
325
		/*----------------------------- handle the last (probably partial) block */
308
		/* 
326
		/*
309
		 * "k[2]&0xffffff" actually reads beyond the end of the string, but
327
		 * "k[2]&0xffffff" actually reads beyond the end of the string, but
310
		 * then masks off the part it's not allowed to read.  Because the
328
		 * then masks off the part it's not allowed to read.  Because the
311
		 * string is aligned, the masked-off tail is in the same word as the
329
		 * string is aligned, the masked-off tail is in the same word as the
312
		 * rest of the string.  Every machine with memory protection I've seen
330
		 * rest of the string.  Every machine with memory protection I've seen
313
		 * does it on word boundaries, so is OK with this.  But VALGRIND will
331
		 * does it on word boundaries, so is OK with this.  But VALGRIND will
314
		 * still catch it and complain.  The masking trick does make the hash
332
		 * still catch it and complain.  The masking trick does make the hash
315
		 * noticably faster for short strings (like English words).
333
		 * noticably faster for short strings (like English words).
316
		 */
334
		 */
317
#ifndef VALGRIND
335
#ifndef VALGRIND
318
 
336
 
319
		switch(length)
337
		switch (length)
320
		{
338
		{
-
 
339
			case 12:
-
 
340
				c += k[2];
-
 
341
				b += k[1];
321
			case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
342
				a += k[0];
-
 
343
				break;
-
 
344
 
-
 
345
			case 11:
322
			case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
346
				c += k[2] & 0xffffff;
-
 
347
				b += k[1];
-
 
348
				a += k[0];
-
 
349
				break;
-
 
350
 
-
 
351
			case 10:
323
			case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
352
				c += k[2] & 0xffff;
-
 
353
				b += k[1];
-
 
354
				a += k[0];
-
 
355
				break;
-
 
356
 
-
 
357
			case 9 :
324
			case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
358
				c += k[2] & 0xff;
-
 
359
				b += k[1];
-
 
360
				a += k[0];
-
 
361
				break;
-
 
362
 
-
 
363
			case 8 :
-
 
364
				b += k[1];
325
			case 8 : b+=k[1]; a+=k[0]; break;
365
				a += k[0];
-
 
366
				break;
-
 
367
 
-
 
368
			case 7 :
326
			case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
369
				b += k[1] & 0xffffff;
-
 
370
				a += k[0];
-
 
371
				break;
-
 
372
 
-
 
373
			case 6 :
327
			case 6 : b+=k[1]&0xffff; a+=k[0]; break;
374
				b += k[1] & 0xffff;
-
 
375
				a += k[0];
-
 
376
				break;
-
 
377
 
-
 
378
			case 5 :
328
			case 5 : b+=k[1]&0xff; a+=k[0]; break;
379
				b += k[1] & 0xff;
-
 
380
				a += k[0];
-
 
381
				break;
-
 
382
 
-
 
383
			case 4 :
329
			case 4 : a+=k[0]; break;
384
				a += k[0];
-
 
385
				break;
-
 
386
 
-
 
387
			case 3 :
330
			case 3 : a+=k[0]&0xffffff; break;
388
				a += k[0] & 0xffffff;
-
 
389
				break;
-
 
390
 
-
 
391
			case 2 :
331
			case 2 : a+=k[0]&0xffff; break;
392
				a += k[0] & 0xffff;
-
 
393
				break;
-
 
394
 
-
 
395
			case 1 :
332
			case 1 : a+=k[0]&0xff; break;
396
				a += k[0] & 0xff;
-
 
397
				break;
-
 
398
 
-
 
399
			case 0 :
333
			case 0 : return c;              /* zero length strings require no mixing */
400
				return c;              /* zero length strings require no mixing */
334
		}
401
		}
-
 
402
 
335
#else /* make valgrind happy */
403
#else /* make valgrind happy */
336
		k8 = (const uint8_t *)k;
404
		k8 = (const uint8_t *)k;
-
 
405
 
337
		switch(length)
406
		switch (length)
338
		{
407
		{
-
 
408
			case 12:
-
 
409
				c += k[2];
-
 
410
				b += k[1];
339
			case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
411
				a += k[0];
-
 
412
				break;
-
 
413
 
-
 
414
			case 11:
340
			case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
415
				c += ((uint32_t)k8[10]) << 16; /* fall through */
-
 
416
 
-
 
417
			case 10:
341
			case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
418
				c += ((uint32_t)k8[9]) << 8; /* fall through */
-
 
419
 
-
 
420
			case 9 :
342
			case 9 : c+=k8[8];                   /* fall through */
421
				c += k8[8];                 /* fall through */
-
 
422
 
-
 
423
			case 8 :
-
 
424
				b += k[1];
343
			case 8 : b+=k[1]; a+=k[0]; break;
425
				a += k[0];
-
 
426
				break;
-
 
427
 
-
 
428
			case 7 :
344
			case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
429
				b += ((uint32_t)k8[6]) << 16; /* fall through */
-
 
430
 
-
 
431
			case 6 :
345
			case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
432
				b += ((uint32_t)k8[5]) << 8; /* fall through */
-
 
433
 
-
 
434
			case 5 :
346
			case 5 : b+=k8[4];                   /* fall through */
435
				b += k8[4];                 /* fall through */
-
 
436
 
-
 
437
			case 4 :
347
			case 4 : a+=k[0]; break;
438
				a += k[0];
-
 
439
				break;
-
 
440
 
-
 
441
			case 3 :
348
			case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
442
				a += ((uint32_t)k8[2]) << 16; /* fall through */
-
 
443
 
-
 
444
			case 2 :
349
			case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
445
				a += ((uint32_t)k8[1]) << 8; /* fall through */
-
 
446
 
-
 
447
			case 1 :
350
			case 1 : a+=k8[0]; break;
448
				a += k8[0];
-
 
449
				break;
-
 
450
 
-
 
451
			case 0 :
351
			case 0 : return c;
452
				return c;
352
		}
453
		}
-
 
454
 
353
#endif /* !valgrind */
455
#endif /* !valgrind */
-
 
456
	}
354
	} else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0))
457
	else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0))
355
	{
458
	{
356
		const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
459
		const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
357
		const uint8_t  *k8;
460
		const uint8_t  *k8;
358
 
461
 
359
		/*--------------- all but last block: aligned reads and different mixing */
462
		/*--------------- all but last block: aligned reads and different mixing */
360
		while (length > 12)
463
		while (length > 12)
361
		{
464
		{
362
			a += k[0] + (((uint32_t)k[1])<<16);
465
			a += k[0] + (((uint32_t)k[1]) << 16);
363
			b += k[2] + (((uint32_t)k[3])<<16);
466
			b += k[2] + (((uint32_t)k[3]) << 16);
364
			c += k[4] + (((uint32_t)k[5])<<16);
467
			c += k[4] + (((uint32_t)k[5]) << 16);
365
			mix(a,b,c);
468
			mix(a, b, c);
366
			length -= 12;
469
			length -= 12;
367
			k += 6;
470
			k += 6;
368
		}
471
		}
-
 
472
 
369
		/*----------------------------- handle the last (probably partial) block */
473
		/*----------------------------- handle the last (probably partial) block */
370
		k8 = (const uint8_t *)k;
474
		k8 = (const uint8_t *)k;
371
 
475
 
372
		switch(length)
476
		switch (length)
373
		{
477
		{
374
			case 12:
478
			case 12:
375
				c+=k[4]+(((uint32_t)k[5])<<16);
479
				c += k[4] + (((uint32_t)k[5]) << 16);
376
				b+=k[2]+(((uint32_t)k[3])<<16);
480
				b += k[2] + (((uint32_t)k[3]) << 16);
377
				a+=k[0]+(((uint32_t)k[1])<<16);
481
				a += k[0] + (((uint32_t)k[1]) << 16);
378
			break;
482
				break;
379
 
483
 
-
 
484
			case 11:
380
			case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
485
				c += ((uint32_t)k8[10]) << 16; /* fall through */
381
 
486
 
382
			case 10:
487
			case 10:
383
				c+=k[4];
488
				c += k[4];
384
				b+=k[2]+(((uint32_t)k[3])<<16);
489
				b += k[2] + (((uint32_t)k[3]) << 16);
385
				a+=k[0]+(((uint32_t)k[1])<<16);
490
				a += k[0] + (((uint32_t)k[1]) << 16);
386
			break;
491
				break;
387
 
492
 
-
 
493
			case 9 :
388
			case 9 : c+=k8[8];                      /* fall through */
494
				c += k8[8];                    /* fall through */
389
 
495
 
390
			case 8 :
496
			case 8 :
391
				b+=k[2]+(((uint32_t)k[3])<<16);
497
				b += k[2] + (((uint32_t)k[3]) << 16);
392
				a+=k[0]+(((uint32_t)k[1])<<16);
498
				a += k[0] + (((uint32_t)k[1]) << 16);
393
			break;
499
				break;
394
 
500
 
-
 
501
			case 7 :
395
			case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
502
				b += ((uint32_t)k8[6]) << 16;  /* fall through */
396
 
503
 
397
			case 6 :
504
			case 6 :
398
				b+=k[2];
505
				b += k[2];
-
 
506
				a += k[0] + (((uint32_t)k[1]) << 16);
-
 
507
				break;
-
 
508
 
-
 
509
			case 5 :
-
 
510
				b += k8[4];                    /* fall through */
-
 
511
 
-
 
512
			case 4 :
399
				a+=k[0]+(((uint32_t)k[1])<<16);
513
				a += k[0] + (((uint32_t)k[1]) << 16);
-
 
514
				break;
-
 
515
 
-
 
516
			case 3 :
-
 
517
				a += ((uint32_t)k8[2]) << 16;  /* fall through */
-
 
518
 
-
 
519
			case 2 :
-
 
520
				a += k[0];
-
 
521
				break;
-
 
522
 
-
 
523
			case 1 :
-
 
524
				a += k8[0];
400
			break;
525
				break;
401
 
526
 
402
			case 5 : b+=k8[4];                      /* fall through */
-
 
403
			case 4 : a+=k[0]+(((uint32_t)k[1])<<16); break;
-
 
404
			case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
-
 
405
			case 2 : a+=k[0]; break;
527
			case 0 :
406
			case 1 : a+=k8[0]; break;
-
 
407
			case 0 : return c;                     /* zero length requires no mixing */
528
				return c;                     /* zero length requires no mixing */
408
		}
529
		}
409
	}
530
	}
410
	else                        /* need to read the key one byte at a time */
531
	else                        /* need to read the key one byte at a time */
411
	{
532
	{
412
		const uint8_t *k = (const uint8_t *)key;
533
		const uint8_t *k = (const uint8_t *)key;
413
 
534
 
414
		/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
535
		/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
415
		while (length > 12)
536
		while (length > 12)
416
		{
537
		{
417
			a += k[0];
538
			a += k[0];
418
			a += ((uint32_t)k[1])<<8;
539
			a += ((uint32_t)k[1]) << 8;
419
			a += ((uint32_t)k[2])<<16;
540
			a += ((uint32_t)k[2]) << 16;
420
			a += ((uint32_t)k[3])<<24;
541
			a += ((uint32_t)k[3]) << 24;
421
			b += k[4];
542
			b += k[4];
422
			b += ((uint32_t)k[5])<<8;
543
			b += ((uint32_t)k[5]) << 8;
423
			b += ((uint32_t)k[6])<<16;
544
			b += ((uint32_t)k[6]) << 16;
424
			b += ((uint32_t)k[7])<<24;
545
			b += ((uint32_t)k[7]) << 24;
425
			c += k[8];
546
			c += k[8];
426
			c += ((uint32_t)k[9])<<8;
547
			c += ((uint32_t)k[9]) << 8;
427
			c += ((uint32_t)k[10])<<16;
548
			c += ((uint32_t)k[10]) << 16;
428
			c += ((uint32_t)k[11])<<24;
549
			c += ((uint32_t)k[11]) << 24;
429
			mix(a,b,c);
550
			mix(a, b, c);
430
			length -= 12;
551
			length -= 12;
431
			k += 12;
552
			k += 12;
432
		}
553
		}
433
 
554
 
434
	    /*-------------------------------- last block: affect all 32 bits of (c) */
555
		/*-------------------------------- last block: affect all 32 bits of (c) */
435
		switch(length)                   /* all the case statements fall through */
556
		switch (length)                  /* all the case statements fall through */
436
		{
557
		{
-
 
558
			case 12:
437
			case 12: c+=((uint32_t)k[11])<<24;
559
				c += ((uint32_t)k[11]) << 24;
-
 
560
 
-
 
561
			case 11:
438
			case 11: c+=((uint32_t)k[10])<<16;
562
				c += ((uint32_t)k[10]) << 16;
-
 
563
 
-
 
564
			case 10:
439
			case 10: c+=((uint32_t)k[9])<<8;
565
				c += ((uint32_t)k[9]) << 8;
-
 
566
 
440
			case 9 : c+=k[8];
567
			case 9 :
-
 
568
				c += k[8];
-
 
569
 
-
 
570
			case 8 :
441
			case 8 : b+=((uint32_t)k[7])<<24;
571
				b += ((uint32_t)k[7]) << 24;
-
 
572
 
-
 
573
			case 7 :
442
			case 7 : b+=((uint32_t)k[6])<<16;
574
				b += ((uint32_t)k[6]) << 16;
-
 
575
 
-
 
576
			case 6 :
443
			case 6 : b+=((uint32_t)k[5])<<8;
577
				b += ((uint32_t)k[5]) << 8;
-
 
578
 
444
			case 5 : b+=k[4];
579
			case 5 :
-
 
580
				b += k[4];
-
 
581
 
-
 
582
			case 4 :
445
			case 4 : a+=((uint32_t)k[3])<<24;
583
				a += ((uint32_t)k[3]) << 24;
-
 
584
 
-
 
585
			case 3 :
446
			case 3 : a+=((uint32_t)k[2])<<16;
586
				a += ((uint32_t)k[2]) << 16;
-
 
587
 
-
 
588
			case 2 :
447
			case 2 : a+=((uint32_t)k[1])<<8;
589
				a += ((uint32_t)k[1]) << 8;
-
 
590
 
-
 
591
			case 1 :
448
			case 1 : a+=k[0];
592
				a += k[0];
449
			break;
593
				break;
-
 
594
 
-
 
595
			case 0 :
450
			case 0 : return c;
596
				return c;
451
		}
597
		}
452
	}
598
	}
453
 
599
 
454
	final(a,b,c);
600
	final(a, b, c);
455
	return c;
601
	return c;
456
}
602
}
457
 
603
 
458
/*
604
/*
459
 * hashlittle2: return 2 32-bit hash values
605
 * hashlittle2: return 2 32-bit hash values
Line 464... Line 610...
464
 * happy with the first, or if you want a probably-unique 64-bit ID for
610
 * happy with the first, or if you want a probably-unique 64-bit ID for
465
 * the key.  *pc is better mixed than *pb, so use *pc first.  If you want
611
 * the key.  *pc is better mixed than *pb, so use *pc first.  If you want
466
 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
612
 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
467
 */
613
 */
468
void hashlittle2(
614
void hashlittle2(
469
		const void *key,       /* the key to hash */
615
    const void *key,       /* the key to hash */
470
		size_t      length,    /* length of the key */
616
    size_t      length,    /* length of the key */
471
		uint32_t   *pc,        /* IN: primary initval, OUT: primary hash */
617
    uint32_t   *pc,        /* IN: primary initval, OUT: primary hash */
472
		uint32_t   *pb)        /* IN: secondary initval, OUT: secondary hash */
618
    uint32_t   *pb)        /* IN: secondary initval, OUT: secondary hash */
473
{
619
{
474
	uint32_t a,b,c;                                          /* internal state */
620
	uint32_t a, b, c;                                        /* internal state */
-
 
621
	union
-
 
622
	{
-
 
623
		const void *ptr;
-
 
624
		size_t i;
475
	union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
625
	} u;     /* needed for Mac Powerbook G4 */
476
 
626
 
477
	/* Set up the internal state */
627
	/* Set up the internal state */
478
	a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
628
	a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
479
	c += *pb;
629
	c += *pb;
480
 
630
 
Line 489... Line 639...
489
		while (length > 12)
639
		while (length > 12)
490
		{
640
		{
491
			a += k[0];
641
			a += k[0];
492
			b += k[1];
642
			b += k[1];
493
			c += k[2];
643
			c += k[2];
494
			mix(a,b,c);
644
			mix(a, b, c);
495
			length -= 12;
645
			length -= 12;
496
			k += 3;
646
			k += 3;
497
		}
647
		}
498
 
648
 
499
		/*----------------------------- handle the last (probably partial) block */
649
		/*----------------------------- handle the last (probably partial) block */
500
		/* 
650
		/*
501
		 * "k[2]&0xffffff" actually reads beyond the end of the string, but
651
		 * "k[2]&0xffffff" actually reads beyond the end of the string, but
502
		 * then masks off the part it's not allowed to read.  Because the
652
		 * then masks off the part it's not allowed to read.  Because the
503
		 * string is aligned, the masked-off tail is in the same word as the
653
		 * string is aligned, the masked-off tail is in the same word as the
504
		 * rest of the string.  Every machine with memory protection I've seen
654
		 * rest of the string.  Every machine with memory protection I've seen
505
		 * does it on word boundaries, so is OK with this.  But VALGRIND will
655
		 * does it on word boundaries, so is OK with this.  But VALGRIND will
506
		 * still catch it and complain.  The masking trick does make the hash
656
		 * still catch it and complain.  The masking trick does make the hash
507
		 * noticably faster for short strings (like English words).
657
		 * noticably faster for short strings (like English words).
508
		 */
658
		 */
509
#ifndef VALGRIND
659
#ifndef VALGRIND
-
 
660
 
510
		switch(length)
661
		switch (length)
511
		{
662
		{
-
 
663
			case 12:
-
 
664
				c += k[2];
-
 
665
				b += k[1];
512
			case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
666
				a += k[0];
-
 
667
				break;
-
 
668
 
-
 
669
			case 11:
513
			case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
670
				c += k[2] & 0xffffff;
-
 
671
				b += k[1];
-
 
672
				a += k[0];
-
 
673
				break;
-
 
674
 
-
 
675
			case 10:
514
			case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
676
				c += k[2] & 0xffff;
-
 
677
				b += k[1];
-
 
678
				a += k[0];
-
 
679
				break;
-
 
680
 
-
 
681
			case 9 :
515
			case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
682
				c += k[2] & 0xff;
-
 
683
				b += k[1];
-
 
684
				a += k[0];
-
 
685
				break;
-
 
686
 
-
 
687
			case 8 :
-
 
688
				b += k[1];
516
			case 8 : b+=k[1]; a+=k[0]; break;
689
				a += k[0];
-
 
690
				break;
-
 
691
 
-
 
692
			case 7 :
517
			case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
693
				b += k[1] & 0xffffff;
-
 
694
				a += k[0];
-
 
695
				break;
-
 
696
 
-
 
697
			case 6 :
518
			case 6 : b+=k[1]&0xffff; a+=k[0]; break;
698
				b += k[1] & 0xffff;
-
 
699
				a += k[0];
-
 
700
				break;
-
 
701
 
-
 
702
			case 5 :
519
			case 5 : b+=k[1]&0xff; a+=k[0]; break;
703
				b += k[1] & 0xff;
-
 
704
				a += k[0];
-
 
705
				break;
-
 
706
 
-
 
707
			case 4 :
520
			case 4 : a+=k[0]; break;
708
				a += k[0];
-
 
709
				break;
-
 
710
 
-
 
711
			case 3 :
521
			case 3 : a+=k[0]&0xffffff; break;
712
				a += k[0] & 0xffffff;
-
 
713
				break;
-
 
714
 
-
 
715
			case 2 :
522
			case 2 : a+=k[0]&0xffff; break;
716
				a += k[0] & 0xffff;
-
 
717
				break;
-
 
718
 
-
 
719
			case 1 :
523
			case 1 : a+=k[0]&0xff; break;
720
				a += k[0] & 0xff;
-
 
721
				break;
-
 
722
 
-
 
723
			case 0 :
-
 
724
				*pc = c;
-
 
725
				*pb = b;
524
			case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
726
				return;  /* zero length strings require no mixing */
525
		}
727
		}
-
 
728
 
526
#else /* make valgrind happy */
729
#else /* make valgrind happy */
527
		k8 = (const uint8_t *)k;
730
		k8 = (const uint8_t *)k;
-
 
731
 
528
		switch(length)
732
		switch (length)
529
		{
733
		{
-
 
734
			case 12:
-
 
735
				c += k[2];
-
 
736
				b += k[1];
530
			case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
737
				a += k[0];
-
 
738
				break;
-
 
739
 
-
 
740
			case 11:
531
			case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
741
				c += ((uint32_t)k8[10]) << 16; /* fall through */
-
 
742
 
-
 
743
			case 10:
532
			case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
744
				c += ((uint32_t)k8[9]) << 8; /* fall through */
-
 
745
 
-
 
746
			case 9 :
533
			case 9 : c+=k8[8];                   /* fall through */
747
				c += k8[8];                 /* fall through */
-
 
748
 
-
 
749
			case 8 :
-
 
750
				b += k[1];
534
			case 8 : b+=k[1]; a+=k[0]; break;
751
				a += k[0];
-
 
752
				break;
-
 
753
 
-
 
754
			case 7 :
535
			case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
755
				b += ((uint32_t)k8[6]) << 16; /* fall through */
-
 
756
 
-
 
757
			case 6 :
536
			case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
758
				b += ((uint32_t)k8[5]) << 8; /* fall through */
-
 
759
 
-
 
760
			case 5 :
537
			case 5 : b+=k8[4];                   /* fall through */
761
				b += k8[4];                 /* fall through */
-
 
762
 
-
 
763
			case 4 :
538
			case 4 : a+=k[0]; break;
764
				a += k[0];
-
 
765
				break;
-
 
766
 
-
 
767
			case 3 :
539
			case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
768
				a += ((uint32_t)k8[2]) << 16; /* fall through */
-
 
769
 
-
 
770
			case 2 :
540
			case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
771
				a += ((uint32_t)k8[1]) << 8; /* fall through */
-
 
772
 
-
 
773
			case 1 :
541
			case 1 : a+=k8[0]; break;
774
				a += k8[0];
-
 
775
				break;
-
 
776
 
-
 
777
			case 0 :
-
 
778
				*pc = c;
-
 
779
				*pb = b;
542
			case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
780
				return;  /* zero length strings require no mixing */
543
		}
781
		}
-
 
782
 
544
#endif /* !valgrind */
783
#endif /* !valgrind */
545
 
784
 
546
	}
785
	}
547
	else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0))
786
	else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0))
548
	{
787
	{
Line 550... Line 789...
550
		const uint8_t  *k8;
789
		const uint8_t  *k8;
551
 
790
 
552
		/*--------------- all but last block: aligned reads and different mixing */
791
		/*--------------- all but last block: aligned reads and different mixing */
553
		while (length > 12)
792
		while (length > 12)
554
		{
793
		{
555
			a += k[0] + (((uint32_t)k[1])<<16);
794
			a += k[0] + (((uint32_t)k[1]) << 16);
556
			b += k[2] + (((uint32_t)k[3])<<16);
795
			b += k[2] + (((uint32_t)k[3]) << 16);
557
			c += k[4] + (((uint32_t)k[5])<<16);
796
			c += k[4] + (((uint32_t)k[5]) << 16);
558
			mix(a,b,c);
797
			mix(a, b, c);
559
			length -= 12;
798
			length -= 12;
560
			k += 6;
799
			k += 6;
561
		}
800
		}
562
 
801
 
563
	    /*----------------------------- handle the last (probably partial) block */
802
		/*----------------------------- handle the last (probably partial) block */
564
		k8 = (const uint8_t *)k;
803
		k8 = (const uint8_t *)k;
-
 
804
 
565
		switch(length)
805
		switch (length)
566
		{
806
		{
567
			case 12:
807
			case 12:
568
				c+=k[4]+(((uint32_t)k[5])<<16);
808
				c += k[4] + (((uint32_t)k[5]) << 16);
569
				b+=k[2]+(((uint32_t)k[3])<<16);
809
				b += k[2] + (((uint32_t)k[3]) << 16);
570
				a+=k[0]+(((uint32_t)k[1])<<16);
810
				a += k[0] + (((uint32_t)k[1]) << 16);
571
			break;
811
				break;
572
 
812
 
-
 
813
			case 11:
573
			case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
814
				c += ((uint32_t)k8[10]) << 16; /* fall through */
574
 
815
 
575
			case 10:
816
			case 10:
576
				c+=k[4];
817
				c += k[4];
577
				b+=k[2]+(((uint32_t)k[3])<<16);
818
				b += k[2] + (((uint32_t)k[3]) << 16);
578
				a+=k[0]+(((uint32_t)k[1])<<16);
819
				a += k[0] + (((uint32_t)k[1]) << 16);
579
			break;
820
				break;
580
 
821
 
-
 
822
			case 9 :
581
			case 9 : c+=k8[8];                      /* fall through */
823
				c += k8[8];                    /* fall through */
582
 
824
 
583
			case 8 :
825
			case 8 :
584
				b+=k[2]+(((uint32_t)k[3])<<16);
826
				b += k[2] + (((uint32_t)k[3]) << 16);
585
				a+=k[0]+(((uint32_t)k[1])<<16);
827
				a += k[0] + (((uint32_t)k[1]) << 16);
586
			break;
828
				break;
587
 
829
 
-
 
830
			case 7 :
588
			case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
831
				b += ((uint32_t)k8[6]) << 16;  /* fall through */
589
 
832
 
590
			case 6 :
833
			case 6 :
591
				b+=k[2];
834
				b += k[2];
592
				a+=k[0]+(((uint32_t)k[1])<<16);
835
				a += k[0] + (((uint32_t)k[1]) << 16);
593
			break;
836
				break;
594
 
837
 
-
 
838
			case 5 :
595
			case 5 : b+=k8[4];                      /* fall through */
839
				b += k8[4];                    /* fall through */
-
 
840
 
-
 
841
			case 4 :
596
			case 4 : a+=k[0]+(((uint32_t)k[1])<<16); break;
842
				a += k[0] + (((uint32_t)k[1]) << 16);
-
 
843
				break;
-
 
844
 
-
 
845
			case 3 :
597
			case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
846
				a += ((uint32_t)k8[2]) << 16;  /* fall through */
-
 
847
 
-
 
848
			case 2 :
598
			case 2 : a+=k[0]; break;
849
				a += k[0];
-
 
850
				break;
-
 
851
 
-
 
852
			case 1 :
599
			case 1 : a+=k8[0]; break;
853
				a += k8[0];
-
 
854
				break;
-
 
855
 
-
 
856
			case 0 :
-
 
857
				*pc = c;
-
 
858
				*pb = b;
600
			case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
859
				return;  /* zero length strings require no mixing */
601
		}
860
		}
602
	}
861
	}
603
	else                        /* need to read the key one byte at a time */
862
	else                        /* need to read the key one byte at a time */
604
	{
863
	{
605
		const uint8_t *k = (const uint8_t *)key;
864
		const uint8_t *k = (const uint8_t *)key;
606
		 
865
 
607
		/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
866
		/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
608
		while (length > 12)
867
		while (length > 12)
609
		{
868
		{
610
			a += k[0];
869
			a += k[0];
611
			a += ((uint32_t)k[1])<<8;
870
			a += ((uint32_t)k[1]) << 8;
612
			a += ((uint32_t)k[2])<<16;
871
			a += ((uint32_t)k[2]) << 16;
613
			a += ((uint32_t)k[3])<<24;
872
			a += ((uint32_t)k[3]) << 24;
614
			b += k[4];
873
			b += k[4];
615
			b += ((uint32_t)k[5])<<8;
874
			b += ((uint32_t)k[5]) << 8;
616
			b += ((uint32_t)k[6])<<16;
875
			b += ((uint32_t)k[6]) << 16;
617
			b += ((uint32_t)k[7])<<24;
876
			b += ((uint32_t)k[7]) << 24;
618
			c += k[8];
877
			c += k[8];
619
			c += ((uint32_t)k[9])<<8;
878
			c += ((uint32_t)k[9]) << 8;
620
			c += ((uint32_t)k[10])<<16;
879
			c += ((uint32_t)k[10]) << 16;
621
			c += ((uint32_t)k[11])<<24;
880
			c += ((uint32_t)k[11]) << 24;
622
			mix(a,b,c);
881
			mix(a, b, c);
623
			length -= 12;
882
			length -= 12;
624
			k += 12;
883
			k += 12;
625
		}
884
		}
626
 
885
 
627
		/*-------------------------------- last block: affect all 32 bits of (c) */
886
		/*-------------------------------- last block: affect all 32 bits of (c) */
628
		switch(length)                   /* all the case statements fall through */
887
		switch (length)                  /* all the case statements fall through */
629
		{
888
		{
-
 
889
			case 12:
630
			case 12: c+=((uint32_t)k[11])<<24;
890
				c += ((uint32_t)k[11]) << 24;
-
 
891
 
-
 
892
			case 11:
631
			case 11: c+=((uint32_t)k[10])<<16;
893
				c += ((uint32_t)k[10]) << 16;
-
 
894
 
-
 
895
			case 10:
632
			case 10: c+=((uint32_t)k[9])<<8;
896
				c += ((uint32_t)k[9]) << 8;
-
 
897
 
633
			case 9 : c+=k[8];
898
			case 9 :
-
 
899
				c += k[8];
-
 
900
 
-
 
901
			case 8 :
634
			case 8 : b+=((uint32_t)k[7])<<24;
902
				b += ((uint32_t)k[7]) << 24;
-
 
903
 
-
 
904
			case 7 :
635
			case 7 : b+=((uint32_t)k[6])<<16;
905
				b += ((uint32_t)k[6]) << 16;
-
 
906
 
-
 
907
			case 6 :
636
			case 6 : b+=((uint32_t)k[5])<<8;
908
				b += ((uint32_t)k[5]) << 8;
-
 
909
 
637
			case 5 : b+=k[4];
910
			case 5 :
-
 
911
				b += k[4];
-
 
912
 
-
 
913
			case 4 :
638
			case 4 : a+=((uint32_t)k[3])<<24;
914
				a += ((uint32_t)k[3]) << 24;
-
 
915
 
-
 
916
			case 3 :
639
			case 3 : a+=((uint32_t)k[2])<<16;
917
				a += ((uint32_t)k[2]) << 16;
-
 
918
 
-
 
919
			case 2 :
640
			case 2 : a+=((uint32_t)k[1])<<8;
920
				a += ((uint32_t)k[1]) << 8;
-
 
921
 
-
 
922
			case 1 :
641
			case 1 : a+=k[0]; break;
923
				a += k[0];
-
 
924
				break;
-
 
925
 
-
 
926
			case 0 :
-
 
927
				*pc = c;
-
 
928
				*pb = b;
642
			case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
929
				return;  /* zero length strings require no mixing */
643
		}
930
		}
644
	}
931
	}
645
 
932
 
646
	final(a,b,c);
933
	final(a, b, c);
-
 
934
	*pc = c;
647
	*pc=c; *pb=b;
935
	*pb = b;
648
}
936
}
649
 
937
 
650
/*
938
/*
651
 * hashbig():
939
 * hashbig():
652
 * This is the same as hashword() on big-endian machines.  It is different
940
 * This is the same as hashword() on big-endian machines.  It is different
653
 * from hashlittle() on all machines.  hashbig() takes advantage of
941
 * from hashlittle() on all machines.  hashbig() takes advantage of
654
 * big-endian byte ordering. 
942
 * big-endian byte ordering.
655
 */
943
 */
656
uint32_t hashbig( const void *key, size_t length, uint32_t initval)
944
uint32_t hashbig( const void *key, size_t length, uint32_t initval)
657
{
945
{
658
	uint32_t a,b,c;
946
	uint32_t a, b, c;
-
 
947
	union
-
 
948
	{
-
 
949
		const void *ptr;
-
 
950
		size_t i;
659
	union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
951
	} u; /* to cast key to (size_t) happily */
660
 
952
 
661
	/* Set up the internal state */
953
	/* Set up the internal state */
662
	a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
954
	a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
663
	u.ptr = key;
955
	u.ptr = key;
664
 
956
 
Line 671... Line 963...
671
		while (length > 12)
963
		while (length > 12)
672
		{
964
		{
673
			a += k[0];
965
			a += k[0];
674
			b += k[1];
966
			b += k[1];
675
			c += k[2];
967
			c += k[2];
676
			mix(a,b,c);
968
			mix(a, b, c);
677
			length -= 12;
969
			length -= 12;
678
			k += 3;
970
			k += 3;
679
		}
971
		}
680
 
972
 
681
		/*----------------------------- handle the last (probably partial) block */
973
		/*----------------------------- handle the last (probably partial) block */
682
		/* 
974
		/*
683
		 * "k[2]<<8" actually reads beyond the end of the string, but
975
		 * "k[2]<<8" actually reads beyond the end of the string, but
684
		 * then shifts out the part it's not allowed to read.  Because the
976
		 * then shifts out the part it's not allowed to read.  Because the
685
		 * string is aligned, the illegal read is in the same word as the
977
		 * string is aligned, the illegal read is in the same word as the
686
		 * rest of the string.  Every machine with memory protection I've seen
978
		 * rest of the string.  Every machine with memory protection I've seen
687
		 * does it on word boundaries, so is OK with this.  But VALGRIND will
979
		 * does it on word boundaries, so is OK with this.  But VALGRIND will
688
		 * still catch it and complain.  The masking trick does make the hash
980
		 * still catch it and complain.  The masking trick does make the hash
689
		 * noticably faster for short strings (like English words).
981
		 * noticably faster for short strings (like English words).
690
		 */
982
		 */
691
#ifndef VALGRIND
983
#ifndef VALGRIND
-
 
984
 
692
		switch(length)
985
		switch (length)
693
		{
986
		{
-
 
987
			case 12:
-
 
988
				c += k[2];
-
 
989
				b += k[1];
694
			case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
990
				a += k[0];
-
 
991
				break;
-
 
992
 
-
 
993
			case 11:
695
			case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
994
				c += k[2] & 0xffffff00;
-
 
995
				b += k[1];
-
 
996
				a += k[0];
-
 
997
				break;
-
 
998
 
-
 
999
			case 10:
696
			case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
1000
				c += k[2] & 0xffff0000;
-
 
1001
				b += k[1];
-
 
1002
				a += k[0];
-
 
1003
				break;
-
 
1004
 
-
 
1005
			case 9 :
697
			case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
1006
				c += k[2] & 0xff000000;
-
 
1007
				b += k[1];
-
 
1008
				a += k[0];
-
 
1009
				break;
-
 
1010
 
-
 
1011
			case 8 :
-
 
1012
				b += k[1];
698
			case 8 : b+=k[1]; a+=k[0]; break;
1013
				a += k[0];
-
 
1014
				break;
-
 
1015
 
-
 
1016
			case 7 :
699
			case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
1017
				b += k[1] & 0xffffff00;
-
 
1018
				a += k[0];
-
 
1019
				break;
-
 
1020
 
-
 
1021
			case 6 :
700
			case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
1022
				b += k[1] & 0xffff0000;
-
 
1023
				a += k[0];
-
 
1024
				break;
-
 
1025
 
-
 
1026
			case 5 :
701
			case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
1027
				b += k[1] & 0xff000000;
-
 
1028
				a += k[0];
-
 
1029
				break;
-
 
1030
 
-
 
1031
			case 4 :
702
			case 4 : a+=k[0]; break;
1032
				a += k[0];
-
 
1033
				break;
-
 
1034
 
-
 
1035
			case 3 :
703
			case 3 : a+=k[0]&0xffffff00; break;
1036
				a += k[0] & 0xffffff00;
-
 
1037
				break;
-
 
1038
 
-
 
1039
			case 2 :
704
			case 2 : a+=k[0]&0xffff0000; break;
1040
				a += k[0] & 0xffff0000;
-
 
1041
				break;
-
 
1042
 
-
 
1043
			case 1 :
705
			case 1 : a+=k[0]&0xff000000; break;
1044
				a += k[0] & 0xff000000;
-
 
1045
				break;
-
 
1046
 
-
 
1047
			case 0 :
706
			case 0 : return c;              /* zero length strings require no mixing */
1048
				return c;              /* zero length strings require no mixing */
707
		}
1049
		}
-
 
1050
 
708
#else  /* make valgrind happy */
1051
#else  /* make valgrind happy */
709
		k8 = (const uint8_t *)k;
1052
		k8 = (const uint8_t *)k;
710
 
1053
 
711
		switch(length)                   /* all the case statements fall through */
1054
		switch (length)                  /* all the case statements fall through */
712
		{
1055
		{
-
 
1056
			case 12:
-
 
1057
				c += k[2];
-
 
1058
				b += k[1];
713
			case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
1059
				a += k[0];
-
 
1060
				break;
-
 
1061
 
-
 
1062
			case 11:
714
			case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
1063
				c += ((uint32_t)k8[10]) << 8; /* fall through */
-
 
1064
 
-
 
1065
			case 10:
715
			case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
1066
				c += ((uint32_t)k8[9]) << 16; /* fall through */
-
 
1067
 
-
 
1068
			case 9 :
716
			case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
1069
				c += ((uint32_t)k8[8]) << 24; /* fall through */
-
 
1070
 
-
 
1071
			case 8 :
-
 
1072
				b += k[1];
717
			case 8 : b+=k[1]; a+=k[0]; break;
1073
				a += k[0];
-
 
1074
				break;
-
 
1075
 
-
 
1076
			case 7 :
718
			case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
1077
				b += ((uint32_t)k8[6]) << 8; /* fall through */
-
 
1078
 
-
 
1079
			case 6 :
719
			case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
1080
				b += ((uint32_t)k8[5]) << 16; /* fall through */
-
 
1081
 
-
 
1082
			case 5 :
720
			case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
1083
				b += ((uint32_t)k8[4]) << 24; /* fall through */
-
 
1084
 
-
 
1085
			case 4 :
721
			case 4 : a+=k[0]; break;
1086
				a += k[0];
-
 
1087
				break;
-
 
1088
 
-
 
1089
			case 3 :
722
			case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
1090
				a += ((uint32_t)k8[2]) << 8; /* fall through */
-
 
1091
 
-
 
1092
			case 2 :
723
			case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
1093
				a += ((uint32_t)k8[1]) << 16; /* fall through */
-
 
1094
 
-
 
1095
			case 1 :
724
			case 1 : a+=((uint32_t)k8[0])<<24; break;
1096
				a += ((uint32_t)k8[0]) << 24;
-
 
1097
				break;
-
 
1098
 
-
 
1099
			case 0 :
725
			case 0 : return c;
1100
				return c;
726
	}
1101
		}
-
 
1102
 
727
#endif /* !VALGRIND */
1103
#endif /* !VALGRIND */
728
 
1104
 
729
	}
1105
	}
730
	else                        /* need to read the key one byte at a time */
1106
	else                        /* need to read the key one byte at a time */
731
	{
1107
	{
732
		const uint8_t *k = (const uint8_t *)key;
1108
		const uint8_t *k = (const uint8_t *)key;
733
	   
1109
 
734
		/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
1110
		/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
735
		while (length > 12)
1111
		while (length > 12)
736
		{
1112
		{
737
			a += ((uint32_t)k[0])<<24;
1113
			a += ((uint32_t)k[0]) << 24;
738
			a += ((uint32_t)k[1])<<16;
1114
			a += ((uint32_t)k[1]) << 16;
739
			a += ((uint32_t)k[2])<<8;
1115
			a += ((uint32_t)k[2]) << 8;
740
			a += ((uint32_t)k[3]);
1116
			a += ((uint32_t)k[3]);
741
			b += ((uint32_t)k[4])<<24;
1117
			b += ((uint32_t)k[4]) << 24;
742
			b += ((uint32_t)k[5])<<16;
1118
			b += ((uint32_t)k[5]) << 16;
743
			b += ((uint32_t)k[6])<<8;
1119
			b += ((uint32_t)k[6]) << 8;
744
			b += ((uint32_t)k[7]);
1120
			b += ((uint32_t)k[7]);
745
			c += ((uint32_t)k[8])<<24;
1121
			c += ((uint32_t)k[8]) << 24;
746
			c += ((uint32_t)k[9])<<16;
1122
			c += ((uint32_t)k[9]) << 16;
747
			c += ((uint32_t)k[10])<<8;
1123
			c += ((uint32_t)k[10]) << 8;
748
			c += ((uint32_t)k[11]);
1124
			c += ((uint32_t)k[11]);
749
			mix(a,b,c);
1125
			mix(a, b, c);
750
			length -= 12;
1126
			length -= 12;
751
			k += 12;
1127
			k += 12;
752
		}
1128
		}
753
 
1129
 
754
		/*-------------------------------- last block: affect all 32 bits of (c) */
1130
		/*-------------------------------- last block: affect all 32 bits of (c) */
755
		switch(length)                   /* all the case statements fall through */
1131
		switch (length)                  /* all the case statements fall through */
756
		{
1132
		{
-
 
1133
			case 12:
757
			case 12: c+=k[11];
1134
				c += k[11];
-
 
1135
 
-
 
1136
			case 11:
758
			case 11: c+=((uint32_t)k[10])<<8;
1137
				c += ((uint32_t)k[10]) << 8;
-
 
1138
 
-
 
1139
			case 10:
759
			case 10: c+=((uint32_t)k[9])<<16;
1140
				c += ((uint32_t)k[9]) << 16;
-
 
1141
 
-
 
1142
			case 9 :
760
			case 9 : c+=((uint32_t)k[8])<<24;
1143
				c += ((uint32_t)k[8]) << 24;
-
 
1144
 
761
			case 8 : b+=k[7];
1145
			case 8 :
-
 
1146
				b += k[7];
-
 
1147
 
-
 
1148
			case 7 :
762
			case 7 : b+=((uint32_t)k[6])<<8;
1149
				b += ((uint32_t)k[6]) << 8;
-
 
1150
 
-
 
1151
			case 6 :
763
			case 6 : b+=((uint32_t)k[5])<<16;
1152
				b += ((uint32_t)k[5]) << 16;
-
 
1153
 
-
 
1154
			case 5 :
764
			case 5 : b+=((uint32_t)k[4])<<24;
1155
				b += ((uint32_t)k[4]) << 24;
-
 
1156
 
-
 
1157
			case 4 :
765
			case 4 : a+=k[3];
1158
				a += k[3];
-
 
1159
 
-
 
1160
			case 3 :
766
			case 3 : a+=((uint32_t)k[2])<<8;
1161
				a += ((uint32_t)k[2]) << 8;
-
 
1162
 
-
 
1163
			case 2 :
767
			case 2 : a+=((uint32_t)k[1])<<16;
1164
				a += ((uint32_t)k[1]) << 16;
-
 
1165
 
-
 
1166
			case 1 :
768
			case 1 : a+=((uint32_t)k[0])<<24; break;
1167
				a += ((uint32_t)k[0]) << 24;
-
 
1168
				break;
-
 
1169
 
-
 
1170
			case 0 :
769
			case 0 : return c;
1171
				return c;
770
		}
1172
		}
771
	}
1173
	}
772
 
1174
 
773
	final(a,b,c);
1175
	final(a, b, c);
774
	return c;
1176
	return c;
775
}
1177
}
776
 
1178
 
777
#ifdef SELF_TEST
1179
#ifdef SELF_TEST
778
/* used for timings */
1180
/* used for timings */
779
void driver1()
1181
void driver1()
780
{
1182
{
781
	uint8_t buf[256];
1183
	uint8_t buf[256];
782
	uint32_t i;
1184
	uint32_t i;
783
	uint32_t h=0;
1185
	uint32_t h = 0;
784
	time_t a,z;
1186
	time_t a, z;
785
 
1187
 
786
	time(&a);
1188
	time(&a);
787
 
1189
 
788
	for (i=0; i<256; ++i)
1190
	for (i = 0; i < 256; ++i)
789
		buf[i] = 'x';
1191
		buf[i] = 'x';
790
 
1192
 
791
	for (i=0; i<1; ++i) 
1193
	for (i = 0; i < 1; ++i)
792
		h = hashlittle(&buf[0],1,h);
1194
		h = hashlittle(&buf[0], 1, h);
793
 
1195
 
794
	time(&z);
1196
	time(&z);
795
 
1197
 
796
	if (z-a > 0)
1198
	if (z - a > 0)
797
		printf("time %d %.8x\n", z-a, h);
1199
		printf("time %d %.8x\n", z - a, h);
798
}
1200
}
799
 
1201
 
800
/* check that every input bit changes every output bit half the time */
1202
/* check that every input bit changes every output bit half the time */
801
#define HASHSTATE 1
1203
#define HASHSTATE 1
802
#define HASHLEN   1
1204
#define HASHLEN   1
803
#define MAXPAIR 60
1205
#define MAXPAIR 60
804
#define MAXLEN  70
1206
#define MAXLEN  70
805
void driver2()
1207
void driver2()
806
{
1208
{
807
	uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
1209
	uint8_t qa[MAXLEN + 1], qb[MAXLEN + 2], *a = &qa[0], *b = &qb[1];
808
	uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
1210
	uint32_t c[HASHSTATE], d[HASHSTATE], i = 0, j = 0, k, l, m = 0, z;
809
	uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
1211
	uint32_t e[HASHSTATE], f[HASHSTATE], g[HASHSTATE], h[HASHSTATE];
810
	uint32_t x[HASHSTATE],y[HASHSTATE];
1212
	uint32_t x[HASHSTATE], y[HASHSTATE];
811
	uint32_t hlen;
1213
	uint32_t hlen;
812
	 
-
 
813
	printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
-
 
814
 
1214
 
-
 
1215
	printf("No more than %d trials should ever be needed \n", MAXPAIR / 2);
-
 
1216
 
815
	for (hlen=0; hlen < MAXLEN; ++hlen)
1217
	for (hlen = 0; hlen < MAXLEN; ++hlen)
816
	{
1218
	{
817
		z=0;
1219
		z = 0;
818
 
1220
 
819
		for (i=0; i<hlen; ++i)  /*----------------------- for each input byte, */
1221
		for (i = 0; i < hlen; ++i) /*----------------------- for each input byte, */
820
		{
1222
		{
821
			for (j=0; j<8; ++j)   /*------------------------ for each input bit, */
1223
			for (j = 0; j < 8; ++j) /*------------------------ for each input bit, */
822
			{
1224
			{
823
				for (m=1; m<8; ++m) /*------------ for serveral possible initvals, */
1225
				for (m = 1; m < 8; ++m) /*------------ for serveral possible initvals, */
824
				{
1226
				{
825
					for (l=0; l<HASHSTATE; ++l)
1227
					for (l = 0; l < HASHSTATE; ++l)
826
						e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
1228
						e[l] = f[l] = g[l] = h[l] = x[l] = y[l] = ~((uint32_t)0);
827
 
1229
 
828
					/*---- check that every output bit is affected by that input bit */
1230
					/*---- check that every output bit is affected by that input bit */
829
					for (k=0; k<MAXPAIR; k+=2)
1231
					for (k = 0; k < MAXPAIR; k += 2)
830
					{ 
1232
					{
831
						uint32_t finished=1;
1233
						uint32_t finished = 1;
-
 
1234
 
832
						/* keys have one bit different */
1235
						/* keys have one bit different */
833
						for (l=0; l<hlen+1; ++l)
1236
						for (l = 0; l < hlen + 1; ++l)
834
							a[l] = b[l] = (uint8_t)0;
1237
							a[l] = b[l] = (uint8_t)0;
835
 
1238
 
836
						/* have a and b be two keys differing in only one bit */
1239
						/* have a and b be two keys differing in only one bit */
837
						a[i] ^= (k<<j);
1240
						a[i] ^= (k << j);
838
						a[i] ^= (k>>(8-j));
1241
						a[i] ^= (k >> (8 - j));
839
						c[0] = hashlittle(a, hlen, m);
1242
						c[0] = hashlittle(a, hlen, m);
840
						b[i] ^= ((k+1)<<j);
1243
						b[i] ^= ((k + 1) << j);
841
						b[i] ^= ((k+1)>>(8-j));
1244
						b[i] ^= ((k + 1) >> (8 - j));
842
						d[0] = hashlittle(b, hlen, m);
1245
						d[0] = hashlittle(b, hlen, m);
-
 
1246
 
843
						/* check every bit is 1, 0, set, and not set at least once */
1247
						/* check every bit is 1, 0, set, and not set at least once */
844
						for (l=0; l<HASHSTATE; ++l)
1248
						for (l = 0; l < HASHSTATE; ++l)
845
						{
1249
						{
846
							e[l] &= (c[l]^d[l]);
1250
							e[l] &= (c[l] ^ d[l]);
847
							f[l] &= ~(c[l]^d[l]);
1251
							f[l] &= ~(c[l] ^ d[l]);
848
							g[l] &= c[l];
1252
							g[l] &= c[l];
849
							h[l] &= ~c[l];
1253
							h[l] &= ~c[l];
850
							x[l] &= d[l];
1254
							x[l] &= d[l];
851
							y[l] &= ~d[l];
1255
							y[l] &= ~d[l];
-
 
1256
 
852
							if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
1257
							if (e[l] | f[l] | g[l] | h[l] | x[l] | y[l]) finished = 0;
853
						}
1258
						}
854
 
1259
 
855
						if (finished) break;
1260
						if (finished) break;
856
					}
1261
					}
857
 
1262
 
858
					if (k>z)
1263
					if (k > z)
859
						z=k;
1264
						z = k;
860
 
1265
 
861
					if (k==MAXPAIR) 
1266
					if (k == MAXPAIR)
862
					{
1267
					{
863
						printf("Some bit didn't change: ");
1268
						printf("Some bit didn't change: ");
864
						printf("%.8x %.8x %.8x %.8x %.8x %.8x  ", e[0],f[0],g[0],h[0],x[0],y[0]);
1269
						printf("%.8x %.8x %.8x %.8x %.8x %.8x  ", e[0], f[0], g[0], h[0], x[0], y[0]);
865
						printf("i %d j %d m %d len %d\n", i, j, m, hlen);
1270
						printf("i %d j %d m %d len %d\n", i, j, m, hlen);
866
					}
1271
					}
867
 
1272
 
868
					if (z==MAXPAIR)
1273
					if (z == MAXPAIR)
869
						goto done;
1274
						goto done;
870
				}
1275
				}
871
			}
1276
			}
872
		}
1277
		}
-
 
1278
 
873
	done:
1279
	done:
-
 
1280
 
874
		if (z < MAXPAIR)
1281
		if (z < MAXPAIR)
875
		{
1282
		{
876
			printf("Mix success  %2d bytes  %2d initvals  ",i,m);
1283
			printf("Mix success  %2d bytes  %2d initvals  ", i, m);
877
			printf("required  %d  trials\n", z/2);
1284
			printf("required  %d  trials\n", z / 2);
878
		}
1285
		}
879
	}
1286
	}
880
 
1287
 
881
	printf("\n");
1288
	printf("\n");
882
}
1289
}
883
 
1290
 
884
 /* Check for reading beyond the end of the buffer and alignment problems */
-
 
885
 void driver3()
-
 
886
 {
-
 
887
	 uint8_t buf[MAXLEN+20], *b;
-
 
888
	 uint32_t len;
-
 
889
	 uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
-
 
890
	 uint32_t h;
-
 
891
	 uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
-
 
892
	 uint32_t i;
-
 
893
	 uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
-
 
894
	 uint32_t j;
-
 
895
	 uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
-
 
896
	 uint32_t ref,x,y;
-
 
897
	 uint8_t *p;
-
 
898
	 
-
 
899
	 printf("Endianness.  These lines should all be the same (for values filled in):\n");
-
 
900
	 printf("%.8x                            %.8x                            %.8x\n",
-
 
901
			hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
-
 
902
			hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
-
 
903
			hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
-
 
904
	 p = q;
-
 
905
	 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
-
 
906
			hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
-
 
907
			hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
-
 
908
			hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
-
 
909
			hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
-
 
910
			hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
-
 
911
			hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
-
 
912
	 p = &qq[1];
-
 
913
	 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
-
 
914
			hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
-
 
915
			hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
-
 
916
			hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
-
 
917
			hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
-
 
918
			hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
-
 
919
			hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
-
 
920
	 p = &qqq[2];
-
 
921
	 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
-
 
922
			hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
-
 
923
			hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
-
 
924
			hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
-
 
925
			hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
-
 
926
			hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
-
 
927
			hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
-
 
928
	 p = &qqqq[3];
-
 
929
	 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
-
 
930
			hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
-
 
931
			hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
-
 
932
			hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
-
 
933
			hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
-
 
934
			hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
-
 
935
			hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
-
 
936
	 printf("\n");
-
 
937
	 
-
 
938
	 /* check that hashlittle2 and hashlittle produce the same results */
-
 
939
	 i=47; j=0;
-
 
940
	 hashlittle2(q, sizeof(q), &i, &j);
-
 
941
	 if (hashlittle(q, sizeof(q), 47) != i)
-
 
942
		 printf("hashlittle2 and hashlittle mismatch\n");
-
 
943
	 
-
 
944
	 /* check that hashword2 and hashword produce the same results */
-
 
945
	 len = 0xdeadbeef;
-
 
946
	 i=47, j=0;
-
 
947
	 hashword2(&len, 1, &i, &j);
-
 
948
	 if (hashword(&len, 1, 47) != i)
-
 
949
		 printf("hashword2 and hashword mismatch %x %x\n", 
-
 
950
				i, hashword(&len, 1, 47));
-
 
951
		 
-
 
952
		 /* check hashlittle doesn't read before or after the ends of the string */
-
 
953
		 for (h=0, b=buf+1; h<8; ++h, ++b)
-
 
954
		 {
-
 
955
			 for (i=0; i<MAXLEN; ++i)
-
 
956
			 {
-
 
957
				 len = i;
-
 
958
				 for (j=0; j<i; ++j) *(b+j)=0;
-
 
959
				 
-
 
960
				 /* these should all be equal */
-
 
961
				 ref = hashlittle(b, len, (uint32_t)1);
-
 
962
				 *(b+i)=(uint8_t)~0;
-
 
963
				 *(b-1)=(uint8_t)~0;
-
 
964
				 x = hashlittle(b, len, (uint32_t)1);
-
 
965
				 y = hashlittle(b, len, (uint32_t)1);
-
 
966
				 if ((ref != x) || (ref != y)) 
-
 
967
				 {
-
 
968
					 printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
-
 
969
							h, i);
-
 
970
				 }
-
 
971
			 }
-
 
972
		 }
-
 
973
 }
-
 
974
													 
-
 
975
													 /* check for problems with nulls */
-
 
976
													 void driver4()
-
 
977
													 {
-
 
978
														 uint8_t buf[1];
-
 
979
														 uint32_t h,i,state[HASHSTATE];
-
 
980
														 
-
 
981
														 
-
 
982
														 buf[0] = ~0;
-
 
983
														 for (i=0; i<HASHSTATE; ++i) state[i] = 1;
-
 
984
														 printf("These should all be different\n");
-
 
985
														 for (i=0, h=0; i<8; ++i)
-
 
986
														 {
-
 
987
															 h = hashlittle(buf, 0, h);
-
 
988
															 printf("%2ld  0-byte strings, hash is  %.8x\n", i, h);
-
 
989
														 }
-
 
990
													 }
-
 
991
													 
-
 
992
													 void driver5()
-
 
993
													 {
-
 
994
														 uint32_t b,c;
-
 
995
														 b=0, c=0, hashlittle2("", 0, &c, &b);
-
 
996
														 printf("hash is %.8lx %.8lx\n", c, b);   /* deadbeef deadbeef */
-
 
997
														 b=0xdeadbeef, c=0, hashlittle2("", 0, &c, &b);
-
 
998
														 printf("hash is %.8lx %.8lx\n", c, b);   /* bd5b7dde deadbeef */
-
 
999
														 b=0xdeadbeef, c=0xdeadbeef, hashlittle2("", 0, &c, &b);
-
 
1000
														 printf("hash is %.8lx %.8lx\n", c, b);   /* 9c093ccd bd5b7dde */
-
 
1001
														 b=0, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
-
 
1002
														 printf("hash is %.8lx %.8lx\n", c, b);   /* 17770551 ce7226e6 */
-
 
1003
														 b=1, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
-
 
1004
														 printf("hash is %.8lx %.8lx\n", c, b);   /* e3607cae bd371de4 */
-
 
1005
														 b=0, c=1, hashlittle2("Four score and seven years ago", 30, &c, &b);
-
 
1006
														 printf("hash is %.8lx %.8lx\n", c, b);   /* cd628161 6cbea4b3 */
-
 
1007
														 c = hashlittle("Four score and seven years ago", 30, 0);
-
 
1008
														 printf("hash is %.8lx\n", c);   /* 17770551 */
-
 
1009
														 c = hashlittle("Four score and seven years ago", 30, 1);
-
 
1010
														 printf("hash is %.8lx\n", c);   /* cd628161 */
-
 
1011
													 }
-
 
1012
													 
-
 
1013
													 
-
 
1014
													 int main()
-
 
1015
													 {
-
 
1016
														 driver1();   /* test that the key is hashed: used for timings */
-
 
1017
														 driver2();   /* test that whole key is hashed thoroughly */
-
 
1018
														 driver3();   /* test that nothing but the key is hashed */
-
 
1019
														 driver4();   /* test hashing multiple buffers (all buffers are null) */
-
 
1020
														 driver5();   /* test the hash against known vectors */
-
 
1021
														 return 1;
-
 
1022
													 }
-
 
1023
													 
-
 
1024
													 #endif  /* SELF_TEST */
-
 
1025
													 
-
 
1026
 
1291
/* Check for reading beyond the end of the buffer and alignment problems */
-
 
1292
void driver3()
-
 
1293
{
-
 
1294
	uint8_t buf[MAXLEN + 20], *b;
-
 
1295
	uint32_t len;
-
 
1296
	uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
-
 
1297
	uint32_t h;
-
 
1298
	uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
-
 
1299
	uint32_t i;
-
 
1300
	uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
-
 
1301
	uint32_t j;
-
 
1302
	uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
-
 
1303
	uint32_t ref, x, y;
-
 
1304
	uint8_t *p;
-
 
1305
 
-
 
1306
	printf("Endianness.  These lines should all be the same (for values filled in):\n");
-
 
1307
	printf("%.8x                            %.8x                            %.8x\n",
-
 
1308
	       hashword((const uint32_t *)q, (sizeof(q) - 1) / 4, 13),
-
 
1309
	       hashword((const uint32_t *)q, (sizeof(q) - 5) / 4, 13),
-
 
1310
	       hashword((const uint32_t *)q, (sizeof(q) - 9) / 4, 13));
-
 
1311
	p = q;
-
 
1312
	printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
-
 
1313
	       hashlittle(p, sizeof(q) - 1, 13), hashlittle(p, sizeof(q) - 2, 13),
-
 
1314
	       hashlittle(p, sizeof(q) - 3, 13), hashlittle(p, sizeof(q) - 4, 13),
-
 
1315
	       hashlittle(p, sizeof(q) - 5, 13), hashlittle(p, sizeof(q) - 6, 13),
-
 
1316
	       hashlittle(p, sizeof(q) - 7, 13), hashlittle(p, sizeof(q) - 8, 13),
-
 
1317
	       hashlittle(p, sizeof(q) - 9, 13), hashlittle(p, sizeof(q) - 10, 13),
-
 
1318
	       hashlittle(p, sizeof(q) - 11, 13), hashlittle(p, sizeof(q) - 12, 13));
-
 
1319
	p = &qq[1];
-
 
1320
	printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
-
 
1321
	       hashlittle(p, sizeof(q) - 1, 13), hashlittle(p, sizeof(q) - 2, 13),
-
 
1322
	       hashlittle(p, sizeof(q) - 3, 13), hashlittle(p, sizeof(q) - 4, 13),
-
 
1323
	       hashlittle(p, sizeof(q) - 5, 13), hashlittle(p, sizeof(q) - 6, 13),
-
 
1324
	       hashlittle(p, sizeof(q) - 7, 13), hashlittle(p, sizeof(q) - 8, 13),
-
 
1325
	       hashlittle(p, sizeof(q) - 9, 13), hashlittle(p, sizeof(q) - 10, 13),
-
 
1326
	       hashlittle(p, sizeof(q) - 11, 13), hashlittle(p, sizeof(q) - 12, 13));
-
 
1327
	p = &qqq[2];
-
 
1328
	printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
-
 
1329
	       hashlittle(p, sizeof(q) - 1, 13), hashlittle(p, sizeof(q) - 2, 13),
-
 
1330
	       hashlittle(p, sizeof(q) - 3, 13), hashlittle(p, sizeof(q) - 4, 13),
-
 
1331
	       hashlittle(p, sizeof(q) - 5, 13), hashlittle(p, sizeof(q) - 6, 13),
-
 
1332
	       hashlittle(p, sizeof(q) - 7, 13), hashlittle(p, sizeof(q) - 8, 13),
-
 
1333
	       hashlittle(p, sizeof(q) - 9, 13), hashlittle(p, sizeof(q) - 10, 13),
-
 
1334
	       hashlittle(p, sizeof(q) - 11, 13), hashlittle(p, sizeof(q) - 12, 13));
-
 
1335
	p = &qqqq[3];
-
 
1336
	printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
-
 
1337
	       hashlittle(p, sizeof(q) - 1, 13), hashlittle(p, sizeof(q) - 2, 13),
-
 
1338
	       hashlittle(p, sizeof(q) - 3, 13), hashlittle(p, sizeof(q) - 4, 13),
-
 
1339
	       hashlittle(p, sizeof(q) - 5, 13), hashlittle(p, sizeof(q) - 6, 13),
-
 
1340
	       hashlittle(p, sizeof(q) - 7, 13), hashlittle(p, sizeof(q) - 8, 13),
-
 
1341
	       hashlittle(p, sizeof(q) - 9, 13), hashlittle(p, sizeof(q) - 10, 13),
-
 
1342
	       hashlittle(p, sizeof(q) - 11, 13), hashlittle(p, sizeof(q) - 12, 13));
-
 
1343
	printf("\n");
-
 
1344
 
-
 
1345
	/* check that hashlittle2 and hashlittle produce the same results */
-
 
1346
	i = 47;
-
 
1347
	j = 0;
-
 
1348
	hashlittle2(q, sizeof(q), &i, &j);
-
 
1349
 
-
 
1350
	if (hashlittle(q, sizeof(q), 47) != i)
-
 
1351
		printf("hashlittle2 and hashlittle mismatch\n");
-
 
1352
 
-
 
1353
	/* check that hashword2 and hashword produce the same results */
-
 
1354
	len = 0xdeadbeef;
-
 
1355
	i = 47, j = 0;
-
 
1356
	hashword2(&len, 1, &i, &j);
-
 
1357
 
-
 
1358
	if (hashword(&len, 1, 47) != i)
-
 
1359
		printf("hashword2 and hashword mismatch %x %x\n",
-
 
1360
		       i, hashword(&len, 1, 47));
-
 
1361
 
-
 
1362
	/* check hashlittle doesn't read before or after the ends of the string */
-
 
1363
	for (h = 0, b = buf + 1; h < 8; ++h, ++b)
-
 
1364
	{
-
 
1365
		for (i = 0; i < MAXLEN; ++i)
-
 
1366
		{
-
 
1367
			len = i;
-
 
1368
 
-
 
1369
			for (j = 0; j < i; ++j) *(b + j) = 0;
-
 
1370
 
-
 
1371
			/* these should all be equal */
-
 
1372
			ref = hashlittle(b, len, (uint32_t)1);
-
 
1373
			*(b + i) = (uint8_t)~0;
-
 
1374
			*(b - 1) = (uint8_t)~0;
-
 
1375
			x = hashlittle(b, len, (uint32_t)1);
-
 
1376
			y = hashlittle(b, len, (uint32_t)1);
-
 
1377
 
-
 
1378
			if ((ref != x) || (ref != y))
-
 
1379
			{
-
 
1380
				printf("alignment error: %.8x %.8x %.8x %d %d\n", ref, x, y,
-
 
1381
				       h, i);
-
 
1382
			}
-
 
1383
		}
-
 
1384
	}
-
 
1385
}
-
 
1386
 
-
 
1387
/* check for problems with nulls */
-
 
1388
void driver4()
-
 
1389
{
-
 
1390
	uint8_t buf[1];
-
 
1391
	uint32_t h, i, state[HASHSTATE];
-
 
1392
 
-
 
1393
 
-
 
1394
	buf[0] = ~0;
-
 
1395
 
-
 
1396
	for (i = 0; i < HASHSTATE; ++i) state[i] = 1;
-
 
1397
 
-
 
1398
	printf("These should all be different\n");
-
 
1399
 
-
 
1400
	for (i = 0, h = 0; i < 8; ++i)
-
 
1401
	{
-
 
1402
		h = hashlittle(buf, 0, h);
-
 
1403
		printf("%2ld  0-byte strings, hash is  %.8x\n", i, h);
-
 
1404
	}
-
 
1405
}
-
 
1406
 
-
 
1407
void driver5()
-
 
1408
{
-
 
1409
	uint32_t b, c;
-
 
1410
	b = 0, c = 0, hashlittle2("", 0, &c, &b);
-
 
1411
	printf("hash is %.8lx %.8lx\n", c, b);   /* deadbeef deadbeef */
-
 
1412
	b = 0xdeadbeef, c = 0, hashlittle2("", 0, &c, &b);
-
 
1413
	printf("hash is %.8lx %.8lx\n", c, b);   /* bd5b7dde deadbeef */
-
 
1414
	b = 0xdeadbeef, c = 0xdeadbeef, hashlittle2("", 0, &c, &b);
-
 
1415
	printf("hash is %.8lx %.8lx\n", c, b);   /* 9c093ccd bd5b7dde */
-
 
1416
	b = 0, c = 0, hashlittle2("Four score and seven years ago", 30, &c, &b);
-
 
1417
	printf("hash is %.8lx %.8lx\n", c, b);   /* 17770551 ce7226e6 */
-
 
1418
	b = 1, c = 0, hashlittle2("Four score and seven years ago", 30, &c, &b);
-
 
1419
	printf("hash is %.8lx %.8lx\n", c, b);   /* e3607cae bd371de4 */
-
 
1420
	b = 0, c = 1, hashlittle2("Four score and seven years ago", 30, &c, &b);
-
 
1421
	printf("hash is %.8lx %.8lx\n", c, b);   /* cd628161 6cbea4b3 */
-
 
1422
	c = hashlittle("Four score and seven years ago", 30, 0);
-
 
1423
	printf("hash is %.8lx\n", c);   /* 17770551 */
-
 
1424
	c = hashlittle("Four score and seven years ago", 30, 1);
-
 
1425
	printf("hash is %.8lx\n", c);   /* cd628161 */
-
 
1426
}
-
 
1427
 
-
 
1428
 
-
 
1429
int main()
-
 
1430
{
-
 
1431
	driver1();   /* test that the key is hashed: used for timings */
-
 
1432
	driver2();   /* test that whole key is hashed thoroughly */
-
 
1433
	driver3();   /* test that nothing but the key is hashed */
-
 
1434
	driver4();   /* test hashing multiple buffers (all buffers are null) */
-
 
1435
	driver5();   /* test the hash against known vectors */
-
 
1436
	return 1;
-
 
1437
}
-
 
1438
 
-
 
1439
#endif  /* SELF_TEST */
-
 
1440
 
-
 
1441