farmhash_iface.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "common.h"
  2. #include "farmhash_iface.h"
  3. #include <util/system/cpu_id.h>
  4. namespace NAMESPACE_FOR_HASH_FUNCTIONS {
  5. // BASIC STRING HASHING
  6. // Hash function for a byte array. See also Hash(), below.
  7. // May change from time to time, may differ on different platforms, may differ
  8. // depending on NDEBUG.
  9. uint32_t Hash32(const char* s, size_t len) {
  10. return DebugTweak(
  11. (NX86::CachedHaveSSE41() & x86_64) ? farmhashnt::Hash32(s, len) :
  12. (NX86::CachedHaveSSE42() & NX86::CachedHaveAES()) ? farmhashsu::Hash32(s, len) :
  13. NX86::CachedHaveSSE42() ? farmhashsa::Hash32(s, len) :
  14. farmhashmk::Hash32(s, len));
  15. }
  16. // Hash function for a byte array. For convenience, a 32-bit seed is also
  17. // hashed into the result.
  18. // May change from time to time, may differ on different platforms, may differ
  19. // depending on NDEBUG.
  20. uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed) {
  21. return DebugTweak(
  22. (NX86::CachedHaveSSE41() & x86_64) ? farmhashnt::Hash32WithSeed(s, len, seed) :
  23. (NX86::CachedHaveSSE42() & NX86::CachedHaveAES()) ? farmhashsu::Hash32WithSeed(s, len, seed) :
  24. NX86::CachedHaveSSE42() ? farmhashsa::Hash32WithSeed(s, len, seed) :
  25. farmhashmk::Hash32WithSeed(s, len, seed));
  26. }
  27. // Hash function for a byte array. For convenience, a 64-bit seed is also
  28. // hashed into the result. See also Hash(), below.
  29. // May change from time to time, may differ on different platforms, may differ
  30. // depending on NDEBUG.
  31. uint64_t Hash64(const char* s, size_t len) {
  32. return DebugTweak(
  33. (NX86::CachedHaveSSE42() & x86_64) ?
  34. farmhashte::Hash64(s, len) :
  35. farmhashxo::Hash64(s, len));
  36. }
  37. // Hash function for a byte array.
  38. // May change from time to time, may differ on different platforms, may differ
  39. // depending on NDEBUG.
  40. size_t Hash(const char* s, size_t len) {
  41. return sizeof(size_t) == 8 ? Hash64(s, len) : Hash32(s, len);
  42. }
  43. // Hash function for a byte array. For convenience, a 64-bit seed is also
  44. // hashed into the result.
  45. // May change from time to time, may differ on different platforms, may differ
  46. // depending on NDEBUG.
  47. uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed) {
  48. return DebugTweak(farmhashna::Hash64WithSeed(s, len, seed));
  49. }
  50. // Hash function for a byte array. For convenience, two seeds are also
  51. // hashed into the result.
  52. // May change from time to time, may differ on different platforms, may differ
  53. // depending on NDEBUG.
  54. uint64_t Hash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1) {
  55. return DebugTweak(farmhashna::Hash64WithSeeds(s, len, seed0, seed1));
  56. }
  57. // Hash function for a byte array.
  58. // May change from time to time, may differ on different platforms, may differ
  59. // depending on NDEBUG.
  60. uint128_t Hash128(const char* s, size_t len) {
  61. return DebugTweak(farmhashcc::Fingerprint128(s, len));
  62. }
  63. // Hash function for a byte array. For convenience, a 128-bit seed is also
  64. // hashed into the result.
  65. // May change from time to time, may differ on different platforms, may differ
  66. // depending on NDEBUG.
  67. uint128_t Hash128WithSeed(const char* s, size_t len, uint128_t seed) {
  68. return DebugTweak(farmhashcc::CityHash128WithSeed(s, len, seed));
  69. }
  70. // BASIC NON-STRING HASHING
  71. // FINGERPRINTING (i.e., good, portable, forever-fixed hash functions)
  72. // Fingerprint function for a byte array. Most useful in 32-bit binaries.
  73. uint32_t Fingerprint32(const char* s, size_t len) {
  74. return farmhashmk::Hash32(s, len);
  75. }
  76. // Fingerprint function for a byte array.
  77. uint64_t Fingerprint64(const char* s, size_t len) {
  78. return farmhashna::Hash64(s, len);
  79. }
  80. // Fingerprint function for a byte array.
  81. uint128_t Fingerprint128(const char* s, size_t len) {
  82. return farmhashcc::Fingerprint128(s, len);
  83. }
  84. // Older and still available but perhaps not as fast as the above:
  85. // farmhashns::Hash32{,WithSeed}()
  86. } // namespace NAMESPACE_FOR_HASH_FUNCTIONS