2 * Hash function duk_util_hashbytes().
4 * Currently, 32-bit MurmurHash2.
6 * Don't rely on specific hash values; hash function may be endianness
7 * dependent, for instance.
10 #include "duk_internal.h"
12 #if defined(DUK_USE_STRHASH_DENSE)
13 /* 'magic' constants for Murmurhash2 */
14 #define DUK__MAGIC_M ((duk_uint32_t) 0x5bd1e995UL)
15 #define DUK__MAGIC_R 24
17 DUK_INTERNAL duk_uint32_t
duk_util_hashbytes(const duk_uint8_t
*data
, duk_size_t len
, duk_uint32_t seed
) {
18 duk_uint32_t h
= seed
^ ((duk_uint32_t
) len
);
21 /* Portability workaround is required for platforms without
22 * unaligned access. The replacement code emulates little
23 * endian access even on big endian architectures, which is
24 * OK as long as it is consistent for a build.
26 #ifdef DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS
27 duk_uint32_t k
= *((const duk_uint32_t
*) (const void *) data
);
29 duk_uint32_t k
= ((duk_uint32_t
) data
[0]) |
30 (((duk_uint32_t
) data
[1]) << 8) |
31 (((duk_uint32_t
) data
[2]) << 16) |
32 (((duk_uint32_t
) data
[3]) << 24);
36 k
^= k
>> DUK__MAGIC_R
;
45 case 3: h
^= data
[2] << 16;
46 case 2: h
^= data
[1] << 8;
57 #endif /* DUK_USE_STRHASH_DENSE */