// boost/endian/buffers.hpp ----------------------------------------------------------// // (C) Copyright Darin Adler 2000 // (C) Copyright Beman Dawes 2006, 2009, 2014 // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt // See library home page at http://www.boost.org/libs/endian //--------------------------------------------------------------------------------------// // Original design developed by Darin Adler based on classes developed by Mark // Borgerding. Four original class templates were combined into a single endian // class template by Beman Dawes, who also added the unrolled_byte_loops sign // partial specialization to correctly extend the sign when cover integer size // differs from endian representation size. // TODO: When a compiler supporting constexpr becomes available, try possible uses. #ifndef BOOST_ENDIAN_BUFFERS_HPP #define BOOST_ENDIAN_BUFFERS_HPP #if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable:4365) // conversion ... signed/unsigned mismatch #endif #ifdef BOOST_ENDIAN_LOG # include #endif #if defined(__BORLANDC__) || defined( __CODEGEARC__) # pragma pack(push, 1) #endif #include #include #include #include #include #include #include #include #include # if CHAR_BIT != 8 # error Platforms with CHAR_BIT != 8 are not supported # endif # ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS # define BOOST_ENDIAN_DEFAULT_CONSTRUCT {} // C++03 # else # define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x # endif # if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && defined(BOOST_ENDIAN_FORCE_PODNESS) # define BOOST_ENDIAN_NO_CTORS # endif //---------------------------------- synopsis ----------------------------------------// namespace boost { namespace endian { BOOST_SCOPED_ENUM_START(align) {no, yes # ifdef BOOST_ENDIAN_DEPRECATED_NAMES , unaligned = no, aligned = yes # endif }; BOOST_SCOPED_ENUM_END template class endian_buffer; // aligned big endian signed integer buffers typedef endian_buffer big_int8_buf_at; typedef endian_buffer big_int16_buf_at; typedef endian_buffer big_int32_buf_at; typedef endian_buffer big_int64_buf_at; // aligned big endian unsigned integer buffers typedef endian_buffer big_uint8_buf_at; typedef endian_buffer big_uint16_buf_at; typedef endian_buffer big_uint32_buf_at; typedef endian_buffer big_uint64_buf_at; // aligned little endian signed integer buffers typedef endian_buffer little_int8_buf_at; typedef endian_buffer little_int16_buf_at; typedef endian_buffer little_int32_buf_at; typedef endian_buffer little_int64_buf_at; // aligned little endian unsigned integer buffers typedef endian_buffer little_uint8_buf_at; typedef endian_buffer little_uint16_buf_at; typedef endian_buffer little_uint32_buf_at; typedef endian_buffer little_uint64_buf_at; // aligned native endian typedefs are not provided because // types are superior for this use case // unaligned big endian signed integer buffers typedef endian_buffer big_int8_buf_t; typedef endian_buffer big_int16_buf_t; typedef endian_buffer big_int24_buf_t; typedef endian_buffer big_int32_buf_t; typedef endian_buffer big_int40_buf_t; typedef endian_buffer big_int48_buf_t; typedef endian_buffer big_int56_buf_t; typedef endian_buffer big_int64_buf_t; // unaligned big endian unsigned integer buffers typedef endian_buffer big_uint8_buf_t; typedef endian_buffer big_uint16_buf_t; typedef endian_buffer big_uint24_buf_t; typedef endian_buffer big_uint32_buf_t; typedef endian_buffer big_uint40_buf_t; typedef endian_buffer big_uint48_buf_t; typedef endian_buffer big_uint56_buf_t; typedef endian_buffer big_uint64_buf_t; // unaligned little endian signed integer buffers typedef endian_buffer little_int8_buf_t; typedef endian_buffer little_int16_buf_t; typedef endian_buffer little_int24_buf_t; typedef endian_buffer little_int32_buf_t; typedef endian_buffer little_int40_buf_t; typedef endian_buffer little_int48_buf_t; typedef endian_buffer little_int56_buf_t; typedef endian_buffer little_int64_buf_t; // unaligned little endian unsigned integer buffers typedef endian_buffer little_uint8_buf_t; typedef endian_buffer little_uint16_buf_t; typedef endian_buffer little_uint24_buf_t; typedef endian_buffer little_uint32_buf_t; typedef endian_buffer little_uint40_buf_t; typedef endian_buffer little_uint48_buf_t; typedef endian_buffer little_uint56_buf_t; typedef endian_buffer little_uint64_buf_t; # ifdef BOOST_BIG_ENDIAN // unaligned native endian signed integer buffers typedef big_int8_buf_t native_int8_buf_t; typedef big_int16_buf_t native_int16_buf_t; typedef big_int24_buf_t native_int24_buf_t; typedef big_int32_buf_t native_int32_buf_t; typedef big_int40_buf_t native_int40_buf_t; typedef big_int48_buf_t native_int48_buf_t; typedef big_int56_buf_t native_int56_buf_t; typedef big_int64_buf_t native_int64_buf_t; // unaligned native endian unsigned integer buffers typedef big_uint8_buf_t native_uint8_buf_t; typedef big_uint16_buf_t native_uint16_buf_t; typedef big_uint24_buf_t native_uint24_buf_t; typedef big_uint32_buf_t native_uint32_buf_t; typedef big_uint40_buf_t native_uint40_buf_t; typedef big_uint48_buf_t native_uint48_buf_t; typedef big_uint56_buf_t native_uint56_buf_t; typedef big_uint64_buf_t native_uint64_buf_t; # else // unaligned native endian signed integer buffers typedef little_int8_buf_t native_int8_buf_t; typedef little_int16_buf_t native_int16_buf_t; typedef little_int24_buf_t native_int24_buf_t; typedef little_int32_buf_t native_int32_buf_t; typedef little_int40_buf_t native_int40_buf_t; typedef little_int48_buf_t native_int48_buf_t; typedef little_int56_buf_t native_int56_buf_t; typedef little_int64_buf_t native_int64_buf_t; // unaligned native endian unsigned integer buffers typedef little_uint8_buf_t native_uint8_buf_t; typedef little_uint16_buf_t native_uint16_buf_t; typedef little_uint24_buf_t native_uint24_buf_t; typedef little_uint32_buf_t native_uint32_buf_t; typedef little_uint40_buf_t native_uint40_buf_t; typedef little_uint48_buf_t native_uint48_buf_t; typedef little_uint56_buf_t native_uint56_buf_t; typedef little_uint64_buf_t native_uint64_buf_t; # endif // Stream inserter template std::basic_ostream& operator<<(std::basic_ostream& os, const endian_buffer& x) { return os << x.value(); } // Stream extractor template std::basic_istream& operator>>(std::basic_istream& is, endian_buffer& x) { T i; if (is >> i) x = i; return is; } //---------------------------------- end synopsis ------------------------------------// namespace detail { // Unrolled loops for loading and storing streams of bytes. template ::value > struct unrolled_byte_loops { typedef unrolled_byte_loops next; static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT { return static_cast(*(bytes - 1) | (next::load_big(bytes - 1) << 8)); } static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT { return static_cast(*bytes | (next::load_little(bytes + 1) << 8)); } static void store_big(char* bytes, T value) BOOST_NOEXCEPT { *(bytes - 1) = static_cast(value); next::store_big(bytes - 1, static_cast(value >> 8)); } static void store_little(char* bytes, T value) BOOST_NOEXCEPT { *bytes = static_cast(value); next::store_little(bytes + 1, static_cast(value >> 8)); } }; template struct unrolled_byte_loops { static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT { return *(bytes - 1); } static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT { return *bytes; } static void store_big(char* bytes, T value) BOOST_NOEXCEPT { *(bytes - 1) = static_cast(value); } static void store_little(char* bytes, T value) BOOST_NOEXCEPT { *bytes = static_cast(value); } }; template struct unrolled_byte_loops { static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT { return *reinterpret_cast(bytes - 1); } static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT { return *reinterpret_cast(bytes); } static void store_big(char* bytes, T value) BOOST_NOEXCEPT { *(bytes - 1) = static_cast(value); } static void store_little(char* bytes, T value) BOOST_NOEXCEPT { *bytes = static_cast(value); } }; template inline T load_big_endian(const void* bytes) BOOST_NOEXCEPT { return unrolled_byte_loops::load_big (static_cast(bytes) + n_bytes); } template inline T load_little_endian(const void* bytes) BOOST_NOEXCEPT { # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) // On x86 (which is little endian), unaligned loads are permitted if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this // test and generate code only for the applicable return // case since sizeof(T) and n_bytes are known at compile // time. { return *reinterpret_cast(bytes); } # endif return unrolled_byte_loops::load_little (static_cast(bytes)); } template inline void store_big_endian(void* bytes, T value) BOOST_NOEXCEPT { unrolled_byte_loops::store_big (static_cast(bytes) + n_bytes, value); } template inline void store_little_endian(void* bytes, T value) BOOST_NOEXCEPT { # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) // On x86 (which is little endian), unaligned stores are permitted if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this // test and generate code only for the applicable return // case since sizeof(T) and n_bytes are known at compile // time. { *reinterpret_cast(bytes) = value; return; } # endif unrolled_byte_loops::store_little (static_cast(bytes), value); } } // namespace detail # ifdef BOOST_ENDIAN_LOG bool endian_log(true); # endif // endian_buffer class template specializations --------------------------------------// // Specializations that represent unaligned bytes. // Taking an integer type as a parameter provides a nice way to pass both // the size and signedness of the desired integer and get the appropriate // corresponding integer type for the interface. // Q: Should endian_buffer supply "value_type operator value_type() const noexcept"? // A: No. The rationale for endian_buffers is to prevent high-cost hidden // conversions. If an implicit conversion operator is supplied, hidden conversions // can occur. // unaligned big endian_buffer specialization template class endian_buffer< order::big, T, n_bits, align::no > { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT explicit endian_buffer(T val) BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::cout << "big, unaligned, " << n_bits << "-bits, construct(" << val << ")\n"; # endif detail::store_big_endian(m_value, val); } # endif endian_buffer & operator=(T val) BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if (endian_log) std::cout << "big, unaligned, " << n_bits << "-bits, assign(" << val << ")\n"; # endif detail::store_big_endian(m_value, val); return *this; } value_type value() const BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::cout << "big, unaligned, " << n_bits << "-bits, convert(" << detail::load_big_endian(m_value) << ")\n"; # endif return detail::load_big_endian(m_value); } const char* data() const BOOST_NOEXCEPT { return m_value; } protected: char m_value[n_bits/8]; }; // unaligned little endian_buffer specialization template class endian_buffer< order::little, T, n_bits, align::no > { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT explicit endian_buffer(T val) BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::cout << "little, unaligned, " << n_bits << "-bits, construct(" << val << ")\n"; # endif detail::store_little_endian(m_value, val); } # endif endian_buffer & operator=(T val) BOOST_NOEXCEPT { detail::store_little_endian(m_value, val); return *this; } value_type value() const BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::cout << "little, unaligned, " << n_bits << "-bits, convert(" << detail::load_little_endian(m_value) << ")\n"; # endif return detail::load_little_endian(m_value); } const char* data() const BOOST_NOEXCEPT { return m_value; } protected: char m_value[n_bits/8]; }; // align::yes specializations; only n_bits == 16/32/64 supported // aligned big endian_buffer specialization template class endian_buffer { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT explicit endian_buffer(T val) BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::cout << "big, aligned, " << n_bits << "-bits, construct(" << val << ")\n"; # endif m_value = ::boost::endian::native_to_big(val); } # endif endian_buffer& operator=(T val) BOOST_NOEXCEPT { m_value = ::boost::endian::native_to_big(val); return *this; } //operator value_type() const BOOST_NOEXCEPT //{ // return ::boost::endian::big_to_native(m_value); //} value_type value() const BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::cout << "big, aligned, " << n_bits << "-bits, convert(" << ::boost::endian::big_to_native(m_value) << ")\n"; # endif return ::boost::endian::big_to_native(m_value); } const char* data() const BOOST_NOEXCEPT {return reinterpret_cast(&m_value);} protected: T m_value; }; // aligned little endian_buffer specialization template class endian_buffer { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT explicit endian_buffer(T val) BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::cout << "little, aligned, " << n_bits << "-bits, construct(" << val << ")\n"; # endif m_value = ::boost::endian::native_to_little(val); } # endif endian_buffer& operator=(T val) BOOST_NOEXCEPT { m_value = ::boost::endian::native_to_little(val); return *this; } value_type value() const BOOST_NOEXCEPT { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::cout << "little, aligned, " << n_bits << "-bits, convert(" << ::boost::endian::little_to_native(m_value) << ")\n"; # endif return ::boost::endian::little_to_native(m_value); } const char* data() const BOOST_NOEXCEPT {return reinterpret_cast(&m_value);} protected: T m_value; }; } // namespace endian } // namespace boost #if defined(__BORLANDC__) || defined( __CODEGEARC__) # pragma pack(pop) #endif #if defined(_MSC_VER) # pragma warning(pop) #endif #endif // BOOST_ENDIAN_BUFFERS_HPP