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 */ 408fb3f3f6SDavid E. O'Brien #include <sys/cdefs.h> 418fb3f3f6SDavid E. O'Brien __FBSDID("$FreeBSD$"); 4258f0484fSRodney W. Grimes 4358f0484fSRodney W. Grimes #include <sys/types.h> 4458f0484fSRodney W. Grimes 4558f0484fSRodney W. Grimes #include <db.h> 4658f0484fSRodney W. Grimes #include "hash.h" 4758f0484fSRodney W. Grimes #include "page.h" 4858f0484fSRodney W. Grimes #include "extern.h" 4958f0484fSRodney W. Grimes 5058f0484fSRodney W. Grimes static u_int32_t hash1 __P((const void *, size_t)); 5158f0484fSRodney W. Grimes static u_int32_t hash2 __P((const void *, size_t)); 5258f0484fSRodney W. Grimes static u_int32_t hash3 __P((const void *, size_t)); 5358f0484fSRodney W. Grimes static u_int32_t hash4 __P((const void *, size_t)); 5458f0484fSRodney W. Grimes 5558f0484fSRodney W. Grimes /* Global default hash function */ 5658f0484fSRodney W. Grimes u_int32_t (*__default_hash) __P((const void *, size_t)) = hash4; 5758f0484fSRodney W. Grimes 5858f0484fSRodney W. Grimes /* 5958f0484fSRodney W. Grimes * HASH FUNCTIONS 6058f0484fSRodney W. Grimes * 6158f0484fSRodney W. Grimes * Assume that we've already split the bucket to which this key hashes, 6258f0484fSRodney W. Grimes * calculate that bucket, and check that in fact we did already split it. 6358f0484fSRodney W. Grimes * 6458f0484fSRodney W. Grimes * This came from ejb's hsearch. 6558f0484fSRodney W. Grimes */ 6658f0484fSRodney W. Grimes 6758f0484fSRodney W. Grimes #define PRIME1 37 6858f0484fSRodney W. Grimes #define PRIME2 1048583 6958f0484fSRodney W. Grimes 7058f0484fSRodney W. Grimes static u_int32_t 7158f0484fSRodney W. Grimes hash1(keyarg, len) 7258f0484fSRodney W. Grimes const void *keyarg; 738fb3f3f6SDavid E. O'Brien size_t len; 7458f0484fSRodney W. Grimes { 758fb3f3f6SDavid E. O'Brien const u_char *key; 768fb3f3f6SDavid E. O'Brien u_int32_t h; 7758f0484fSRodney W. Grimes 7858f0484fSRodney W. Grimes /* Convert string to integer */ 7958f0484fSRodney W. Grimes for (key = keyarg, h = 0; len--;) 8058f0484fSRodney W. Grimes h = h * PRIME1 ^ (*key++ - ' '); 8158f0484fSRodney W. Grimes h %= PRIME2; 8258f0484fSRodney W. Grimes return (h); 8358f0484fSRodney W. Grimes } 8458f0484fSRodney W. Grimes 8558f0484fSRodney W. Grimes /* 8658f0484fSRodney W. Grimes * Phong's linear congruential hash 8758f0484fSRodney W. Grimes */ 8858f0484fSRodney W. Grimes #define dcharhash(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c)) 8958f0484fSRodney W. Grimes 9058f0484fSRodney W. Grimes static u_int32_t 9158f0484fSRodney W. Grimes hash2(keyarg, len) 9258f0484fSRodney W. Grimes const void *keyarg; 9358f0484fSRodney W. Grimes size_t len; 9458f0484fSRodney W. Grimes { 958fb3f3f6SDavid E. O'Brien const u_char *e, *key; 968fb3f3f6SDavid E. O'Brien u_int32_t h; 978fb3f3f6SDavid E. O'Brien u_char c; 9858f0484fSRodney W. Grimes 9958f0484fSRodney W. Grimes key = keyarg; 10058f0484fSRodney W. Grimes e = key + len; 10158f0484fSRodney W. Grimes for (h = 0; key != e;) { 10258f0484fSRodney W. Grimes c = *key++; 10358f0484fSRodney W. Grimes if (!c && key > e) 10458f0484fSRodney W. Grimes break; 10558f0484fSRodney W. Grimes dcharhash(h, c); 10658f0484fSRodney W. Grimes } 10758f0484fSRodney W. Grimes return (h); 10858f0484fSRodney W. Grimes } 10958f0484fSRodney W. Grimes 11058f0484fSRodney W. Grimes /* 11158f0484fSRodney W. Grimes * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte 11258f0484fSRodney W. Grimes * units. On the first time through the loop we get the "leftover bytes" 11358f0484fSRodney W. Grimes * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle 11458f0484fSRodney W. Grimes * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If 11558f0484fSRodney W. Grimes * this routine is heavily used enough, it's worth the ugly coding. 11658f0484fSRodney W. Grimes * 11758f0484fSRodney W. Grimes * OZ's original sdbm hash 11858f0484fSRodney W. Grimes */ 11958f0484fSRodney W. Grimes static u_int32_t 12058f0484fSRodney W. Grimes hash3(keyarg, len) 12158f0484fSRodney W. Grimes const void *keyarg; 1228fb3f3f6SDavid E. O'Brien size_t len; 12358f0484fSRodney W. Grimes { 1248fb3f3f6SDavid E. O'Brien const u_char *key; 1258fb3f3f6SDavid E. O'Brien size_t loop; 1268fb3f3f6SDavid E. O'Brien u_int32_t h; 12758f0484fSRodney W. Grimes 12858f0484fSRodney W. Grimes #define HASHC h = *key++ + 65599 * h 12958f0484fSRodney W. Grimes 13058f0484fSRodney W. Grimes h = 0; 13158f0484fSRodney W. Grimes key = keyarg; 13258f0484fSRodney W. Grimes if (len > 0) { 13358f0484fSRodney W. Grimes loop = (len + 8 - 1) >> 3; 13458f0484fSRodney W. Grimes 13558f0484fSRodney W. Grimes switch (len & (8 - 1)) { 13658f0484fSRodney W. Grimes case 0: 13758f0484fSRodney W. Grimes do { 13858f0484fSRodney W. Grimes HASHC; 13958f0484fSRodney W. Grimes /* FALLTHROUGH */ 14058f0484fSRodney W. Grimes case 7: 14158f0484fSRodney W. Grimes HASHC; 14258f0484fSRodney W. Grimes /* FALLTHROUGH */ 14358f0484fSRodney W. Grimes case 6: 14458f0484fSRodney W. Grimes HASHC; 14558f0484fSRodney W. Grimes /* FALLTHROUGH */ 14658f0484fSRodney W. Grimes case 5: 14758f0484fSRodney W. Grimes HASHC; 14858f0484fSRodney W. Grimes /* FALLTHROUGH */ 14958f0484fSRodney W. Grimes case 4: 15058f0484fSRodney W. Grimes HASHC; 15158f0484fSRodney W. Grimes /* FALLTHROUGH */ 15258f0484fSRodney W. Grimes case 3: 15358f0484fSRodney W. Grimes HASHC; 15458f0484fSRodney W. Grimes /* FALLTHROUGH */ 15558f0484fSRodney W. Grimes case 2: 15658f0484fSRodney W. Grimes HASHC; 15758f0484fSRodney W. Grimes /* FALLTHROUGH */ 15858f0484fSRodney W. Grimes case 1: 15958f0484fSRodney W. Grimes HASHC; 16058f0484fSRodney W. Grimes } while (--loop); 16158f0484fSRodney W. Grimes } 16258f0484fSRodney W. Grimes } 16358f0484fSRodney W. Grimes return (h); 16458f0484fSRodney W. Grimes } 16558f0484fSRodney W. Grimes 16658f0484fSRodney W. Grimes /* Hash function from Chris Torek. */ 16758f0484fSRodney W. Grimes static u_int32_t 16858f0484fSRodney W. Grimes hash4(keyarg, len) 16958f0484fSRodney W. Grimes const void *keyarg; 1708fb3f3f6SDavid E. O'Brien size_t len; 17158f0484fSRodney W. Grimes { 1728fb3f3f6SDavid E. O'Brien const u_char *key; 1738fb3f3f6SDavid E. O'Brien size_t loop; 1748fb3f3f6SDavid E. O'Brien u_int32_t h; 17558f0484fSRodney W. Grimes 17658f0484fSRodney W. Grimes #define HASH4a h = (h << 5) - h + *key++; 17758f0484fSRodney W. Grimes #define HASH4b h = (h << 5) + h + *key++; 17858f0484fSRodney W. Grimes #define HASH4 HASH4b 17958f0484fSRodney W. Grimes 18058f0484fSRodney W. Grimes h = 0; 18158f0484fSRodney W. Grimes key = keyarg; 18258f0484fSRodney W. Grimes if (len > 0) { 18358f0484fSRodney W. Grimes loop = (len + 8 - 1) >> 3; 18458f0484fSRodney W. Grimes 18558f0484fSRodney W. Grimes switch (len & (8 - 1)) { 18658f0484fSRodney W. Grimes case 0: 18758f0484fSRodney W. Grimes do { 18858f0484fSRodney W. Grimes HASH4; 18958f0484fSRodney W. Grimes /* FALLTHROUGH */ 19058f0484fSRodney W. Grimes case 7: 19158f0484fSRodney W. Grimes HASH4; 19258f0484fSRodney W. Grimes /* FALLTHROUGH */ 19358f0484fSRodney W. Grimes case 6: 19458f0484fSRodney W. Grimes HASH4; 19558f0484fSRodney W. Grimes /* FALLTHROUGH */ 19658f0484fSRodney W. Grimes case 5: 19758f0484fSRodney W. Grimes HASH4; 19858f0484fSRodney W. Grimes /* FALLTHROUGH */ 19958f0484fSRodney W. Grimes case 4: 20058f0484fSRodney W. Grimes HASH4; 20158f0484fSRodney W. Grimes /* FALLTHROUGH */ 20258f0484fSRodney W. Grimes case 3: 20358f0484fSRodney W. Grimes HASH4; 20458f0484fSRodney W. Grimes /* FALLTHROUGH */ 20558f0484fSRodney W. Grimes case 2: 20658f0484fSRodney W. Grimes HASH4; 20758f0484fSRodney W. Grimes /* FALLTHROUGH */ 20858f0484fSRodney W. Grimes case 1: 20958f0484fSRodney W. Grimes HASH4; 21058f0484fSRodney W. Grimes } while (--loop); 21158f0484fSRodney W. Grimes } 21258f0484fSRodney W. Grimes } 21358f0484fSRodney W. Grimes return (h); 21458f0484fSRodney W. Grimes } 215