michael@0: /*- michael@0: * Copyright (c) 1990, 1993 michael@0: * The Regents of the University of California. All rights reserved. michael@0: * michael@0: * This code is derived from software contributed to Berkeley by michael@0: * Margo Seltzer. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. ***REMOVED*** - see michael@0: * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change michael@0: * 4. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: */ michael@0: michael@0: #if defined(LIBC_SCCS) && !defined(lint) michael@0: static char sccsid[] = "@(#)hash_func.c 8.2 (Berkeley) 2/21/94"; michael@0: #endif /* LIBC_SCCS and not lint */ michael@0: michael@0: #ifndef macintosh michael@0: #include michael@0: #endif michael@0: #include "mcom_db.h" michael@0: #include "hash.h" michael@0: #include "page.h" michael@0: /* #include "extern.h" */ michael@0: michael@0: #if 0 michael@0: static uint32 hash1 __P((const void *, size_t)); michael@0: static uint32 hash2 __P((const void *, size_t)); michael@0: static uint32 hash3 __P((const void *, size_t)); michael@0: #endif michael@0: static uint32 hash4 __P((const void *, size_t)); michael@0: michael@0: /* Global default hash function */ michael@0: uint32 (*__default_hash) __P((const void *, size_t)) = hash4; michael@0: michael@0: /* michael@0: * HASH FUNCTIONS michael@0: * michael@0: * Assume that we've already split the bucket to which this key hashes, michael@0: * calculate that bucket, and check that in fact we did already split it. michael@0: * michael@0: * This came from ejb's hsearch. michael@0: */ michael@0: michael@0: #define PRIME1 37 michael@0: #define PRIME2 1048583 michael@0: michael@0: #if 0 michael@0: static uint32 michael@0: hash1(const void *keyarg, register size_t len) michael@0: { michael@0: register const uint8 *key; michael@0: register uint32 h; michael@0: michael@0: /* Convert string to integer */ michael@0: for (key = (const uint8 *)keyarg, h = 0; len--;) michael@0: h = h * PRIME1 ^ (*key++ - ' '); michael@0: h %= PRIME2; michael@0: return (h); michael@0: } michael@0: michael@0: /* michael@0: * Phong's linear congruential hash michael@0: */ michael@0: #define dcharhash(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c)) michael@0: michael@0: static uint32 michael@0: hash2(const void *keyarg, size_t len) michael@0: { michael@0: register const uint8 *e, *key; michael@0: register uint32 h; michael@0: register uint8 c; michael@0: michael@0: key = (const uint8 *)keyarg; michael@0: e = key + len; michael@0: for (h = 0; key != e;) { michael@0: c = *key++; michael@0: if (!c && key > e) michael@0: break; michael@0: dcharhash(h, c); michael@0: } michael@0: return (h); michael@0: } michael@0: michael@0: /* michael@0: * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte michael@0: * units. On the first time through the loop we get the "leftover bytes" michael@0: * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle michael@0: * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If michael@0: * this routine is heavily used enough, it's worth the ugly coding. michael@0: * michael@0: * OZ's original sdbm hash michael@0: */ michael@0: static uint32 michael@0: hash3(const void *keyarg, register size_t len) michael@0: { michael@0: register const uint8 *key; michael@0: register size_t loop; michael@0: register uint32 h; michael@0: michael@0: #define HASHC h = *key++ + 65599 * h michael@0: michael@0: h = 0; michael@0: key = (const uint8 *)keyarg; michael@0: if (len > 0) { michael@0: loop = (len + 8 - 1) >> 3; michael@0: michael@0: switch (len & (8 - 1)) { michael@0: case 0: michael@0: do { michael@0: HASHC; michael@0: /* FALLTHROUGH */ michael@0: case 7: michael@0: HASHC; michael@0: /* FALLTHROUGH */ michael@0: case 6: michael@0: HASHC; michael@0: /* FALLTHROUGH */ michael@0: case 5: michael@0: HASHC; michael@0: /* FALLTHROUGH */ michael@0: case 4: michael@0: HASHC; michael@0: /* FALLTHROUGH */ michael@0: case 3: michael@0: HASHC; michael@0: /* FALLTHROUGH */ michael@0: case 2: michael@0: HASHC; michael@0: /* FALLTHROUGH */ michael@0: case 1: michael@0: HASHC; michael@0: } while (--loop); michael@0: } michael@0: } michael@0: return (h); michael@0: } michael@0: #endif /* 0 */ michael@0: michael@0: /* Hash function from Chris Torek. */ michael@0: static uint32 michael@0: hash4(const void *keyarg, register size_t len) michael@0: { michael@0: register const uint8 *key; michael@0: register size_t loop; michael@0: register uint32 h; michael@0: michael@0: #define HASH4a h = (h << 5) - h + *key++; michael@0: #define HASH4b h = (h << 5) + h + *key++; michael@0: #define HASH4 HASH4b michael@0: michael@0: h = 0; michael@0: key = (const uint8 *)keyarg; michael@0: if (len > 0) { michael@0: loop = (len + 8 - 1) >> 3; michael@0: michael@0: switch (len & (8 - 1)) { michael@0: case 0: michael@0: do { michael@0: HASH4; michael@0: /* FALLTHROUGH */ michael@0: case 7: michael@0: HASH4; michael@0: /* FALLTHROUGH */ michael@0: case 6: michael@0: HASH4; michael@0: /* FALLTHROUGH */ michael@0: case 5: michael@0: HASH4; michael@0: /* FALLTHROUGH */ michael@0: case 4: michael@0: HASH4; michael@0: /* FALLTHROUGH */ michael@0: case 3: michael@0: HASH4; michael@0: /* FALLTHROUGH */ michael@0: case 2: michael@0: HASH4; michael@0: /* FALLTHROUGH */ michael@0: case 1: michael@0: HASH4; michael@0: } while (--loop); michael@0: } michael@0: } michael@0: return (h); michael@0: }