16 #ifndef ABSL_BASE_INTERNAL_ENDIAN_H_
17 #define ABSL_BASE_INTERNAL_ENDIAN_H_
22 #elif defined(__FreeBSD__)
23 #include <sys/endian.h>
24 #elif defined(__GLIBC__)
29 #include "absl/base/casts.h"
30 #include "absl/base/config.h"
31 #include "absl/base/internal/unaligned_access.h"
32 #include "absl/base/port.h"
41 #if defined(__clang__) || \
42 (defined(__GNUC__) && \
43 ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5))
45 return __builtin_bswap64(host_int);
48 return __builtin_bswap32(host_int);
51 return __builtin_bswap16(host_int);
54 #elif defined(_MSC_VER)
56 return _byteswap_uint64(host_int);
59 return _byteswap_ulong(host_int);
62 return _byteswap_ushort(host_int);
67 #if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__)
69 if (__builtin_constant_p(host_int)) {
70 return __bswap_constant_64(host_int);
73 __asm__(
"bswap %0" :
"=r"(
result) :
"0"(host_int));
76 #elif defined(__GLIBC__)
79 return (((host_int &
uint64_t{0xFF}) << 56) |
80 ((host_int &
uint64_t{0xFF00}) << 40) |
81 ((host_int &
uint64_t{0xFF0000}) << 24) |
82 ((host_int &
uint64_t{0xFF000000}) << 8) |
83 ((host_int &
uint64_t{0xFF00000000}) >> 8) |
84 ((host_int &
uint64_t{0xFF0000000000}) >> 24) |
85 ((host_int &
uint64_t{0xFF000000000000}) >> 40) |
86 ((host_int &
uint64_t{0xFF00000000000000}) >> 56));
91 #if defined(__GLIBC__)
94 return (((host_int &
uint32_t{0xFF}) << 24) |
95 ((host_int &
uint32_t{0xFF00}) << 8) |
96 ((host_int &
uint32_t{0xFF0000}) >> 8) |
97 ((host_int &
uint32_t{0xFF000000}) >> 24));
102 #if defined(__GLIBC__)
105 return (((host_int &
uint16_t{0xFF}) << 8) |
106 ((host_int &
uint16_t{0xFF00}) >> 8));
110 #endif // intrinsics available
112 #ifdef ABSL_IS_LITTLE_ENDIAN
124 #elif defined ABSL_IS_BIG_ENDIAN
135 "Unsupported byte order: Either ABSL_IS_BIG_ENDIAN or " \
136 "ABSL_IS_LITTLE_ENDIAN must be defined"
147 namespace little_endian {
149 #ifdef ABSL_IS_LITTLE_ENDIAN
160 inline constexpr
bool IsLittleEndian() {
return true; }
162 #elif defined ABSL_IS_BIG_ENDIAN
173 inline constexpr
bool IsLittleEndian() {
return false; }
188 return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
191 return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
194 return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
198 return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
201 return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
204 return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
209 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
213 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(
v));
217 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
221 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(
v));
225 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
229 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(
v));
238 namespace big_endian {
239 #ifdef ABSL_IS_LITTLE_ENDIAN
250 inline constexpr
bool IsLittleEndian() {
return true; }
252 #elif defined ABSL_IS_BIG_ENDIAN
263 inline constexpr
bool IsLittleEndian() {
return false; }
278 return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
281 return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
284 return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
288 return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
291 return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
294 return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
299 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
303 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(
v));
307 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
311 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(
v));
315 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
319 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(
v));
327 #endif // ABSL_BASE_INTERNAL_ENDIAN_H_