hash_func.c revision 1.7 1 /* $NetBSD: hash_func.c,v 1.7 1997/07/13 18:52:05 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
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.7 1997/07/13 18:52:05 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 static u_int32_t hash1 __P((const void *, size_t)) __attribute__((__unused__));
56 static u_int32_t hash2 __P((const void *, size_t)) __attribute__((__unused__));
57 static u_int32_t hash3 __P((const void *, size_t)) __attribute__((__unused__));
58 static u_int32_t hash4 __P((const void *, size_t)) __attribute__((__unused__));
59
60 /* Global default hash function */
61 u_int32_t (*__default_hash) __P((const void *, size_t)) = hash4;
62
63 /*
64 * HASH FUNCTIONS
65 *
66 * Assume that we've already split the bucket to which this key hashes,
67 * calculate that bucket, and check that in fact we did already split it.
68 *
69 * This came from ejb's hsearch.
70 */
71
72 #define PRIME1 37
73 #define PRIME2 1048583
74
75 static u_int32_t
76 hash1(keyarg, len)
77 const void *keyarg;
78 register size_t len;
79 {
80 register const u_char *key;
81 register 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(keyarg, len)
97 const void *keyarg;
98 size_t len;
99 {
100 register const u_char *e, *key;
101 register u_int32_t h;
102 register u_char c;
103
104 key = keyarg;
105 e = key + len;
106 for (h = 0; key != e;) {
107 c = *key++;
108 if (!c && key > e)
109 break;
110 dcharhash(h, c);
111 }
112 return (h);
113 }
114
115 /*
116 * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte
117 * units. On the first time through the loop we get the "leftover bytes"
118 * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle
119 * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If
120 * this routine is heavily used enough, it's worth the ugly coding.
121 *
122 * OZ's original sdbm hash
123 */
124 static u_int32_t
125 hash3(keyarg, len)
126 const void *keyarg;
127 register size_t len;
128 {
129 register const u_char *key;
130 register size_t loop;
131 register u_int32_t h;
132
133 #define HASHC h = *key++ + 65599 * h
134
135 h = 0;
136 key = keyarg;
137 if (len > 0) {
138 loop = (len + 8 - 1) >> 3;
139
140 switch (len & (8 - 1)) {
141 case 0:
142 do {
143 HASHC;
144 /* FALLTHROUGH */
145 case 7:
146 HASHC;
147 /* FALLTHROUGH */
148 case 6:
149 HASHC;
150 /* FALLTHROUGH */
151 case 5:
152 HASHC;
153 /* FALLTHROUGH */
154 case 4:
155 HASHC;
156 /* FALLTHROUGH */
157 case 3:
158 HASHC;
159 /* FALLTHROUGH */
160 case 2:
161 HASHC;
162 /* FALLTHROUGH */
163 case 1:
164 HASHC;
165 } while (--loop);
166 }
167 }
168 return (h);
169 }
170
171 /* Hash function from Chris Torek. */
172 static u_int32_t
173 hash4(keyarg, len)
174 const void *keyarg;
175 register size_t len;
176 {
177 register const u_char *key;
178 register size_t loop;
179 register u_int32_t h;
180
181 #define HASH4a h = (h << 5) - h + *key++;
182 #define HASH4b h = (h << 5) + h + *key++;
183 #define HASH4 HASH4b
184
185 h = 0;
186 key = keyarg;
187 if (len > 0) {
188 loop = (len + 8 - 1) >> 3;
189
190 switch (len & (8 - 1)) {
191 case 0:
192 do {
193 HASH4;
194 /* FALLTHROUGH */
195 case 7:
196 HASH4;
197 /* FALLTHROUGH */
198 case 6:
199 HASH4;
200 /* FALLTHROUGH */
201 case 5:
202 HASH4;
203 /* FALLTHROUGH */
204 case 4:
205 HASH4;
206 /* FALLTHROUGH */
207 case 3:
208 HASH4;
209 /* FALLTHROUGH */
210 case 2:
211 HASH4;
212 /* FALLTHROUGH */
213 case 1:
214 HASH4;
215 } while (--loop);
216 }
217 }
218 return (h);
219 }
220