endian.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2017 The Abseil Authors.
  2. //
  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
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  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.
  14. //
  15. #ifndef ABSL_BASE_INTERNAL_ENDIAN_H_
  16. #define ABSL_BASE_INTERNAL_ENDIAN_H_
  17. // The following guarantees declaration of the byte swap functions
  18. #ifdef _MSC_VER
  19. #include <stdlib.h> // NOLINT(build/include)
  20. #elif defined(__FreeBSD__)
  21. #include <sys/endian.h>
  22. #elif defined(__GLIBC__)
  23. #include <byteswap.h> // IWYU pragma: export
  24. #endif
  25. #include <cstdint>
  26. #include "absl/base/casts.h"
  27. #include "absl/base/config.h"
  28. #include "absl/base/internal/unaligned_access.h"
  29. #include "absl/base/port.h"
  30. namespace absl {
  31. ABSL_NAMESPACE_BEGIN
  32. // Use compiler byte-swapping intrinsics if they are available. 32-bit
  33. // and 64-bit versions are available in Clang and GCC as of GCC 4.3.0.
  34. // The 16-bit version is available in Clang and GCC only as of GCC 4.8.0.
  35. // For simplicity, we enable them all only for GCC 4.8.0 or later.
  36. #if defined(__clang__) || \
  37. (defined(__GNUC__) && \
  38. ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5))
  39. inline uint64_t gbswap_64(uint64_t host_int) {
  40. return __builtin_bswap64(host_int);
  41. }
  42. inline uint32_t gbswap_32(uint32_t host_int) {
  43. return __builtin_bswap32(host_int);
  44. }
  45. inline uint16_t gbswap_16(uint16_t host_int) {
  46. return __builtin_bswap16(host_int);
  47. }
  48. #elif defined(_MSC_VER)
  49. inline uint64_t gbswap_64(uint64_t host_int) {
  50. return _byteswap_uint64(host_int);
  51. }
  52. inline uint32_t gbswap_32(uint32_t host_int) {
  53. return _byteswap_ulong(host_int);
  54. }
  55. inline uint16_t gbswap_16(uint16_t host_int) {
  56. return _byteswap_ushort(host_int);
  57. }
  58. #else
  59. inline uint64_t gbswap_64(uint64_t host_int) {
  60. #if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__)
  61. // Adapted from /usr/include/byteswap.h. Not available on Mac.
  62. if (__builtin_constant_p(host_int)) {
  63. return __bswap_constant_64(host_int);
  64. } else {
  65. uint64_t result;
  66. __asm__("bswap %0" : "=r"(result) : "0"(host_int));
  67. return result;
  68. }
  69. #elif defined(__GLIBC__)
  70. return bswap_64(host_int);
  71. #else
  72. return (((host_int & uint64_t{0xFF}) << 56) |
  73. ((host_int & uint64_t{0xFF00}) << 40) |
  74. ((host_int & uint64_t{0xFF0000}) << 24) |
  75. ((host_int & uint64_t{0xFF000000}) << 8) |
  76. ((host_int & uint64_t{0xFF00000000}) >> 8) |
  77. ((host_int & uint64_t{0xFF0000000000}) >> 24) |
  78. ((host_int & uint64_t{0xFF000000000000}) >> 40) |
  79. ((host_int & uint64_t{0xFF00000000000000}) >> 56));
  80. #endif // bswap_64
  81. }
  82. inline uint32_t gbswap_32(uint32_t host_int) {
  83. #if defined(__GLIBC__)
  84. return bswap_32(host_int);
  85. #else
  86. return (((host_int & uint32_t{0xFF}) << 24) |
  87. ((host_int & uint32_t{0xFF00}) << 8) |
  88. ((host_int & uint32_t{0xFF0000}) >> 8) |
  89. ((host_int & uint32_t{0xFF000000}) >> 24));
  90. #endif
  91. }
  92. inline uint16_t gbswap_16(uint16_t host_int) {
  93. #if defined(__GLIBC__)
  94. return bswap_16(host_int);
  95. #else
  96. return (((host_int & uint16_t{0xFF}) << 8) |
  97. ((host_int & uint16_t{0xFF00}) >> 8));
  98. #endif
  99. }
  100. #endif // intrinsics available
  101. #ifdef ABSL_IS_LITTLE_ENDIAN
  102. // Definitions for ntohl etc. that don't require us to include
  103. // netinet/in.h. We wrap gbswap_32 and gbswap_16 in functions rather
  104. // than just #defining them because in debug mode, gcc doesn't
  105. // correctly handle the (rather involved) definitions of bswap_32.
  106. // gcc guarantees that inline functions are as fast as macros, so
  107. // this isn't a performance hit.
  108. inline uint16_t ghtons(uint16_t x) { return gbswap_16(x); }
  109. inline uint32_t ghtonl(uint32_t x) { return gbswap_32(x); }
  110. inline uint64_t ghtonll(uint64_t x) { return gbswap_64(x); }
  111. #elif defined ABSL_IS_BIG_ENDIAN
  112. // These definitions are simpler on big-endian machines
  113. // These are functions instead of macros to avoid self-assignment warnings
  114. // on calls such as "i = ghtnol(i);". This also provides type checking.
  115. inline uint16_t ghtons(uint16_t x) { return x; }
  116. inline uint32_t ghtonl(uint32_t x) { return x; }
  117. inline uint64_t ghtonll(uint64_t x) { return x; }
  118. #else
  119. #error \
  120. "Unsupported byte order: Either ABSL_IS_BIG_ENDIAN or " \
  121. "ABSL_IS_LITTLE_ENDIAN must be defined"
  122. #endif // byte order
  123. inline uint16_t gntohs(uint16_t x) { return ghtons(x); }
  124. inline uint32_t gntohl(uint32_t x) { return ghtonl(x); }
  125. inline uint64_t gntohll(uint64_t x) { return ghtonll(x); }
  126. // Utilities to convert numbers between the current hosts's native byte
  127. // order and little-endian byte order
  128. //
  129. // Load/Store methods are alignment safe
  130. namespace little_endian {
  131. // Conversion functions.
  132. #ifdef ABSL_IS_LITTLE_ENDIAN
  133. inline uint16_t FromHost16(uint16_t x) { return x; }
  134. inline uint16_t ToHost16(uint16_t x) { return x; }
  135. inline uint32_t FromHost32(uint32_t x) { return x; }
  136. inline uint32_t ToHost32(uint32_t x) { return x; }
  137. inline uint64_t FromHost64(uint64_t x) { return x; }
  138. inline uint64_t ToHost64(uint64_t x) { return x; }
  139. inline constexpr bool IsLittleEndian() { return true; }
  140. #elif defined ABSL_IS_BIG_ENDIAN
  141. inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
  142. inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
  143. inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
  144. inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
  145. inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
  146. inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
  147. inline constexpr bool IsLittleEndian() { return false; }
  148. #endif /* ENDIAN */
  149. inline uint8_t FromHost(uint8_t x) { return x; }
  150. inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
  151. inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
  152. inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
  153. inline uint8_t ToHost(uint8_t x) { return x; }
  154. inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
  155. inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
  156. inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
  157. inline int8_t FromHost(int8_t x) { return x; }
  158. inline int16_t FromHost(int16_t x) {
  159. return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
  160. }
  161. inline int32_t FromHost(int32_t x) {
  162. return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
  163. }
  164. inline int64_t FromHost(int64_t x) {
  165. return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
  166. }
  167. inline int8_t ToHost(int8_t x) { return x; }
  168. inline int16_t ToHost(int16_t x) {
  169. return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
  170. }
  171. inline int32_t ToHost(int32_t x) {
  172. return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
  173. }
  174. inline int64_t ToHost(int64_t x) {
  175. return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
  176. }
  177. // Functions to do unaligned loads and stores in little-endian order.
  178. inline uint16_t Load16(const void *p) {
  179. return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
  180. }
  181. inline void Store16(void *p, uint16_t v) {
  182. ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
  183. }
  184. inline uint32_t Load32(const void *p) {
  185. return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
  186. }
  187. inline void Store32(void *p, uint32_t v) {
  188. ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
  189. }
  190. inline uint64_t Load64(const void *p) {
  191. return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
  192. }
  193. inline void Store64(void *p, uint64_t v) {
  194. ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
  195. }
  196. } // namespace little_endian
  197. // Utilities to convert numbers between the current hosts's native byte
  198. // order and big-endian byte order (same as network byte order)
  199. //
  200. // Load/Store methods are alignment safe
  201. namespace big_endian {
  202. #ifdef ABSL_IS_LITTLE_ENDIAN
  203. inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
  204. inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
  205. inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
  206. inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
  207. inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
  208. inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
  209. inline constexpr bool IsLittleEndian() { return true; }
  210. #elif defined ABSL_IS_BIG_ENDIAN
  211. inline uint16_t FromHost16(uint16_t x) { return x; }
  212. inline uint16_t ToHost16(uint16_t x) { return x; }
  213. inline uint32_t FromHost32(uint32_t x) { return x; }
  214. inline uint32_t ToHost32(uint32_t x) { return x; }
  215. inline uint64_t FromHost64(uint64_t x) { return x; }
  216. inline uint64_t ToHost64(uint64_t x) { return x; }
  217. inline constexpr bool IsLittleEndian() { return false; }
  218. #endif /* ENDIAN */
  219. inline uint8_t FromHost(uint8_t x) { return x; }
  220. inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
  221. inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
  222. inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
  223. inline uint8_t ToHost(uint8_t x) { return x; }
  224. inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
  225. inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
  226. inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
  227. inline int8_t FromHost(int8_t x) { return x; }
  228. inline int16_t FromHost(int16_t x) {
  229. return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
  230. }
  231. inline int32_t FromHost(int32_t x) {
  232. return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
  233. }
  234. inline int64_t FromHost(int64_t x) {
  235. return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
  236. }
  237. inline int8_t ToHost(int8_t x) { return x; }
  238. inline int16_t ToHost(int16_t x) {
  239. return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
  240. }
  241. inline int32_t ToHost(int32_t x) {
  242. return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
  243. }
  244. inline int64_t ToHost(int64_t x) {
  245. return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
  246. }
  247. // Functions to do unaligned loads and stores in big-endian order.
  248. inline uint16_t Load16(const void *p) {
  249. return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
  250. }
  251. inline void Store16(void *p, uint16_t v) {
  252. ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
  253. }
  254. inline uint32_t Load32(const void *p) {
  255. return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
  256. }
  257. inline void Store32(void *p, uint32_t v) {
  258. ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
  259. }
  260. inline uint64_t Load64(const void *p) {
  261. return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
  262. }
  263. inline void Store64(void *p, uint64_t v) {
  264. ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
  265. }
  266. } // namespace big_endian
  267. ABSL_NAMESPACE_END
  268. } // namespace absl
  269. #endif // ABSL_BASE_INTERNAL_ENDIAN_H_