endian_reverse.hpp 3.4 KB

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