16 #ifndef ABSL_BASE_INTERNAL_ENDIAN_H_
17 #define ABSL_BASE_INTERNAL_ENDIAN_H_
22 #include "absl/base/casts.h"
23 #include "absl/base/config.h"
24 #include "absl/base/internal/unaligned_access.h"
25 #include "absl/base/port.h"
31 #if ABSL_HAVE_BUILTIN(__builtin_bswap64) || defined(__GNUC__)
32 return __builtin_bswap64(host_int);
33 #elif defined(_MSC_VER)
34 return _byteswap_uint64(host_int);
36 return (((host_int &
uint64_t{0xFF}) << 56) |
37 ((host_int &
uint64_t{0xFF00}) << 40) |
38 ((host_int &
uint64_t{0xFF0000}) << 24) |
39 ((host_int &
uint64_t{0xFF000000}) << 8) |
40 ((host_int &
uint64_t{0xFF00000000}) >> 8) |
41 ((host_int &
uint64_t{0xFF0000000000}) >> 24) |
42 ((host_int &
uint64_t{0xFF000000000000}) >> 40) |
43 ((host_int &
uint64_t{0xFF00000000000000}) >> 56));
48 #if ABSL_HAVE_BUILTIN(__builtin_bswap32) || defined(__GNUC__)
49 return __builtin_bswap32(host_int);
50 #elif defined(_MSC_VER)
51 return _byteswap_ulong(host_int);
53 return (((host_int &
uint32_t{0xFF}) << 24) |
54 ((host_int &
uint32_t{0xFF00}) << 8) |
55 ((host_int &
uint32_t{0xFF0000}) >> 8) |
56 ((host_int &
uint32_t{0xFF000000}) >> 24));
61 #if ABSL_HAVE_BUILTIN(__builtin_bswap16) || defined(__GNUC__)
62 return __builtin_bswap16(host_int);
63 #elif defined(_MSC_VER)
64 return _byteswap_ushort(host_int);
66 return (((host_int &
uint16_t{0xFF}) << 8) |
67 ((host_int &
uint16_t{0xFF00}) >> 8));
71 #ifdef ABSL_IS_LITTLE_ENDIAN
79 #elif defined ABSL_IS_BIG_ENDIAN
90 "Unsupported byte order: Either ABSL_IS_BIG_ENDIAN or " \
91 "ABSL_IS_LITTLE_ENDIAN must be defined"
102 namespace little_endian {
104 #ifdef ABSL_IS_LITTLE_ENDIAN
115 inline constexpr
bool IsLittleEndian() {
return true; }
117 #elif defined ABSL_IS_BIG_ENDIAN
128 inline constexpr
bool IsLittleEndian() {
return false; }
143 return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(
x)));
146 return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(
x)));
149 return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(
x)));
153 return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(
x)));
156 return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(
x)));
159 return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(
x)));
164 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
168 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(
v));
172 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
176 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(
v));
180 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
184 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(
v));
193 namespace big_endian {
194 #ifdef ABSL_IS_LITTLE_ENDIAN
205 inline constexpr
bool IsLittleEndian() {
return true; }
207 #elif defined ABSL_IS_BIG_ENDIAN
218 inline constexpr
bool IsLittleEndian() {
return false; }
233 return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(
x)));
236 return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(
x)));
239 return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(
x)));
243 return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(
x)));
246 return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(
x)));
249 return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(
x)));
254 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
258 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(
v));
262 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
266 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(
v));
270 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
274 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(
v));
282 #endif // ABSL_BASE_INTERNAL_ENDIAN_H_