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