2 * hash_32 - 32 bit Fowler/Noll/Vo hash code
9 * The basis of this hash algorithm was taken from an idea sent
10 * as reviewer comments to the IEEE POSIX P1003.2 committee by:
12 * Phong Vo (http://www.research.att.com/info/kpv/)
13 * Glenn Fowler (http://www.research.att.com/~gsf/)
15 * In a subsequent ballot round:
17 * Landon Curt Noll (http://reality.sgi.com/chongo/)
19 * improved on their algorithm. Some people tried this hash
20 * and found that it worked rather well. In an EMail message
21 * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
23 * FNV hashes are architected to be fast while maintaining a low
24 * collision rate. The FNV speed allows one to quickly hash lots
25 * of data while maintaining a reasonable collision rate. See:
27 * http://reality.sgi.com/chongo/tech/comp/fnv/
29 * for more details as well as other forms of the FNV hash.
32 * NOTE: The FNV-0 historic hash is not recommended. One should use
33 * the FNV-1 hash instead.
35 * To use the 32 bit FNV-0 historic hash, pass FNV0_32_INIT as the
36 * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
38 * To use the recommended 32 bit FNV-1 hash, pass FNV1_32_INIT as the
39 * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
43 * Please do not copyright this code. This code is in the public domain.
45 * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
46 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
47 * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
48 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
49 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
50 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
51 * PERFORMANCE OF THIS SOFTWARE.
54 * chongo <Landon Curt Noll> /\oo/\
55 * http://reality.sgi.com/chongo/
56 * EMail: chongo_fnv at prime dot engr dot sgi dot com
58 * Share and Enjoy! :-)
66 * 32 bit magic FNV-0 and FNV-1 prime
68 #define FNV_32_PRIME ((Fnv32_t)0x01000193)
72 * fnv_32_buf - perform a 32 bit Fowler/Noll/Vo hash on a buffer
75 * buf - start of buffer to hash
76 * len - length of buffer in octets
77 * hval - previous hash value or 0 if first call
80 * 32 bit hash as a static hash type
82 * NOTE: To use the 32 bit FNV-0 historic hash, use FNV0_32_INIT as the hval
83 * argument on the first call to either fnv_32_buf() or fnv_32_str().
85 * NOTE: To use the recommended 32 bit FNV-1 hash, use FNV1_32_INIT as the hval
86 * argument on the first call to either fnv_32_buf() or fnv_32_str().
89 fnv_32_buf(void *buf, size_t len, Fnv32_t hval)
91 unsigned char *bp = (unsigned char *)buf; /* start of buffer */
92 unsigned char *be = bp + len; /* beyond end of buffer */
95 * FNV-1 hash each octet in the buffer
99 /* multiply by the 32 bit FNV magic prime mod 2^64 */
100 hval *= FNV_32_PRIME;
102 /* xor the bottom with the current octet */
103 hval ^= (Fnv32_t)*bp++;
106 /* return our new hash value */
112 * fnv_32_str - perform a 32 bit Fowler/Noll/Vo hash on a string
115 * str - string to hash
116 * hval - previous hash value or 0 if first call
119 * 32 bit hash as a static hash type
121 * NOTE: To use the 32 bit FNV-0 historic hash, use FNV0_32_INIT as the hval
122 * argument on the first call to either fnv_32_buf() or fnv_32_str().
124 * NOTE: To use the recommended 32 bit FNV-1 hash, use FNV1_32_INIT as the hval
125 * argument on the first call to either fnv_32_buf() or fnv_32_str().
128 fnv_32_str(char *str, Fnv32_t hval)
130 unsigned char *s = (unsigned char *)str; /* unsigned string */
133 * FNV-1 hash each octet in the buffer
137 /* multiply by the 32 bit FNV magic prime mod 2^64 */
138 hval *= FNV_32_PRIME;
140 /* xor the bottom with the current octet */
141 hval ^= (Fnv32_t)*s++;
144 /* return our new hash value */
148 /* a wrapper function for fnv_32_str */
149 unsigned long FnvHash(char *str)
151 return fnv_32_str(str,FNV1_32_INIT);