xref: /freebsd/lib/libc/db/hash/hash_func.c (revision 58f0484fa251c266ede97b591b499fe3dd4f578e)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Margo Seltzer.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3558f0484fSRodney W. Grimes  */
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3858f0484fSRodney W. Grimes static char sccsid[] = "@(#)hash_func.c	8.2 (Berkeley) 2/21/94";
3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <sys/types.h>
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <db.h>
4458f0484fSRodney W. Grimes #include "hash.h"
4558f0484fSRodney W. Grimes #include "page.h"
4658f0484fSRodney W. Grimes #include "extern.h"
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes static u_int32_t hash1 __P((const void *, size_t));
4958f0484fSRodney W. Grimes static u_int32_t hash2 __P((const void *, size_t));
5058f0484fSRodney W. Grimes static u_int32_t hash3 __P((const void *, size_t));
5158f0484fSRodney W. Grimes static u_int32_t hash4 __P((const void *, size_t));
5258f0484fSRodney W. Grimes 
5358f0484fSRodney W. Grimes /* Global default hash function */
5458f0484fSRodney W. Grimes u_int32_t (*__default_hash) __P((const void *, size_t)) = hash4;
5558f0484fSRodney W. Grimes 
5658f0484fSRodney W. Grimes /*
5758f0484fSRodney W. Grimes  * HASH FUNCTIONS
5858f0484fSRodney W. Grimes  *
5958f0484fSRodney W. Grimes  * Assume that we've already split the bucket to which this key hashes,
6058f0484fSRodney W. Grimes  * calculate that bucket, and check that in fact we did already split it.
6158f0484fSRodney W. Grimes  *
6258f0484fSRodney W. Grimes  * This came from ejb's hsearch.
6358f0484fSRodney W. Grimes  */
6458f0484fSRodney W. Grimes 
6558f0484fSRodney W. Grimes #define PRIME1		37
6658f0484fSRodney W. Grimes #define PRIME2		1048583
6758f0484fSRodney W. Grimes 
6858f0484fSRodney W. Grimes static u_int32_t
6958f0484fSRodney W. Grimes hash1(keyarg, len)
7058f0484fSRodney W. Grimes 	const void *keyarg;
7158f0484fSRodney W. Grimes 	register size_t len;
7258f0484fSRodney W. Grimes {
7358f0484fSRodney W. Grimes 	register const u_char *key;
7458f0484fSRodney W. Grimes 	register u_int32_t h;
7558f0484fSRodney W. Grimes 
7658f0484fSRodney W. Grimes 	/* Convert string to integer */
7758f0484fSRodney W. Grimes 	for (key = keyarg, h = 0; len--;)
7858f0484fSRodney W. Grimes 		h = h * PRIME1 ^ (*key++ - ' ');
7958f0484fSRodney W. Grimes 	h %= PRIME2;
8058f0484fSRodney W. Grimes 	return (h);
8158f0484fSRodney W. Grimes }
8258f0484fSRodney W. Grimes 
8358f0484fSRodney W. Grimes /*
8458f0484fSRodney W. Grimes  * Phong's linear congruential hash
8558f0484fSRodney W. Grimes  */
8658f0484fSRodney W. Grimes #define dcharhash(h, c)	((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
8758f0484fSRodney W. Grimes 
8858f0484fSRodney W. Grimes static u_int32_t
8958f0484fSRodney W. Grimes hash2(keyarg, len)
9058f0484fSRodney W. Grimes 	const void *keyarg;
9158f0484fSRodney W. Grimes 	size_t len;
9258f0484fSRodney W. Grimes {
9358f0484fSRodney W. Grimes 	register const u_char *e, *key;
9458f0484fSRodney W. Grimes 	register u_int32_t h;
9558f0484fSRodney W. Grimes 	register u_char c;
9658f0484fSRodney W. Grimes 
9758f0484fSRodney W. Grimes 	key = keyarg;
9858f0484fSRodney W. Grimes 	e = key + len;
9958f0484fSRodney W. Grimes 	for (h = 0; key != e;) {
10058f0484fSRodney W. Grimes 		c = *key++;
10158f0484fSRodney W. Grimes 		if (!c && key > e)
10258f0484fSRodney W. Grimes 			break;
10358f0484fSRodney W. Grimes 		dcharhash(h, c);
10458f0484fSRodney W. Grimes 	}
10558f0484fSRodney W. Grimes 	return (h);
10658f0484fSRodney W. Grimes }
10758f0484fSRodney W. Grimes 
10858f0484fSRodney W. Grimes /*
10958f0484fSRodney W. Grimes  * This is INCREDIBLY ugly, but fast.  We break the string up into 8 byte
11058f0484fSRodney W. Grimes  * units.  On the first time through the loop we get the "leftover bytes"
11158f0484fSRodney W. Grimes  * (strlen % 8).  On every other iteration, we perform 8 HASHC's so we handle
11258f0484fSRodney W. Grimes  * all 8 bytes.  Essentially, this saves us 7 cmp & branch instructions.  If
11358f0484fSRodney W. Grimes  * this routine is heavily used enough, it's worth the ugly coding.
11458f0484fSRodney W. Grimes  *
11558f0484fSRodney W. Grimes  * OZ's original sdbm hash
11658f0484fSRodney W. Grimes  */
11758f0484fSRodney W. Grimes static u_int32_t
11858f0484fSRodney W. Grimes hash3(keyarg, len)
11958f0484fSRodney W. Grimes 	const void *keyarg;
12058f0484fSRodney W. Grimes 	register size_t len;
12158f0484fSRodney W. Grimes {
12258f0484fSRodney W. Grimes 	register const u_char *key;
12358f0484fSRodney W. Grimes 	register size_t loop;
12458f0484fSRodney W. Grimes 	register u_int32_t h;
12558f0484fSRodney W. Grimes 
12658f0484fSRodney W. Grimes #define HASHC   h = *key++ + 65599 * h
12758f0484fSRodney W. Grimes 
12858f0484fSRodney W. Grimes 	h = 0;
12958f0484fSRodney W. Grimes 	key = keyarg;
13058f0484fSRodney W. Grimes 	if (len > 0) {
13158f0484fSRodney W. Grimes 		loop = (len + 8 - 1) >> 3;
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes 		switch (len & (8 - 1)) {
13458f0484fSRodney W. Grimes 		case 0:
13558f0484fSRodney W. Grimes 			do {
13658f0484fSRodney W. Grimes 				HASHC;
13758f0484fSRodney W. Grimes 				/* FALLTHROUGH */
13858f0484fSRodney W. Grimes 		case 7:
13958f0484fSRodney W. Grimes 				HASHC;
14058f0484fSRodney W. Grimes 				/* FALLTHROUGH */
14158f0484fSRodney W. Grimes 		case 6:
14258f0484fSRodney W. Grimes 				HASHC;
14358f0484fSRodney W. Grimes 				/* FALLTHROUGH */
14458f0484fSRodney W. Grimes 		case 5:
14558f0484fSRodney W. Grimes 				HASHC;
14658f0484fSRodney W. Grimes 				/* FALLTHROUGH */
14758f0484fSRodney W. Grimes 		case 4:
14858f0484fSRodney W. Grimes 				HASHC;
14958f0484fSRodney W. Grimes 				/* FALLTHROUGH */
15058f0484fSRodney W. Grimes 		case 3:
15158f0484fSRodney W. Grimes 				HASHC;
15258f0484fSRodney W. Grimes 				/* FALLTHROUGH */
15358f0484fSRodney W. Grimes 		case 2:
15458f0484fSRodney W. Grimes 				HASHC;
15558f0484fSRodney W. Grimes 				/* FALLTHROUGH */
15658f0484fSRodney W. Grimes 		case 1:
15758f0484fSRodney W. Grimes 				HASHC;
15858f0484fSRodney W. Grimes 			} while (--loop);
15958f0484fSRodney W. Grimes 		}
16058f0484fSRodney W. Grimes 	}
16158f0484fSRodney W. Grimes 	return (h);
16258f0484fSRodney W. Grimes }
16358f0484fSRodney W. Grimes 
16458f0484fSRodney W. Grimes /* Hash function from Chris Torek. */
16558f0484fSRodney W. Grimes static u_int32_t
16658f0484fSRodney W. Grimes hash4(keyarg, len)
16758f0484fSRodney W. Grimes 	const void *keyarg;
16858f0484fSRodney W. Grimes 	register size_t len;
16958f0484fSRodney W. Grimes {
17058f0484fSRodney W. Grimes 	register const u_char *key;
17158f0484fSRodney W. Grimes 	register size_t loop;
17258f0484fSRodney W. Grimes 	register u_int32_t h;
17358f0484fSRodney W. Grimes 
17458f0484fSRodney W. Grimes #define HASH4a   h = (h << 5) - h + *key++;
17558f0484fSRodney W. Grimes #define HASH4b   h = (h << 5) + h + *key++;
17658f0484fSRodney W. Grimes #define HASH4 HASH4b
17758f0484fSRodney W. Grimes 
17858f0484fSRodney W. Grimes 	h = 0;
17958f0484fSRodney W. Grimes 	key = keyarg;
18058f0484fSRodney W. Grimes 	if (len > 0) {
18158f0484fSRodney W. Grimes 		loop = (len + 8 - 1) >> 3;
18258f0484fSRodney W. Grimes 
18358f0484fSRodney W. Grimes 		switch (len & (8 - 1)) {
18458f0484fSRodney W. Grimes 		case 0:
18558f0484fSRodney W. Grimes 			do {
18658f0484fSRodney W. Grimes 				HASH4;
18758f0484fSRodney W. Grimes 				/* FALLTHROUGH */
18858f0484fSRodney W. Grimes 		case 7:
18958f0484fSRodney W. Grimes 				HASH4;
19058f0484fSRodney W. Grimes 				/* FALLTHROUGH */
19158f0484fSRodney W. Grimes 		case 6:
19258f0484fSRodney W. Grimes 				HASH4;
19358f0484fSRodney W. Grimes 				/* FALLTHROUGH */
19458f0484fSRodney W. Grimes 		case 5:
19558f0484fSRodney W. Grimes 				HASH4;
19658f0484fSRodney W. Grimes 				/* FALLTHROUGH */
19758f0484fSRodney W. Grimes 		case 4:
19858f0484fSRodney W. Grimes 				HASH4;
19958f0484fSRodney W. Grimes 				/* FALLTHROUGH */
20058f0484fSRodney W. Grimes 		case 3:
20158f0484fSRodney W. Grimes 				HASH4;
20258f0484fSRodney W. Grimes 				/* FALLTHROUGH */
20358f0484fSRodney W. Grimes 		case 2:
20458f0484fSRodney W. Grimes 				HASH4;
20558f0484fSRodney W. Grimes 				/* FALLTHROUGH */
20658f0484fSRodney W. Grimes 		case 1:
20758f0484fSRodney W. Grimes 				HASH4;
20858f0484fSRodney W. Grimes 			} while (--loop);
20958f0484fSRodney W. Grimes 		}
21058f0484fSRodney W. Grimes 	}
21158f0484fSRodney W. Grimes 	return (h);
21258f0484fSRodney W. Grimes }
213