My Project
Loading...
Searching...
No Matches
hash_me.c
Go to the documentation of this file.
1/* https://burtleburtle.net/bob/hash/index.html#lookup
2-------------------------------------------------------------------------------
3lookup3.c, by Bob Jenkins, May 2006, Public Domain.
4-------------------------------------------------------------------------------
5*/
6
7#include "hash_me.h"
8
9#define hashsize(n) ((uint32_t)1<<(n))
10#define hashmask(n) (hashsize(n)-1)
11#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
12
13/*
14-------------------------------------------------------------------------------
15mix -- mix 3 32-bit values reversibly.
16-------------------------------------------------------------------------------
17*/
18#define mix(a,b,c) \
19{ \
20 a -= c; a ^= rot(c, 4); c += b; \
21 b -= a; b ^= rot(a, 6); a += c; \
22 c -= b; c ^= rot(b, 8); b += a; \
23 a -= c; a ^= rot(c,16); c += b; \
24 b -= a; b ^= rot(a,19); a += c; \
25 c -= b; c ^= rot(b, 4); b += a; \
26}
27
28/*
29-------------------------------------------------------------------------------
30final -- final mixing of 3 32-bit values (a,b,c) into c
31-------------------------------------------------------------------------------
32*/
33#define final(a,b,c) \
34{ \
35 c ^= b; c -= rot(b,14); \
36 a ^= c; a -= rot(c,11); \
37 b ^= a; b -= rot(a,25); \
38 c ^= b; c -= rot(b,16); \
39 a ^= c; a -= rot(c,4); \
40 b ^= a; b -= rot(a,14); \
41 c ^= b; c -= rot(b,24); \
42}
43
44/*
45-------------------------------------------------------------------------------
46hashlittle() -- hash a variable-length key into a 32-bit value
47 k : the key (the unaligned variable-length array of bytes)
48 length : the length of the key, counting by bytes
49 initval : can be any 4-byte value
50Returns a 32-bit value. Every bit of the key affects every bit of
51the return value. Two keys differing by one or two bits will have
52totally different hash values.
53
54The best hash table sizes are powers of 2. There is no need to do
55mod a prime (mod is sooo slow!). If you need less than 32 bits,
56use a bitmask. For example, if you need only 10 bits, do
57 h = (h & hashmask(10));
58In which case, the hash table should have hashsize(10) elements.
59
60By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
61code any way you wish, private, educational, or commercial. It's free.
62
63Use for hash table lookup, or anything where one collision in 2^^32 is
64acceptable. Do NOT use for cryptographic purposes.
65-------------------------------------------------------------------------------
66*/
67
68uint32_t hashlittle( const void *key, size_t length)
69{
70 uint32_t a,b,c; /* internal state */
71 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
72
73 /* Set up the internal state */
74 a = b = c = 0xdeadbeef + ((uint32_t)length);
75
76 u.ptr = key;
77 {
78 const uint8_t *k = (const uint8_t *)key;
79
80 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
81 while (length > 12)
82 {
83 a += k[0];
84 a += ((uint32_t)k[1])<<8;
85 a += ((uint32_t)k[2])<<16;
86 a += ((uint32_t)k[3])<<24;
87 b += k[4];
88 b += ((uint32_t)k[5])<<8;
89 b += ((uint32_t)k[6])<<16;
90 b += ((uint32_t)k[7])<<24;
91 c += k[8];
92 c += ((uint32_t)k[9])<<8;
93 c += ((uint32_t)k[10])<<16;
94 c += ((uint32_t)k[11])<<24;
95 mix(a,b,c);
96 length -= 12;
97 k += 12;
98 }
99
100 /*-------------------------------- last block: affect all 32 bits of (c) */
101 switch(length) /* all the case statements fall through */
102 {
103 case 12: c+=((uint32_t)k[11])<<24;
104 case 11: c+=((uint32_t)k[10])<<16;
105 case 10: c+=((uint32_t)k[9])<<8;
106 case 9 : c+=k[8];
107 case 8 : b+=((uint32_t)k[7])<<24;
108 case 7 : b+=((uint32_t)k[6])<<16;
109 case 6 : b+=((uint32_t)k[5])<<8;
110 case 5 : b+=k[4];
111 case 4 : a+=((uint32_t)k[3])<<24;
112 case 3 : a+=((uint32_t)k[2])<<16;
113 case 2 : a+=((uint32_t)k[1])<<8;
114 case 1 : a+=k[0];
115 break;
116 case 0 : return c;
117 }
118 }
119
120 final(a,b,c);
121 return c;
122}
int i
Definition cfEzgcd.cc:132
int k
Definition cfEzgcd.cc:99
CanonicalForm b
Definition cfModGcd.cc:4111
uint32_t hashlittle(const void *key, size_t length)
Definition hash_me.c:68
#define mix(a, b, c)
Definition hash_me.c:18
static BOOLEAN length(leftv result, leftv arg)
Definition interval.cc:257