/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Margo Seltzer.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/types.h>3536#include <db.h>37#include "hash.h"38#include "page.h"39#include "extern.h"4041#ifdef notdef42static u_int32_t hash1(const void *, size_t) __unused;43static u_int32_t hash2(const void *, size_t) __unused;44static u_int32_t hash3(const void *, size_t) __unused;45#endif46static u_int32_t hash4(const void *, size_t);4748/* Default hash function. */49u_int32_t (*__default_hash)(const void *, size_t) = hash4;5051#ifdef notdef52/*53* Assume that we've already split the bucket to which this key hashes,54* calculate that bucket, and check that in fact we did already split it.55*56* EJB's original hsearch hash.57*/58#define PRIME1 3759#define PRIME2 10485836061u_int32_t62hash1(const void *key, size_t len)63{64u_int32_t h;65u_int8_t *k;6667h = 0;68k = (u_int8_t *)key;69/* Convert string to integer */70while (len--)71h = h * PRIME1 ^ (*k++ - ' ');72h %= PRIME2;73return (h);74}7576/*77* Phong Vo's linear congruential hash78*/79#define dcharhash(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))8081u_int32_t82hash2(const void *key, size_t len)83{84u_int32_t h;85u_int8_t *e, c, *k;8687k = (u_int8_t *)key;88e = k + len;89for (h = 0; k != e;) {90c = *k++;91if (!c && k > e)92break;93dcharhash(h, c);94}95return (h);96}9798/*99* This is INCREDIBLY ugly, but fast. We break the string up into 8 byte100* units. On the first time through the loop we get the "leftover bytes"101* (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle102* all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If103* this routine is heavily used enough, it's worth the ugly coding.104*105* Ozan Yigit's original sdbm hash.106*/107u_int32_t108hash3(const void *key, size_t len)109{110u_int32_t n, loop;111u_int8_t *k;112113#define HASHC n = *k++ + 65599 * n114115n = 0;116k = (u_int8_t *)key;117if (len > 0) {118loop = (len + 8 - 1) >> 3;119120switch (len & (8 - 1)) {121case 0:122do { /* All fall throughs */123HASHC;124case 7:125HASHC;126case 6:127HASHC;128case 5:129HASHC;130case 4:131HASHC;132case 3:133HASHC;134case 2:135HASHC;136case 1:137HASHC;138} while (--loop);139}140141}142return (n);143}144#endif /* notdef */145146/* Chris Torek's hash function. */147u_int32_t148hash4(const void *key, size_t len)149{150u_int32_t h, loop;151const u_int8_t *k;152153#define HASH4a h = (h << 5) - h + *k++;154#define HASH4b h = (h << 5) + h + *k++;155#define HASH4 HASH4b156157h = 0;158k = key;159if (len > 0) {160loop = (len + 8 - 1) >> 3;161162switch (len & (8 - 1)) {163case 0:164do { /* All fall throughs */165HASH4;166case 7:167HASH4;168case 6:169HASH4;170case 5:171HASH4;172case 4:173HASH4;174case 3:175HASH4;176case 2:177HASH4;178case 1:179HASH4;180} while (--loop);181}182183}184return (h);185}186187188