murmur_hash.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
20 
22 
23 #include <string.h>
24 
25 #include "absl/base/attributes.h"
26 
27 #define ROTL32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
28 
29 #define FMIX32(h) \
30  (h) ^= (h) >> 16; \
31  (h) *= 0x85ebca6b; \
32  (h) ^= (h) >> 13; \
33  (h) *= 0xc2b2ae35; \
34  (h) ^= (h) >> 16;
35 
36 uint32_t gpr_murmur_hash3(const void* key, size_t len, uint32_t seed) {
37  uint32_t h1 = seed;
38  uint32_t k1;
39 
40  const uint32_t c1 = 0xcc9e2d51;
41  const uint32_t c2 = 0x1b873593;
42 
43  const uint8_t* keyptr = static_cast<const uint8_t*>(key);
44  const size_t bsize = sizeof(k1);
45  const size_t nblocks = len / bsize;
46 
47  /* body */
48  for (size_t i = 0; i < nblocks; i++, keyptr += bsize) {
49  memcpy(&k1, keyptr, bsize);
50 
51  k1 *= c1;
52  k1 = ROTL32(k1, 15);
53  k1 *= c2;
54 
55  h1 ^= k1;
56  h1 = ROTL32(h1, 13);
57  h1 = h1 * 5 + 0xe6546b64;
58  }
59 
60  k1 = 0;
61 
62  /* tail */
63  switch (len & 3) {
64  case 3:
65  k1 ^= (static_cast<uint32_t>(keyptr[2])) << 16;
67  case 2:
68  k1 ^= (static_cast<uint32_t>(keyptr[1])) << 8;
70  case 1:
71  k1 ^= keyptr[0];
72  k1 *= c1;
73  k1 = ROTL32(k1, 15);
74  k1 *= c2;
75  h1 ^= k1;
76  };
77 
78  /* finalization */
79  h1 ^= static_cast<uint32_t>(len);
80  FMIX32(h1);
81  return h1;
82 }
FMIX32
#define FMIX32(h)
Definition: murmur_hash.cc:29
string.h
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ROTL32
#define ROTL32(x, r)
Definition: murmur_hash.cc:27
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
absl::hash_internal::c1
static const uint32_t c1
Definition: abseil-cpp/absl/hash/internal/city.cc:58
key
const char * key
Definition: hpack_parser_table.cc:164
absl::hash_internal::k1
static const uint64_t k1
Definition: abseil-cpp/absl/hash/internal/city.cc:54
murmur_hash.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
gpr_murmur_hash3
uint32_t gpr_murmur_hash3(const void *key, size_t len, uint32_t seed)
Definition: murmur_hash.cc:36
ABSL_FALLTHROUGH_INTENDED
#define ABSL_FALLTHROUGH_INTENDED
Definition: abseil-cpp/absl/base/attributes.h:641
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
absl::hash_internal::c2
static const uint32_t c2
Definition: abseil-cpp/absl/hash/internal/city.cc:59
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:30