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 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_ENDIAN_H__
  31. #define GOOGLE_PROTOBUF_ENDIAN_H__
  32. #if defined(_MSC_VER)
  33. #include <stdlib.h>
  34. #endif
  35. #include <cstdint>
  36. // Must be included last.
  37. #include <google/protobuf/port_def.inc>
  38. namespace google
  39. {
  40. namespace protobuf
  41. {
  42. namespace internal
  43. {
  44. inline uint64_t BSwap64(uint64_t host_int)
  45. {
  46. #if defined(PROTOBUF_BUILTIN_BSWAP64)
  47. return PROTOBUF_BUILTIN_BSWAP64(host_int);
  48. #elif defined(_MSC_VER)
  49. return _byteswap_uint64(host_int);
  50. #else
  51. return (((host_int & uint64_t{0xFF}) << 56) | ((host_int & uint64_t{0xFF00}) << 40) | ((host_int & uint64_t{0xFF0000}) << 24) | ((host_int & uint64_t{0xFF000000}) << 8) | ((host_int & uint64_t{0xFF00000000}) >> 8) | ((host_int & uint64_t{0xFF0000000000}) >> 24) | ((host_int & uint64_t{0xFF000000000000}) >> 40) | ((host_int & uint64_t{0xFF00000000000000}) >> 56));
  52. #endif
  53. }
  54. inline uint32_t BSwap32(uint32_t host_int)
  55. {
  56. #if defined(PROTOBUF_BUILTIN_BSWAP32)
  57. return PROTOBUF_BUILTIN_BSWAP32(host_int);
  58. #elif defined(_MSC_VER)
  59. return _byteswap_ulong(host_int);
  60. #else
  61. return (((host_int & uint32_t{0xFF}) << 24) | ((host_int & uint32_t{0xFF00}) << 8) | ((host_int & uint32_t{0xFF0000}) >> 8) | ((host_int & uint32_t{0xFF000000}) >> 24));
  62. #endif
  63. }
  64. inline uint16_t BSwap16(uint16_t host_int)
  65. {
  66. #if defined(PROTOBUF_BUILTIN_BSWAP16)
  67. return PROTOBUF_BUILTIN_BSWAP16(host_int);
  68. #elif defined(_MSC_VER)
  69. return _byteswap_ushort(host_int);
  70. #else
  71. return (((host_int & uint16_t{0xFF}) << 8) | ((host_int & uint16_t{0xFF00}) >> 8));
  72. #endif
  73. }
  74. namespace little_endian
  75. {
  76. inline uint16_t FromHost(uint16_t value)
  77. {
  78. #if defined(PROTOBUF_BIG_ENDIAN)
  79. return BSwap16(value);
  80. #else
  81. return value;
  82. #endif
  83. }
  84. inline uint32_t FromHost(uint32_t value)
  85. {
  86. #if defined(PROTOBUF_BIG_ENDIAN)
  87. return BSwap32(value);
  88. #else
  89. return value;
  90. #endif
  91. }
  92. inline uint64_t FromHost(uint64_t value)
  93. {
  94. #if defined(PROTOBUF_BIG_ENDIAN)
  95. return BSwap64(value);
  96. #else
  97. return value;
  98. #endif
  99. }
  100. inline uint16_t ToHost(uint16_t value)
  101. {
  102. #if defined(PROTOBUF_BIG_ENDIAN)
  103. return BSwap16(value);
  104. #else
  105. return value;
  106. #endif
  107. }
  108. inline uint32_t ToHost(uint32_t value)
  109. {
  110. #if defined(PROTOBUF_BIG_ENDIAN)
  111. return BSwap32(value);
  112. #else
  113. return value;
  114. #endif
  115. }
  116. inline uint64_t ToHost(uint64_t value)
  117. {
  118. #if defined(PROTOBUF_BIG_ENDIAN)
  119. return BSwap64(value);
  120. #else
  121. return value;
  122. #endif
  123. }
  124. } // namespace little_endian
  125. namespace big_endian
  126. {
  127. inline uint16_t FromHost(uint16_t value)
  128. {
  129. #if defined(PROTOBUF_BIG_ENDIAN)
  130. return value;
  131. #else
  132. return BSwap16(value);
  133. #endif
  134. }
  135. inline uint32_t FromHost(uint32_t value)
  136. {
  137. #if defined(PROTOBUF_BIG_ENDIAN)
  138. return value;
  139. #else
  140. return BSwap32(value);
  141. #endif
  142. }
  143. inline uint64_t FromHost(uint64_t value)
  144. {
  145. #if defined(PROTOBUF_BIG_ENDIAN)
  146. return value;
  147. #else
  148. return BSwap64(value);
  149. #endif
  150. }
  151. inline uint16_t ToHost(uint16_t value)
  152. {
  153. #if defined(PROTOBUF_BIG_ENDIAN)
  154. return value;
  155. #else
  156. return BSwap16(value);
  157. #endif
  158. }
  159. inline uint32_t ToHost(uint32_t value)
  160. {
  161. #if defined(PROTOBUF_BIG_ENDIAN)
  162. return value;
  163. #else
  164. return BSwap32(value);
  165. #endif
  166. }
  167. inline uint64_t ToHost(uint64_t value)
  168. {
  169. #if defined(PROTOBUF_BIG_ENDIAN)
  170. return value;
  171. #else
  172. return BSwap64(value);
  173. #endif
  174. }
  175. } // namespace big_endian
  176. } // namespace internal
  177. } // namespace protobuf
  178. } // namespace google
  179. #include <google/protobuf/port_undef.inc>
  180. #endif // GOOGLE_PROTOBUF_ENDIAN_H__