tanjent@gmail.com | 9808b17 | 2011-03-20 04:25:41 +0000 | [diff] [blame] | 1 | #include "Platform.h"
|
tanjent@gmail.com | b39a5f0 | 2011-03-20 04:29:19 +0000 | [diff] [blame^] | 2 | #include <stdio.h> // for NULL
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 3 |
|
| 4 | /* By Paul Hsieh (C) 2004, 2005. Covered under the Paul Hsieh derivative
|
| 5 | license. See:
|
| 6 | http://www.azillionmonkeys.com/qed/weblicense.html for license details.
|
| 7 |
|
| 8 | http://www.azillionmonkeys.com/qed/hash.html */
|
| 9 |
|
| 10 | #undef get16bits
|
| 11 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
|
| 12 | || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
|
| 13 | #define get16bits(d) (*((const uint16_t *) (d)))
|
| 14 | #endif
|
| 15 |
|
| 16 | #if !defined (get16bits)
|
| 17 | #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
|
| 18 | +(uint32_t)(((const uint8_t *)(d))[0]) )
|
| 19 | #endif
|
| 20 |
|
| 21 | uint32_t SuperFastHash (const char * data, int len) {
|
| 22 | uint32_t hash = 0, tmp;
|
| 23 | int rem;
|
| 24 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 25 | if (len <= 0 || data == NULL) return 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 26 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 27 | rem = len & 3;
|
| 28 | len >>= 2;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 29 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 30 | /* Main loop */
|
| 31 | for (;len > 0; len--) {
|
| 32 | hash += get16bits (data);
|
| 33 | tmp = (get16bits (data+2) << 11) ^ hash;
|
| 34 | hash = (hash << 16) ^ tmp;
|
| 35 | data += 2*sizeof (uint16_t);
|
| 36 | hash += hash >> 11;
|
| 37 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 38 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 39 | /* Handle end cases */
|
| 40 | switch (rem) {
|
| 41 | case 3: hash += get16bits (data);
|
| 42 | hash ^= hash << 16;
|
| 43 | hash ^= data[sizeof (uint16_t)] << 18;
|
| 44 | hash += hash >> 11;
|
| 45 | break;
|
| 46 | case 2: hash += get16bits (data);
|
| 47 | hash ^= hash << 11;
|
| 48 | hash += hash >> 17;
|
| 49 | break;
|
| 50 | case 1: hash += *data;
|
| 51 | hash ^= hash << 10;
|
| 52 | hash += hash >> 1;
|
| 53 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 54 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 55 | /* Force "avalanching" of final 127 bits */
|
| 56 | hash ^= hash << 3;
|
| 57 | hash += hash >> 5;
|
| 58 | hash ^= hash << 4;
|
| 59 | hash += hash >> 17;
|
| 60 | hash ^= hash << 25;
|
| 61 | hash += hash >> 6;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 62 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 63 | return hash;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 64 | }
|
| 65 |
|
| 66 | void SuperFastHash ( const void * key, int len, uint32_t /*seed*/, void * out )
|
| 67 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 68 | *(uint32_t*)out = SuperFastHash((const char*)key,len);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 69 | } |