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