Home | History | Annotate | Line # | Download | only in hash
hash_func.c revision 1.11
      1 /*	$NetBSD: hash_func.c,v 1.11 2007/02/03 23:46:09 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Margo Seltzer.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)hash_func.c	8.2 (Berkeley) 2/21/94";
     43 #else
     44 __RCSID("$NetBSD: hash_func.c,v 1.11 2007/02/03 23:46:09 christos Exp $");
     45 #endif
     46 #endif /* LIBC_SCCS and not lint */
     47 
     48 #include <sys/types.h>
     49 
     50 #include <db.h>
     51 #include "hash.h"
     52 #include "page.h"
     53 #include "extern.h"
     54 
     55 #if 0
     56 static u_int32_t hash1(const void *, size_t) __attribute__((__unused__));
     57 static u_int32_t hash2(const void *, size_t) __attribute__((__unused__));
     58 static u_int32_t hash3(const void *, size_t) __attribute__((__unused__));
     59 #endif
     60 static u_int32_t hash4(const void *, size_t) __attribute__((__unused__));
     61 
     62 /* Global default hash function */
     63 u_int32_t (*__default_hash)(const void *, size_t) = hash4;
     64 #if 0
     65 /*
     66  * HASH FUNCTIONS
     67  *
     68  * Assume that we've already split the bucket to which this key hashes,
     69  * calculate that bucket, and check that in fact we did already split it.
     70  *
     71  * This came from ejb's hsearch.
     72  */
     73 
     74 #define PRIME1		37
     75 #define PRIME2		1048583
     76 
     77 static u_int32_t
     78 hash1(const void *keyarg, size_t len)
     79 {
     80 	const u_char *key;
     81 	u_int32_t h;
     82 
     83 	/* Convert string to integer */
     84 	for (key = keyarg, h = 0; len--;)
     85 		h = h * PRIME1 ^ (*key++ - ' ');
     86 	h %= PRIME2;
     87 	return (h);
     88 }
     89 
     90 /*
     91  * Phong's linear congruential hash
     92  */
     93 #define dcharhash(h, c)	((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
     94 
     95 static u_int32_t
     96 hash2(const void *keyarg, size_t len)
     97 {
     98 	const u_char *e, *key;
     99 	u_int32_t h;
    100 	u_char c;
    101 
    102 	key = keyarg;
    103 	e = key + len;
    104 	for (h = 0; key != e;) {
    105 		c = *key++;
    106 		if (!c && key > e)
    107 			break;
    108 		dcharhash(h, c);
    109 	}
    110 	return (h);
    111 }
    112 
    113 /*
    114  * This is INCREDIBLY ugly, but fast.  We break the string up into 8 byte
    115  * units.  On the first time through the loop we get the "leftover bytes"
    116  * (strlen % 8).  On every other iteration, we perform 8 HASHC's so we handle
    117  * all 8 bytes.  Essentially, this saves us 7 cmp & branch instructions.  If
    118  * this routine is heavily used enough, it's worth the ugly coding.
    119  *
    120  * OZ's original sdbm hash
    121  */
    122 static u_int32_t
    123 hash3(const void *keyarg, size_t len)
    124 {
    125 	const u_char *key;
    126 	size_t loop;
    127 	u_int32_t h;
    128 
    129 #define HASHC   h = *key++ + 65599 * h
    130 
    131 	h = 0;
    132 	key = keyarg;
    133 	if (len > 0) {
    134 		loop = (len + 8 - 1) >> 3;
    135 
    136 		switch (len & (8 - 1)) {
    137 		case 0:
    138 			do {
    139 				HASHC;
    140 				/* FALLTHROUGH */
    141 		case 7:
    142 				HASHC;
    143 				/* FALLTHROUGH */
    144 		case 6:
    145 				HASHC;
    146 				/* FALLTHROUGH */
    147 		case 5:
    148 				HASHC;
    149 				/* FALLTHROUGH */
    150 		case 4:
    151 				HASHC;
    152 				/* FALLTHROUGH */
    153 		case 3:
    154 				HASHC;
    155 				/* FALLTHROUGH */
    156 		case 2:
    157 				HASHC;
    158 				/* FALLTHROUGH */
    159 		case 1:
    160 				HASHC;
    161 			} while (--loop);
    162 		}
    163 	}
    164 	return (h);
    165 }
    166 #endif
    167 
    168 /* Hash function from Chris Torek. */
    169 static u_int32_t
    170 hash4(const void *keyarg, size_t len)
    171 {
    172 	const u_char *key;
    173 	size_t loop;
    174 	u_int32_t h;
    175 
    176 #define HASH4a   h = (h << 5) - h + *key++;
    177 #define HASH4b   h = (h << 5) + h + *key++;
    178 #define HASH4 HASH4b
    179 
    180 	h = 0;
    181 	key = keyarg;
    182 	if (len > 0) {
    183 		loop = (len + 8 - 1) >> 3;
    184 
    185 		switch (len & (8 - 1)) {
    186 		case 0:
    187 			do {
    188 				HASH4;
    189 				/* FALLTHROUGH */
    190 		case 7:
    191 				HASH4;
    192 				/* FALLTHROUGH */
    193 		case 6:
    194 				HASH4;
    195 				/* FALLTHROUGH */
    196 		case 5:
    197 				HASH4;
    198 				/* FALLTHROUGH */
    199 		case 4:
    200 				HASH4;
    201 				/* FALLTHROUGH */
    202 		case 3:
    203 				HASH4;
    204 				/* FALLTHROUGH */
    205 		case 2:
    206 				HASH4;
    207 				/* FALLTHROUGH */
    208 		case 1:
    209 				HASH4;
    210 			} while (--loop);
    211 		}
    212 	}
    213 	return (h);
    214 }
    215