1 // Copyright 2017 The Abseil Authors.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 #ifndef ABSL_BASE_INTERNAL_ENDIAN_H_
17 #define ABSL_BASE_INTERNAL_ENDIAN_H_
19 // The following guarantees declaration of the byte swap functions
21 #include <stdlib.h> // NOLINT(build/include)
22 #elif defined(__APPLE__)
23 // Mac OS X / Darwin features
24 #include <libkern/OSByteOrder.h>
25 #elif defined(__FreeBSD__)
26 #include <sys/endian.h>
27 #elif defined(__GLIBC__)
28 #include <byteswap.h> // IWYU pragma: export
32 #include "absl/base/config.h"
33 #include "absl/base/internal/unaligned_access.h"
34 #include "absl/base/port.h"
38 // Use compiler byte-swapping intrinsics if they are available. 32-bit
39 // and 64-bit versions are available in Clang and GCC as of GCC 4.3.0.
40 // The 16-bit version is available in Clang and GCC only as of GCC 4.8.0.
41 // For simplicity, we enable them all only for GCC 4.8.0 or later.
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); }
72 inline uint64_t gbswap_64(uint64_t host_int) {
73 #if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__)
74 // Adapted from /usr/include/byteswap.h. Not available on Mac.
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));
96 inline uint32_t gbswap_32(uint32_t host_int) {
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));
107 inline uint16_t gbswap_16(uint16_t host_int) {
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
120 // Definitions for ntohl etc. that don't require us to include
121 // netinet/in.h. We wrap gbswap_32 and gbswap_16 in functions rather
122 // than just #defining them because in debug mode, gcc doesn't
123 // correctly handle the (rather involved) definitions of bswap_32.
124 // gcc guarantees that inline functions are as fast as macros, so
125 // this isn't a performance hit.
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
132 // These definitions are simpler on big-endian machines
133 // These are functions instead of macros to avoid self-assignment warnings
134 // on calls such as "i = ghtnol(i);". This also provides type checking.
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); }
149 // Utilities to convert numbers between the current hosts's native byte
150 // order and little-endian byte order
152 // Load/Store methods are alignment safe
153 namespace little_endian {
154 // Conversion functions.
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; }
183 // Functions to do unaligned loads and stores in little-endian order.
184 inline uint16_t Load16(const void *p) {
185 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
188 inline void Store16(void *p, uint16_t v) {
189 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
192 inline uint32_t Load32(const void *p) {
193 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
196 inline void Store32(void *p, uint32_t v) {
197 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
200 inline uint64_t Load64(const void *p) {
201 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
204 inline void Store64(void *p, uint64_t v) {
205 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
208 } // namespace little_endian
210 // Utilities to convert numbers between the current hosts's native byte
211 // order and big-endian byte order (same as network byte order)
213 // Load/Store methods are alignment safe
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; }
243 // Functions to do unaligned loads and stores in big-endian order.
244 inline uint16_t Load16(const void *p) {
245 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
248 inline void Store16(void *p, uint16_t v) {
249 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
252 inline uint32_t Load32(const void *p) {
253 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
256 inline void Store32(void *p, uint32_t v) {
257 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
260 inline uint64_t Load64(const void *p) {
261 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
264 inline void Store64(void *p, uint64_t v) {
265 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
268 } // namespace big_endian
272 #endif // ABSL_BASE_INTERNAL_ENDIAN_H_