3 * Copyright 2015, Google Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "murmur_hash.h"
36 #define ROTL32(x, r) ((x) << (r)) | ((x) >> (32 - (r)))
45 /* Block read - if your platform needs to do endian-swapping or can only
46 handle aligned reads, do the conversion here */
47 #define GETBLOCK32(p, i) (p)[(i)]
49 uint32_t gpr_murmur_hash3(const void *key
, size_t len
, uint32_t seed
) {
50 const uint8_t *data
= (const uint8_t *)key
;
51 const size_t nblocks
= len
/ 4;
57 const uint32_t c1
= 0xcc9e2d51;
58 const uint32_t c2
= 0x1b873593;
60 const uint32_t *blocks
= ((const uint32_t *)key
) + nblocks
;
61 const uint8_t *tail
= (const uint8_t *)(data
+ nblocks
* 4);
64 for (i
= -(int)nblocks
; i
; i
++) {
65 k1
= GETBLOCK32(blocks
, i
);
73 h1
= h1
* 5 + 0xe6546b64;
81 k1
^= ((uint32_t)tail
[2]) << 16;
83 k1
^= ((uint32_t)tail
[1]) << 8;