farmhash-c.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (c) 2014 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. // FarmHash, by Geoff Pike
  22. //
  23. // http://code.google.com/p/farmhash/
  24. //
  25. // This file provides a few functions for hashing strings and other
  26. // data. All of them are high-quality functions in the sense that
  27. // they do well on standard tests such as Austin Appleby's SMHasher.
  28. // They're also fast. FarmHash is the successor to CityHash.
  29. //
  30. // Functions in the FarmHash family are not suitable for cryptography.
  31. //
  32. // WARNING: This code has been only lightly tested on big-endian platforms!
  33. // It is known to work well on little-endian platforms that have a small penalty
  34. // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
  35. // It should work on all 32-bit and 64-bit platforms that allow unaligned reads;
  36. // bug reports are welcome.
  37. //
  38. // By the way, for some hash functions, given strings a and b, the hash
  39. // of a+b is easily derived from the hashes of a and b. This property
  40. // doesn't hold for any hash functions in this file.
  41. #ifndef FARMHASH_C_H
  42. #define FARMHASH_C_H
  43. #if defined (__cplusplus)
  44. extern "C" {
  45. #endif
  46. #include <stdlib.h>
  47. #include <stdint.h>
  48. struct uint128_c_t {
  49. uint64_t a;
  50. uint64_t b;
  51. };
  52. typedef struct uint128_c_t uint128_c_t;
  53. static inline uint64_t uint128_c_t_low64(const uint128_c_t x) { return x.a; }
  54. static inline uint64_t uint128_c_t_high64(const uint128_c_t x) { return x.b; }
  55. static inline uint128_c_t make_uint128_c_t(uint64_t lo, uint64_t hi) { uint128_c_t x = {lo, hi}; return x; }
  56. // BASIC STRING HASHING
  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. size_t farmhash(const char* s, size_t len);
  61. // Hash function for a byte array. Most useful in 32-bit binaries.
  62. // May change from time to time, may differ on different platforms, may differ
  63. // depending on NDEBUG.
  64. uint32_t farmhash32(const char* s, size_t len);
  65. // Hash function for a byte array. For convenience, a 32-bit seed is also
  66. // hashed into the result.
  67. // May change from time to time, may differ on different platforms, may differ
  68. // depending on NDEBUG.
  69. uint32_t farmhash32_with_seed(const char* s, size_t len, uint32_t seed);
  70. // Hash 128 input bits down to 64 bits of output.
  71. // Hash function for a byte array.
  72. // May change from time to time, may differ on different platforms, may differ
  73. // depending on NDEBUG.
  74. uint64_t farmhash64(const char* s, size_t len);
  75. // Hash function for a byte array. For convenience, a 64-bit seed is also
  76. // hashed into the result.
  77. // May change from time to time, may differ on different platforms, may differ
  78. // depending on NDEBUG.
  79. uint64_t farmhash64_with_seed(const char* s, size_t len, uint64_t seed);
  80. // Hash function for a byte array. For convenience, two seeds are also
  81. // hashed into the result.
  82. // May change from time to time, may differ on different platforms, may differ
  83. // depending on NDEBUG.
  84. uint64_t farmhash64_with_seeds(const char* s, size_t len,
  85. uint64_t seed0, uint64_t seed1);
  86. // Hash function for a byte array.
  87. // May change from time to time, may differ on different platforms, may differ
  88. // depending on NDEBUG.
  89. uint128_c_t farmhash128(const char* s, size_t len);
  90. // Hash function for a byte array. For convenience, a 128-bit seed is also
  91. // hashed into the result.
  92. // May change from time to time, may differ on different platforms, may differ
  93. // depending on NDEBUG.
  94. uint128_c_t farmhash128_with_seed(const char* s, size_t len, uint128_c_t seed);
  95. // BASIC NON-STRING HASHING
  96. // This is intended to be a reasonably good hash function.
  97. // May change from time to time, may differ on different platforms, may differ
  98. // depending on NDEBUG.
  99. static inline uint64_t farmhash128_to_64(uint128_c_t x) {
  100. // Murmur-inspired hashing.
  101. const uint64_t k_mul = 0x9ddfea08eb382d69ULL;
  102. uint64_t a = (uint128_c_t_low64(x) ^ uint128_c_t_high64(x)) * k_mul;
  103. a ^= (a >> 47);
  104. uint64_t b = (uint128_c_t_high64(x) ^ a) * k_mul;
  105. b ^= (b >> 47);
  106. b *= k_mul;
  107. return b;
  108. }
  109. // FINGERPRINTING (i.e., good, portable, forever-fixed hash functions)
  110. // Fingerprint function for a byte array. Most useful in 32-bit binaries.
  111. uint32_t farmhash_fingerprint32(const char* s, size_t len);
  112. // Fingerprint function for a byte array.
  113. uint64_t farmhash_fingerprint64(const char* s, size_t len);
  114. // Fingerprint function for a byte array.
  115. uint128_c_t farmhash_fingerprint128(const char* s, size_t len);
  116. // This is intended to be a good fingerprinting primitive.
  117. // See below for more overloads.
  118. static inline uint64_t farmhash_fingerprint_uint128_c_t(uint128_c_t x) {
  119. // Murmur-inspired hashing.
  120. const uint64_t k_mul = 0x9ddfea08eb382d69ULL;
  121. uint64_t a = (uint128_c_t_low64(x) ^ uint128_c_t_high64(x)) * k_mul;
  122. a ^= (a >> 47);
  123. uint64_t b = (uint128_c_t_high64(x) ^ a) * k_mul;
  124. b ^= (b >> 44);
  125. b *= k_mul;
  126. b ^= (b >> 41);
  127. b *= k_mul;
  128. return b;
  129. }
  130. // This is intended to be a good fingerprinting primitive.
  131. static inline uint64_t farmhash_fingerprint_uint64_t(uint64_t x) {
  132. // Murmur-inspired hashing.
  133. const uint64_t k_mul = 0x9ddfea08eb382d69ULL;
  134. uint64_t b = x * k_mul;
  135. b ^= (b >> 44);
  136. b *= k_mul;
  137. b ^= (b >> 41);
  138. b *= k_mul;
  139. return b;
  140. }
  141. #if defined (__cplusplus)
  142. }
  143. #endif
  144. #endif // FARMHASH_C_H