1/* 2 * Copyright (c) 2005 Voltaire Inc. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 * 32 */ 33 34/* 35 * By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this 36 * code any way you wish, private, educational, or commercial. It's free. 37 * 38 * See http://burtleburtle.net/bob/hash/evahash.html 39 * Use for hash table lookup, or anything where one collision in 2^^32 is 40 * acceptable. Do NOT use for cryptographic purposes. 41 */ 42 43#include <common.h> 44 45#define hashsize(n) ((uint32)1<<(n)) 46#define hashmask(n) (hashsize(n)-1) 47 48 49/* 50-------------------------------------------------------------------- 51mix -- mix 3 32-bit values reversibly. 52For every delta with one or two bits set, and the deltas of all three 53 high bits or all three low bits, whether the original value of a,b,c 54 is almost all zero or is uniformly distributed, 55* If mix() is run forward or backward, at least 32 bits in a,b,c 56 have at least 1/4 probability of changing. 57* If mix() is run forward, every bit of c will change between 1/3 and 58 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.) 59mix() was built out of 36 single-cycle latency instructions in a 60 structure that could supported 2x parallelism, like so: 61 a -= b; 62 a -= c; x = (c>>13); 63 b -= c; a ^= x; 64 b -= a; x = (a<<8); 65 c -= a; b ^= x; 66 c -= b; x = (b>>13); 67 ... 68 Unfortunately, superscalar Pentiums and Sparcs can't take advantage 69 of that parallelism. They've also turned some of those single-cycle 70 latency instructions into multi-cycle latency instructions. Still, 71 this is the fastest good hash I could find. There were about 2^^68 72 to choose from. I only looked at a billion or so. 73-------------------------------------------------------------------- 74*/ 75#define mix(a,b,c) \ 76{ \ 77 a -= b; a -= c; a ^= (c>>13); \ 78 b -= c; b -= a; b ^= (a<<8); \ 79 c -= a; c -= b; c ^= (b>>13); \ 80 a -= b; a -= c; a ^= (c>>12); \ 81 b -= c; b -= a; b ^= (a<<16); \ 82 c -= a; c -= b; c ^= (b>>5); \ 83 a -= b; a -= c; a ^= (c>>3); \ 84 b -= c; b -= a; b ^= (a<<10); \ 85 c -= a; c -= b; c ^= (b>>15); \ 86} 87 88/* 89-------------------------------------------------------------------- 90fhash() -- hash a variable-length key into a 32-bit value 91 k : the key (the unaligned variable-length array of bytes) 92 len : the length of the key, counting by bytes 93 initval : can be any 4-byte value 94Returns a 32-bit value. Every bit of the key affects every bit of 95the return value. Every 1-bit and 2-bit delta achieves avalanche. 96About 6*len+35 instructions. 97 98The best hash table sizes are powers of 2. There is no need to do 99mod a prime (mod is sooo slow!). If you need less than 32 bits, 100use a bitmask. For example, if you need only 10 bits, do 101 h = (h & hashmask(10)); 102In which case, the hash table should have hashsize(10) elements. 103 104If you are hashing n strings (uint8 **)k, do it like this: 105 for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h); 106 107-------------------------------------------------------------------- 108*/ 109 110uint32_t 111fhash(uint8_t *k, int length, uint32_t initval) 112{ 113 uint32_t a, b, c, len; 114 115 /* Set up the internal state */ 116 len = length; 117 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ 118 c = initval; /* the previous hash value */ 119 120 /* handle most of the key */ 121 while (len >= 12) { 122 a += (k[0] + ((uint32_t)k[1]<<8) + 123 ((uint32_t)k[2]<<16) + ((uint32_t)k[3]<<24)); 124 b += (k[4] + ((uint32_t)k[5]<<8) + ((uint32_t)k[6]<<16) + 125 ((uint32_t)k[7]<<24)); 126 c += (k[8] + ((uint32_t)k[9]<<8) + ((uint32_t)k[10]<<16) + 127 ((uint32_t)k[11]<<24)); 128 mix(a, b, c); 129 k += 12; len -= 12; 130 } 131 132 /* handle the last 11 bytes */ 133 c += length; 134 switch (len) { /* all the case statements fall through */ 135 case 11: c += ((uint32_t)k[10]<<24); 136 case 10: c += ((uint32_t)k[9]<<16); 137 case 9 : c += ((uint32_t)k[8]<<8); 138 /* the first byte of c is reserved for the length */ 139 case 8 : b += ((uint32_t)k[7]<<24); 140 case 7 : b += ((uint32_t)k[6]<<16); 141 case 6 : b += ((uint32_t)k[5]<<8); 142 case 5 : b += k[4]; 143 case 4 : a += ((uint32_t)k[3]<<24); 144 case 3 : a += ((uint32_t)k[2]<<16); 145 case 2 : a += ((uint32_t)k[1]<<8); 146 case 1 : a += k[0]; 147 /* case 0: nothing left to add */ 148 } 149 150 mix(a, b, c); 151 152 return c; 153} 154