3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/gpr/murmur_hash.h"
25 #define ROTL32(x, r) ((x) << (r)) | ((x) >> (32 - (r)))
34 uint32_t gpr_murmur_hash3(const void* key, size_t len, uint32_t seed) {
38 const uint32_t c1 = 0xcc9e2d51;
39 const uint32_t c2 = 0x1b873593;
41 const uint8_t* keyptr = static_cast<const uint8_t*>(key);
42 const size_t bsize = sizeof(k1);
43 const size_t nblocks = len / bsize;
46 for (size_t i = 0; i < nblocks; i++, keyptr += bsize) {
47 memcpy(&k1, keyptr, bsize);
55 h1 = h1 * 5 + 0xe6546b64;
63 k1 ^= (static_cast<uint32_t>(keyptr[2])) << 16;
66 k1 ^= (static_cast<uint32_t>(keyptr[1])) << 8;
77 h1 ^= static_cast<uint32_t>(len);