2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
8 * Copyright (c) 1990, 1993
9 * Margo Seltzer. All rights reserved.
12 * Copyright (c) 1990, 1993
13 * The Regents of the University of California. All rights reserved.
15 * This code is derived from software contributed to Berkeley by
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 static const char sccsid
[] = "@(#)hash_func.c 10.8 (Sleepycat) 4/10/98";
53 #ifndef NO_SYSTEM_INCLUDES
54 #include <sys/types.h>
63 * Phong Vo's linear congruential hash.
65 * PUBLIC: u_int32_t __ham_func2 __P((const void *, u_int32_t));
67 #define DCHARHASH(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
74 const u_int8_t
*e
, *k
;
80 for (h
= 0; k
!= e
;) {
91 * Ozan Yigit's original sdbm hash.
93 * Ugly, but fast. Break the string up into 8 byte units. On the first time
94 * through the loop get the "leftover bytes" (strlen % 8). On every other
95 * iteration, perform 8 HASHC's so we handle all 8 bytes. Essentially, this
96 * saves us 7 cmp & branch instructions.
98 * PUBLIC: u_int32_t __ham_func3 __P((const void *, u_int32_t));
101 __ham_func3(key
, len
)
111 #define HASHC n = *k++ + 65599 * n
115 loop
= (len
+ 8 - 1) >> 3;
116 switch (len
& (8 - 1)) {
141 * Chris Torek's hash function. Although this function performs only
142 * slightly worse than __ham_func5 on strings, it performs horribly on
145 * PUBLIC: u_int32_t __ham_func4 __P((const void *, u_int32_t));
148 __ham_func4(key
, len
)
158 #define HASH4a h = (h << 5) - h + *k++;
159 #define HASH4b h = (h << 5) + h + *k++;
164 loop
= (len
+ 8 - 1) >> 3;
165 switch (len
& (8 - 1)) {
189 * Fowler/Noll/Vo hash
191 * The basis of the hash algorithm was taken from an idea sent by email to the
192 * IEEE Posix P1003.2 mailing list from Phong Vo (kpv@research.att.com) and
193 * Glenn Fowler (gsf@research.att.com). Landon Curt Noll (chongo@toad.com)
194 * later improved on their algorithm.
196 * The magic is in the interesting relationship between the special prime
197 * 16777619 (2^24 + 403) and 2^32 and 2^8.
199 * This hash produces the fewest collisions of any function that we've seen so
200 * far, and works well on both numbers and strings.
202 * PUBLIC: u_int32_t __ham_func5 __P((const void *, u_int32_t));
205 __ham_func5(key
, len
)
209 const u_int8_t
*k
, *e
;
214 for (h
= 0; k
< e
; ++k
) {