city.h
Go to the documentation of this file.
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // https://code.google.com/p/cityhash/
16 //
17 // This file provides a few functions for hashing strings. All of them are
18 // high-quality functions in the sense that they pass standard tests such
19 // as Austin Appleby's SMHasher. They are also fast.
20 //
21 // For 64-bit x86 code, on short strings, we don't know of anything faster than
22 // CityHash64 that is of comparable quality. We believe our nearest competitor
23 // is Murmur3. For 64-bit x86 code, CityHash64 is an excellent choice for hash
24 // tables and most other hashing (excluding cryptography).
25 //
26 // For 32-bit x86 code, we don't know of anything faster than CityHash32 that
27 // is of comparable quality. We believe our nearest competitor is Murmur3A.
28 // (On 64-bit CPUs, it is typically faster to use the other CityHash variants.)
29 //
30 // Functions in the CityHash family are not suitable for cryptography.
31 //
32 // Please see CityHash's README file for more details on our performance
33 // measurements and so on.
34 //
35 // WARNING: This code has been only lightly tested on big-endian platforms!
36 // It is known to work well on little-endian platforms that have a small penalty
37 // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
38 // It should work on all 32-bit and 64-bit platforms that allow unaligned reads;
39 // bug reports are welcome.
40 //
41 // By the way, for some hash functions, given strings a and b, the hash
42 // of a+b is easily derived from the hashes of a and b. This property
43 // doesn't hold for any hash functions in this file.
44 
45 #ifndef ABSL_HASH_INTERNAL_CITY_H_
46 #define ABSL_HASH_INTERNAL_CITY_H_
47 
48 #include <stdint.h>
49 #include <stdlib.h> // for size_t.
50 #include <utility>
51 
52 namespace absl {
53 namespace hash_internal {
54 
55 typedef std::pair<uint64_t, uint64_t> uint128;
56 
57 inline uint64_t Uint128Low64(const uint128 &x) { return x.first; }
58 inline uint64_t Uint128High64(const uint128 &x) { return x.second; }
59 
60 // Hash function for a byte array.
61 uint64_t CityHash64(const char *s, size_t len);
62 
63 // Hash function for a byte array. For convenience, a 64-bit seed is also
64 // hashed into the result.
65 uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed);
66 
67 // Hash function for a byte array. For convenience, two seeds are also
68 // hashed into the result.
69 uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
70  uint64_t seed1);
71 
72 // Hash function for a byte array. Most useful in 32-bit binaries.
73 uint32_t CityHash32(const char *s, size_t len);
74 
75 // Hash 128 input bits down to 64 bits of output.
76 // This is intended to be a reasonably good hash function.
77 inline uint64_t Hash128to64(const uint128 &x) {
78  // Murmur-inspired hashing.
79  const uint64_t kMul = 0x9ddfea08eb382d69ULL;
80  uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul;
81  a ^= (a >> 47);
82  uint64_t b = (Uint128High64(x) ^ a) * kMul;
83  b ^= (b >> 47);
84  b *= kMul;
85  return b;
86 }
87 
88 } // namespace hash_internal
89 } // namespace absl
90 
91 #endif // ABSL_HASH_INTERNAL_CITY_H_
uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed)
Definition: city.cc:334
uint32_t CityHash32(const char *s, size_t len)
Definition: city.cc:123
std::pair< uint64_t, uint64_t > uint128
Definition: city.h:55
Definition: algorithm.h:29
uint64_t CityHash64(const char *s, size_t len)
Definition: city.cc:296
uint64_t Uint128Low64(const uint128 &x)
Definition: city.h:57
uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1)
Definition: city.cc:338
uint64_t Hash128to64(const uint128 &x)
Definition: city.h:77
uint64_t b
Definition: layout_test.cc:50
uint64_t Uint128High64(const uint128 &x)
Definition: city.h:58


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:17