2 * The contents of this file are public domain.
4 * Based on: lookup3.c, by Bob Jenkins, May 2006, Public Domain.
11 * A simple version of Bob Jenkins' lookup3.c hash.
13 * It is supposed to give same results as hashlittle() on little-endian
14 * and hashbig() on big-endian machines.
16 * Speed seems comparable to Jenkins' optimized version (~ -10%).
17 * Actual difference varies as it depends on cpu/compiler/libc details.
21 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
23 /* mix 3 32-bit values reversibly */
24 #define mix(a, b, c) do { \
25 a -= c; a ^= rot(c, 4); c += b; \
26 b -= a; b ^= rot(a, 6); a += c; \
27 c -= b; c ^= rot(b, 8); b += a; \
28 a -= c; a ^= rot(c,16); c += b; \
29 b -= a; b ^= rot(a,19); a += c; \
30 c -= b; c ^= rot(b, 4); b += a; \
33 /* final mixing of 3 32-bit values (a,b,c) into c */
34 #define final(a, b, c) do { \
35 c ^= b; c -= rot(b,14); \
36 a ^= c; a -= rot(c,11); \
37 b ^= a; b -= rot(a,25); \
38 c ^= b; c -= rot(b,16); \
39 a ^= c; a -= rot(c, 4); \
40 b ^= a; b -= rot(a,14); \
41 c ^= b; c -= rot(b,24); \
45 * GCC does not know how to optimize short variable-length copies.
46 * Its faster to do dumb inlined copy than call out to libc.
48 static inline void simple_memcpy(void *dst_
, const void *src_
, size_t len
)
50 const uint8_t *src
= src_
;
56 /* short version - let compiler worry about memory access */
57 uint32_t lookup3_hash(const void *data
, size_t len
)
61 const uint8_t *p
= data
;
63 a
= b
= c
= 0xdeadbeef + len
;
77 buf
[0] = buf
[1] = buf
[2] = 0;
78 simple_memcpy(buf
, p
, len
);
89 * Reversible integer hash function by Thomas Wang.
92 uint32_t hash32(uint32_t v
)