tanjent@gmail.com | f3b7897 | 2012-03-01 03:38:55 +0000 | [diff] [blame] | 1 | #include "Platform.h" |
| 2 | #include <stdio.h> // for NULL |
| 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 | /* |
| 11 | #undef get16bits |
| 12 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ |
| 13 | || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) |
| 14 | #define get16bits(d) (*((const uint16_t *) (d))) |
| 15 | #endif |
| 16 | |
| 17 | #if !defined (get16bits) |
| 18 | #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ |
| 19 | +(uint32_t)(((const uint8_t *)(d))[0]) ) |
| 20 | #endif |
| 21 | */ |
| 22 | |
| 23 | FORCE_INLINE uint16_t get16bits ( const void * p ) |
| 24 | { |
| 25 | return *(const uint16_t*)p; |
| 26 | } |
| 27 | |
| 28 | uint32_t SuperFastHash (const signed char * data, int len) { |
| 29 | uint32_t hash = 0, tmp; |
| 30 | int rem; |
| 31 | |
| 32 | if (len <= 0 || data == NULL) return 0; |
| 33 | |
| 34 | rem = len & 3; |
| 35 | len >>= 2; |
| 36 | |
| 37 | /* Main loop */ |
| 38 | for (;len > 0; len--) { |
| 39 | hash += get16bits (data); |
| 40 | tmp = (get16bits (data+2) << 11) ^ hash; |
| 41 | hash = (hash << 16) ^ tmp; |
| 42 | data += 2*sizeof (uint16_t); |
| 43 | hash += hash >> 11; |
| 44 | } |
| 45 | |
| 46 | /* Handle end cases */ |
| 47 | switch (rem) { |
| 48 | case 3: hash += get16bits (data); |
| 49 | hash ^= hash << 16; |
| 50 | hash ^= data[sizeof (uint16_t)] << 18; |
| 51 | hash += hash >> 11; |
| 52 | break; |
| 53 | case 2: hash += get16bits (data); |
| 54 | hash ^= hash << 11; |
| 55 | hash += hash >> 17; |
| 56 | break; |
| 57 | case 1: hash += *data; |
| 58 | hash ^= hash << 10; |
| 59 | hash += hash >> 1; |
| 60 | } |
| 61 | |
| 62 | /* Force "avalanching" of final 127 bits */ |
| 63 | hash ^= hash << 3; |
| 64 | hash += hash >> 5; |
| 65 | hash ^= hash << 4; |
| 66 | hash += hash >> 17; |
| 67 | hash ^= hash << 25; |
| 68 | hash += hash >> 6; |
| 69 | |
| 70 | return hash; |
| 71 | } |
| 72 | |
| 73 | void SuperFastHash ( const void * key, int len, uint32_t /*seed*/, void * out ) |
| 74 | { |
| 75 | *(uint32_t*)out = SuperFastHash((const signed char*)key,len); |
| 76 | } |