endian.h 8.2 KB

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