You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

endian.h 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include <cstdint>
  18. #include <cstdlib>
  19. #include "absl/base/casts.h"
  20. #include "absl/base/config.h"
  21. #include "absl/base/internal/unaligned_access.h"
  22. #include "absl/base/port.h"
  23. namespace absl {
  24. ABSL_NAMESPACE_BEGIN
  25. inline uint64_t gbswap_64(uint64_t host_int) {
  26. #if ABSL_HAVE_BUILTIN(__builtin_bswap64) || defined(__GNUC__)
  27. return __builtin_bswap64(host_int);
  28. #elif defined(_MSC_VER)
  29. return _byteswap_uint64(host_int);
  30. #else
  31. return (((host_int & uint64_t{0xFF}) << 56) |
  32. ((host_int & uint64_t{0xFF00}) << 40) |
  33. ((host_int & uint64_t{0xFF0000}) << 24) |
  34. ((host_int & uint64_t{0xFF000000}) << 8) |
  35. ((host_int & uint64_t{0xFF00000000}) >> 8) |
  36. ((host_int & uint64_t{0xFF0000000000}) >> 24) |
  37. ((host_int & uint64_t{0xFF000000000000}) >> 40) |
  38. ((host_int & uint64_t{0xFF00000000000000}) >> 56));
  39. #endif
  40. }
  41. inline uint32_t gbswap_32(uint32_t host_int) {
  42. #if ABSL_HAVE_BUILTIN(__builtin_bswap32) || defined(__GNUC__)
  43. return __builtin_bswap32(host_int);
  44. #elif defined(_MSC_VER)
  45. return _byteswap_ulong(host_int);
  46. #else
  47. return (((host_int & uint32_t{0xFF}) << 24) |
  48. ((host_int & uint32_t{0xFF00}) << 8) |
  49. ((host_int & uint32_t{0xFF0000}) >> 8) |
  50. ((host_int & uint32_t{0xFF000000}) >> 24));
  51. #endif
  52. }
  53. inline uint16_t gbswap_16(uint16_t host_int) {
  54. #if ABSL_HAVE_BUILTIN(__builtin_bswap16) || defined(__GNUC__)
  55. return __builtin_bswap16(host_int);
  56. #elif defined(_MSC_VER)
  57. return _byteswap_ushort(host_int);
  58. #else
  59. return (((host_int & uint16_t{0xFF}) << 8) |
  60. ((host_int & uint16_t{0xFF00}) >> 8));
  61. #endif
  62. }
  63. #ifdef ABSL_IS_LITTLE_ENDIAN
  64. // Portable definitions for htonl (host-to-network) and friends on little-endian
  65. // architectures.
  66. inline uint16_t ghtons(uint16_t x) { return gbswap_16(x); }
  67. inline uint32_t ghtonl(uint32_t x) { return gbswap_32(x); }
  68. inline uint64_t ghtonll(uint64_t x) { return gbswap_64(x); }
  69. #elif defined ABSL_IS_BIG_ENDIAN
  70. // Portable definitions for htonl (host-to-network) etc on big-endian
  71. // architectures. These definitions are simpler since the host byte order is the
  72. // same as network byte order.
  73. inline uint16_t ghtons(uint16_t x) { return x; }
  74. inline uint32_t ghtonl(uint32_t x) { return x; }
  75. inline uint64_t ghtonll(uint64_t x) { return x; }
  76. #else
  77. #error \
  78. "Unsupported byte order: Either ABSL_IS_BIG_ENDIAN or " \
  79. "ABSL_IS_LITTLE_ENDIAN must be defined"
  80. #endif // byte order
  81. inline uint16_t gntohs(uint16_t x) { return ghtons(x); }
  82. inline uint32_t gntohl(uint32_t x) { return ghtonl(x); }
  83. inline uint64_t gntohll(uint64_t x) { return ghtonll(x); }
  84. // Utilities to convert numbers between the current hosts's native byte
  85. // order and little-endian byte order
  86. //
  87. // Load/Store methods are alignment safe
  88. namespace little_endian {
  89. // Conversion functions.
  90. #ifdef ABSL_IS_LITTLE_ENDIAN
  91. inline uint16_t FromHost16(uint16_t x) { return x; }
  92. inline uint16_t ToHost16(uint16_t x) { return x; }
  93. inline uint32_t FromHost32(uint32_t x) { return x; }
  94. inline uint32_t ToHost32(uint32_t x) { return x; }
  95. inline uint64_t FromHost64(uint64_t x) { return x; }
  96. inline uint64_t ToHost64(uint64_t x) { return x; }
  97. inline constexpr bool IsLittleEndian() { return true; }
  98. #elif defined ABSL_IS_BIG_ENDIAN
  99. inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
  100. inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
  101. inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
  102. inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
  103. inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
  104. inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
  105. inline constexpr bool IsLittleEndian() { return false; }
  106. #endif /* ENDIAN */
  107. inline uint8_t FromHost(uint8_t x) { return x; }
  108. inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
  109. inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
  110. inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
  111. inline uint8_t ToHost(uint8_t x) { return x; }
  112. inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
  113. inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
  114. inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
  115. inline int8_t FromHost(int8_t x) { return x; }
  116. inline int16_t FromHost(int16_t x) {
  117. return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
  118. }
  119. inline int32_t FromHost(int32_t x) {
  120. return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
  121. }
  122. inline int64_t FromHost(int64_t x) {
  123. return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
  124. }
  125. inline int8_t ToHost(int8_t x) { return x; }
  126. inline int16_t ToHost(int16_t x) {
  127. return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
  128. }
  129. inline int32_t ToHost(int32_t x) {
  130. return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
  131. }
  132. inline int64_t ToHost(int64_t x) {
  133. return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
  134. }
  135. // Functions to do unaligned loads and stores in little-endian order.
  136. inline uint16_t Load16(const void *p) {
  137. return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
  138. }
  139. inline void Store16(void *p, uint16_t v) {
  140. ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
  141. }
  142. inline uint32_t Load32(const void *p) {
  143. return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
  144. }
  145. inline void Store32(void *p, uint32_t v) {
  146. ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
  147. }
  148. inline uint64_t Load64(const void *p) {
  149. return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
  150. }
  151. inline void Store64(void *p, uint64_t v) {
  152. ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
  153. }
  154. } // namespace little_endian
  155. // Utilities to convert numbers between the current hosts's native byte
  156. // order and big-endian byte order (same as network byte order)
  157. //
  158. // Load/Store methods are alignment safe
  159. namespace big_endian {
  160. #ifdef ABSL_IS_LITTLE_ENDIAN
  161. inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
  162. inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
  163. inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
  164. inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
  165. inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
  166. inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
  167. inline constexpr bool IsLittleEndian() { return true; }
  168. #elif defined ABSL_IS_BIG_ENDIAN
  169. inline uint16_t FromHost16(uint16_t x) { return x; }
  170. inline uint16_t ToHost16(uint16_t x) { return x; }
  171. inline uint32_t FromHost32(uint32_t x) { return x; }
  172. inline uint32_t ToHost32(uint32_t x) { return x; }
  173. inline uint64_t FromHost64(uint64_t x) { return x; }
  174. inline uint64_t ToHost64(uint64_t x) { return x; }
  175. inline constexpr bool IsLittleEndian() { return false; }
  176. #endif /* ENDIAN */
  177. inline uint8_t FromHost(uint8_t x) { return x; }
  178. inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
  179. inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
  180. inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
  181. inline uint8_t ToHost(uint8_t x) { return x; }
  182. inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
  183. inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
  184. inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
  185. inline int8_t FromHost(int8_t x) { return x; }
  186. inline int16_t FromHost(int16_t x) {
  187. return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
  188. }
  189. inline int32_t FromHost(int32_t x) {
  190. return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
  191. }
  192. inline int64_t FromHost(int64_t x) {
  193. return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
  194. }
  195. inline int8_t ToHost(int8_t x) { return x; }
  196. inline int16_t ToHost(int16_t x) {
  197. return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
  198. }
  199. inline int32_t ToHost(int32_t x) {
  200. return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
  201. }
  202. inline int64_t ToHost(int64_t x) {
  203. return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
  204. }
  205. // Functions to do unaligned loads and stores in big-endian order.
  206. inline uint16_t Load16(const void *p) {
  207. return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
  208. }
  209. inline void Store16(void *p, uint16_t v) {
  210. ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
  211. }
  212. inline uint32_t Load32(const void *p) {
  213. return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
  214. }
  215. inline void Store32(void *p, uint32_t v) {
  216. ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
  217. }
  218. inline uint64_t Load64(const void *p) {
  219. return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
  220. }
  221. inline void Store64(void *p, uint64_t v) {
  222. ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
  223. }
  224. } // namespace big_endian
  225. ABSL_NAMESPACE_END
  226. } // namespace absl
  227. #endif // ABSL_BASE_INTERNAL_ENDIAN_H_