Subversion Repositories mdb

Rev

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

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