1219820Sjeff/* 2219820Sjeff * Copyright (c) 2005 Voltaire Inc. All rights reserved. 3219820Sjeff * 4219820Sjeff * This software is available to you under a choice of one of two 5219820Sjeff * licenses. You may choose to be licensed under the terms of the GNU 6219820Sjeff * General Public License (GPL) Version 2, available from the file 7219820Sjeff * COPYING in the main directory of this source tree, or the 8219820Sjeff * OpenIB.org BSD license below: 9219820Sjeff * 10219820Sjeff * Redistribution and use in source and binary forms, with or 11219820Sjeff * without modification, are permitted provided that the following 12219820Sjeff * conditions are met: 13219820Sjeff * 14219820Sjeff * - Redistributions of source code must retain the above 15219820Sjeff * copyright notice, this list of conditions and the following 16219820Sjeff * disclaimer. 17219820Sjeff * 18219820Sjeff * - Redistributions in binary form must reproduce the above 19219820Sjeff * copyright notice, this list of conditions and the following 20219820Sjeff * disclaimer in the documentation and/or other materials 21219820Sjeff * provided with the distribution. 22219820Sjeff * 23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30219820Sjeff * SOFTWARE. 31219820Sjeff * 32219820Sjeff */ 33219820Sjeff 34219820Sjeff/* 35219820Sjeff * By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this 36219820Sjeff * code any way you wish, private, educational, or commercial. It's free. 37219820Sjeff * 38219820Sjeff * See http://burtleburtle.net/bob/hash/evahash.html 39219820Sjeff * Use for hash table lookup, or anything where one collision in 2^^32 is 40219820Sjeff * acceptable. Do NOT use for cryptographic purposes. 41219820Sjeff */ 42219820Sjeff 43219820Sjeff#include <common.h> 44219820Sjeff 45219820Sjeff#define hashsize(n) ((uint32)1<<(n)) 46219820Sjeff#define hashmask(n) (hashsize(n)-1) 47219820Sjeff 48219820Sjeff 49219820Sjeff/* 50219820Sjeff-------------------------------------------------------------------- 51219820Sjeffmix -- mix 3 32-bit values reversibly. 52219820SjeffFor every delta with one or two bits set, and the deltas of all three 53219820Sjeff high bits or all three low bits, whether the original value of a,b,c 54219820Sjeff is almost all zero or is uniformly distributed, 55219820Sjeff* If mix() is run forward or backward, at least 32 bits in a,b,c 56219820Sjeff have at least 1/4 probability of changing. 57219820Sjeff* If mix() is run forward, every bit of c will change between 1/3 and 58219820Sjeff 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.) 59219820Sjeffmix() was built out of 36 single-cycle latency instructions in a 60219820Sjeff structure that could supported 2x parallelism, like so: 61219820Sjeff a -= b; 62219820Sjeff a -= c; x = (c>>13); 63219820Sjeff b -= c; a ^= x; 64219820Sjeff b -= a; x = (a<<8); 65219820Sjeff c -= a; b ^= x; 66219820Sjeff c -= b; x = (b>>13); 67219820Sjeff ... 68219820Sjeff Unfortunately, superscalar Pentiums and Sparcs can't take advantage 69219820Sjeff of that parallelism. They've also turned some of those single-cycle 70219820Sjeff latency instructions into multi-cycle latency instructions. Still, 71219820Sjeff this is the fastest good hash I could find. There were about 2^^68 72219820Sjeff to choose from. I only looked at a billion or so. 73219820Sjeff-------------------------------------------------------------------- 74219820Sjeff*/ 75219820Sjeff#define mix(a,b,c) \ 76219820Sjeff{ \ 77219820Sjeff a -= b; a -= c; a ^= (c>>13); \ 78219820Sjeff b -= c; b -= a; b ^= (a<<8); \ 79219820Sjeff c -= a; c -= b; c ^= (b>>13); \ 80219820Sjeff a -= b; a -= c; a ^= (c>>12); \ 81219820Sjeff b -= c; b -= a; b ^= (a<<16); \ 82219820Sjeff c -= a; c -= b; c ^= (b>>5); \ 83219820Sjeff a -= b; a -= c; a ^= (c>>3); \ 84219820Sjeff b -= c; b -= a; b ^= (a<<10); \ 85219820Sjeff c -= a; c -= b; c ^= (b>>15); \ 86219820Sjeff} 87219820Sjeff 88219820Sjeff/* 89219820Sjeff-------------------------------------------------------------------- 90219820Sjefffhash() -- hash a variable-length key into a 32-bit value 91219820Sjeff k : the key (the unaligned variable-length array of bytes) 92219820Sjeff len : the length of the key, counting by bytes 93219820Sjeff initval : can be any 4-byte value 94219820SjeffReturns a 32-bit value. Every bit of the key affects every bit of 95219820Sjeffthe return value. Every 1-bit and 2-bit delta achieves avalanche. 96219820SjeffAbout 6*len+35 instructions. 97219820Sjeff 98219820SjeffThe best hash table sizes are powers of 2. There is no need to do 99219820Sjeffmod a prime (mod is sooo slow!). If you need less than 32 bits, 100219820Sjeffuse a bitmask. For example, if you need only 10 bits, do 101219820Sjeff h = (h & hashmask(10)); 102219820SjeffIn which case, the hash table should have hashsize(10) elements. 103219820Sjeff 104219820SjeffIf you are hashing n strings (uint8 **)k, do it like this: 105219820Sjeff for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h); 106219820Sjeff 107219820Sjeff-------------------------------------------------------------------- 108219820Sjeff*/ 109219820Sjeff 110219820Sjeffuint32_t 111219820Sjefffhash(uint8_t *k, int length, uint32_t initval) 112219820Sjeff{ 113219820Sjeff uint32_t a, b, c, len; 114219820Sjeff 115219820Sjeff /* Set up the internal state */ 116219820Sjeff len = length; 117219820Sjeff a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ 118219820Sjeff c = initval; /* the previous hash value */ 119219820Sjeff 120219820Sjeff /* handle most of the key */ 121219820Sjeff while (len >= 12) { 122219820Sjeff a += (k[0] + ((uint32_t)k[1]<<8) + 123219820Sjeff ((uint32_t)k[2]<<16) + ((uint32_t)k[3]<<24)); 124219820Sjeff b += (k[4] + ((uint32_t)k[5]<<8) + ((uint32_t)k[6]<<16) + 125219820Sjeff ((uint32_t)k[7]<<24)); 126219820Sjeff c += (k[8] + ((uint32_t)k[9]<<8) + ((uint32_t)k[10]<<16) + 127219820Sjeff ((uint32_t)k[11]<<24)); 128219820Sjeff mix(a, b, c); 129219820Sjeff k += 12; len -= 12; 130219820Sjeff } 131219820Sjeff 132219820Sjeff /* handle the last 11 bytes */ 133219820Sjeff c += length; 134219820Sjeff switch (len) { /* all the case statements fall through */ 135219820Sjeff case 11: c += ((uint32_t)k[10]<<24); 136219820Sjeff case 10: c += ((uint32_t)k[9]<<16); 137219820Sjeff case 9 : c += ((uint32_t)k[8]<<8); 138219820Sjeff /* the first byte of c is reserved for the length */ 139219820Sjeff case 8 : b += ((uint32_t)k[7]<<24); 140219820Sjeff case 7 : b += ((uint32_t)k[6]<<16); 141219820Sjeff case 6 : b += ((uint32_t)k[5]<<8); 142219820Sjeff case 5 : b += k[4]; 143219820Sjeff case 4 : a += ((uint32_t)k[3]<<24); 144219820Sjeff case 3 : a += ((uint32_t)k[2]<<16); 145219820Sjeff case 2 : a += ((uint32_t)k[1]<<8); 146219820Sjeff case 1 : a += k[0]; 147219820Sjeff /* case 0: nothing left to add */ 148219820Sjeff } 149219820Sjeff 150219820Sjeff mix(a, b, c); 151219820Sjeff 152219820Sjeff return c; 153219820Sjeff} 154