endian_reverse.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/endian/detail/is_integral.hpp>
  10. #include <boost/endian/detail/static_assert.hpp>
  11. #include <boost/config.hpp>
  12. #include <type_traits>
  13. #include <cstdint>
  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 std::uint8_t BOOST_CONSTEXPR endian_reverse_impl( std::uint8_t x ) BOOST_NOEXCEPT
  41. {
  42. return x;
  43. }
  44. inline std::uint16_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( std::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 std::uint32_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( std::uint32_t x ) BOOST_NOEXCEPT
  53. {
  54. #ifdef BOOST_ENDIAN_NO_INTRINSICS
  55. std::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 std::uint64_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( std::uint64_t x ) BOOST_NOEXCEPT
  62. {
  63. #ifdef BOOST_ENDIAN_NO_INTRINSICS
  64. std::uint64_t step32 = x << 32 | x >> 32;
  65. std::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(__SIZEOF_INT128__)
  72. inline __uint128_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( __uint128_t x ) BOOST_NOEXCEPT
  73. {
  74. return endian_reverse_impl( static_cast<std::uint64_t>( x >> 64 ) ) |
  75. static_cast<__uint128_t>( endian_reverse_impl( static_cast<std::uint64_t>( x ) ) ) << 64;
  76. }
  77. #endif
  78. // is_endian_reversible
  79. template<class T> struct is_endian_reversible: std::integral_constant<bool,
  80. (is_integral<T>::value && !std::is_same<T, bool>::value) || is_scoped_enum<T>::value>
  81. {
  82. };
  83. // is_endian_reversible_inplace
  84. template<class T> struct is_endian_reversible_inplace: std::integral_constant<bool,
  85. is_integral<T>::value || std::is_enum<T>::value || std::is_same<T, float>::value || std::is_same<T, double>::value>
  86. {
  87. };
  88. } // namespace detail
  89. // Requires:
  90. // T is non-bool integral or scoped enumeration type
  91. template<class T> inline BOOST_CONSTEXPR
  92. typename std::enable_if< !std::is_class<T>::value, T >::type
  93. endian_reverse( T x ) BOOST_NOEXCEPT
  94. {
  95. BOOST_ENDIAN_STATIC_ASSERT( detail::is_endian_reversible<T>::value );
  96. typedef typename detail::integral_by_size< sizeof(T) >::type uintN_t;
  97. return static_cast<T>( detail::endian_reverse_impl( static_cast<uintN_t>( x ) ) );
  98. }
  99. // Requires:
  100. // T is integral, enumeration, float or double
  101. template<class T> inline
  102. typename std::enable_if< !std::is_class<T>::value >::type
  103. endian_reverse_inplace( T & x ) BOOST_NOEXCEPT
  104. {
  105. BOOST_ENDIAN_STATIC_ASSERT( detail::is_endian_reversible_inplace<T>::value );
  106. typename detail::integral_by_size< sizeof(T) >::type x2;
  107. std::memcpy( &x2, &x, sizeof(T) );
  108. x2 = detail::endian_reverse_impl( x2 );
  109. std::memcpy( &x, &x2, sizeof(T) );
  110. }
  111. // Default implementation for user-defined types
  112. template<class T> inline
  113. typename std::enable_if< std::is_class<T>::value >::type
  114. endian_reverse_inplace( T & x ) BOOST_NOEXCEPT
  115. {
  116. x = endian_reverse( x );
  117. }
  118. // endian_reverse_inplace for arrays
  119. template<class T, std::size_t N>
  120. inline void endian_reverse_inplace( T (&x)[ N ] ) BOOST_NOEXCEPT
  121. {
  122. for( std::size_t i = 0; i < N; ++i )
  123. {
  124. endian_reverse_inplace( x[i] );
  125. }
  126. }
  127. } // namespace endian
  128. } // namespace boost
  129. #endif // BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED