blob: 8fd74cce33f811395788a8cef2f3048c4a0caa22 [file] [log] [blame]
tanjent@gmail.com9808b172011-03-20 04:25:41 +00001#include "Platform.h"
tanjent@gmail.comb39a5f02011-03-20 04:29:19 +00002#include <stdio.h> // for NULL
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00003
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
21uint32_t SuperFastHash (const char * data, int len) {
22uint32_t hash = 0, tmp;
23int rem;
24
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000025 if (len <= 0 || data == NULL) return 0;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000026
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000027 rem = len & 3;
28 len >>= 2;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000029
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000030 /* 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.com7e5c3632010-11-02 00:50:04 +000038
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000039 /* 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.com7e5c3632010-11-02 00:50:04 +000054
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000055 /* 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.com7e5c3632010-11-02 00:50:04 +000062
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000063 return hash;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000064}
65
66void SuperFastHash ( const void * key, int len, uint32_t /*seed*/, void * out )
67{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000068 *(uint32_t*)out = SuperFastHash((const char*)key,len);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000069}