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