endian_reverse.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED
  2. #define BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED
  3. // Copyright 2019 Peter Dimov
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/endian/detail/integral_by_size.hpp>
  8. #include <boost/endian/detail/intrinsic.hpp>
  9. #include <boost/type_traits/is_integral.hpp>
  10. #include <boost/type_traits/is_same.hpp>
  11. #include <boost/static_assert.hpp>
  12. #include <boost/cstdint.hpp>
  13. #include <boost/config.hpp>
  14. #include <cstddef>
  15. #include <cstring>
  16. #if defined(BOOST_ENDIAN_NO_INTRINSICS)
  17. # if defined(BOOST_NO_CXX14_CONSTEXPR)
  18. # define BOOST_ENDIAN_CONSTEXPR
  19. # else
  20. # define BOOST_ENDIAN_CONSTEXPR constexpr
  21. # endif
  22. #else
  23. # if defined(BOOST_ENDIAN_CONSTEXPR_INTRINSICS)
  24. # define BOOST_ENDIAN_CONSTEXPR BOOST_CONSTEXPR
  25. # else
  26. # define BOOST_ENDIAN_CONSTEXPR
  27. # endif
  28. #endif
  29. namespace boost
  30. {
  31. namespace endian
  32. {
  33. namespace detail
  34. {
  35. // -- portable approach suggested by tymofey, with avoidance of undefined behavior
  36. // as suggested by Giovanni Piero Deretta, with a further refinement suggested
  37. // by Pyry Jahkola.
  38. // -- intrinsic approach suggested by reviewers, and by David Stone, who provided
  39. // his Boost licensed macro implementation (detail/intrinsic.hpp)
  40. inline uint8_t BOOST_CONSTEXPR endian_reverse_impl( uint8_t x ) BOOST_NOEXCEPT
  41. {
  42. return x;
  43. }
  44. inline uint16_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint16_t x ) BOOST_NOEXCEPT
  45. {
  46. #ifdef BOOST_ENDIAN_NO_INTRINSICS
  47. return (x << 8) | (x >> 8);
  48. #else
  49. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x);
  50. #endif
  51. }
  52. inline uint32_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint32_t x ) BOOST_NOEXCEPT
  53. {
  54. #ifdef BOOST_ENDIAN_NO_INTRINSICS
  55. uint32_t step16 = x << 16 | x >> 16;
  56. return ((step16 << 8) & 0xff00ff00) | ((step16 >> 8) & 0x00ff00ff);
  57. #else
  58. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x);
  59. #endif
  60. }
  61. inline uint64_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint64_t x ) BOOST_NOEXCEPT
  62. {
  63. #ifdef BOOST_ENDIAN_NO_INTRINSICS
  64. uint64_t step32 = x << 32 | x >> 32;
  65. uint64_t step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  66. return (step16 & 0x00FF00FF00FF00FFULL) << 8 | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
  67. #else
  68. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x);
  69. # endif
  70. }
  71. #if defined(BOOST_HAS_INT128)
  72. inline uint128_type BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint128_type x ) BOOST_NOEXCEPT
  73. {
  74. return endian_reverse_impl( static_cast<uint64_t>( x >> 64 ) ) |
  75. static_cast<uint128_type>( endian_reverse_impl( static_cast<uint64_t>( x ) ) ) << 64;
  76. }
  77. #endif
  78. } // namespace detail
  79. // Requires:
  80. // T is non-bool integral
  81. template<class T> inline BOOST_CONSTEXPR T endian_reverse( T x ) BOOST_NOEXCEPT
  82. {
  83. BOOST_STATIC_ASSERT( is_integral<T>::value && !(is_same<T, bool>::value) );
  84. typedef typename detail::integral_by_size< sizeof(T) >::type uintN_t;
  85. return static_cast<T>( detail::endian_reverse_impl( static_cast<uintN_t>( x ) ) );
  86. }
  87. template <class EndianReversible>
  88. inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT
  89. {
  90. x = endian_reverse( x );
  91. }
  92. } // namespace endian
  93. } // namespace boost
  94. #endif // BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED