00001 // Copyright 2018 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 // 00015 // https://code.google.com/p/cityhash/ 00016 // 00017 // This file provides a few functions for hashing strings. All of them are 00018 // high-quality functions in the sense that they pass standard tests such 00019 // as Austin Appleby's SMHasher. They are also fast. 00020 // 00021 // For 64-bit x86 code, on short strings, we don't know of anything faster than 00022 // CityHash64 that is of comparable quality. We believe our nearest competitor 00023 // is Murmur3. For 64-bit x86 code, CityHash64 is an excellent choice for hash 00024 // tables and most other hashing (excluding cryptography). 00025 // 00026 // For 32-bit x86 code, we don't know of anything faster than CityHash32 that 00027 // is of comparable quality. We believe our nearest competitor is Murmur3A. 00028 // (On 64-bit CPUs, it is typically faster to use the other CityHash variants.) 00029 // 00030 // Functions in the CityHash family are not suitable for cryptography. 00031 // 00032 // Please see CityHash's README file for more details on our performance 00033 // measurements and so on. 00034 // 00035 // WARNING: This code has been only lightly tested on big-endian platforms! 00036 // It is known to work well on little-endian platforms that have a small penalty 00037 // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs. 00038 // It should work on all 32-bit and 64-bit platforms that allow unaligned reads; 00039 // bug reports are welcome. 00040 // 00041 // By the way, for some hash functions, given strings a and b, the hash 00042 // of a+b is easily derived from the hashes of a and b. This property 00043 // doesn't hold for any hash functions in this file. 00044 00045 #ifndef ABSL_HASH_INTERNAL_CITY_H_ 00046 #define ABSL_HASH_INTERNAL_CITY_H_ 00047 00048 #include <stdint.h> 00049 #include <stdlib.h> // for size_t. 00050 #include <utility> 00051 00052 namespace absl { 00053 namespace hash_internal { 00054 00055 typedef std::pair<uint64_t, uint64_t> uint128; 00056 00057 inline uint64_t Uint128Low64(const uint128 &x) { return x.first; } 00058 inline uint64_t Uint128High64(const uint128 &x) { return x.second; } 00059 00060 // Hash function for a byte array. 00061 uint64_t CityHash64(const char *s, size_t len); 00062 00063 // Hash function for a byte array. For convenience, a 64-bit seed is also 00064 // hashed into the result. 00065 uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed); 00066 00067 // Hash function for a byte array. For convenience, two seeds are also 00068 // hashed into the result. 00069 uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, 00070 uint64_t seed1); 00071 00072 // Hash function for a byte array. Most useful in 32-bit binaries. 00073 uint32_t CityHash32(const char *s, size_t len); 00074 00075 // Hash 128 input bits down to 64 bits of output. 00076 // This is intended to be a reasonably good hash function. 00077 inline uint64_t Hash128to64(const uint128 &x) { 00078 // Murmur-inspired hashing. 00079 const uint64_t kMul = 0x9ddfea08eb382d69ULL; 00080 uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; 00081 a ^= (a >> 47); 00082 uint64_t b = (Uint128High64(x) ^ a) * kMul; 00083 b ^= (b >> 47); 00084 b *= kMul; 00085 return b; 00086 } 00087 00088 } // namespace hash_internal 00089 } // namespace absl 00090 00091 #endif // ABSL_HASH_INTERNAL_CITY_H_