1#if !defined(phmap_bits_h_guard_)
2#define phmap_bits_h_guard_
40#elif defined(__APPLE__)
42 #include <libkern/OSByteOrder.h>
43#elif defined(__FreeBSD__)
44 #include <sys/endian.h>
45#elif defined(__GLIBC__)
55 #pragma warning(disable : 4514)
66#if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
67 defined(MEMORY_SANITIZER)
71 uint16_t __sanitizer_unaligned_load16(
const void *p);
72 uint32_t __sanitizer_unaligned_load32(
const void *p);
73 uint64_t __sanitizer_unaligned_load64(
const void *p);
74 void __sanitizer_unaligned_store16(
void *p, uint16_t v);
75 void __sanitizer_unaligned_store32(
void *p, uint32_t v);
76 void __sanitizer_unaligned_store64(
void *p, uint64_t v);
83 return __sanitizer_unaligned_load16(p);
87 return __sanitizer_unaligned_load32(p);
91 return __sanitizer_unaligned_load64(p);
95 __sanitizer_unaligned_store16(p, v);
99 __sanitizer_unaligned_store32(p, v);
103 __sanitizer_unaligned_store64(p, v);
109#define PHMAP_INTERNAL_UNALIGNED_LOAD16(_p) (phmap::bits::UnalignedLoad16(_p))
110#define PHMAP_INTERNAL_UNALIGNED_LOAD32(_p) (phmap::bits::UnalignedLoad32(_p))
111#define PHMAP_INTERNAL_UNALIGNED_LOAD64(_p) (phmap::bits::UnalignedLoad64(_p))
113#define PHMAP_INTERNAL_UNALIGNED_STORE16(_p, _val) (phmap::bits::UnalignedStore16(_p, _val))
114#define PHMAP_INTERNAL_UNALIGNED_STORE32(_p, _val) (phmap::bits::UnalignedStore32(_p, _val))
115#define PHMAP_INTERNAL_UNALIGNED_STORE64(_p, _val) (phmap::bits::UnalignedStore64(_p, _val))
124 memcpy(&t, p,
sizeof t);
130 memcpy(&t, p,
sizeof t);
136 memcpy(&t, p,
sizeof t);
149#define PHMAP_INTERNAL_UNALIGNED_LOAD16(_p) (phmap::bits::UnalignedLoad16(_p))
150#define PHMAP_INTERNAL_UNALIGNED_LOAD32(_p) (phmap::bits::UnalignedLoad32(_p))
151#define PHMAP_INTERNAL_UNALIGNED_LOAD64(_p) (phmap::bits::UnalignedLoad64(_p))
153#define PHMAP_INTERNAL_UNALIGNED_STORE16(_p, _val) (phmap::bits::UnalignedStore16(_p, _val))
154#define PHMAP_INTERNAL_UNALIGNED_STORE32(_p, _val) (phmap::bits::UnalignedStore32(_p, _val))
155#define PHMAP_INTERNAL_UNALIGNED_STORE64(_p, _val) (phmap::bits::UnalignedStore64(_p, _val))
163#if defined(__pnacl__)
164 #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
165#elif defined(__clang__)
167 #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
168#elif defined(__GNUC__)
170 #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
171#elif defined(_MSC_VER)
174 #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
176 #define PHMAP_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
180 #pragma GCC diagnostic push
181 #pragma GCC diagnostic ignored "-Wpedantic"
184#ifdef PHMAP_HAVE_INTRINSIC_INT128
185 __extension__
typedef unsigned __int128 phmap_uint128;
186 inline uint64_t umul128(uint64_t a, uint64_t b, uint64_t* high)
188 auto result =
static_cast<phmap_uint128
>(a) *
static_cast<phmap_uint128
>(b);
189 *high =
static_cast<uint64_t
>(result >> 64);
190 return static_cast<uint64_t
>(result);
192 #define PHMAP_HAS_UMUL128 1
193#elif (defined(_MSC_VER))
195 #pragma intrinsic(_umul128)
196 inline uint64_t umul128(uint64_t a, uint64_t b, uint64_t* high)
198 return _umul128(a, b, high);
200 #define PHMAP_HAS_UMUL128 1
205 #pragma GCC diagnostic pop
210 #if defined(__i386__) || defined(__x86_64__)
211 #define PHMAP_CACHELINE_SIZE 64
212 #elif defined(__powerpc64__)
213 #define PHMAP_CACHELINE_SIZE 128
214 #elif defined(__aarch64__)
217 #define PHMAP_CACHELINE_SIZE 64
218 #elif defined(__arm__)
223 #if defined(__ARM_ARCH_5T__)
224 #define PHMAP_CACHELINE_SIZE 32
225 #elif defined(__ARM_ARCH_7A__)
226 #define PHMAP_CACHELINE_SIZE 64
230 #ifndef PHMAP_CACHELINE_SIZE
233 #define PHMAP_CACHELINE_SIZE 64
236 #define PHMAP_CACHELINE_ALIGNED __attribute__((aligned(PHMAP_CACHELINE_SIZE)))
237#elif defined(_MSC_VER)
238 #define PHMAP_CACHELINE_SIZE 64
239 #define PHMAP_CACHELINE_ALIGNED __declspec(align(PHMAP_CACHELINE_SIZE))
241 #define PHMAP_CACHELINE_SIZE 64
242 #define PHMAP_CACHELINE_ALIGNED
246#if PHMAP_HAVE_BUILTIN(__builtin_expect) || \
247 (defined(__GNUC__) && !defined(__clang__))
248 #define PHMAP_PREDICT_FALSE(x) (__builtin_expect(x, 0))
249 #define PHMAP_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
251 #define PHMAP_PREDICT_FALSE(x) (x)
252 #define PHMAP_PREDICT_TRUE(x) (x)
263 #define PHMAP_BASE_INTERNAL_FORCEINLINE __forceinline
266 #define PHMAP_BASE_INTERNAL_FORCEINLINE inline PHMAP_ATTRIBUTE_ALWAYS_INLINE
271namespace base_internal {
275 if (n >> 32) zeroes -= 32, n >>= 32;
276 if (n >> 16) zeroes -= 16, n >>= 16;
277 if (n >> 8) zeroes -= 8, n >>= 8;
278 if (n >> 4) zeroes -= 4, n >>= 4;
279 return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
283#if defined(_MSC_VER) && defined(_M_X64)
285 unsigned long result = 0;
286 if (_BitScanReverse64(&result, n)) {
287 return (
int)(63 - result);
290#elif defined(_MSC_VER) && !defined(__clang__)
292 unsigned long result = 0;
293 if ((n >> 32) && _BitScanReverse(&result, (
unsigned long)(n >> 32))) {
296 if (_BitScanReverse(&result, (
unsigned long)n)) {
300#elif defined(__GNUC__) || defined(__clang__)
305 static_assert(
sizeof(
unsigned long long) ==
sizeof(n),
306 "__builtin_clzll does not take 64-bit arg");
312 return __builtin_clzll(n);
319 uint32_t zeroes = 28;
320 if (n >> 16) zeroes -= 16, n >>= 16;
321 if (n >> 8) zeroes -= 8, n >>= 8;
322 if (n >> 4) zeroes -= 4, n >>= 4;
323 return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
327#if defined(_MSC_VER) && !defined(__clang__)
328 unsigned long result = 0;
329 if (_BitScanReverse(&result, n)) {
330 return (uint32_t)(31 - result);
333#elif defined(__GNUC__) || defined(__clang__)
338 static_assert(
sizeof(int) ==
sizeof(n),
339 "__builtin_clz does not take 32-bit arg");
345 return __builtin_clz(n);
354 if (n & 0x00000000FFFFFFFF) c -= 32;
355 if (n & 0x0000FFFF0000FFFF) c -= 16;
356 if (n & 0x00FF00FF00FF00FF) c -= 8;
357 if (n & 0x0F0F0F0F0F0F0F0F) c -= 4;
358 if (n & 0x3333333333333333) c -= 2;
359 if (n & 0x5555555555555555) c -= 1;
364#if defined(_MSC_VER) && !defined(__clang__) && defined(_M_X64)
365 unsigned long result = 0;
366 _BitScanForward64(&result, n);
367 return (uint32_t)result;
368#elif defined(_MSC_VER) && !defined(__clang__)
369 unsigned long result = 0;
370 if (
static_cast<uint32_t
>(n) == 0) {
371 _BitScanForward(&result, (
unsigned long)(n >> 32));
374 _BitScanForward(&result, (
unsigned long)n);
376#elif defined(__GNUC__) || defined(__clang__)
377 static_assert(
sizeof(
unsigned long long) ==
sizeof(n),
378 "__builtin_ctzll does not take 64-bit arg");
379 return __builtin_ctzll(n);
388 if (n & 0x0000FFFF) c -= 16;
389 if (n & 0x00FF00FF) c -= 8;
390 if (n & 0x0F0F0F0F) c -= 4;
391 if (n & 0x33333333) c -= 2;
392 if (n & 0x55555555) c -= 1;
397#if defined(_MSC_VER) && !defined(__clang__)
398 unsigned long result = 0;
399 _BitScanForward(&result, n);
400 return (uint32_t)result;
401#elif defined(__GNUC__) || defined(__clang__)
402 static_assert(
sizeof(int) ==
sizeof(n),
403 "__builtin_ctz does not take 32-bit arg");
404 return __builtin_ctz(n);
410#undef PHMAP_BASE_INTERNAL_FORCEINLINE
425#if defined(__clang__) || \
426 (defined(__GNUC__) && \
427 ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5))
429 inline uint64_t
gbswap_64(uint64_t host_int) {
430 return __builtin_bswap64(host_int);
432 inline uint32_t
gbswap_32(uint32_t host_int) {
433 return __builtin_bswap32(host_int);
435 inline uint16_t
gbswap_16(uint16_t host_int) {
436 return __builtin_bswap16(host_int);
439#elif defined(_MSC_VER)
441 inline uint64_t
gbswap_64(uint64_t host_int) {
442 return _byteswap_uint64(host_int);
444 inline uint32_t
gbswap_32(uint32_t host_int) {
445 return _byteswap_ulong(host_int);
447 inline uint16_t
gbswap_16(uint16_t host_int) {
448 return _byteswap_ushort(host_int);
451#elif defined(__APPLE__)
453 inline uint64_t
gbswap_64(uint64_t host_int) {
return OSSwapInt16(host_int); }
454 inline uint32_t
gbswap_32(uint32_t host_int) {
return OSSwapInt32(host_int); }
455 inline uint16_t
gbswap_16(uint16_t host_int) {
return OSSwapInt64(host_int); }
460#if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__)
462 if (__builtin_constant_p(host_int)) {
463 return __bswap_constant_64(host_int);
466 __asm__(
"bswap %0" :
"=r"(result) :
"0"(host_int));
469#elif defined(__GLIBC__)
470 return bswap_64(host_int);
472 return (((host_int & uint64_t{0xFF}) << 56) |
473 ((host_int & uint64_t{0xFF00}) << 40) |
474 ((host_int & uint64_t{0xFF0000}) << 24) |
475 ((host_int & uint64_t{0xFF000000}) << 8) |
476 ((host_int & uint64_t{0xFF00000000}) >> 8) |
477 ((host_int & uint64_t{0xFF0000000000}) >> 24) |
478 ((host_int & uint64_t{0xFF000000000000}) >> 40) |
479 ((host_int & uint64_t{0xFF00000000000000}) >> 56));
484#if defined(__GLIBC__)
485 return bswap_32(host_int);
487 return (((host_int & uint32_t{0xFF}) << 24) |
488 ((host_int & uint32_t{0xFF00}) << 8) |
489 ((host_int & uint32_t{0xFF0000}) >> 8) |
490 ((host_int & uint32_t{0xFF000000}) >> 24));
495#if defined(__GLIBC__)
496 return bswap_16(host_int);
498 return (((host_int & uint16_t{0xFF}) << 8) |
499 ((host_int & uint16_t{0xFF00}) >> 8));
505#ifdef PHMAP_IS_LITTLE_ENDIAN
513 inline uint16_t ghtons(uint16_t x) {
return gbswap_16(x); }
514 inline uint32_t ghtonl(uint32_t x) {
return gbswap_32(x); }
515 inline uint64_t ghtonll(uint64_t x) {
return gbswap_64(x); }
517#elif defined PHMAP_IS_BIG_ENDIAN
522 inline uint16_t ghtons(uint16_t x) {
return x; }
523 inline uint32_t ghtonl(uint32_t x) {
return x; }
524 inline uint64_t ghtonll(uint64_t x) {
return x; }
528 "Unsupported byte order: Either PHMAP_IS_BIG_ENDIAN or " \
529 "PHMAP_IS_LITTLE_ENDIAN must be defined"
532inline uint16_t
gntohs(uint16_t x) {
return ghtons(x); }
533inline uint32_t
gntohl(uint32_t x) {
return ghtonl(x); }
534inline uint64_t
gntohll(uint64_t x) {
return ghtonll(x); }
540namespace little_endian {
542#ifdef PHMAP_IS_LITTLE_ENDIAN
544 inline uint16_t FromHost16(uint16_t x) {
return x; }
545 inline uint16_t ToHost16(uint16_t x) {
return x; }
547 inline uint32_t FromHost32(uint32_t x) {
return x; }
548 inline uint32_t ToHost32(uint32_t x) {
return x; }
550 inline uint64_t FromHost64(uint64_t x) {
return x; }
551 inline uint64_t ToHost64(uint64_t x) {
return x; }
553 inline constexpr bool IsLittleEndian() {
return true; }
555#elif defined PHMAP_IS_BIG_ENDIAN
557 inline uint16_t FromHost16(uint16_t x) {
return gbswap_16(x); }
558 inline uint16_t ToHost16(uint16_t x) {
return gbswap_16(x); }
560 inline uint32_t FromHost32(uint32_t x) {
return gbswap_32(x); }
561 inline uint32_t ToHost32(uint32_t x) {
return gbswap_32(x); }
563 inline uint64_t FromHost64(uint64_t x) {
return gbswap_64(x); }
564 inline uint64_t ToHost64(uint64_t x) {
return gbswap_64(x); }
566 inline constexpr bool IsLittleEndian() {
return false; }
602namespace big_endian {
603#ifdef PHMAP_IS_LITTLE_ENDIAN
605 inline uint16_t FromHost16(uint16_t x) {
return gbswap_16(x); }
606 inline uint16_t ToHost16(uint16_t x) {
return gbswap_16(x); }
608 inline uint32_t FromHost32(uint32_t x) {
return gbswap_32(x); }
609 inline uint32_t ToHost32(uint32_t x) {
return gbswap_32(x); }
611 inline uint64_t FromHost64(uint64_t x) {
return gbswap_64(x); }
612 inline uint64_t ToHost64(uint64_t x) {
return gbswap_64(x); }
614 inline constexpr bool IsLittleEndian() {
return true; }
616#elif defined PHMAP_IS_BIG_ENDIAN
618 inline uint16_t FromHost16(uint16_t x) {
return x; }
619 inline uint16_t ToHost16(uint16_t x) {
return x; }
621 inline uint32_t FromHost32(uint32_t x) {
return x; }
622 inline uint32_t ToHost32(uint32_t x) {
return x; }
624 inline uint64_t FromHost64(uint64_t x) {
return x; }
625 inline uint64_t ToHost64(uint64_t x) {
return x; }
627 inline constexpr bool IsLittleEndian() {
return false; }
PHMAP_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64Slow(uint64_t n)
Definition phmap_bits.h:273
PHMAP_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64(uint64_t n)
Definition phmap_bits.h:282
PHMAP_BASE_INTERNAL_FORCEINLINE uint32_t CountTrailingZerosNonZero32Slow(uint32_t n)
Definition phmap_bits.h:385
PHMAP_BASE_INTERNAL_FORCEINLINE uint32_t CountTrailingZerosNonZero64(uint64_t n)
Definition phmap_bits.h:363
PHMAP_BASE_INTERNAL_FORCEINLINE uint32_t CountTrailingZerosNonZero32(uint32_t n)
Definition phmap_bits.h:396
PHMAP_BASE_INTERNAL_FORCEINLINE uint32_t CountTrailingZerosNonZero64Slow(uint64_t n)
Definition phmap_bits.h:351
PHMAP_BASE_INTERNAL_FORCEINLINE uint32_t CountLeadingZeros32(uint32_t n)
Definition phmap_bits.h:326
PHMAP_BASE_INTERNAL_FORCEINLINE uint32_t CountLeadingZeros32Slow(uint64_t n)
Definition phmap_bits.h:318
void Store16(void *p, uint16_t v)
Definition phmap_bits.h:636
uint32_t Load32(const void *p)
Definition phmap_bits.h:640
uint64_t Load64(const void *p)
Definition phmap_bits.h:648
uint16_t Load16(const void *p)
Definition phmap_bits.h:632
void Store32(void *p, uint32_t v)
Definition phmap_bits.h:644
void Store64(void *p, uint64_t v)
Definition phmap_bits.h:652
void UnalignedStore32(void *p, uint32_t v)
Definition phmap_bits.h:142
void UnalignedStore16(void *p, uint16_t v)
Definition phmap_bits.h:140
uint16_t UnalignedLoad16(const void *p)
Definition phmap_bits.h:122
uint32_t UnalignedLoad32(const void *p)
Definition phmap_bits.h:128
uint64_t UnalignedLoad64(const void *p)
Definition phmap_bits.h:134
void UnalignedStore64(void *p, uint64_t v)
Definition phmap_bits.h:144
uint32_t Load32(const void *p)
Definition phmap_bits.h:580
uint64_t Load64(const void *p)
Definition phmap_bits.h:588
void Store16(void *p, uint16_t v)
Definition phmap_bits.h:576
void Store64(void *p, uint64_t v)
Definition phmap_bits.h:592
void Store32(void *p, uint32_t v)
Definition phmap_bits.h:584
uint16_t Load16(const void *p)
Definition phmap_bits.h:572
uint32_t gntohl(uint32_t x)
Definition phmap_bits.h:533
uint16_t gntohs(uint16_t x)
Definition phmap_bits.h:532
uint64_t gbswap_64(uint64_t host_int)
Definition phmap_bits.h:459
uint16_t gbswap_16(uint16_t host_int)
Definition phmap_bits.h:494
uint32_t gbswap_32(uint32_t host_int)
Definition phmap_bits.h:483
uint64_t gntohll(uint64_t x)
Definition phmap_bits.h:534
#define PHMAP_INTERNAL_UNALIGNED_LOAD64(_p)
Definition phmap_bits.h:151
#define PHMAP_INTERNAL_UNALIGNED_STORE16(_p, _val)
Definition phmap_bits.h:153
#define PHMAP_INTERNAL_UNALIGNED_LOAD16(_p)
Definition phmap_bits.h:149
#define PHMAP_INTERNAL_UNALIGNED_LOAD32(_p)
Definition phmap_bits.h:150
#define PHMAP_INTERNAL_UNALIGNED_STORE64(_p, _val)
Definition phmap_bits.h:155
#define PHMAP_INTERNAL_UNALIGNED_STORE32(_p, _val)
Definition phmap_bits.h:154
#define PHMAP_BASE_INTERNAL_FORCEINLINE
Definition phmap_bits.h:266