michael@0: // Copyright (c) 2011 Google, Inc. michael@0: // michael@0: // Permission is hereby granted, free of charge, to any person obtaining a copy michael@0: // of this software and associated documentation files (the "Software"), to deal michael@0: // in the Software without restriction, including without limitation the rights michael@0: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell michael@0: // copies of the Software, and to permit persons to whom the Software is michael@0: // furnished to do so, subject to the following conditions: michael@0: // michael@0: // The above copyright notice and this permission notice shall be included in michael@0: // all copies or substantial portions of the Software. michael@0: // michael@0: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, michael@0: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE michael@0: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER michael@0: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, michael@0: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN michael@0: // THE SOFTWARE. michael@0: // michael@0: // CityHash Version 1, by Geoff Pike and Jyrki Alakuijala michael@0: // michael@0: // This file provides a few functions for hashing strings. On x86-64 michael@0: // hardware in 2011, CityHash64() is faster than other high-quality michael@0: // hash functions, such as Murmur. This is largely due to higher michael@0: // instruction-level parallelism. CityHash64() and CityHash128() also perform michael@0: // well on hash-quality tests. michael@0: // michael@0: // CityHash128() is optimized for relatively long strings and returns michael@0: // a 128-bit hash. For strings more than about 2000 bytes it can be michael@0: // faster than CityHash64(). michael@0: // michael@0: // Functions in the CityHash family are not suitable for cryptography. michael@0: // michael@0: // WARNING: This code has not been tested on big-endian platforms! michael@0: // It is known to work well on little-endian platforms that have a small penalty michael@0: // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs. michael@0: // michael@0: // By the way, for some hash functions, given strings a and b, the hash michael@0: // of a+b is easily derived from the hashes of a and b. This property michael@0: // doesn't hold for any hash functions in this file. michael@0: michael@0: #ifndef CITY_HASH_H_ michael@0: #define CITY_HASH_H_ michael@0: michael@0: #include "../CityHash.h" // added by moz, specific to nsis project michael@0: michael@0: #include // for size_t. michael@0: #include michael@0: michael@0: typedef unsigned __int8 uint8; michael@0: typedef unsigned __int32 uint32; michael@0: typedef unsigned __int64 uint64; michael@0: typedef std::pair uint128; michael@0: michael@0: inline uint64 Uint128Low64(const uint128& x) { return x.first; } michael@0: inline uint64 Uint128High64(const uint128& x) { return x.second; } michael@0: michael@0: // Hash function for a byte array. michael@0: uint64 CityHash64(const char *buf, size_t len); michael@0: michael@0: // Hash function for a byte array. For convenience, a 64-bit seed is also michael@0: // hashed into the result. michael@0: uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed); michael@0: michael@0: // Hash function for a byte array. For convenience, two seeds are also michael@0: // hashed into the result. michael@0: uint64 CityHash64WithSeeds(const char *buf, size_t len, michael@0: uint64 seed0, uint64 seed1); michael@0: michael@0: // Hash function for a byte array. michael@0: uint128 CityHash128(const char *s, size_t len); michael@0: michael@0: // Hash function for a byte array. For convenience, a 128-bit seed is also michael@0: // hashed into the result. michael@0: uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed); michael@0: michael@0: // Hash 128 input bits down to 64 bits of output. michael@0: // This is intended to be a reasonably good hash function. michael@0: inline uint64 Hash128to64(const uint128& x) { michael@0: // Murmur-inspired hashing. michael@0: const uint64 kMul = 0x9ddfea08eb382d69; michael@0: uint64 a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; michael@0: a ^= (a >> 47); michael@0: uint64 b = (Uint128High64(x) ^ a) * kMul; michael@0: b ^= (b >> 47); michael@0: b *= kMul; michael@0: return b; michael@0: } michael@0: michael@0: #endif // CITY_HASH_H_