Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // Copyright (c) 2011 Google, Inc. |
michael@0 | 2 | // |
michael@0 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
michael@0 | 4 | // of this software and associated documentation files (the "Software"), to deal |
michael@0 | 5 | // in the Software without restriction, including without limitation the rights |
michael@0 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
michael@0 | 7 | // copies of the Software, and to permit persons to whom the Software is |
michael@0 | 8 | // furnished to do so, subject to the following conditions: |
michael@0 | 9 | // |
michael@0 | 10 | // The above copyright notice and this permission notice shall be included in |
michael@0 | 11 | // all copies or substantial portions of the Software. |
michael@0 | 12 | // |
michael@0 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
michael@0 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
michael@0 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
michael@0 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
michael@0 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
michael@0 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
michael@0 | 19 | // THE SOFTWARE. |
michael@0 | 20 | // |
michael@0 | 21 | // CityHash Version 1, by Geoff Pike and Jyrki Alakuijala |
michael@0 | 22 | // |
michael@0 | 23 | // This file provides a few functions for hashing strings. On x86-64 |
michael@0 | 24 | // hardware in 2011, CityHash64() is faster than other high-quality |
michael@0 | 25 | // hash functions, such as Murmur. This is largely due to higher |
michael@0 | 26 | // instruction-level parallelism. CityHash64() and CityHash128() also perform |
michael@0 | 27 | // well on hash-quality tests. |
michael@0 | 28 | // |
michael@0 | 29 | // CityHash128() is optimized for relatively long strings and returns |
michael@0 | 30 | // a 128-bit hash. For strings more than about 2000 bytes it can be |
michael@0 | 31 | // faster than CityHash64(). |
michael@0 | 32 | // |
michael@0 | 33 | // Functions in the CityHash family are not suitable for cryptography. |
michael@0 | 34 | // |
michael@0 | 35 | // WARNING: This code has not been tested on big-endian platforms! |
michael@0 | 36 | // It is known to work well on little-endian platforms that have a small penalty |
michael@0 | 37 | // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs. |
michael@0 | 38 | // |
michael@0 | 39 | // By the way, for some hash functions, given strings a and b, the hash |
michael@0 | 40 | // of a+b is easily derived from the hashes of a and b. This property |
michael@0 | 41 | // doesn't hold for any hash functions in this file. |
michael@0 | 42 | |
michael@0 | 43 | #ifndef CITY_HASH_H_ |
michael@0 | 44 | #define CITY_HASH_H_ |
michael@0 | 45 | |
michael@0 | 46 | #include "../CityHash.h" // added by moz, specific to nsis project |
michael@0 | 47 | |
michael@0 | 48 | #include <stdlib.h> // for size_t. |
michael@0 | 49 | #include <utility> |
michael@0 | 50 | |
michael@0 | 51 | typedef unsigned __int8 uint8; |
michael@0 | 52 | typedef unsigned __int32 uint32; |
michael@0 | 53 | typedef unsigned __int64 uint64; |
michael@0 | 54 | typedef std::pair<uint64, uint64> uint128; |
michael@0 | 55 | |
michael@0 | 56 | inline uint64 Uint128Low64(const uint128& x) { return x.first; } |
michael@0 | 57 | inline uint64 Uint128High64(const uint128& x) { return x.second; } |
michael@0 | 58 | |
michael@0 | 59 | // Hash function for a byte array. |
michael@0 | 60 | uint64 CityHash64(const char *buf, size_t len); |
michael@0 | 61 | |
michael@0 | 62 | // Hash function for a byte array. For convenience, a 64-bit seed is also |
michael@0 | 63 | // hashed into the result. |
michael@0 | 64 | uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed); |
michael@0 | 65 | |
michael@0 | 66 | // Hash function for a byte array. For convenience, two seeds are also |
michael@0 | 67 | // hashed into the result. |
michael@0 | 68 | uint64 CityHash64WithSeeds(const char *buf, size_t len, |
michael@0 | 69 | uint64 seed0, uint64 seed1); |
michael@0 | 70 | |
michael@0 | 71 | // Hash function for a byte array. |
michael@0 | 72 | uint128 CityHash128(const char *s, size_t len); |
michael@0 | 73 | |
michael@0 | 74 | // Hash function for a byte array. For convenience, a 128-bit seed is also |
michael@0 | 75 | // hashed into the result. |
michael@0 | 76 | uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed); |
michael@0 | 77 | |
michael@0 | 78 | // Hash 128 input bits down to 64 bits of output. |
michael@0 | 79 | // This is intended to be a reasonably good hash function. |
michael@0 | 80 | inline uint64 Hash128to64(const uint128& x) { |
michael@0 | 81 | // Murmur-inspired hashing. |
michael@0 | 82 | const uint64 kMul = 0x9ddfea08eb382d69; |
michael@0 | 83 | uint64 a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; |
michael@0 | 84 | a ^= (a >> 47); |
michael@0 | 85 | uint64 b = (Uint128High64(x) ^ a) * kMul; |
michael@0 | 86 | b ^= (b >> 47); |
michael@0 | 87 | b *= kMul; |
michael@0 | 88 | return b; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | #endif // CITY_HASH_H_ |