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 /* 'magic' constants for Murmurhash2 */
13 #define DUK__MAGIC_M ((duk_uint32_t) 0x5bd1e995UL)
14 #define DUK__MAGIC_R 24
16 DUK_INTERNAL duk_uint32_t
duk_util_hashbytes(const duk_uint8_t
*data
, duk_size_t len
, duk_uint32_t seed
) {
17 duk_uint32_t h
= seed
^ ((duk_uint32_t
) len
);
20 /* Portability workaround is required for platforms without
21 * unaligned access. The replacement code emulates little
22 * endian access even on big endian architectures, which is
23 * OK as long as it is consistent for a build.
25 #ifdef DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS
26 duk_uint32_t k
= *((duk_uint32_t
*) (void *) data
);
28 duk_uint32_t k
= ((duk_uint32_t
) data
[0]) |
29 (((duk_uint32_t
) data
[1]) << 8) |
30 (((duk_uint32_t
) data
[2]) << 16) |
31 (((duk_uint32_t
) data
[3]) << 24);
35 k
^= k
>> DUK__MAGIC_R
;
44 case 3: h
^= data
[2] << 16;
45 case 2: h
^= data
[1] << 8;