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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. namespace protobuf {
  40. namespace internal {
  41. inline uint64_t BSwap64(uint64_t host_int) {
  42. #if defined(PROTOBUF_BUILTIN_BSWAP64)
  43. return PROTOBUF_BUILTIN_BSWAP64(host_int);
  44. #elif defined(_MSC_VER)
  45. return _byteswap_uint64(host_int);
  46. #else
  47. return (((host_int & uint64_t{0xFF}) << 56) |
  48. ((host_int & uint64_t{0xFF00}) << 40) |
  49. ((host_int & uint64_t{0xFF0000}) << 24) |
  50. ((host_int & uint64_t{0xFF000000}) << 8) |
  51. ((host_int & uint64_t{0xFF00000000}) >> 8) |
  52. ((host_int & uint64_t{0xFF0000000000}) >> 24) |
  53. ((host_int & uint64_t{0xFF000000000000}) >> 40) |
  54. ((host_int & uint64_t{0xFF00000000000000}) >> 56));
  55. #endif
  56. }
  57. inline uint32_t BSwap32(uint32_t host_int) {
  58. #if defined(PROTOBUF_BUILTIN_BSWAP32)
  59. return PROTOBUF_BUILTIN_BSWAP32(host_int);
  60. #elif defined(_MSC_VER)
  61. return _byteswap_ulong(host_int);
  62. #else
  63. return (((host_int & uint32_t{0xFF}) << 24) |
  64. ((host_int & uint32_t{0xFF00}) << 8) |
  65. ((host_int & uint32_t{0xFF0000}) >> 8) |
  66. ((host_int & uint32_t{0xFF000000}) >> 24));
  67. #endif
  68. }
  69. inline uint16_t BSwap16(uint16_t host_int) {
  70. #if defined(PROTOBUF_BUILTIN_BSWAP16)
  71. return PROTOBUF_BUILTIN_BSWAP16(host_int);
  72. #elif defined(_MSC_VER)
  73. return _byteswap_ushort(host_int);
  74. #else
  75. return (((host_int & uint16_t{0xFF}) << 8) |
  76. ((host_int & uint16_t{0xFF00}) >> 8));
  77. #endif
  78. }
  79. namespace little_endian {
  80. inline uint16_t FromHost(uint16_t value) {
  81. #if defined(PROTOBUF_BIG_ENDIAN)
  82. return BSwap16(value);
  83. #else
  84. return value;
  85. #endif
  86. }
  87. inline uint32_t FromHost(uint32_t value) {
  88. #if defined(PROTOBUF_BIG_ENDIAN)
  89. return BSwap32(value);
  90. #else
  91. return value;
  92. #endif
  93. }
  94. inline uint64_t FromHost(uint64_t value) {
  95. #if defined(PROTOBUF_BIG_ENDIAN)
  96. return BSwap64(value);
  97. #else
  98. return value;
  99. #endif
  100. }
  101. inline uint16_t ToHost(uint16_t value) {
  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. #if defined(PROTOBUF_BIG_ENDIAN)
  110. return BSwap32(value);
  111. #else
  112. return value;
  113. #endif
  114. }
  115. inline uint64_t ToHost(uint64_t value) {
  116. #if defined(PROTOBUF_BIG_ENDIAN)
  117. return BSwap64(value);
  118. #else
  119. return value;
  120. #endif
  121. }
  122. } // namespace little_endian
  123. namespace big_endian {
  124. inline uint16_t FromHost(uint16_t value) {
  125. #if defined(PROTOBUF_BIG_ENDIAN)
  126. return value;
  127. #else
  128. return BSwap16(value);
  129. #endif
  130. }
  131. inline uint32_t FromHost(uint32_t value) {
  132. #if defined(PROTOBUF_BIG_ENDIAN)
  133. return value;
  134. #else
  135. return BSwap32(value);
  136. #endif
  137. }
  138. inline uint64_t FromHost(uint64_t value) {
  139. #if defined(PROTOBUF_BIG_ENDIAN)
  140. return value;
  141. #else
  142. return BSwap64(value);
  143. #endif
  144. }
  145. inline uint16_t ToHost(uint16_t value) {
  146. #if defined(PROTOBUF_BIG_ENDIAN)
  147. return value;
  148. #else
  149. return BSwap16(value);
  150. #endif
  151. }
  152. inline uint32_t ToHost(uint32_t value) {
  153. #if defined(PROTOBUF_BIG_ENDIAN)
  154. return value;
  155. #else
  156. return BSwap32(value);
  157. #endif
  158. }
  159. inline uint64_t ToHost(uint64_t value) {
  160. #if defined(PROTOBUF_BIG_ENDIAN)
  161. return value;
  162. #else
  163. return BSwap64(value);
  164. #endif
  165. }
  166. } // namespace big_endian
  167. } // namespace internal
  168. } // namespace protobuf
  169. } // namespace google
  170. #include <google/protobuf/port_undef.inc>
  171. #endif // GOOGLE_PROTOBUF_ENDIAN_H__