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