endian_reverse.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED
  2. #define BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED
  3. // Copyright 2019, 2020 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #include <boost/endian/detail/integral_by_size.hpp>
  7. #include <boost/endian/detail/intrinsic.hpp>
  8. #include <boost/endian/detail/is_scoped_enum.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/type_traits/integral_constant.hpp>
  14. #include <boost/static_assert.hpp>
  15. #include <boost/cstdint.hpp>
  16. #include <boost/config.hpp>
  17. #include <cstddef>
  18. #include <cstring>
  19. #if defined(BOOST_ENDIAN_NO_INTRINSICS)
  20. # if defined(BOOST_NO_CXX14_CONSTEXPR)
  21. # define BOOST_ENDIAN_CONSTEXPR
  22. # else
  23. # define BOOST_ENDIAN_CONSTEXPR constexpr
  24. # endif
  25. #else
  26. # if defined(BOOST_ENDIAN_CONSTEXPR_INTRINSICS)
  27. # define BOOST_ENDIAN_CONSTEXPR BOOST_CONSTEXPR
  28. # else
  29. # define BOOST_ENDIAN_CONSTEXPR
  30. # endif
  31. #endif
  32. namespace boost
  33. {
  34. namespace endian
  35. {
  36. namespace detail
  37. {
  38. // -- portable approach suggested by tymofey, with avoidance of undefined behavior
  39. // as suggested by Giovanni Piero Deretta, with a further refinement suggested
  40. // by Pyry Jahkola.
  41. // -- intrinsic approach suggested by reviewers, and by David Stone, who provided
  42. // his Boost licensed macro implementation (detail/intrinsic.hpp)
  43. inline uint8_t BOOST_CONSTEXPR endian_reverse_impl( uint8_t x ) BOOST_NOEXCEPT
  44. {
  45. return x;
  46. }
  47. inline uint16_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint16_t x ) BOOST_NOEXCEPT
  48. {
  49. #ifdef BOOST_ENDIAN_NO_INTRINSICS
  50. return (x << 8) | (x >> 8);
  51. #else
  52. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x);
  53. #endif
  54. }
  55. inline uint32_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint32_t x ) BOOST_NOEXCEPT
  56. {
  57. #ifdef BOOST_ENDIAN_NO_INTRINSICS
  58. uint32_t step16 = x << 16 | x >> 16;
  59. return ((step16 << 8) & 0xff00ff00) | ((step16 >> 8) & 0x00ff00ff);
  60. #else
  61. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x);
  62. #endif
  63. }
  64. inline uint64_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint64_t x ) BOOST_NOEXCEPT
  65. {
  66. #ifdef BOOST_ENDIAN_NO_INTRINSICS
  67. uint64_t step32 = x << 32 | x >> 32;
  68. uint64_t step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  69. return (step16 & 0x00FF00FF00FF00FFULL) << 8 | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
  70. #else
  71. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x);
  72. # endif
  73. }
  74. #if defined(BOOST_HAS_INT128)
  75. inline uint128_type BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint128_type x ) BOOST_NOEXCEPT
  76. {
  77. return endian_reverse_impl( static_cast<uint64_t>( x >> 64 ) ) |
  78. static_cast<uint128_type>( endian_reverse_impl( static_cast<uint64_t>( x ) ) ) << 64;
  79. }
  80. #endif
  81. // is_endian_reversible
  82. template<class T> struct is_endian_reversible: boost::integral_constant<bool,
  83. (boost::is_integral<T>::value && !boost::is_same<T, bool>::value) || is_scoped_enum<T>::value>
  84. {
  85. };
  86. // is_endian_reversible_inplace
  87. template<class T> struct is_endian_reversible_inplace: boost::integral_constant<bool,
  88. boost::is_integral<T>::value || boost::is_enum<T>::value || boost::is_same<T, float>::value || boost::is_same<T, double>::value>
  89. {
  90. };
  91. } // namespace detail
  92. // Requires:
  93. // T is non-bool integral or scoped enumeration type
  94. template<class T> inline BOOST_CONSTEXPR
  95. typename enable_if_< !is_class<T>::value, T >::type
  96. endian_reverse( T x ) BOOST_NOEXCEPT
  97. {
  98. BOOST_STATIC_ASSERT( detail::is_endian_reversible<T>::value );
  99. typedef typename detail::integral_by_size< sizeof(T) >::type uintN_t;
  100. return static_cast<T>( detail::endian_reverse_impl( static_cast<uintN_t>( x ) ) );
  101. }
  102. // Requires:
  103. // T is integral, enumeration, float or double
  104. template<class T> inline
  105. typename enable_if_< !is_class<T>::value >::type
  106. endian_reverse_inplace( T & x ) BOOST_NOEXCEPT
  107. {
  108. BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<T>::value );
  109. typename detail::integral_by_size< sizeof(T) >::type x2;
  110. std::memcpy( &x2, &x, sizeof(T) );
  111. x2 = detail::endian_reverse_impl( x2 );
  112. std::memcpy( &x, &x2, sizeof(T) );
  113. }
  114. // Default implementation for user-defined types
  115. template<class T> inline
  116. typename enable_if_< is_class<T>::value >::type
  117. endian_reverse_inplace( T & x ) BOOST_NOEXCEPT
  118. {
  119. x = endian_reverse( x );
  120. }
  121. // endian_reverse_inplace for arrays
  122. template<class T, std::size_t N>
  123. inline void endian_reverse_inplace( T (&x)[ N ] ) BOOST_NOEXCEPT
  124. {
  125. for( std::size_t i = 0; i < N; ++i )
  126. {
  127. endian_reverse_inplace( x[i] );
  128. }
  129. }
  130. } // namespace endian
  131. } // namespace boost
  132. #endif // BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED