1 //#include "pstdint.h" /* Replace with <stdint.h> if appropriate */
2 #include "Common.h"
3 
4 #undef get16bits
5 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
6   || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
7 #define get16bits(d) (*((const uint16_t *) (d)))
8 #endif
9 
10 #if !defined (get16bits)
11 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
12                        +(uint32_t)(((const uint8_t *)(d))[0]) )
13 #endif
14 
SuperFastHash(const char * data,int len)15 uint32_t SuperFastHash (const char * data, int len)
16 {
17 	uint32_t hash = len, tmp;
18 	int rem;
19 
20     if (len <= 0 || data == NULL) return 0;
21 
22     rem = len & 3;
23     len >>= 2;
24 
25     /* Main loop */
26     for (;len > 0; len--) {
27         hash  += get16bits (data);
28         tmp    = (get16bits (data+2) << 11) ^ hash;
29         hash   = (hash << 16) ^ tmp;
30         data  += 2*sizeof (uint16_t);
31         hash  += hash >> 11;
32     }
33 
34     /* Handle end cases */
35     switch (rem) {
36         case 3: hash += get16bits (data);
37                 hash ^= hash << 16;
38                 hash ^= data[sizeof (uint16_t)] << 18;
39                 hash += hash >> 11;
40                 break;
41         case 2: hash += get16bits (data);
42                 hash ^= hash << 11;
43                 hash += hash >> 17;
44                 break;
45         case 1: hash += *data;
46                 hash ^= hash << 10;
47                 hash += hash >> 1;
48     }
49 
50     /* Force "avalanching" of final 127 bits */
51     hash ^= hash << 3;
52     hash += hash >> 5;
53     hash ^= hash << 4;
54     hash += hash >> 17;
55     hash ^= hash << 25;
56     hash += hash >> 6;
57 
58     return hash;
59 }
60