1/*
2 * hash_64 - 64 bit Fowler/Noll/Vo-0 FNV-1a hash code
3 *
4 * @(#) $Revision: 5.1 $
5 * @(#) $Id: hash_64a.c,v 5.1 2009/06/30 09:01:38 chongo Exp $
6 * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_64a.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 *
36 * To use the recommended 64 bit FNV-1a hash, pass FNV1A_64_INIT as the
37 * Fnv64_t hashval argument to fnv_64a_buf() or fnv_64a_str().
38 *
39 ***
40 *
41 * Please do not copyright this code.  This code is in the public domain.
42 *
43 * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
44 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
45 * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
46 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
47 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
48 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
49 * PERFORMANCE OF THIS SOFTWARE.
50 *
51 * By:
52 *	chongo <Landon Curt Noll> /\oo/\
53 *      http://www.isthe.com/chongo/
54 *
55 * Share and Enjoy!	:-)
56 */
57
58#include <stdlib.h>
59#include "fnv.h"
60
61
62/*
63 * FNV-1a defines the initial basis to be non-zero
64 */
65#if !defined(HAVE_64BIT_LONG_LONG)
66const Fnv64_t fnv1a_64_init = { 0x84222325, 0xcbf29ce4 };
67#endif /* ! HAVE_64BIT_LONG_LONG */
68
69
70/*
71 * 64 bit magic FNV-1a prime
72 */
73#if defined(HAVE_64BIT_LONG_LONG)
74#define FNV_64_PRIME ((Fnv64_t)0x100000001b3ULL)
75#else /* HAVE_64BIT_LONG_LONG */
76#define FNV_64_PRIME_LOW ((unsigned long)0x1b3)	/* lower bits of FNV prime */
77#define FNV_64_PRIME_SHIFT (8)		/* top FNV prime shift above 2^32 */
78#endif /* HAVE_64BIT_LONG_LONG */
79
80
81/*
82 * fnv_64a_str - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
83 *
84 * input:
85 *	buf	- start of buffer to hash
86 *	hval	- previous hash value or 0 if first call
87 *
88 * returns:
89 *	64 bit hash as a static hash type
90 *
91 * NOTE: To use the recommended 64 bit FNV-1a hash, use FNV1A_64_INIT as the
92 * 	 hval arg on the first call to either fnv_64a_buf() or fnv_64a_str().
93 */
94uint64_t
95fnv_64a_str(char *str, uint64_t hval)
96{
97    unsigned char *s = (unsigned char *)str;	/* unsigned string */
98
99#if defined(HAVE_64BIT_LONG_LONG)
100
101    /*
102     * FNV-1a hash each octet of the string
103     */
104    while (*s) {
105
106	/* xor the bottom with the current octet */
107	hval ^= (uint64_t)*s++;
108
109	/* multiply by the 64 bit FNV magic prime mod 2^64 */
110#if defined(NO_FNV_GCC_OPTIMIZATION)
111	hval *= FNV_64_PRIME;
112#else /* NO_FNV_GCC_OPTIMIZATION */
113	hval += (hval << 1) + (hval << 4) + (hval << 5) +
114		(hval << 7) + (hval << 8) + (hval << 40);
115#endif /* NO_FNV_GCC_OPTIMIZATION */
116    }
117
118#else /* !HAVE_64BIT_LONG_LONG */
119
120    unsigned long val[4];	/* hash value in base 2^16 */
121    unsigned long tmp[4];	/* tmp 64 bit value */
122
123    /*
124     * Convert Fnv64_t hval into a base 2^16 array
125     */
126    val[0] = hval.w32[0];
127    val[1] = (val[0] >> 16);
128    val[0] &= 0xffff;
129    val[2] = hval.w32[1];
130    val[3] = (val[2] >> 16);
131    val[2] &= 0xffff;
132
133    /*
134     * FNV-1a hash each octet of the string
135     */
136    while (*s) {
137
138	/* xor the bottom with the current octet */
139
140	/*
141	 * multiply by the 64 bit FNV magic prime mod 2^64
142	 *
143	 * Using 1099511628211, we have the following digits base 2^16:
144	 *
145	 *	0x0	0x100	0x0	0x1b3
146	 *
147	 * which is the same as:
148	 *
149	 *	0x0	1<<FNV_64_PRIME_SHIFT	0x0	FNV_64_PRIME_LOW
150	 */
151	/* multiply by the lowest order digit base 2^16 */
152	tmp[0] = val[0] * FNV_64_PRIME_LOW;
153	tmp[1] = val[1] * FNV_64_PRIME_LOW;
154	tmp[2] = val[2] * FNV_64_PRIME_LOW;
155	tmp[3] = val[3] * FNV_64_PRIME_LOW;
156	/* multiply by the other non-zero digit */
157	tmp[2] += val[0] << FNV_64_PRIME_SHIFT;	/* tmp[2] += val[0] * 0x100 */
158	tmp[3] += val[1] << FNV_64_PRIME_SHIFT;	/* tmp[3] += val[1] * 0x100 */
159	/* propagate carries */
160	tmp[1] += (tmp[0] >> 16);
161	val[0] = tmp[0] & 0xffff;
162	tmp[2] += (tmp[1] >> 16);
163	val[1] = tmp[1] & 0xffff;
164	val[3] = tmp[3] + (tmp[2] >> 16);
165	val[2] = tmp[2] & 0xffff;
166	/*
167	 * Doing a val[3] &= 0xffff; is not really needed since it simply
168	 * removes multiples of 2^64.  We can discard these excess bits
169	 * outside of the loop when we convert to Fnv64_t.
170	 */
171	val[0] ^= (unsigned long)(*s++);
172    }
173
174    /*
175     * Convert base 2^16 array back into an Fnv64_t
176     */
177    hval.w32[1] = ((val[3]<<16) | val[2]);
178    hval.w32[0] = ((val[1]<<16) | val[0]);
179
180#endif /* !HAVE_64BIT_LONG_LONG */
181
182    /* return our new hash value */
183    return hval;
184}
185