phmap_bits.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. #if !defined(phmap_bits_h_guard_)
  2. #define phmap_bits_h_guard_
  3. // ---------------------------------------------------------------------------
  4. // Copyright (c) 2019, Gregory Popovitch - [email protected]
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // https://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. // Includes work from abseil-cpp (https://github.com/abseil/abseil-cpp)
  19. // with modifications.
  20. //
  21. // Copyright 2018 The Abseil Authors.
  22. //
  23. // Licensed under the Apache License, Version 2.0 (the "License");
  24. // you may not use this file except in compliance with the License.
  25. // You may obtain a copy of the License at
  26. //
  27. // https://www.apache.org/licenses/LICENSE-2.0
  28. //
  29. // Unless required by applicable law or agreed to in writing, software
  30. // distributed under the License is distributed on an "AS IS" BASIS,
  31. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. // See the License for the specific language governing permissions and
  33. // limitations under the License.
  34. // ---------------------------------------------------------------------------
  35. // The following guarantees declaration of the byte swap functions
  36. #ifdef _MSC_VER
  37. #include <stdlib.h> // NOLINT(build/include)
  38. #elif defined(__APPLE__)
  39. // Mac OS X / Darwin features
  40. #include <libkern/OSByteOrder.h>
  41. #elif defined(__FreeBSD__)
  42. #include <sys/endian.h>
  43. #elif defined(__GLIBC__)
  44. #include <byteswap.h> // IWYU pragma: export
  45. #endif
  46. #include <string.h>
  47. #include <cstdint>
  48. #include "phmap_config.h"
  49. #ifdef _MSC_VER
  50. #pragma warning(push)
  51. #pragma warning(disable : 4514) // unreferenced inline function has been removed
  52. #endif
  53. // -----------------------------------------------------------------------------
  54. // unaligned APIs
  55. // -----------------------------------------------------------------------------
  56. // Portable handling of unaligned loads, stores, and copies.
  57. // On some platforms, like ARM, the copy functions can be more efficient
  58. // then a load and a store.
  59. // -----------------------------------------------------------------------------
  60. #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
  61. defined(MEMORY_SANITIZER)
  62. #include <stdint.h>
  63. extern "C" {
  64. uint16_t __sanitizer_unaligned_load16(const void *p);
  65. uint32_t __sanitizer_unaligned_load32(const void *p);
  66. uint64_t __sanitizer_unaligned_load64(const void *p);
  67. void __sanitizer_unaligned_store16(void *p, uint16_t v);
  68. void __sanitizer_unaligned_store32(void *p, uint32_t v);
  69. void __sanitizer_unaligned_store64(void *p, uint64_t v);
  70. } // extern "C"
  71. namespace phmap {
  72. namespace bits {
  73. LL_INLINE uint16_t UnalignedLoad16(const void *p) {
  74. return __sanitizer_unaligned_load16(p);
  75. }
  76. LL_INLINE uint32_t UnalignedLoad32(const void *p) {
  77. return __sanitizer_unaligned_load32(p);
  78. }
  79. LL_INLINE uint64_t UnalignedLoad64(const void *p) {
  80. return __sanitizer_unaligned_load64(p);
  81. }
  82. LL_INLINE void UnalignedStore16(void *p, uint16_t v) {
  83. __sanitizer_unaligned_store16(p, v);
  84. }
  85. LL_INLINE void UnalignedStore32(void *p, uint32_t v) {
  86. __sanitizer_unaligned_store32(p, v);
  87. }
  88. LL_INLINE void UnalignedStore64(void *p, uint64_t v) {
  89. __sanitizer_unaligned_store64(p, v);
  90. }
  91. } // namespace bits
  92. } // namespace phmap
  93. #define PHMAP_INTERNAL_UNALIGNED_LOAD16(_p) (phmap::bits::UnalignedLoad16(_p))
  94. #define PHMAP_INTERNAL_UNALIGNED_LOAD32(_p) (phmap::bits::UnalignedLoad32(_p))
  95. #define PHMAP_INTERNAL_UNALIGNED_LOAD64(_p) (phmap::bits::UnalignedLoad64(_p))
  96. #define PHMAP_INTERNAL_UNALIGNED_STORE16(_p, _val) (phmap::bits::UnalignedStore16(_p, _val))
  97. #define PHMAP_INTERNAL_UNALIGNED_STORE32(_p, _val) (phmap::bits::UnalignedStore32(_p, _val))
  98. #define PHMAP_INTERNAL_UNALIGNED_STORE64(_p, _val) (phmap::bits::UnalignedStore64(_p, _val))
  99. #else
  100. namespace phmap {
  101. namespace bits {
  102. LL_INLINE uint16_t UnalignedLoad16(const void *p) {
  103. uint16_t t;
  104. memcpy(&t, p, sizeof t);
  105. return t;
  106. }
  107. LL_INLINE uint32_t UnalignedLoad32(const void *p) {
  108. uint32_t t;
  109. memcpy(&t, p, sizeof t);
  110. return t;
  111. }
  112. LL_INLINE uint64_t UnalignedLoad64(const void *p) {
  113. uint64_t t;
  114. memcpy(&t, p, sizeof t);
  115. return t;
  116. }
  117. LL_INLINE void UnalignedStore16(void *p, uint16_t v) { memcpy(p, &v, sizeof v); }
  118. LL_INLINE void UnalignedStore32(void *p, uint32_t v) { memcpy(p, &v, sizeof v); }
  119. LL_INLINE void UnalignedStore64(void *p, uint64_t v) { memcpy(p, &v, sizeof v); }
  120. } // namespace bits
  121. } // namespace phmap
  122. #define PHMAP_INTERNAL_UNALIGNED_LOAD16(_p) (phmap::bits::UnalignedLoad16(_p))
  123. #define PHMAP_INTERNAL_UNALIGNED_LOAD32(_p) (phmap::bits::UnalignedLoad32(_p))
  124. #define PHMAP_INTERNAL_UNALIGNED_LOAD64(_p) (phmap::bits::UnalignedLoad64(_p))
  125. #define PHMAP_INTERNAL_UNALIGNED_STORE16(_p, _val) (phmap::bits::UnalignedStore16(_p, _val))
  126. #define PHMAP_INTERNAL_UNALIGNED_STORE32(_p, _val) (phmap::bits::UnalignedStore32(_p, _val))
  127. #define PHMAP_INTERNAL_UNALIGNED_STORE64(_p, _val) (phmap::bits::UnalignedStore64(_p, _val))
  128. #endif
  129. // -----------------------------------------------------------------------------
  130. // File: optimization.h
  131. // -----------------------------------------------------------------------------
  132. #if defined(__pnacl__)
  133. #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
  134. #elif defined(__clang__)
  135. // Clang will not tail call given inline volatile assembly.
  136. #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  137. #elif defined(__GNUC__)
  138. // GCC will not tail call given inline volatile assembly.
  139. #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  140. #elif defined(_MSC_VER)
  141. #include <intrin.h>
  142. // The __nop() intrinsic blocks the optimisation.
  143. #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
  144. #else
  145. #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
  146. #endif
  147. #if defined(__GNUC__)
  148. #pragma GCC diagnostic push
  149. #pragma GCC diagnostic ignored "-Wpedantic"
  150. #endif
  151. #ifdef PHMAP_HAVE_INTRINSIC_INT128
  152. __extension__ typedef unsigned __int128 phmap_uint128;
  153. LL_INLINE uint64_t umul128(uint64_t a, uint64_t b, uint64_t* high)
  154. {
  155. auto result = static_cast<phmap_uint128>(a) * static_cast<phmap_uint128>(b);
  156. *high = static_cast<uint64_t>(result >> 64);
  157. return static_cast<uint64_t>(result);
  158. }
  159. #define PHMAP_HAS_UMUL128 1
  160. #elif (defined(_MSC_VER))
  161. #if defined(_M_X64)
  162. #pragma intrinsic(_umul128)
  163. LL_INLINE uint64_t umul128(uint64_t a, uint64_t b, uint64_t* high)
  164. {
  165. return _umul128(a, b, high);
  166. }
  167. #define PHMAP_HAS_UMUL128 1
  168. #endif
  169. #endif
  170. #if defined(__GNUC__)
  171. #pragma GCC diagnostic pop
  172. #endif
  173. #if defined(__GNUC__)
  174. // Cache line alignment
  175. #if defined(__i386__) || defined(__x86_64__)
  176. #define PHMAP_CACHELINE_SIZE 64
  177. #elif defined(__powerpc64__)
  178. #define PHMAP_CACHELINE_SIZE 128
  179. #elif defined(__aarch64__)
  180. // We would need to read special register ctr_el0 to find out L1 dcache size.
  181. // This value is a good estimate based on a real aarch64 machine.
  182. #define PHMAP_CACHELINE_SIZE 64
  183. #elif defined(__arm__)
  184. // Cache line sizes for ARM: These values are not strictly correct since
  185. // cache line sizes depend on implementations, not architectures. There
  186. // are even implementations with cache line sizes configurable at boot
  187. // time.
  188. #if defined(__ARM_ARCH_5T__)
  189. #define PHMAP_CACHELINE_SIZE 32
  190. #elif defined(__ARM_ARCH_7A__)
  191. #define PHMAP_CACHELINE_SIZE 64
  192. #endif
  193. #endif
  194. #ifndef PHMAP_CACHELINE_SIZE
  195. // A reasonable default guess. Note that overestimates tend to waste more
  196. // space, while underestimates tend to waste more time.
  197. #define PHMAP_CACHELINE_SIZE 64
  198. #endif
  199. #define PHMAP_CACHELINE_ALIGNED __attribute__((aligned(PHMAP_CACHELINE_SIZE)))
  200. #elif defined(_MSC_VER)
  201. #define PHMAP_CACHELINE_SIZE 64
  202. #define PHMAP_CACHELINE_ALIGNED __declspec(align(PHMAP_CACHELINE_SIZE))
  203. #else
  204. #define PHMAP_CACHELINE_SIZE 64
  205. #define PHMAP_CACHELINE_ALIGNED
  206. #endif
  207. #if PHMAP_HAVE_BUILTIN(__builtin_expect) || \
  208. (defined(__GNUC__) && !defined(__clang__))
  209. #define PHMAP_PREDICT_FALSE(x) (__builtin_expect(x, 0))
  210. #define PHMAP_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
  211. #else
  212. #define PHMAP_PREDICT_FALSE(x) (x)
  213. #define PHMAP_PREDICT_TRUE(x) (x)
  214. #endif
  215. // -----------------------------------------------------------------------------
  216. // File: bits.h
  217. // -----------------------------------------------------------------------------
  218. namespace phmap {
  219. namespace base_internal {
  220. LL_INLINE uint32_t CountLeadingZeros64Slow(uint64_t n) {
  221. int zeroes = 60;
  222. if (n >> 32) zeroes -= 32, n >>= 32;
  223. if (n >> 16) zeroes -= 16, n >>= 16;
  224. if (n >> 8) zeroes -= 8, n >>= 8;
  225. if (n >> 4) zeroes -= 4, n >>= 4;
  226. return (uint32_t)("\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes);
  227. }
  228. LL_INLINE uint32_t CountLeadingZeros64(uint64_t n) {
  229. #if defined(_MSC_VER) && defined(_M_X64)
  230. // MSVC does not have __buitin_clzll. Use _BitScanReverse64.
  231. unsigned long result = 0; // NOLINT(runtime/int)
  232. if (_BitScanReverse64(&result, n)) {
  233. return (uint32_t)(63 - result);
  234. }
  235. return 64;
  236. #elif defined(_MSC_VER) && !defined(__clang__)
  237. // MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse
  238. unsigned long result = 0; // NOLINT(runtime/int)
  239. if ((n >> 32) && _BitScanReverse(&result, (unsigned long)(n >> 32))) {
  240. return (uint32_t)(31 - result);
  241. }
  242. if (_BitScanReverse(&result, (unsigned long)n)) {
  243. return (uint32_t)(63 - result);
  244. }
  245. return 64;
  246. #elif defined(__GNUC__) || defined(__clang__)
  247. // Use __builtin_clzll, which uses the following instructions:
  248. // x86: bsr
  249. // ARM64: clz
  250. // PPC: cntlzd
  251. static_assert(sizeof(unsigned long long) == sizeof(n), // NOLINT(runtime/int)
  252. "__builtin_clzll does not take 64-bit arg");
  253. // Handle 0 as a special case because __builtin_clzll(0) is undefined.
  254. if (n == 0) {
  255. return 64;
  256. }
  257. return (uint32_t)__builtin_clzll(n);
  258. #else
  259. return CountLeadingZeros64Slow(n);
  260. #endif
  261. }
  262. LL_INLINE uint32_t CountLeadingZeros32Slow(uint64_t n) {
  263. uint32_t zeroes = 28;
  264. if (n >> 16) zeroes -= 16, n >>= 16;
  265. if (n >> 8) zeroes -= 8, n >>= 8;
  266. if (n >> 4) zeroes -= 4, n >>= 4;
  267. return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
  268. }
  269. LL_INLINE uint32_t CountLeadingZeros32(uint32_t n) {
  270. #if defined(_MSC_VER) && !defined(__clang__)
  271. unsigned long result = 0; // NOLINT(runtime/int)
  272. if (_BitScanReverse(&result, n)) {
  273. return (uint32_t)(31 - result);
  274. }
  275. return 32;
  276. #elif defined(__GNUC__) || defined(__clang__)
  277. // Use __builtin_clz, which uses the following instructions:
  278. // x86: bsr
  279. // ARM64: clz
  280. // PPC: cntlzd
  281. static_assert(sizeof(int) == sizeof(n),
  282. "__builtin_clz does not take 32-bit arg");
  283. // Handle 0 as a special case because __builtin_clz(0) is undefined.
  284. if (n == 0) {
  285. return 32;
  286. }
  287. return __builtin_clz(n);
  288. #else
  289. return CountLeadingZeros32Slow(n);
  290. #endif
  291. }
  292. LL_INLINE uint32_t CountTrailingZerosNonZero64Slow(uint64_t n) {
  293. uint32_t c = 63;
  294. n &= ~n + 1;
  295. if (n & 0x00000000FFFFFFFF) c -= 32;
  296. if (n & 0x0000FFFF0000FFFF) c -= 16;
  297. if (n & 0x00FF00FF00FF00FF) c -= 8;
  298. if (n & 0x0F0F0F0F0F0F0F0F) c -= 4;
  299. if (n & 0x3333333333333333) c -= 2;
  300. if (n & 0x5555555555555555) c -= 1;
  301. return c;
  302. }
  303. LL_INLINE uint32_t CountTrailingZerosNonZero64(uint64_t n) {
  304. #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_X64)
  305. unsigned long result = 0; // NOLINT(runtime/int)
  306. _BitScanForward64(&result, n);
  307. return (uint32_t)result;
  308. #elif defined(_MSC_VER) && !defined(__clang__)
  309. unsigned long result = 0; // NOLINT(runtime/int)
  310. if (static_cast<uint32_t>(n) == 0) {
  311. _BitScanForward(&result, (unsigned long)(n >> 32));
  312. return result + 32;
  313. }
  314. _BitScanForward(&result, (unsigned long)n);
  315. return result;
  316. #elif defined(__GNUC__) || defined(__clang__)
  317. static_assert(sizeof(unsigned long long) == sizeof(n), // NOLINT(runtime/int)
  318. "__builtin_ctzll does not take 64-bit arg");
  319. return __builtin_ctzll(n);
  320. #else
  321. return CountTrailingZerosNonZero64Slow(n);
  322. #endif
  323. }
  324. LL_INLINE uint32_t CountTrailingZerosNonZero32Slow(uint32_t n) {
  325. uint32_t c = 31;
  326. n &= ~n + 1;
  327. if (n & 0x0000FFFF) c -= 16;
  328. if (n & 0x00FF00FF) c -= 8;
  329. if (n & 0x0F0F0F0F) c -= 4;
  330. if (n & 0x33333333) c -= 2;
  331. if (n & 0x55555555) c -= 1;
  332. return c;
  333. }
  334. LL_INLINE uint32_t CountTrailingZerosNonZero32(uint32_t n) {
  335. #if defined(_MSC_VER) && !defined(__clang__)
  336. unsigned long result = 0; // NOLINT(runtime/int)
  337. _BitScanForward(&result, n);
  338. return (uint32_t)result;
  339. #elif defined(__GNUC__) || defined(__clang__)
  340. static_assert(sizeof(int) == sizeof(n),
  341. "__builtin_ctz does not take 32-bit arg");
  342. return __builtin_ctz(n);
  343. #else
  344. return CountTrailingZerosNonZero32Slow(n);
  345. #endif
  346. }
  347. } // namespace base_internal
  348. } // namespace phmap
  349. // -----------------------------------------------------------------------------
  350. // File: endian.h
  351. // -----------------------------------------------------------------------------
  352. namespace phmap {
  353. // Use compiler byte-swapping intrinsics if they are available. 32-bit
  354. // and 64-bit versions are available in Clang and GCC as of GCC 4.3.0.
  355. // The 16-bit version is available in Clang and GCC only as of GCC 4.8.0.
  356. // For simplicity, we enable them all only for GCC 4.8.0 or later.
  357. #if defined(__clang__) || \
  358. (defined(__GNUC__) && \
  359. ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5))
  360. LL_INLINE uint64_t gbswap_64(uint64_t host_int) {
  361. return __builtin_bswap64(host_int);
  362. }
  363. LL_INLINE uint32_t gbswap_32(uint32_t host_int) {
  364. return __builtin_bswap32(host_int);
  365. }
  366. LL_INLINE uint16_t gbswap_16(uint16_t host_int) {
  367. return __builtin_bswap16(host_int);
  368. }
  369. #elif defined(_MSC_VER)
  370. LL_INLINE uint64_t gbswap_64(uint64_t host_int) {
  371. return _byteswap_uint64(host_int);
  372. }
  373. LL_INLINE uint32_t gbswap_32(uint32_t host_int) {
  374. return _byteswap_ulong(host_int);
  375. }
  376. LL_INLINE uint16_t gbswap_16(uint16_t host_int) {
  377. return _byteswap_ushort(host_int);
  378. }
  379. #elif defined(__APPLE__)
  380. LL_INLINE uint64_t gbswap_64(uint64_t host_int) { return OSSwapInt16(host_int); }
  381. LL_INLINE uint32_t gbswap_32(uint32_t host_int) { return OSSwapInt32(host_int); }
  382. LL_INLINE uint16_t gbswap_16(uint16_t host_int) { return OSSwapInt64(host_int); }
  383. #else
  384. LL_INLINE uint64_t gbswap_64(uint64_t host_int) {
  385. #if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__)
  386. // Adapted from /usr/include/byteswap.h. Not available on Mac.
  387. if (__builtin_constant_p(host_int)) {
  388. return __bswap_constant_64(host_int);
  389. } else {
  390. uint64_t result;
  391. __asm__("bswap %0" : "=r"(result) : "0"(host_int));
  392. return result;
  393. }
  394. #elif defined(__GLIBC__)
  395. return bswap_64(host_int);
  396. #else
  397. return (((host_int & uint64_t{0xFF}) << 56) |
  398. ((host_int & uint64_t{0xFF00}) << 40) |
  399. ((host_int & uint64_t{0xFF0000}) << 24) |
  400. ((host_int & uint64_t{0xFF000000}) << 8) |
  401. ((host_int & uint64_t{0xFF00000000}) >> 8) |
  402. ((host_int & uint64_t{0xFF0000000000}) >> 24) |
  403. ((host_int & uint64_t{0xFF000000000000}) >> 40) |
  404. ((host_int & uint64_t{0xFF00000000000000}) >> 56));
  405. #endif // bswap_64
  406. }
  407. LL_INLINE uint32_t gbswap_32(uint32_t host_int) {
  408. #if defined(__GLIBC__)
  409. return bswap_32(host_int);
  410. #else
  411. return (((host_int & uint32_t{0xFF}) << 24) |
  412. ((host_int & uint32_t{0xFF00}) << 8) |
  413. ((host_int & uint32_t{0xFF0000}) >> 8) |
  414. ((host_int & uint32_t{0xFF000000}) >> 24));
  415. #endif
  416. }
  417. LL_INLINE uint16_t gbswap_16(uint16_t host_int) {
  418. #if defined(__GLIBC__)
  419. return bswap_16(host_int);
  420. #else
  421. return (((host_int & uint16_t{0xFF}) << 8) |
  422. ((host_int & uint16_t{0xFF00}) >> 8));
  423. #endif
  424. }
  425. #endif // intrinics available
  426. #ifdef PHMAP_IS_LITTLE_ENDIAN
  427. // Definitions for ntohl etc. that don't require us to include
  428. // netinet/in.h. We wrap gbswap_32 and gbswap_16 in functions rather
  429. // than just #defining them because in debug mode, gcc doesn't
  430. // correctly handle the (rather involved) definitions of bswap_32.
  431. // gcc guarantees that inline functions are as fast as macros, so
  432. // this isn't a performance hit.
  433. LL_INLINE uint16_t ghtons(uint16_t x) { return gbswap_16(x); }
  434. LL_INLINE uint32_t ghtonl(uint32_t x) { return gbswap_32(x); }
  435. LL_INLINE uint64_t ghtonll(uint64_t x) { return gbswap_64(x); }
  436. #elif defined PHMAP_IS_BIG_ENDIAN
  437. // These definitions are simpler on big-endian machines
  438. // These are functions instead of macros to avoid self-assignment warnings
  439. // on calls such as "i = ghtnol(i);". This also provides type checking.
  440. LL_INLINE uint16_t ghtons(uint16_t x) { return x; }
  441. LL_INLINE uint32_t ghtonl(uint32_t x) { return x; }
  442. LL_INLINE uint64_t ghtonll(uint64_t x) { return x; }
  443. #else
  444. #error \
  445. "Unsupported byte order: Either PHMAP_IS_BIG_ENDIAN or " \
  446. "PHMAP_IS_LITTLE_ENDIAN must be defined"
  447. #endif // byte order
  448. LL_INLINE uint16_t gntohs(uint16_t x) { return ghtons(x); }
  449. LL_INLINE uint32_t gntohl(uint32_t x) { return ghtonl(x); }
  450. LL_INLINE uint64_t gntohll(uint64_t x) { return ghtonll(x); }
  451. // Utilities to convert numbers between the current hosts's native byte
  452. // order and little-endian byte order
  453. //
  454. // Load/Store methods are alignment safe
  455. namespace little_endian {
  456. // Conversion functions.
  457. #ifdef PHMAP_IS_LITTLE_ENDIAN
  458. LL_INLINE uint16_t FromHost16(uint16_t x) { return x; }
  459. LL_INLINE uint16_t ToHost16(uint16_t x) { return x; }
  460. LL_INLINE uint32_t FromHost32(uint32_t x) { return x; }
  461. LL_INLINE uint32_t ToHost32(uint32_t x) { return x; }
  462. LL_INLINE uint64_t FromHost64(uint64_t x) { return x; }
  463. LL_INLINE uint64_t ToHost64(uint64_t x) { return x; }
  464. LL_INLINE constexpr bool IsLittleEndian() { return true; }
  465. #elif defined PHMAP_IS_BIG_ENDIAN
  466. LL_INLINE uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
  467. LL_INLINE uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
  468. LL_INLINE uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
  469. LL_INLINE uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
  470. LL_INLINE uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
  471. LL_INLINE uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
  472. LL_INLINE constexpr bool IsLittleEndian() { return false; }
  473. #endif /* ENDIAN */
  474. // Functions to do unaligned loads and stores in little-endian order.
  475. // ------------------------------------------------------------------
  476. LL_INLINE uint16_t Load16(const void *p) {
  477. return ToHost16(PHMAP_INTERNAL_UNALIGNED_LOAD16(p));
  478. }
  479. LL_INLINE void Store16(void *p, uint16_t v) {
  480. PHMAP_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
  481. }
  482. LL_INLINE uint32_t Load32(const void *p) {
  483. return ToHost32(PHMAP_INTERNAL_UNALIGNED_LOAD32(p));
  484. }
  485. LL_INLINE void Store32(void *p, uint32_t v) {
  486. PHMAP_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
  487. }
  488. LL_INLINE uint64_t Load64(const void *p) {
  489. return ToHost64(PHMAP_INTERNAL_UNALIGNED_LOAD64(p));
  490. }
  491. LL_INLINE void Store64(void *p, uint64_t v) {
  492. PHMAP_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
  493. }
  494. } // namespace little_endian
  495. // Utilities to convert numbers between the current hosts's native byte
  496. // order and big-endian byte order (same as network byte order)
  497. //
  498. // Load/Store methods are alignment safe
  499. namespace big_endian {
  500. #ifdef PHMAP_IS_LITTLE_ENDIAN
  501. LL_INLINE uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
  502. LL_INLINE uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
  503. LL_INLINE uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
  504. LL_INLINE uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
  505. LL_INLINE uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
  506. LL_INLINE uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
  507. LL_INLINE constexpr bool IsLittleEndian() { return true; }
  508. #elif defined PHMAP_IS_BIG_ENDIAN
  509. LL_INLINE uint16_t FromHost16(uint16_t x) { return x; }
  510. LL_INLINE uint16_t ToHost16(uint16_t x) { return x; }
  511. LL_INLINE uint32_t FromHost32(uint32_t x) { return x; }
  512. LL_INLINE uint32_t ToHost32(uint32_t x) { return x; }
  513. LL_INLINE uint64_t FromHost64(uint64_t x) { return x; }
  514. LL_INLINE uint64_t ToHost64(uint64_t x) { return x; }
  515. LL_INLINE constexpr bool IsLittleEndian() { return false; }
  516. #endif /* ENDIAN */
  517. // Functions to do unaligned loads and stores in big-endian order.
  518. LL_INLINE uint16_t Load16(const void *p) {
  519. return ToHost16(PHMAP_INTERNAL_UNALIGNED_LOAD16(p));
  520. }
  521. LL_INLINE void Store16(void *p, uint16_t v) {
  522. PHMAP_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
  523. }
  524. LL_INLINE uint32_t Load32(const void *p) {
  525. return ToHost32(PHMAP_INTERNAL_UNALIGNED_LOAD32(p));
  526. }
  527. LL_INLINE void Store32(void *p, uint32_t v) {
  528. PHMAP_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
  529. }
  530. LL_INLINE uint64_t Load64(const void *p) {
  531. return ToHost64(PHMAP_INTERNAL_UNALIGNED_LOAD64(p));
  532. }
  533. LL_INLINE void Store64(void *p, uint64_t v) {
  534. PHMAP_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
  535. }
  536. } // namespace big_endian
  537. } // namespace phmap
  538. #ifdef _MSC_VER
  539. #pragma warning(pop)
  540. #endif
  541. #endif // phmap_bits_h_guard_