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