city.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2011 Google, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. //
  21. // CityHash, by Geoff Pike and Jyrki Alakuijala
  22. //
  23. // This file provides a few functions for hashing strings. On x86-64
  24. // hardware in 2011, CityHash64() is faster than other high-quality
  25. // hash functions, such as Murmur. This is largely due to higher
  26. // instruction-level parallelism. CityHash64() and CityHash128() also perform
  27. // well on hash-quality tests.
  28. //
  29. // CityHash128() is optimized for relatively long strings and returns
  30. // a 128-bit hash. For strings more than about 2000 bytes it can be
  31. // faster than CityHash64().
  32. //
  33. // Functions in the CityHash family are not suitable for cryptography.
  34. //
  35. // WARNING: This code has not been 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. //
  39. // By the way, for some hash functions, given strings a and b, the hash
  40. // of a+b is easily derived from the hashes of a and b. This property
  41. // doesn't hold for any hash functions in this file.
  42. #ifndef CITY_HASH_H_
  43. #define CITY_HASH_H_
  44. #include <stdlib.h> // for size_t.
  45. #include <stdint.h>
  46. #include <utility>
  47. /** This is a version of CityHash that predates v1.0.3 algorithm change.
  48. * Why we need exactly this version?
  49. * Although hash values of CityHash are not recommended for storing persistently anywhere,
  50. * it has already been used this way in ClickHouse:
  51. * - for calculation of checksums of compressed chunks and for data parts;
  52. * - this version of CityHash is exposed in cityHash64 function in ClickHouse SQL language;
  53. * - and already used by many users for data ordering, sampling and sharding.
  54. */
  55. namespace CityHash_v1_0_2
  56. {
  57. typedef uint8_t uint8;
  58. typedef uint32_t uint32;
  59. typedef uint64_t uint64;
  60. /// NB: Original CityHash library uses `typedef std::pair<uint64, uint64> uint128`,
  61. /// but ClickHouse's patched version uses its own struct uint128 with low64 and high64 fields.
  62. /// As we need to maintain it somehow in a monorepository, this particular uint128 implementation
  63. /// aims to be compatible with both library versions.
  64. /// https://github.com/ClickHouse/ClickHouse/blob/2442c71e273c041448bc5e0b5a406dedcd9e006c/contrib/cityhash102/include/city.h#L69
  65. struct uint128
  66. {
  67. union {
  68. uint64 low64;
  69. uint64 first;
  70. };
  71. union {
  72. uint64 high64;
  73. uint64 second;
  74. };
  75. uint128() : low64(0), high64(0) {}
  76. uint128(uint64 low64_, uint64 high64_) : low64(low64_), high64(high64_) {}
  77. // Implicit conversion from std::pair for compatibility.
  78. uint128(std::pair<uint64, uint64> other)
  79. : first(other.first), second(other.second)
  80. { }
  81. std::pair<uint64, uint64> toPair() const {
  82. return std::pair{first, second};
  83. }
  84. // Implicit conversion to std::pair for compatibility.
  85. operator std::pair<uint64, uint64>() const {
  86. return toPair();
  87. };
  88. friend auto operator==(uint128 a, uint128 b) {
  89. return a.toPair() == b.toPair();
  90. }
  91. friend auto operator<=>(uint128 a, uint128 b) {
  92. return a.toPair() <=> b.toPair();
  93. }
  94. };
  95. inline uint64 Uint128Low64(const uint128& x) { return x.first; }
  96. inline uint64 Uint128High64(const uint128& x) { return x.second; }
  97. // Hash function for a byte array.
  98. uint64 CityHash64(const char *buf, size_t len);
  99. // Hash function for a byte array. For convenience, a 64-bit seed is also
  100. // hashed into the result.
  101. uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed);
  102. // Hash function for a byte array. For convenience, two seeds are also
  103. // hashed into the result.
  104. uint64 CityHash64WithSeeds(const char *buf, size_t len,
  105. uint64 seed0, uint64 seed1);
  106. // Hash function for a byte array.
  107. uint128 CityHash128(const char *s, size_t len);
  108. // Hash function for a byte array. For convenience, a 128-bit seed is also
  109. // hashed into the result.
  110. uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed);
  111. // Hash 128 input bits down to 64 bits of output.
  112. // This is intended to be a reasonably good hash function.
  113. inline uint64 Hash128to64(const uint128& x) {
  114. // Murmur-inspired hashing.
  115. const uint64 kMul = 0x9ddfea08eb382d69ULL;
  116. uint64 a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul;
  117. a ^= (a >> 47);
  118. uint64 b = (Uint128High64(x) ^ a) * kMul;
  119. b ^= (b >> 47);
  120. b *= kMul;
  121. return b;
  122. }
  123. } // namespace CityHash_v1_0_2
  124. #endif // CITY_HASH_H_