1 //-----------------------------------------------------------------------------
2 //MurmurHash3 was written by Austin Appleby, and is placed in the public
3 //domain. The author hereby disclaims copyright to this source code.
5 // Note - The x86 and x64 versions do _not_ produce the same results, as the
6 // algorithms are optimized for their respective platforms. You can still
7 // compile and run any of them on any platform, but your performance with the
8 // non-native version will be less than optimal.
10 #include "libhashkit/hashkitcon.h"
12 #include "libhashkit/murmur3.h"
14 //-----------------------------------------------------------------------------
15 // Platform-specific functions and macros
18 #define FORCE_INLINE __attribute__((always_inline)) inline
20 #define FORCE_INLINE inline
23 static FORCE_INLINE
uint32_t rotl32 ( uint32_t x
, int8_t r
)
25 return (x
<< r
) | (x
>> (32 - r
));
28 #define ROTL32(x,y) rotl32(x,y)
30 //-----------------------------------------------------------------------------
31 // Block read - if your platform needs to do endian-swapping or can only
32 // handle aligned reads, do the conversion here
37 static inline T
getblock(const T
*blocks
, int i
) {
39 memcpy(&b
, ((const uint8_t *) blocks
) + i
* sizeof(T
), sizeof(T
));
43 //-----------------------------------------------------------------------------
44 // Finalization mix - force all bits of a hash block to avalanche
46 static FORCE_INLINE
uint32_t fmix32 ( uint32_t h
)
57 //-----------------------------------------------------------------------------
59 void MurmurHash3_x86_32 ( const void * key
, int len
,
60 uint32_t seed
, void * out
)
62 const uint8_t * data
= (const uint8_t*)key
;
63 const int nblocks
= len
/ 4;
68 uint32_t c1
= 0xcc9e2d51;
69 uint32_t c2
= 0x1b873593;
74 const uint32_t * blocks
= (const uint32_t *)(data
+ nblocks
*4);
76 for(i
= -nblocks
; i
; i
++)
78 uint32_t k1
= getblock(blocks
,i
);
95 const uint8_t * tail
= (const uint8_t*)(data
+ nblocks
*4);
98 memcpy(&k1
, tail
, len
& 3);
100 k1
= BYTESWAP_32(k1
);
115 *(uint32_t*)out
= h1
;