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