sys_byteorder.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This header defines cross-platform ByteSwap() implementations for 16, 32 and
  5. // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to
  6. // the traditional ntohX() and htonX() functions.
  7. // Use the functions defined here rather than using the platform-specific
  8. // functions directly.
  9. #ifndef BASE_SYS_BYTEORDER_H_
  10. #define BASE_SYS_BYTEORDER_H_
  11. #include <stdint.h>
  12. #include "build/build_config.h"
  13. #if defined(COMPILER_MSVC)
  14. #include <stdlib.h>
  15. #endif
  16. namespace base {
  17. // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
  18. inline uint16_t ByteSwap(uint16_t x) {
  19. #if defined(COMPILER_MSVC)
  20. return _byteswap_ushort(x);
  21. #else
  22. return __builtin_bswap16(x);
  23. #endif
  24. }
  25. inline uint32_t ByteSwap(uint32_t x) {
  26. #if defined(COMPILER_MSVC)
  27. return _byteswap_ulong(x);
  28. #else
  29. return __builtin_bswap32(x);
  30. #endif
  31. }
  32. inline uint64_t ByteSwap(uint64_t x) {
  33. #if defined(COMPILER_MSVC)
  34. return _byteswap_uint64(x);
  35. #else
  36. return __builtin_bswap64(x);
  37. #endif
  38. }
  39. inline uintptr_t ByteSwapUintPtrT(uintptr_t x) {
  40. // We do it this way because some build configurations are ILP32 even when
  41. // defined(ARCH_CPU_64_BITS). Unfortunately, we can't use sizeof in #ifs. But,
  42. // because these conditionals are constexprs, the irrelevant branches will
  43. // likely be optimized away, so this construction should not result in code
  44. // bloat.
  45. static_assert(sizeof(uintptr_t) == 4 || sizeof(uintptr_t) == 8,
  46. "Unsupported uintptr_t size");
  47. if (sizeof(uintptr_t) == 4)
  48. return ByteSwap(static_cast<uint32_t>(x));
  49. return ByteSwap(static_cast<uint64_t>(x));
  50. }
  51. // Converts the bytes in |x| from host order (endianness) to little endian, and
  52. // returns the result.
  53. inline uint16_t ByteSwapToLE16(uint16_t x) {
  54. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  55. return x;
  56. #else
  57. return ByteSwap(x);
  58. #endif
  59. }
  60. inline uint32_t ByteSwapToLE32(uint32_t x) {
  61. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  62. return x;
  63. #else
  64. return ByteSwap(x);
  65. #endif
  66. }
  67. inline uint64_t ByteSwapToLE64(uint64_t x) {
  68. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  69. return x;
  70. #else
  71. return ByteSwap(x);
  72. #endif
  73. }
  74. // Converts the bytes in |x| from network to host order (endianness), and
  75. // returns the result.
  76. inline uint16_t NetToHost16(uint16_t x) {
  77. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  78. return ByteSwap(x);
  79. #else
  80. return x;
  81. #endif
  82. }
  83. inline uint32_t NetToHost32(uint32_t x) {
  84. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  85. return ByteSwap(x);
  86. #else
  87. return x;
  88. #endif
  89. }
  90. inline uint64_t NetToHost64(uint64_t x) {
  91. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  92. return ByteSwap(x);
  93. #else
  94. return x;
  95. #endif
  96. }
  97. // Converts the bytes in |x| from host to network order (endianness), and
  98. // returns the result.
  99. inline uint16_t HostToNet16(uint16_t x) {
  100. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  101. return ByteSwap(x);
  102. #else
  103. return x;
  104. #endif
  105. }
  106. inline uint32_t HostToNet32(uint32_t x) {
  107. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  108. return ByteSwap(x);
  109. #else
  110. return x;
  111. #endif
  112. }
  113. inline uint64_t HostToNet64(uint64_t x) {
  114. #if defined(ARCH_CPU_LITTLE_ENDIAN)
  115. return ByteSwap(x);
  116. #else
  117. return x;
  118. #endif
  119. }
  120. } // namespace base
  121. #endif // BASE_SYS_BYTEORDER_H_