1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1996, 1997
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1990, 1993
1N/A * Margo Seltzer. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1990, 1993
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * This code is derived from software contributed to Berkeley by
1N/A * Margo Seltzer.
1N/A *
1N/A * Redistribution and use in source and binary forms, with or without
1N/A * modification, are permitted provided that the following conditions
1N/A * are met:
1N/A * 1. Redistributions of source code must retain the above copyright
1N/A * notice, this list of conditions and the following disclaimer.
1N/A * 2. Redistributions in binary form must reproduce the above copyright
1N/A * notice, this list of conditions and the following disclaimer in the
1N/A * documentation and/or other materials provided with the distribution.
1N/A * 3. All advertising materials mentioning features or use of this software
1N/A * must display the following acknowledgement:
1N/A * This product includes software developed by the University of
1N/A * California, Berkeley and its contributors.
1N/A * 4. Neither the name of the University nor the names of its contributors
1N/A * may be used to endorse or promote products derived from this software
1N/A * without specific prior written permission.
1N/A *
1N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1N/A * SUCH DAMAGE.
1N/A */
1N/A/*
1N/A * Copyright (c) 1998 by Sun Microsystems, Inc.
1N/A * All rights reserved.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)hash_func.c 10.7 (Sleepycat) 9/16/97";
1N/Astatic const char sccsi2[] = "%W% (Sun) %G%";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "db_page.h"
1N/A#include "hash.h"
1N/A
1N/A/*
1N/A * __ham_func2 --
1N/A * Phong Vo's linear congruential hash.
1N/A *
1N/A * PUBLIC: u_int32_t __ham_func2 __P((const void *, u_int32_t));
1N/A */
1N/A#define DCHARHASH(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
1N/A
1N/Au_int32_t
1N/A__ham_func2(key, len)
1N/A const void *key;
1N/A u_int32_t len;
1N/A{
1N/A const u_int8_t *e, *k;
1N/A u_int32_t h;
1N/A u_int8_t c;
1N/A
1N/A k = key;
1N/A e = k + len;
1N/A for (h = 0; k != e;) {
1N/A c = *k++;
1N/A if (!c && k > e)
1N/A break;
1N/A DCHARHASH(h, c);
1N/A }
1N/A return (h);
1N/A}
1N/A
1N/A/*
1N/A * __ham_func3 --
1N/A * Ozan Yigit's original sdbm hash.
1N/A *
1N/A * Ugly, but fast. Break the string up into 8 byte units. On the first time
1N/A * through the loop get the "leftover bytes" (strlen % 8). On every other
1N/A * iteration, perform 8 HASHC's so we handle all 8 bytes. Essentially, this
1N/A * saves us 7 cmp & branch instructions.
1N/A *
1N/A * PUBLIC: u_int32_t __ham_func3 __P((const void *, u_int32_t));
1N/A */
1N/Au_int32_t
1N/A__ham_func3(key, len)
1N/A const void *key;
1N/A u_int32_t len;
1N/A{
1N/A const u_int8_t *k;
1N/A u_int32_t n, loop;
1N/A
1N/A if (len == 0)
1N/A return (0);
1N/A
1N/A#define HASHC n = *k++ + 65599 * n
1N/A n = 0;
1N/A k = key;
1N/A
1N/A loop = (len + 8 - 1) >> 3;
1N/A switch (len & (8 - 1)) {
1N/A case 0:
1N/A do {
1N/A HASHC;
1N/A case 7:
1N/A HASHC;
1N/A case 6:
1N/A HASHC;
1N/A case 5:
1N/A HASHC;
1N/A case 4:
1N/A HASHC;
1N/A case 3:
1N/A HASHC;
1N/A case 2:
1N/A HASHC;
1N/A case 1:
1N/A HASHC;
1N/A } while (--loop);
1N/A }
1N/A return (n);
1N/A}
1N/A
1N/A/*
1N/A * __ham_func4 --
1N/A * Chris Torek's hash function. Although this function performs only
1N/A * slightly worse than __ham_func5 on strings, it performs horribly on
1N/A * numbers.
1N/A *
1N/A * PUBLIC: u_int32_t __ham_func4 __P((const void *, u_int32_t));
1N/A */
1N/Au_int32_t
1N/A__ham_func4(key, len)
1N/A const void *key;
1N/A u_int32_t len;
1N/A{
1N/A const u_int8_t *k;
1N/A u_int32_t h, loop;
1N/A
1N/A if (len == 0)
1N/A return (0);
1N/A
1N/A#define HASH4a h = (h << 5) - h + *k++;
1N/A#define HASH4b h = (h << 5) + h + *k++;
1N/A#define HASH4 HASH4b
1N/A h = 0;
1N/A k = key;
1N/A
1N/A loop = (len + 8 - 1) >> 3;
1N/A switch (len & (8 - 1)) {
1N/A case 0:
1N/A do {
1N/A HASH4;
1N/A case 7:
1N/A HASH4;
1N/A case 6:
1N/A HASH4;
1N/A case 5:
1N/A HASH4;
1N/A case 4:
1N/A HASH4;
1N/A case 3:
1N/A HASH4;
1N/A case 2:
1N/A HASH4;
1N/A case 1:
1N/A HASH4;
1N/A } while (--loop);
1N/A }
1N/A return (h);
1N/A}
1N/A
1N/A/*
1N/A * Fowler/Noll/Vo hash
1N/A *
1N/A * The basis of the hash algorithm was taken from an idea sent by email to the
1N/A * IEEE Posix P1003.2 mailing list from Phong Vo (kpv@research.att.com) and
1N/A * Glenn Fowler (gsf@research.att.com). Landon Curt Noll (chongo@toad.com)
1N/A * later improved on their algorithm.
1N/A *
1N/A * The magic is in the interesting relationship between the special prime
1N/A * 16777619 (2^24 + 403) and 2^32 and 2^8.
1N/A *
1N/A * This hash produces the fewest collisions of any function that we've seen so
1N/A * far, and works well on both numbers and strings.
1N/A *
1N/A * PUBLIC: u_int32_t __ham_func5 __P((const void *, u_int32_t));
1N/A */
1N/Au_int32_t
1N/A__ham_func5(key, len)
1N/A const void *key;
1N/A u_int32_t len;
1N/A{
1N/A const u_int8_t *k, *e;
1N/A u_int32_t h;
1N/A
1N/A k = key;
1N/A e = k + len;
1N/A for (h = 0; k < e; ++k) {
1N/A h *= 16777619;
1N/A h ^= *k;
1N/A }
1N/A return (h);
1N/A}