1 /* $NetBSD: murmurhash.c,v 1.6 2013/10/26 21:06:38 rmind Exp $ */
4 * MurmurHash2 -- from the original code:
6 * "MurmurHash2 was written by Austin Appleby, and is placed in the public
7 * domain. The author hereby disclaims copyright to this source code."
10 * http://code.google.com/p/smhasher/
11 * https://sites.google.com/site/murmurhash/
14 #include <sys/cdefs.h>
16 #if defined(_KERNEL) || defined(_STANDALONE)
17 __KERNEL_RCSID(0, "$NetBSD: murmurhash.c,v 1.6 2013/10/26 21:06:38 rmind Exp $");
21 #if defined(LIBC_SCCS) && !defined(lint)
22 __RCSID("$NetBSD: murmurhash.c,v 1.6 2013/10/26 21:06:38 rmind Exp $");
23 #endif /* LIBC_SCCS and not lint */
25 #include "namespace.h"
28 #include <sys/types.h>
29 #include <sys/param.h>
32 #if !defined(_KERNEL) && !defined(_STANDALONE)
34 __weak_alias(murmurhash2
,_murmurhash2
)
39 murmurhash2(const void *key
, size_t len
, uint32_t seed
)
42 * Note: 'm' and 'r' are mixing constants generated offline.
43 * They're not really 'magic', they just happen to work well.
44 * Initialize the hash to a 'random' value.
46 const uint32_t m
= 0x5bd1e995;
49 const uint8_t *data
= key
;
50 uint32_t h
= seed
^ (uint32_t)len
;
52 if (__predict_true(ALIGNED_POINTER(key
, uint32_t))) {
53 while (len
>= sizeof(uint32_t)) {
54 uint32_t k
= *(const uint32_t *)data
;
63 data
+= sizeof(uint32_t);
64 len
-= sizeof(uint32_t);
67 while (len
>= sizeof(uint32_t)) {
82 data
+= sizeof(uint32_t);
83 len
-= sizeof(uint32_t);
87 /* Handle the last few bytes of the input array. */
101 * Do a few final mixes of the hash to ensure the last few
102 * bytes are well-incorporated.