16 #ifndef ABSL_BASE_INTERNAL_ENDIAN_H_ 17 #define ABSL_BASE_INTERNAL_ENDIAN_H_ 22 #elif defined(__APPLE__) 24 #include <libkern/OSByteOrder.h> 25 #elif defined(__FreeBSD__) 26 #include <sys/endian.h> 27 #elif defined(__GLIBC__) 42 #if defined(__clang__) || \ 43 (defined(__GNUC__) && \ 44 ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5)) 45 inline uint64_t
gbswap_64(uint64_t host_int) {
46 return __builtin_bswap64(host_int);
48 inline uint32_t
gbswap_32(uint32_t host_int) {
49 return __builtin_bswap32(host_int);
51 inline uint16_t
gbswap_16(uint16_t host_int) {
52 return __builtin_bswap16(host_int);
55 #elif defined(_MSC_VER) 56 inline uint64_t
gbswap_64(uint64_t host_int) {
57 return _byteswap_uint64(host_int);
59 inline uint32_t
gbswap_32(uint32_t host_int) {
60 return _byteswap_ulong(host_int);
62 inline uint16_t
gbswap_16(uint16_t host_int) {
63 return _byteswap_ushort(host_int);
66 #elif defined(__APPLE__) 67 inline uint64_t
gbswap_64(uint64_t host_int) {
return OSSwapInt16(host_int); }
68 inline uint32_t
gbswap_32(uint32_t host_int) {
return OSSwapInt32(host_int); }
69 inline uint16_t
gbswap_16(uint16_t host_int) {
return OSSwapInt64(host_int); }
73 #if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__) 75 if (__builtin_constant_p(host_int)) {
76 return __bswap_constant_64(host_int);
79 __asm__(
"bswap %0" :
"=r"(result) :
"0"(host_int));
82 #elif defined(__GLIBC__) 83 return bswap_64(host_int);
85 return (((host_int & uint64_t{0xFF}) << 56) |
86 ((host_int & uint64_t{0xFF00}) << 40) |
87 ((host_int & uint64_t{0xFF0000}) << 24) |
88 ((host_int & uint64_t{0xFF000000}) << 8) |
89 ((host_int & uint64_t{0xFF00000000}) >> 8) |
90 ((host_int & uint64_t{0xFF0000000000}) >> 24) |
91 ((host_int & uint64_t{0xFF000000000000}) >> 40) |
92 ((host_int & uint64_t{0xFF00000000000000}) >> 56));
97 #if defined(__GLIBC__) 98 return bswap_32(host_int);
100 return (((host_int & uint32_t{0xFF}) << 24) |
101 ((host_int & uint32_t{0xFF00}) << 8) |
102 ((host_int & uint32_t{0xFF0000}) >> 8) |
103 ((host_int & uint32_t{0xFF000000}) >> 24));
108 #if defined(__GLIBC__) 109 return bswap_16(host_int);
111 return (((host_int & uint16_t{0xFF}) << 8) |
112 ((host_int & uint16_t{0xFF00}) >> 8));
116 #endif // intrinics available 118 #ifdef ABSL_IS_LITTLE_ENDIAN 126 inline uint16_t ghtons(uint16_t x) {
return gbswap_16(x); }
127 inline uint32_t ghtonl(uint32_t x) {
return gbswap_32(x); }
128 inline uint64_t ghtonll(uint64_t x) {
return gbswap_64(x); }
130 #elif defined ABSL_IS_BIG_ENDIAN 135 inline uint16_t ghtons(uint16_t x) {
return x; }
136 inline uint32_t ghtonl(uint32_t x) {
return x; }
137 inline uint64_t ghtonll(uint64_t x) {
return x; }
141 "Unsupported byte order: Either ABSL_IS_BIG_ENDIAN or " \ 142 "ABSL_IS_LITTLE_ENDIAN must be defined" 145 inline uint16_t
gntohs(uint16_t x) {
return ghtons(x); }
146 inline uint32_t
gntohl(uint32_t x) {
return ghtonl(x); }
147 inline uint64_t
gntohll(uint64_t x) {
return ghtonll(x); }
153 namespace little_endian {
155 #ifdef ABSL_IS_LITTLE_ENDIAN 157 inline uint16_t FromHost16(uint16_t x) {
return x; }
158 inline uint16_t ToHost16(uint16_t x) {
return x; }
160 inline uint32_t FromHost32(uint32_t x) {
return x; }
161 inline uint32_t ToHost32(uint32_t x) {
return x; }
163 inline uint64_t FromHost64(uint64_t x) {
return x; }
164 inline uint64_t ToHost64(uint64_t x) {
return x; }
166 inline constexpr
bool IsLittleEndian() {
return true; }
168 #elif defined ABSL_IS_BIG_ENDIAN 170 inline uint16_t FromHost16(uint16_t x) {
return gbswap_16(x); }
171 inline uint16_t ToHost16(uint16_t x) {
return gbswap_16(x); }
173 inline uint32_t FromHost32(uint32_t x) {
return gbswap_32(x); }
174 inline uint32_t ToHost32(uint32_t x) {
return gbswap_32(x); }
176 inline uint64_t FromHost64(uint64_t x) {
return gbswap_64(x); }
177 inline uint64_t ToHost64(uint64_t x) {
return gbswap_64(x); }
179 inline constexpr
bool IsLittleEndian() {
return false; }
185 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
189 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
193 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
197 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
201 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
205 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
214 namespace big_endian {
215 #ifdef ABSL_IS_LITTLE_ENDIAN 217 inline uint16_t FromHost16(uint16_t x) {
return gbswap_16(x); }
218 inline uint16_t ToHost16(uint16_t x) {
return gbswap_16(x); }
220 inline uint32_t FromHost32(uint32_t x) {
return gbswap_32(x); }
221 inline uint32_t ToHost32(uint32_t x) {
return gbswap_32(x); }
223 inline uint64_t FromHost64(uint64_t x) {
return gbswap_64(x); }
224 inline uint64_t ToHost64(uint64_t x) {
return gbswap_64(x); }
226 inline constexpr
bool IsLittleEndian() {
return true; }
228 #elif defined ABSL_IS_BIG_ENDIAN 230 inline uint16_t FromHost16(uint16_t x) {
return x; }
231 inline uint16_t ToHost16(uint16_t x) {
return x; }
233 inline uint32_t FromHost32(uint32_t x) {
return x; }
234 inline uint32_t ToHost32(uint32_t x) {
return x; }
236 inline uint64_t FromHost64(uint64_t x) {
return x; }
237 inline uint64_t ToHost64(uint64_t x) {
return x; }
239 inline constexpr
bool IsLittleEndian() {
return false; }
245 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
249 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
253 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
257 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
261 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
265 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
272 #endif // ABSL_BASE_INTERNAL_ENDIAN_H_
void Store32(void *p, uint32_t v)
uint64_t Load64(const void *p)
uint64_t gntohll(uint64_t x)
uint64_t gbswap_64(uint64_t host_int)
uint32_t Load32(const void *p)
void Store64(void *p, uint64_t v)
uint16_t gntohs(uint16_t x)
uint32_t gntohl(uint32_t x)
void Store16(void *p, uint16_t v)
uint32_t gbswap_32(uint32_t host_int)
uint16_t gbswap_16(uint16_t host_int)
uint16_t Load16(const void *p)