common.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #pragma once
  2. // Copyright (c) 2014 Google, Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. // FarmHash, by Geoff Pike
  23. #include "farmhash.h"
  24. // FARMHASH ASSUMPTIONS: Modify as needed, or use -DFARMHASH_ASSUME_SSE42 etc.
  25. // Note that if you use -DFARMHASH_ASSUME_SSE42 you likely need -msse42
  26. // (or its equivalent for your compiler); if you use -DFARMHASH_ASSUME_AESNI
  27. // you likely need -maes (or its equivalent for your compiler).
  28. #ifdef FARMHASH_ASSUME_SSSE3
  29. #undef FARMHASH_ASSUME_SSSE3
  30. #define FARMHASH_ASSUME_SSSE3 1
  31. #endif
  32. #ifdef FARMHASH_ASSUME_SSE41
  33. #undef FARMHASH_ASSUME_SSE41
  34. #define FARMHASH_ASSUME_SSE41 1
  35. #endif
  36. #ifdef FARMHASH_ASSUME_SSE42
  37. #undef FARMHASH_ASSUME_SSE42
  38. #define FARMHASH_ASSUME_SSE42 1
  39. #endif
  40. #ifdef FARMHASH_ASSUME_AESNI
  41. #undef FARMHASH_ASSUME_AESNI
  42. #define FARMHASH_ASSUME_AESNI 1
  43. #endif
  44. #ifdef FARMHASH_ASSUME_AVX
  45. #undef FARMHASH_ASSUME_AVX
  46. #define FARMHASH_ASSUME_AVX 1
  47. #endif
  48. #if !defined(FARMHASH_CAN_USE_CXX11) && defined(LANG_CXX11)
  49. #define FARMHASH_CAN_USE_CXX11 1
  50. #else
  51. #undef FARMHASH_CAN_USE_CXX11
  52. #define FARMHASH_CAN_USE_CXX11 0
  53. #endif
  54. // FARMHASH PORTABILITY LAYER: Runtime error if misconfigured
  55. #ifndef FARMHASH_DIE_IF_MISCONFIGURED
  56. #define FARMHASH_DIE_IF_MISCONFIGURED do { *(char*)(len % 17) = 0; } while (0)
  57. #endif
  58. // FARMHASH PORTABILITY LAYER: "static inline" or similar
  59. #ifndef STATIC_INLINE
  60. #define STATIC_INLINE static inline
  61. #endif
  62. // FARMHASH PORTABILITY LAYER: LIKELY and UNLIKELY
  63. #if defined(_MSC_VER)
  64. # define FARMHASH_NO_BUILTIN_EXPECT
  65. #endif
  66. #if !defined(LIKELY)
  67. #if defined(FARMHASH_NO_BUILTIN_EXPECT) || (defined(FARMHASH_OPTIONAL_BUILTIN_EXPECT) && !defined(HAVE_BUILTIN_EXPECT))
  68. #define LIKELY(x) (x)
  69. #else
  70. #define LIKELY(x) (__builtin_expect(!!(x), 1))
  71. #endif
  72. #endif
  73. #undef UNLIKELY
  74. #define UNLIKELY(x) !LIKELY(!(x))
  75. // FARMHASH PORTABILITY LAYER: endianness and byteswapping functions
  76. #ifdef WORDS_BIGENDIAN
  77. #undef FARMHASH_BIG_ENDIAN
  78. #define FARMHASH_BIG_ENDIAN 1
  79. #endif
  80. #if defined(FARMHASH_LITTLE_ENDIAN) && defined(FARMHASH_BIG_ENDIAN)
  81. #error
  82. #endif
  83. #if !defined(FARMHASH_LITTLE_ENDIAN) && !defined(FARMHASH_BIG_ENDIAN)
  84. #define FARMHASH_UNKNOWN_ENDIAN 1
  85. #endif
  86. #if !defined(bswap_32) || !defined(bswap_64)
  87. #undef bswap_32
  88. #undef bswap_64
  89. #if defined(HAVE_BUILTIN_BSWAP) || defined(__clang__) || \
  90. (defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || \
  91. __GNUC__ >= 5))
  92. // Easy case for bswap: no header file needed.
  93. #define bswap_32(x) __builtin_bswap32(x)
  94. #define bswap_64(x) __builtin_bswap64(x)
  95. #endif
  96. #endif
  97. #if defined(FARMHASH_UNKNOWN_ENDIAN) || !defined(bswap_64)
  98. #ifdef _MSC_VER
  99. #undef bswap_32
  100. #undef bswap_64
  101. #define bswap_32(x) _byteswap_ulong(x)
  102. #define bswap_64(x) _byteswap_uint64(x)
  103. #elif defined(__APPLE__)
  104. // Mac OS X / Darwin features
  105. #include <libkern/OSByteOrder.h>
  106. #undef bswap_32
  107. #undef bswap_64
  108. #define bswap_32(x) OSSwapInt32(x)
  109. #define bswap_64(x) OSSwapInt64(x)
  110. #elif defined(__sun) || defined(sun)
  111. #error #include <sys/byteorder.h>
  112. #undef bswap_32
  113. #undef bswap_64
  114. #define bswap_32(x) BSWAP_32(x)
  115. #define bswap_64(x) BSWAP_64(x)
  116. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  117. #include <sys/endian.h>
  118. #undef bswap_32
  119. #undef bswap_64
  120. #define bswap_32(x) bswap32(x)
  121. #define bswap_64(x) bswap64(x)
  122. #elif defined(__OpenBSD__)
  123. #include <sys/types.h>
  124. #undef bswap_32
  125. #undef bswap_64
  126. #define bswap_32(x) swap32(x)
  127. #define bswap_64(x) swap64(x)
  128. #elif defined(__NetBSD__)
  129. #include <sys/types.h>
  130. #include <machine/bswap.h>
  131. #if defined(__BSWAP_RENAME) && !defined(__bswap_32)
  132. #undef bswap_32
  133. #undef bswap_64
  134. #define bswap_32(x) bswap32(x)
  135. #define bswap_64(x) bswap64(x)
  136. #endif
  137. #else
  138. #undef bswap_32
  139. #undef bswap_64
  140. #include <byteswap.h>
  141. #endif
  142. #ifdef WORDS_BIGENDIAN
  143. #define FARMHASH_BIG_ENDIAN 1
  144. #endif
  145. #endif
  146. #ifdef FARMHASH_BIG_ENDIAN
  147. #define uint32_in_expected_order(x) (bswap_32(x))
  148. #define uint64_in_expected_order(x) (bswap_64(x))
  149. #else
  150. #define uint32_in_expected_order(x) (x)
  151. #define uint64_in_expected_order(x) (x)
  152. #endif
  153. namespace NAMESPACE_FOR_HASH_FUNCTIONS {
  154. STATIC_INLINE uint64_t Fetch64(const char *p) {
  155. uint64_t result;
  156. memcpy(&result, p, sizeof(result));
  157. return uint64_in_expected_order(result);
  158. }
  159. STATIC_INLINE uint32_t Fetch32(const char *p) {
  160. uint32_t result;
  161. memcpy(&result, p, sizeof(result));
  162. return uint32_in_expected_order(result);
  163. }
  164. STATIC_INLINE uint32_t Bswap32(uint32_t val) { return bswap_32(val); }
  165. STATIC_INLINE uint64_t Bswap64(uint64_t val) { return bswap_64(val); }
  166. // FARMHASH PORTABILITY LAYER: bitwise rot
  167. STATIC_INLINE uint32_t BasicRotate32(uint32_t val, int shift) {
  168. // Avoid shifting by 32: doing so yields an undefined result.
  169. return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
  170. }
  171. STATIC_INLINE uint64_t BasicRotate64(uint64_t val, int shift) {
  172. // Avoid shifting by 64: doing so yields an undefined result.
  173. return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
  174. }
  175. #if defined(_MSC_VER) && defined(FARMHASH_ROTR)
  176. STATIC_INLINE uint32_t Rotate32(uint32_t val, int shift) {
  177. return sizeof(unsigned long) == sizeof(val) ?
  178. _lrotr(val, shift) :
  179. BasicRotate32(val, shift);
  180. }
  181. STATIC_INLINE uint64_t Rotate64(uint64_t val, int shift) {
  182. return sizeof(unsigned long) == sizeof(val) ?
  183. _lrotr(val, shift) :
  184. BasicRotate64(val, shift);
  185. }
  186. #else
  187. STATIC_INLINE uint32_t Rotate32(uint32_t val, int shift) {
  188. return BasicRotate32(val, shift);
  189. }
  190. STATIC_INLINE uint64_t Rotate64(uint64_t val, int shift) {
  191. return BasicRotate64(val, shift);
  192. }
  193. #endif
  194. } // namespace NAMESPACE_FOR_HASH_FUNCTIONS
  195. // FARMHASH PORTABILITY LAYER: debug mode or max speed?
  196. // One may use -DFARMHASH_DEBUG=1 or -DFARMHASH_DEBUG=0 to force the issue.
  197. #if !defined(FARMHASH_DEBUG) && (!defined(NDEBUG) || defined(_DEBUG))
  198. #define FARMHASH_DEBUG 1
  199. #endif
  200. #undef debug_mode
  201. #if FARMHASH_DEBUG
  202. #define debug_mode 1
  203. #else
  204. #define debug_mode 0
  205. #endif
  206. // PLATFORM-SPECIFIC FUNCTIONS AND MACROS
  207. #undef x86_64
  208. #if defined (__x86_64) || defined (__x86_64__)
  209. #define x86_64 1
  210. #else
  211. #define x86_64 0
  212. #endif
  213. #undef x86
  214. #if defined(__i386__) || defined(__i386) || defined(__X86__)
  215. #define x86 1
  216. #else
  217. #define x86 x86_64
  218. #endif
  219. #if !defined(is_64bit)
  220. #define is_64bit (x86_64 || (sizeof(void*) == 8))
  221. #endif
  222. #undef can_use_ssse3
  223. #if defined(__SSSE3__) || defined(FARMHASH_ASSUME_SSSE3)
  224. #include <immintrin.h>
  225. #define can_use_ssse3 1
  226. // Now we can use _mm_hsub_epi16 and so on.
  227. #else
  228. #define can_use_ssse3 0
  229. #endif
  230. #undef can_use_sse41
  231. #if defined(__SSE4_1__) || defined(FARMHASH_ASSUME_SSE41)
  232. #include <immintrin.h>
  233. #define can_use_sse41 1
  234. // Now we can use _mm_insert_epi64 and so on.
  235. #else
  236. #define can_use_sse41 0
  237. #endif
  238. #undef can_use_sse42
  239. #if defined(__SSE4_2__) || defined(FARMHASH_ASSUME_SSE42)
  240. #include <nmmintrin.h>
  241. #define can_use_sse42 1
  242. // Now we can use _mm_crc32_u{32,16,8}. And on 64-bit platforms, _mm_crc32_u64.
  243. #else
  244. #define can_use_sse42 0
  245. #endif
  246. #undef can_use_aesni
  247. #if defined(__AES__) || defined(FARMHASH_ASSUME_AESNI)
  248. #include <wmmintrin.h>
  249. #define can_use_aesni 1
  250. // Now we can use _mm_aesimc_si128 and so on.
  251. #else
  252. #define can_use_aesni 0
  253. #endif
  254. #undef can_use_avx
  255. #if defined(__AVX__) || defined(FARMHASH_ASSUME_AVX)
  256. #include <immintrin.h>
  257. #define can_use_avx 1
  258. #else
  259. #define can_use_avx 0
  260. #endif
  261. #if can_use_ssse3 || can_use_sse41 || can_use_sse42 || can_use_aesni || can_use_avx
  262. STATIC_INLINE __m128i Fetch128(const char* s) {
  263. return _mm_loadu_si128(reinterpret_cast<const __m128i*>(s));
  264. }
  265. #endif
  266. // Building blocks for hash functions
  267. // std::swap() was in <algorithm> but is in <utility> from C++11 on.
  268. #if !FARMHASH_CAN_USE_CXX11
  269. #include <algorithm>
  270. #endif
  271. #undef PERMUTE3
  272. #define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0)
  273. namespace NAMESPACE_FOR_HASH_FUNCTIONS {
  274. // Some primes between 2^63 and 2^64 for various uses.
  275. static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
  276. static const uint64_t k1 = 0xb492b66fbe98f273ULL;
  277. static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
  278. // Magic numbers for 32-bit hashing. Copied from Murmur3.
  279. static const uint32_t c1 = 0xcc9e2d51;
  280. static const uint32_t c2 = 0x1b873593;
  281. // A 32-bit to 32-bit integer hash copied from Murmur3.
  282. STATIC_INLINE uint32_t fmix(uint32_t h)
  283. {
  284. h ^= h >> 16;
  285. h *= 0x85ebca6b;
  286. h ^= h >> 13;
  287. h *= 0xc2b2ae35;
  288. h ^= h >> 16;
  289. return h;
  290. }
  291. STATIC_INLINE uint32_t Mur(uint32_t a, uint32_t h) {
  292. // Helper from Murmur3 for combining two 32-bit values.
  293. a *= c1;
  294. a = Rotate32(a, 17);
  295. a *= c2;
  296. h ^= a;
  297. h = Rotate32(h, 19);
  298. return h * 5 + 0xe6546b64;
  299. }
  300. template <typename T> STATIC_INLINE T DebugTweak(T x) {
  301. if (debug_mode) {
  302. if (sizeof(x) == 4) {
  303. x = ~Bswap32(x * c1);
  304. } else {
  305. x = ~Bswap64(x * k1);
  306. }
  307. }
  308. return x;
  309. }
  310. template <> uint128_t DebugTweak(uint128_t x) {
  311. if (debug_mode) {
  312. uint64_t y = DebugTweak(Uint128Low64(x));
  313. uint64_t z = DebugTweak(Uint128High64(x));
  314. y += z;
  315. z += y;
  316. x = Uint128(y, z * k1);
  317. }
  318. return x;
  319. }
  320. } // namespace NAMESPACE_FOR_HASH_FUNCTIONS
  321. using namespace std;
  322. using namespace NAMESPACE_FOR_HASH_FUNCTIONS;