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