1 /*
2  * hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code
3  *
4  * @(#) $Revision: 5.1 $
5  * @(#) $Id: hash_32a.c,v 5.1 2009/06/30 09:13:32 chongo Exp $
6  * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $
7  *
8  ***
9  *
10  * Fowler/Noll/Vo hash
11  *
12  * The basis of this hash algorithm was taken from an idea sent
13  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
14  *
15  *      Phong Vo (http://www.research.att.com/info/kpv/)
16  *      Glenn Fowler (http://www.research.att.com/~gsf/)
17  *
18  * In a subsequent ballot round:
19  *
20  *      Landon Curt Noll (http://www.isthe.com/chongo/)
21  *
22  * improved on their algorithm.  Some people tried this hash
23  * and found that it worked rather well.  In an EMail message
24  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
25  *
26  * FNV hashes are designed to be fast while maintaining a low
27  * collision rate. The FNV speed allows one to quickly hash lots
28  * of data while maintaining a reasonable collision rate.  See:
29  *
30  *      http://www.isthe.com/chongo/tech/comp/fnv/index.html
31  *
32  * for more details as well as other forms of the FNV hash.
33  ***
34  *
35  * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
36  * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
37  *
38  ***
39  *
40  * Please do not copyright this code.  This code is in the public domain.
41  *
42  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
43  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
44  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
45  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
46  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
47  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
48  * PERFORMANCE OF THIS SOFTWARE.
49  *
50  * By:
51  *	chongo <Landon Curt Noll> /\oo/\
52  *      http://www.isthe.com/chongo/
53  *
54  * Share and Enjoy!	:-)
55  */
56 
57 #include <stdlib.h>
58 #include "fnv.h"
59 
60 
61 /*
62  * 32 bit magic FNV-1a prime
63  */
64 #define FNV_32_PRIME ((Fnv32_t)0x01000193)
65 
66 #if 0
67 /*
68  * fnv_32a_buf - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a buffer
69  *
70  * input:
71  *	buf	- start of buffer to hash
72  *	len	- length of buffer in octets
73  *	hval	- previous hash value or 0 if first call
74  *
75  * returns:
76  *	32 bit hash as a static hash type
77  *
78  * NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the
79  * 	 hval arg on the first call to either fnv_32a_buf() or fnv_32a_str().
80  */
81 Fnv32_t
82 fnv_32a_buf(void *buf, size_t len, Fnv32_t hval)
83 {
84     unsigned char *bp = (unsigned char *)buf;	/* start of buffer */
85     unsigned char *be = bp + len;		/* beyond end of buffer */
86 
87     /*
88      * FNV-1a hash each octet in the buffer
89      */
90     while (bp < be) {
91 
92 	/* xor the bottom with the current octet */
93 	hval ^= (Fnv32_t)*bp++;
94 
95 	/* multiply by the 32 bit FNV magic prime mod 2^32 */
96 #if defined(NO_FNV_GCC_OPTIMIZATION)
97 	hval *= FNV_32_PRIME;
98 #else
99 	hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
100 #endif
101     }
102 
103     /* return our new hash value */
104     return hval;
105 }
106 #endif
107 
108 
109 /*
110  * fnv_32a_str - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a string
111  *
112  * input:
113  *	str	- string to hash
114  *	hval	- previous hash value or 0 if first call
115  *
116  * returns:
117  *	32 bit hash as a static hash type
118  *
119  * NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the
120  *  	 hval arg on the first call to either fnv_32a_buf() or fnv_32a_str().
121  */
122 Fnv32_t
fnv_32a_str(const char * str,Fnv32_t hval)123 fnv_32a_str(const char *str, Fnv32_t hval)
124 {
125     unsigned const char *s = (unsigned char *)str;	/* unsigned string */
126 
127     /*
128      * FNV-1a hash each octet in the buffer
129      */
130     while (*s) {
131 
132 	/* xor the bottom with the current octet */
133 	hval ^= (Fnv32_t)*s++;
134 
135 	/* multiply by the 32 bit FNV magic prime mod 2^32 */
136 #if defined(NO_FNV_GCC_OPTIMIZATION)
137 	hval *= FNV_32_PRIME;
138 #else
139 	hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
140 #endif
141     }
142 
143     /* return our new hash value */
144     return hval;
145 }
146