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 
33 #define _DEFAULT_SOURCE
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)hash_func.c	8.2 (Berkeley) 2/21/94";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 #include <sys/types.h>
39 
40 #include "db_local.h"
41 #include "hash.h"
42 #include "page.h"
43 #include "extern.h"
44 
45 #if 0
46 static __uint32_t hash1(const void *, size_t);
47 static __uint32_t hash2(const void *, size_t);
48 static __uint32_t hash3(const void *, size_t);
49 #endif
50 
51 /*
52  * HASH FUNCTIONS
53  *
54  * Assume that we've already split the bucket to which this key hashes,
55  * calculate that bucket, and check that in fact we did already split it.
56  *
57  * This came from ejb's hsearch.
58  */
59 
60 #define PRIME1		37
61 #define PRIME2		1048583
62 
63 #if 0
64 static __uint32_t
65 hash1(keyarg, len)
66 	const void *keyarg;
67 	size_t len;
68 {
69 	const u_char *key;
70 	__uint32_t h;
71 
72 	/* Convert string to integer */
73 	for (key = keyarg, h = 0; len--;)
74 		h = h * PRIME1 ^ (*key++ - ' ');
75 	h %= PRIME2;
76 	return (h);
77 }
78 #endif
79 
80 /*
81  * Phong's linear congruential hash
82  */
83 #define dcharhash(h, c)	((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
84 
85 #if 0
86 static __uint32_t
87 hash2(keyarg, len)
88 	const void *keyarg;
89 	size_t len;
90 {
91 	const u_char *e, *key;
92 	__uint32_t h;
93 	u_char c;
94 
95 	key = keyarg;
96 	e = key + len;
97 	for (h = 0; key != e;) {
98 		c = *key++;
99 		if (!c && key > e)
100 			break;
101 		dcharhash(h, c);
102 	}
103 	return (h);
104 }
105 #endif
106 
107 /*
108  * This is INCREDIBLY ugly, but fast.  We break the string up into 8 byte
109  * units.  On the first time through the loop we get the "leftover bytes"
110  * (strlen % 8).  On every other iteration, we perform 8 HASHC's so we handle
111  * all 8 bytes.  Essentially, this saves us 7 cmp & branch instructions.  If
112  * this routine is heavily used enough, it's worth the ugly coding.
113  *
114  * OZ's original sdbm hash
115  */
116 #if 0
117 static __uint32_t
118 hash3(keyarg, len)
119 	const void *keyarg;
120 	size_t len;
121 {
122 	const u_char *key;
123 	size_t loop;
124 	__uint32_t h;
125 
126 #define HASHC   h = *key++ + 65599 * h
127 
128 	h = 0;
129 	key = keyarg;
130 	if (len > 0) {
131 		loop = (len + 8 - 1) >> 3;
132 
133 		switch (len & (8 - 1)) {
134 		case 0:
135 			do {
136 				HASHC;
137 				FALLTHROUGH;
138 		case 7:
139 				HASHC;
140 				FALLTHROUGH;
141 		case 6:
142 				HASHC;
143 				FALLTHROUGH;
144 		case 5:
145 				HASHC;
146 				FALLTHROUGH;
147 		case 4:
148 				HASHC;
149 				FALLTHROUGH;
150 		case 3:
151 				HASHC;
152 				FALLTHROUGH;
153 		case 2:
154 				HASHC;
155 				FALLTHROUGH;
156 		case 1:
157 				HASHC;
158 			} while (--loop);
159 		}
160 	}
161 	return (h);
162 }
163 #endif
164 
165 /* Hash function from Chris Torek. */
166 __uint32_t
__default_hash(const void * keyarg,size_t len)167 __default_hash(const void *keyarg, size_t len)
168 {
169 	const u_char *key;
170 	__uint32_t h;
171 
172 #define HASH4a   h = (h << 5) - h + *key++;
173 #define HASH4b   h = (h << 5) + h + *key++;
174 #define HASH4 HASH4b
175 
176 	h = 0;
177 	key = keyarg;
178 	while (len--) {
179 		HASH4;
180 	}
181 	return (h);
182 }
183