cityhash.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-FileCopyrightText: 2011 Google, Inc.
  2. // SPDX-FileContributor: Geoff Pike
  3. // SPDX-FileContributor: Jyrki Alakuijala
  4. // SPDX-License-Identifier: MIT
  5. // CityHash, by Geoff Pike and Jyrki Alakuijala
  6. //
  7. // http://code.google.com/p/cityhash/
  8. //
  9. // This file provides a few functions for hashing strings. All of them are
  10. // high-quality functions in the sense that they pass standard tests such
  11. // as Austin Appleby's SMHasher. They are also fast.
  12. //
  13. // For 64-bit x86 code, on short strings, we don't know of anything faster than
  14. // CityHash64 that is of comparable quality. We believe our nearest competitor
  15. // is Murmur3. For 64-bit x86 code, CityHash64 is an excellent choice for hash
  16. // tables and most other hashing (excluding cryptography).
  17. //
  18. // For 64-bit x86 code, on long strings, the picture is more complicated.
  19. // On many recent Intel CPUs, such as Nehalem, Westmere, Sandy Bridge, etc.,
  20. // CityHashCrc128 appears to be faster than all competitors of comparable
  21. // quality. CityHash128 is also good but not quite as fast. We believe our
  22. // nearest competitor is Bob Jenkins' Spooky. We don't have great data for
  23. // other 64-bit CPUs, but for long strings we know that Spooky is slightly
  24. // faster than CityHash on some relatively recent AMD x86-64 CPUs, for example.
  25. // Note that CityHashCrc128 is declared in citycrc.h.
  26. //
  27. // For 32-bit x86 code, we don't know of anything faster than CityHash32 that
  28. // is of comparable quality. We believe our nearest competitor is Murmur3A.
  29. // (On 64-bit CPUs, it is typically faster to use the other CityHash variants.)
  30. //
  31. // Functions in the CityHash family are not suitable for cryptography.
  32. //
  33. // Please see CityHash's README file for more details on our performance
  34. // measurements and so on.
  35. //
  36. // WARNING: This code has been only lightly tested on big-endian platforms!
  37. // It is known to work well on little-endian platforms that have a small penalty
  38. // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
  39. // It should work on all 32-bit and 64-bit platforms that allow unaligned reads;
  40. // bug reports are welcome.
  41. //
  42. // By the way, for some hash functions, given strings a and b, the hash
  43. // of a+b is easily derived from the hashes of a and b. This property
  44. // doesn't hold for any hash functions in this file.
  45. #pragma once
  46. #include <cstddef>
  47. #include "common/common_types.h"
  48. namespace Common {
  49. // Hash function for a byte array.
  50. [[nodiscard]] u64 CityHash64(const char* buf, size_t len);
  51. // Hash function for a byte array. For convenience, a 64-bit seed is also
  52. // hashed into the result.
  53. [[nodiscard]] u64 CityHash64WithSeed(const char* buf, size_t len, u64 seed);
  54. // Hash function for a byte array. For convenience, two seeds are also
  55. // hashed into the result.
  56. [[nodiscard]] u64 CityHash64WithSeeds(const char* buf, size_t len, u64 seed0, u64 seed1);
  57. // Hash function for a byte array.
  58. [[nodiscard]] u128 CityHash128(const char* s, size_t len);
  59. // Hash function for a byte array. For convenience, a 128-bit seed is also
  60. // hashed into the result.
  61. [[nodiscard]] u128 CityHash128WithSeed(const char* s, size_t len, u128 seed);
  62. // Hash 128 input bits down to 64 bits of output.
  63. // This is intended to be a reasonably good hash function.
  64. [[nodiscard]] inline u64 Hash128to64(const u128& x) {
  65. // Murmur-inspired hashing.
  66. const u64 mul = 0x9ddfea08eb382d69ULL;
  67. u64 a = (x[0] ^ x[1]) * mul;
  68. a ^= (a >> 47);
  69. u64 b = (x[1] ^ a) * mul;
  70. b ^= (b >> 47);
  71. b *= mul;
  72. return b;
  73. }
  74. } // namespace Common