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
51 #include <utility>
52
53 #include "absl/base/config.h"
54
55 namespace absl {
56 ABSL_NAMESPACE_BEGIN
57 namespace hash_internal {
58
59 typedef std::pair<uint64_t, uint64_t> uint128;
60
Uint128Low64(const uint128 & x)61 inline uint64_t Uint128Low64(const uint128 &x) { return x.first; }
Uint128High64(const uint128 & x)62 inline uint64_t Uint128High64(const uint128 &x) { return x.second; }
63
64 // Hash function for a byte array.
65 uint64_t CityHash64(const char *s, size_t len);
66
67 // Hash function for a byte array. For convenience, a 64-bit seed is also
68 // hashed into the result.
69 uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed);
70
71 // Hash function for a byte array. For convenience, two seeds are also
72 // hashed into the result.
73 uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
74 uint64_t seed1);
75
76 // Hash function for a byte array. Most useful in 32-bit binaries.
77 uint32_t CityHash32(const char *s, size_t len);
78
79 // Hash 128 input bits down to 64 bits of output.
80 // This is intended to be a reasonably good hash function.
Hash128to64(const uint128 & x)81 inline uint64_t Hash128to64(const uint128 &x) {
82 // Murmur-inspired hashing.
83 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
84 uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul;
85 a ^= (a >> 47);
86 uint64_t b = (Uint128High64(x) ^ a) * kMul;
87 b ^= (b >> 47);
88 b *= kMul;
89 return b;
90 }
91
92 } // namespace hash_internal
93 ABSL_NAMESPACE_END
94 } // namespace absl
95
96 #endif // ABSL_HASH_INTERNAL_CITY_H_
97