17c478bd9Sstevel@tonic-gate /*-
27c478bd9Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, 1997
57c478bd9Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
67c478bd9Sstevel@tonic-gate  */
77c478bd9Sstevel@tonic-gate /*
87c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993
97c478bd9Sstevel@tonic-gate  *	Margo Seltzer.  All rights reserved.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate /*
127c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993
137c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
167c478bd9Sstevel@tonic-gate  * Margo Seltzer.
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
197c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
207c478bd9Sstevel@tonic-gate  * are met:
217c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
227c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
237c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
247c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
257c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
267c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
277c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
287c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
297c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
307c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
317c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
327c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
357c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
367c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
377c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
387c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
397c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
407c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
417c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
427c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
437c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
447c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Copyright (c) 1998 by Sun Microsystems, Inc.
487c478bd9Sstevel@tonic-gate  * All rights reserved.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include "config.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include "db_int.h"
587c478bd9Sstevel@tonic-gate #include "db_page.h"
597c478bd9Sstevel@tonic-gate #include "hash.h"
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * __ham_func2 --
637c478bd9Sstevel@tonic-gate  *	Phong Vo's linear congruential hash.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * PUBLIC: u_int32_t __ham_func2 __P((const void *, u_int32_t));
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate #define	DCHARHASH(h, c)	((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate u_int32_t
__ham_func2(key,len)707c478bd9Sstevel@tonic-gate __ham_func2(key, len)
717c478bd9Sstevel@tonic-gate 	const void *key;
727c478bd9Sstevel@tonic-gate 	u_int32_t len;
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	const u_int8_t *e, *k;
757c478bd9Sstevel@tonic-gate 	u_int32_t h;
767c478bd9Sstevel@tonic-gate 	u_int8_t c;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	k = key;
797c478bd9Sstevel@tonic-gate 	e = k + len;
807c478bd9Sstevel@tonic-gate 	for (h = 0; k != e;) {
817c478bd9Sstevel@tonic-gate 		c = *k++;
827c478bd9Sstevel@tonic-gate 		if (!c && k > e)
837c478bd9Sstevel@tonic-gate 			break;
847c478bd9Sstevel@tonic-gate 		DCHARHASH(h, c);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	return (h);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * __ham_func3 --
917c478bd9Sstevel@tonic-gate  *	Ozan Yigit's original sdbm hash.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * Ugly, but fast.  Break the string up into 8 byte units.  On the first time
947c478bd9Sstevel@tonic-gate  * through the loop get the "leftover bytes" (strlen % 8).  On every other
957c478bd9Sstevel@tonic-gate  * iteration, perform 8 HASHC's so we handle all 8 bytes.  Essentially, this
967c478bd9Sstevel@tonic-gate  * saves us 7 cmp & branch instructions.
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * PUBLIC: u_int32_t __ham_func3 __P((const void *, u_int32_t));
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate u_int32_t
__ham_func3(key,len)1017c478bd9Sstevel@tonic-gate __ham_func3(key, len)
1027c478bd9Sstevel@tonic-gate 	const void *key;
1037c478bd9Sstevel@tonic-gate 	u_int32_t len;
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	const u_int8_t *k;
1067c478bd9Sstevel@tonic-gate 	u_int32_t n, loop;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (len == 0)
1097c478bd9Sstevel@tonic-gate 		return (0);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	HASHC	n = *k++ + 65599 * n
1127c478bd9Sstevel@tonic-gate 	n = 0;
1137c478bd9Sstevel@tonic-gate 	k = key;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	loop = (len + 8 - 1) >> 3;
1167c478bd9Sstevel@tonic-gate 	switch (len & (8 - 1)) {
1177c478bd9Sstevel@tonic-gate 	case 0:
1187c478bd9Sstevel@tonic-gate 		do {
1197c478bd9Sstevel@tonic-gate 			HASHC;
120*fec46055SToomas Soome 			/* FALLTHROUGH */
1217c478bd9Sstevel@tonic-gate 	case 7:
1227c478bd9Sstevel@tonic-gate 			HASHC;
123*fec46055SToomas Soome 			/* FALLTHROUGH */
1247c478bd9Sstevel@tonic-gate 	case 6:
1257c478bd9Sstevel@tonic-gate 			HASHC;
126*fec46055SToomas Soome 			/* FALLTHROUGH */
1277c478bd9Sstevel@tonic-gate 	case 5:
1287c478bd9Sstevel@tonic-gate 			HASHC;
129*fec46055SToomas Soome 			/* FALLTHROUGH */
1307c478bd9Sstevel@tonic-gate 	case 4:
1317c478bd9Sstevel@tonic-gate 			HASHC;
132*fec46055SToomas Soome 			/* FALLTHROUGH */
1337c478bd9Sstevel@tonic-gate 	case 3:
1347c478bd9Sstevel@tonic-gate 			HASHC;
135*fec46055SToomas Soome 			/* FALLTHROUGH */
1367c478bd9Sstevel@tonic-gate 	case 2:
1377c478bd9Sstevel@tonic-gate 			HASHC;
138*fec46055SToomas Soome 			/* FALLTHROUGH */
1397c478bd9Sstevel@tonic-gate 	case 1:
1407c478bd9Sstevel@tonic-gate 			HASHC;
1417c478bd9Sstevel@tonic-gate 		} while (--loop);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	return (n);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * __ham_func4 --
1487c478bd9Sstevel@tonic-gate  *	Chris Torek's hash function.  Although this function performs only
1497c478bd9Sstevel@tonic-gate  *	slightly worse than __ham_func5 on strings, it performs horribly on
1507c478bd9Sstevel@tonic-gate  *	numbers.
1517c478bd9Sstevel@tonic-gate  *
1527c478bd9Sstevel@tonic-gate  * PUBLIC: u_int32_t __ham_func4 __P((const void *, u_int32_t));
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate u_int32_t
__ham_func4(key,len)1557c478bd9Sstevel@tonic-gate __ham_func4(key, len)
1567c478bd9Sstevel@tonic-gate 	const void *key;
1577c478bd9Sstevel@tonic-gate 	u_int32_t len;
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	const u_int8_t *k;
1607c478bd9Sstevel@tonic-gate 	u_int32_t h, loop;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (len == 0)
1637c478bd9Sstevel@tonic-gate 		return (0);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #define	HASH4a	h = (h << 5) - h + *k++;
1667c478bd9Sstevel@tonic-gate #define	HASH4b	h = (h << 5) + h + *k++;
1677c478bd9Sstevel@tonic-gate #define	HASH4	HASH4b
1687c478bd9Sstevel@tonic-gate 	h = 0;
1697c478bd9Sstevel@tonic-gate 	k = key;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	loop = (len + 8 - 1) >> 3;
1727c478bd9Sstevel@tonic-gate 	switch (len & (8 - 1)) {
1737c478bd9Sstevel@tonic-gate 	case 0:
1747c478bd9Sstevel@tonic-gate 		do {
1757c478bd9Sstevel@tonic-gate 			HASH4;
176*fec46055SToomas Soome 			/* FALLTHROUGH */
1777c478bd9Sstevel@tonic-gate 	case 7:
1787c478bd9Sstevel@tonic-gate 			HASH4;
179*fec46055SToomas Soome 			/* FALLTHROUGH */
1807c478bd9Sstevel@tonic-gate 	case 6:
1817c478bd9Sstevel@tonic-gate 			HASH4;
182*fec46055SToomas Soome 			/* FALLTHROUGH */
1837c478bd9Sstevel@tonic-gate 	case 5:
1847c478bd9Sstevel@tonic-gate 			HASH4;
185*fec46055SToomas Soome 			/* FALLTHROUGH */
1867c478bd9Sstevel@tonic-gate 	case 4:
1877c478bd9Sstevel@tonic-gate 			HASH4;
188*fec46055SToomas Soome 			/* FALLTHROUGH */
1897c478bd9Sstevel@tonic-gate 	case 3:
1907c478bd9Sstevel@tonic-gate 			HASH4;
191*fec46055SToomas Soome 			/* FALLTHROUGH */
1927c478bd9Sstevel@tonic-gate 	case 2:
1937c478bd9Sstevel@tonic-gate 			HASH4;
194*fec46055SToomas Soome 			/* FALLTHROUGH */
1957c478bd9Sstevel@tonic-gate 	case 1:
1967c478bd9Sstevel@tonic-gate 			HASH4;
1977c478bd9Sstevel@tonic-gate 		} while (--loop);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	return (h);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Fowler/Noll/Vo hash
2047c478bd9Sstevel@tonic-gate  *
2057c478bd9Sstevel@tonic-gate  * The basis of the hash algorithm was taken from an idea sent by email to the
2067c478bd9Sstevel@tonic-gate  * IEEE Posix P1003.2 mailing list from Phong Vo (kpv@research.att.com) and
2077c478bd9Sstevel@tonic-gate  * Glenn Fowler (gsf@research.att.com).  Landon Curt Noll (chongo@toad.com)
2087c478bd9Sstevel@tonic-gate  * later improved on their algorithm.
2097c478bd9Sstevel@tonic-gate  *
2107c478bd9Sstevel@tonic-gate  * The magic is in the interesting relationship between the special prime
2117c478bd9Sstevel@tonic-gate  * 16777619 (2^24 + 403) and 2^32 and 2^8.
2127c478bd9Sstevel@tonic-gate  *
2137c478bd9Sstevel@tonic-gate  * This hash produces the fewest collisions of any function that we've seen so
2147c478bd9Sstevel@tonic-gate  * far, and works well on both numbers and strings.
2157c478bd9Sstevel@tonic-gate  *
2167c478bd9Sstevel@tonic-gate  * PUBLIC: u_int32_t __ham_func5 __P((const void *, u_int32_t));
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate u_int32_t
__ham_func5(key,len)2197c478bd9Sstevel@tonic-gate __ham_func5(key, len)
2207c478bd9Sstevel@tonic-gate 	const void *key;
2217c478bd9Sstevel@tonic-gate 	u_int32_t len;
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	const u_int8_t *k, *e;
2247c478bd9Sstevel@tonic-gate         u_int32_t h;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	k = key;
2277c478bd9Sstevel@tonic-gate 	e = k + len;
2287c478bd9Sstevel@tonic-gate         for (h = 0; k < e; ++k) {
2297c478bd9Sstevel@tonic-gate                 h *= 16777619;
2307c478bd9Sstevel@tonic-gate                 h ^= *k;
2317c478bd9Sstevel@tonic-gate         }
2327c478bd9Sstevel@tonic-gate         return (h);
2337c478bd9Sstevel@tonic-gate }
234