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