// endian_swap.hpp // Copyright Mike Patterson 2004 - 2011. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_ENDIAN_SWAP_HPP #define BOOST_ENDIAN_SWAP_HPP #include namespace boost { namespace endian { namespace detail { template class endian_swapper { public: template static void swap(T& value) {/*no-op*/} }; template<> class endian_swapper<2> { public: typedef uint16_t value_type; template static void swap(T& value) {do_swap(reinterpret_cast(value));} private: static void do_swap(value_type& value) {value = (value >> 8) | (value << 8);} }; template<> class endian_swapper<4> { public: typedef uint32_t value_type; template static void swap(T& value) {do_swap(reinterpret_cast(value));} private: static void do_swap(value_type& value) { value = (value >> 0x18) | ((value >> 0x08) & 0x0000FF00) | ((value << 0x08) & 0x00FF0000) | (value << 0x18); } }; template<> class endian_swapper<8> { public: typedef uint64_t value_type; template static void swap(T& value) {do_swap(reinterpret_cast(value));} private: static void do_swap(value_type& value) { value = (value >> 0x38) | ((value >> 0x28) & 0x000000000000FF00) | ((value >> 0x18) & 0x0000000000FF0000) | ((value >> 0x08) & 0x00000000FF000000) | ((value << 0x08) & 0x000000FF00000000) | ((value << 0x18) & 0x0000FF0000000000) | ((value << 0x28) & 0x00FF000000000000) | (value << 0x38); } }; } // namespace detail template inline void swap(T& value) {detail::endian_swapper::swap(value);} } // namespace endian } // namespace boost #endif // BOOST_ENDIAN_SWAP_HPP