Chris@102: // boost/endian/arithmetic.hpp -------------------------------------------------------// Chris@102: Chris@102: // (C) Copyright Darin Adler 2000 Chris@102: // (C) Copyright Beman Dawes 2006, 2009, 2014 Chris@102: Chris@102: // Distributed under the Boost Software License, Version 1.0. Chris@102: // See http://www.boost.org/LICENSE_1_0.txt Chris@102: Chris@102: // See library home page at http://www.boost.org/libs/endian Chris@102: Chris@102: //--------------------------------------------------------------------------------------// Chris@102: Chris@102: // Original design developed by Darin Adler based on classes developed by Mark Chris@102: // Borgerding. Four original class templates were combined into a single endian Chris@102: // class template by Beman Dawes, who also added the unrolled_byte_loops sign Chris@102: // partial specialization to correctly extend the sign when cover integer size Chris@102: // differs from endian representation size. Chris@102: Chris@102: // TODO: When a compiler supporting constexpr becomes available, try possible uses. Chris@102: Chris@102: #ifndef BOOST_ENDIAN_ARITHMETIC_HPP Chris@102: #define BOOST_ENDIAN_ARITHMETIC_HPP Chris@102: Chris@102: #if defined(_MSC_VER) Chris@102: # pragma warning(push) Chris@102: # pragma warning(disable:4365) // conversion ... signed/unsigned mismatch Chris@102: #endif Chris@102: Chris@102: #ifdef BOOST_ENDIAN_LOG Chris@102: # include Chris@102: #endif Chris@102: Chris@102: #if defined(__BORLANDC__) || defined( __CODEGEARC__) Chris@102: # pragma pack(push, 1) Chris@102: #endif Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #define BOOST_ENDIAN_MINIMAL_COVER_OPERATORS Chris@102: #include Chris@102: #undef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: # if CHAR_BIT != 8 Chris@102: # error Platforms with CHAR_BIT != 8 are not supported Chris@102: # endif Chris@102: Chris@102: # ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS Chris@102: # define BOOST_ENDIAN_DEFAULT_CONSTRUCT {} // C++03 Chris@102: # else Chris@102: # define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x Chris@102: # endif Chris@102: Chris@102: # if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && defined(BOOST_ENDIAN_FORCE_PODNESS) Chris@102: # define BOOST_ENDIAN_NO_CTORS Chris@102: # endif Chris@102: Chris@102: # ifndef BOOST_ENDIAN_EXPLICIT_CTORS Chris@102: # define BOOST_ENDIAN_EXPLICIT_OPT Chris@102: # else Chris@102: # define BOOST_ENDIAN_EXPLICIT_OPT explicit Chris@102: # endif Chris@102: Chris@102: //---------------------------------- synopsis ----------------------------------------// Chris@102: Chris@102: namespace boost Chris@102: { Chris@102: namespace endian Chris@102: { Chris@102: Chris@102: template Chris@102: class endian_arithmetic; Chris@102: Chris@102: // big endian signed integer aligned types Chris@102: typedef endian_arithmetic big_int8_at; Chris@102: typedef endian_arithmetic big_int16_at; Chris@102: typedef endian_arithmetic big_int32_at; Chris@102: typedef endian_arithmetic big_int64_at; Chris@102: Chris@102: // big endian unsigned integer aligned types Chris@102: typedef endian_arithmetic big_uint8_at; Chris@102: typedef endian_arithmetic big_uint16_at; Chris@102: typedef endian_arithmetic big_uint32_at; Chris@102: typedef endian_arithmetic big_uint64_at; Chris@102: Chris@102: // little endian signed integer aligned types Chris@102: typedef endian_arithmetic little_int8_at; Chris@102: typedef endian_arithmetic little_int16_at; Chris@102: typedef endian_arithmetic little_int32_at; Chris@102: typedef endian_arithmetic little_int64_at; Chris@102: Chris@102: // little endian unsigned integer aligned types Chris@102: typedef endian_arithmetic little_uint8_at; Chris@102: typedef endian_arithmetic little_uint16_at; Chris@102: typedef endian_arithmetic little_uint32_at; Chris@102: typedef endian_arithmetic little_uint64_at; Chris@102: Chris@102: // aligned native endian typedefs are not provided because Chris@102: // types are superior for this use case Chris@102: Chris@102: // big endian signed integer unaligned types Chris@102: typedef endian_arithmetic big_int8_t; Chris@102: typedef endian_arithmetic big_int16_t; Chris@102: typedef endian_arithmetic big_int24_t; Chris@102: typedef endian_arithmetic big_int32_t; Chris@102: typedef endian_arithmetic big_int40_t; Chris@102: typedef endian_arithmetic big_int48_t; Chris@102: typedef endian_arithmetic big_int56_t; Chris@102: typedef endian_arithmetic big_int64_t; Chris@102: Chris@102: // big endian unsigned integer unaligned types Chris@102: typedef endian_arithmetic big_uint8_t; Chris@102: typedef endian_arithmetic big_uint16_t; Chris@102: typedef endian_arithmetic big_uint24_t; Chris@102: typedef endian_arithmetic big_uint32_t; Chris@102: typedef endian_arithmetic big_uint40_t; Chris@102: typedef endian_arithmetic big_uint48_t; Chris@102: typedef endian_arithmetic big_uint56_t; Chris@102: typedef endian_arithmetic big_uint64_t; Chris@102: Chris@102: // little endian signed integer unaligned types Chris@102: typedef endian_arithmetic little_int8_t; Chris@102: typedef endian_arithmetic little_int16_t; Chris@102: typedef endian_arithmetic little_int24_t; Chris@102: typedef endian_arithmetic little_int32_t; Chris@102: typedef endian_arithmetic little_int40_t; Chris@102: typedef endian_arithmetic little_int48_t; Chris@102: typedef endian_arithmetic little_int56_t; Chris@102: typedef endian_arithmetic little_int64_t; Chris@102: Chris@102: // little endian unsigned integer unaligned types Chris@102: typedef endian_arithmetic little_uint8_t; Chris@102: typedef endian_arithmetic little_uint16_t; Chris@102: typedef endian_arithmetic little_uint24_t; Chris@102: typedef endian_arithmetic little_uint32_t; Chris@102: typedef endian_arithmetic little_uint40_t; Chris@102: typedef endian_arithmetic little_uint48_t; Chris@102: typedef endian_arithmetic little_uint56_t; Chris@102: typedef endian_arithmetic little_uint64_t; Chris@102: Chris@102: # ifdef BOOST_BIG_ENDIAN Chris@102: // native endian signed integer unaligned types Chris@102: typedef big_int8_t native_int8_t; Chris@102: typedef big_int16_t native_int16_t; Chris@102: typedef big_int24_t native_int24_t; Chris@102: typedef big_int32_t native_int32_t; Chris@102: typedef big_int40_t native_int40_t; Chris@102: typedef big_int48_t native_int48_t; Chris@102: typedef big_int56_t native_int56_t; Chris@102: typedef big_int64_t native_int64_t; Chris@102: Chris@102: // native endian unsigned integer unaligned types Chris@102: typedef big_uint8_t native_uint8_t; Chris@102: typedef big_uint16_t native_uint16_t; Chris@102: typedef big_uint24_t native_uint24_t; Chris@102: typedef big_uint32_t native_uint32_t; Chris@102: typedef big_uint40_t native_uint40_t; Chris@102: typedef big_uint48_t native_uint48_t; Chris@102: typedef big_uint56_t native_uint56_t; Chris@102: typedef big_uint64_t native_uint64_t; Chris@102: # else Chris@102: // native endian signed integer unaligned types Chris@102: typedef little_int8_t native_int8_t; Chris@102: typedef little_int16_t native_int16_t; Chris@102: typedef little_int24_t native_int24_t; Chris@102: typedef little_int32_t native_int32_t; Chris@102: typedef little_int40_t native_int40_t; Chris@102: typedef little_int48_t native_int48_t; Chris@102: typedef little_int56_t native_int56_t; Chris@102: typedef little_int64_t native_int64_t; Chris@102: Chris@102: // native endian unsigned integer unaligned types Chris@102: typedef little_uint8_t native_uint8_t; Chris@102: typedef little_uint16_t native_uint16_t; Chris@102: typedef little_uint24_t native_uint24_t; Chris@102: typedef little_uint32_t native_uint32_t; Chris@102: typedef little_uint40_t native_uint40_t; Chris@102: typedef little_uint48_t native_uint48_t; Chris@102: typedef little_uint56_t native_uint56_t; Chris@102: typedef little_uint64_t native_uint64_t; Chris@102: # endif Chris@102: Chris@102: # ifdef BOOST_ENDIAN_DEPRECATED_NAMES Chris@102: Chris@102: typedef order endianness; Chris@102: typedef align alignment; Chris@102: Chris@102: # ifndef BOOST_NO_CXX11_TEMPLATE_ALIASES Chris@102: template Chris@102: using endian = endian_arithmetic; Chris@102: # endif Chris@102: Chris@102: // unaligned big endian signed integer types Chris@102: typedef endian_arithmetic< order::big, int_least8_t, 8 > big8_t; Chris@102: typedef endian_arithmetic< order::big, int_least16_t, 16 > big16_t; Chris@102: typedef endian_arithmetic< order::big, int_least32_t, 24 > big24_t; Chris@102: typedef endian_arithmetic< order::big, int_least32_t, 32 > big32_t; Chris@102: typedef endian_arithmetic< order::big, int_least64_t, 40 > big40_t; Chris@102: typedef endian_arithmetic< order::big, int_least64_t, 48 > big48_t; Chris@102: typedef endian_arithmetic< order::big, int_least64_t, 56 > big56_t; Chris@102: typedef endian_arithmetic< order::big, int_least64_t, 64 > big64_t; Chris@102: Chris@102: // unaligned big endian_arithmetic unsigned integer types Chris@102: typedef endian_arithmetic< order::big, uint_least8_t, 8 > ubig8_t; Chris@102: typedef endian_arithmetic< order::big, uint_least16_t, 16 > ubig16_t; Chris@102: typedef endian_arithmetic< order::big, uint_least32_t, 24 > ubig24_t; Chris@102: typedef endian_arithmetic< order::big, uint_least32_t, 32 > ubig32_t; Chris@102: typedef endian_arithmetic< order::big, uint_least64_t, 40 > ubig40_t; Chris@102: typedef endian_arithmetic< order::big, uint_least64_t, 48 > ubig48_t; Chris@102: typedef endian_arithmetic< order::big, uint_least64_t, 56 > ubig56_t; Chris@102: typedef endian_arithmetic< order::big, uint_least64_t, 64 > ubig64_t; Chris@102: Chris@102: // unaligned little endian_arithmetic signed integer types Chris@102: typedef endian_arithmetic< order::little, int_least8_t, 8 > little8_t; Chris@102: typedef endian_arithmetic< order::little, int_least16_t, 16 > little16_t; Chris@102: typedef endian_arithmetic< order::little, int_least32_t, 24 > little24_t; Chris@102: typedef endian_arithmetic< order::little, int_least32_t, 32 > little32_t; Chris@102: typedef endian_arithmetic< order::little, int_least64_t, 40 > little40_t; Chris@102: typedef endian_arithmetic< order::little, int_least64_t, 48 > little48_t; Chris@102: typedef endian_arithmetic< order::little, int_least64_t, 56 > little56_t; Chris@102: typedef endian_arithmetic< order::little, int_least64_t, 64 > little64_t; Chris@102: Chris@102: // unaligned little endian_arithmetic unsigned integer types Chris@102: typedef endian_arithmetic< order::little, uint_least8_t, 8 > ulittle8_t; Chris@102: typedef endian_arithmetic< order::little, uint_least16_t, 16 > ulittle16_t; Chris@102: typedef endian_arithmetic< order::little, uint_least32_t, 24 > ulittle24_t; Chris@102: typedef endian_arithmetic< order::little, uint_least32_t, 32 > ulittle32_t; Chris@102: typedef endian_arithmetic< order::little, uint_least64_t, 40 > ulittle40_t; Chris@102: typedef endian_arithmetic< order::little, uint_least64_t, 48 > ulittle48_t; Chris@102: typedef endian_arithmetic< order::little, uint_least64_t, 56 > ulittle56_t; Chris@102: typedef endian_arithmetic< order::little, uint_least64_t, 64 > ulittle64_t; Chris@102: Chris@102: // unaligned native endian_arithmetic signed integer types Chris@102: typedef endian_arithmetic< order::native, int_least8_t, 8 > native8_t; Chris@102: typedef endian_arithmetic< order::native, int_least16_t, 16 > native16_t; Chris@102: typedef endian_arithmetic< order::native, int_least32_t, 24 > native24_t; Chris@102: typedef endian_arithmetic< order::native, int_least32_t, 32 > native32_t; Chris@102: typedef endian_arithmetic< order::native, int_least64_t, 40 > native40_t; Chris@102: typedef endian_arithmetic< order::native, int_least64_t, 48 > native48_t; Chris@102: typedef endian_arithmetic< order::native, int_least64_t, 56 > native56_t; Chris@102: typedef endian_arithmetic< order::native, int_least64_t, 64 > native64_t; Chris@102: Chris@102: // unaligned native endian_arithmetic unsigned integer types Chris@102: typedef endian_arithmetic< order::native, uint_least8_t, 8 > unative8_t; Chris@102: typedef endian_arithmetic< order::native, uint_least16_t, 16 > unative16_t; Chris@102: typedef endian_arithmetic< order::native, uint_least32_t, 24 > unative24_t; Chris@102: typedef endian_arithmetic< order::native, uint_least32_t, 32 > unative32_t; Chris@102: typedef endian_arithmetic< order::native, uint_least64_t, 40 > unative40_t; Chris@102: typedef endian_arithmetic< order::native, uint_least64_t, 48 > unative48_t; Chris@102: typedef endian_arithmetic< order::native, uint_least64_t, 56 > unative56_t; Chris@102: typedef endian_arithmetic< order::native, uint_least64_t, 64 > unative64_t; Chris@102: Chris@102: // aligned native endian_arithmetic typedefs are not provided because Chris@102: // types are superior for this use case Chris@102: Chris@102: typedef endian_arithmetic< order::big, int16_t, 16, align::yes > aligned_big16_t; Chris@102: typedef endian_arithmetic< order::big, uint16_t, 16, align::yes > aligned_ubig16_t; Chris@102: typedef endian_arithmetic< order::little, int16_t, 16, align::yes > aligned_little16_t; Chris@102: typedef endian_arithmetic< order::little, uint16_t, 16, align::yes > aligned_ulittle16_t; Chris@102: Chris@102: typedef endian_arithmetic< order::big, int32_t, 32, align::yes > aligned_big32_t; Chris@102: typedef endian_arithmetic< order::big, uint32_t, 32, align::yes > aligned_ubig32_t; Chris@102: typedef endian_arithmetic< order::little, int32_t, 32, align::yes > aligned_little32_t; Chris@102: typedef endian_arithmetic< order::little, uint32_t, 32, align::yes > aligned_ulittle32_t; Chris@102: Chris@102: typedef endian_arithmetic< order::big, int64_t, 64, align::yes > aligned_big64_t; Chris@102: typedef endian_arithmetic< order::big, uint64_t, 64, align::yes > aligned_ubig64_t; Chris@102: typedef endian_arithmetic< order::little, int64_t, 64, align::yes > aligned_little64_t; Chris@102: typedef endian_arithmetic< order::little, uint64_t, 64, align::yes > aligned_ulittle64_t; Chris@102: Chris@102: # endif Chris@102: Chris@102: //---------------------------------- end synopsis ------------------------------------// Chris@102: Chris@102: // endian class template specializations ---------------------------------------------// Chris@102: Chris@102: // Specializations that represent unaligned bytes. Chris@102: // Taking an integer type as a parameter provides a nice way to pass both Chris@102: // the size and signness of the desired integer and get the appropriate Chris@102: // corresponding integer type for the interface. Chris@102: Chris@102: // unaligned integer big endian specialization Chris@102: template Chris@102: class endian_arithmetic< order::big, T, n_bits, align::no > Chris@102: : public endian_buffer< order::big, T, n_bits, align::no >, Chris@102: cover_operators, T> Chris@102: { Chris@102: BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); Chris@102: public: Chris@102: typedef T value_type; Chris@102: # ifndef BOOST_ENDIAN_NO_CTORS Chris@102: endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT Chris@102: BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT Chris@102: { Chris@102: # ifdef BOOST_ENDIAN_LOG Chris@102: if ( endian_log ) Chris@102: std::cout << "big, unaligned, " << n_bits << "-bits, construct(" << val << ")\n"; Chris@102: # endif Chris@102: detail::store_big_endian(this->m_value, val); Chris@102: } Chris@102: # endif Chris@102: endian_arithmetic& operator=(T val) BOOST_NOEXCEPT Chris@102: { detail::store_big_endian(this->m_value, val); return *this; } Chris@102: operator value_type() const BOOST_NOEXCEPT { return this->value(); } Chris@102: }; Chris@102: Chris@102: // unaligned little endian specialization Chris@102: template Chris@102: class endian_arithmetic< order::little, T, n_bits, align::no > Chris@102: : public endian_buffer< order::little, T, n_bits, align::no >, Chris@102: cover_operators< endian_arithmetic< order::little, T, n_bits >, T > Chris@102: { Chris@102: BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); Chris@102: public: Chris@102: typedef T value_type; Chris@102: # ifndef BOOST_ENDIAN_NO_CTORS Chris@102: endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT Chris@102: BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT Chris@102: { Chris@102: # ifdef BOOST_ENDIAN_LOG Chris@102: if ( endian_log ) Chris@102: std::cout << "little, unaligned, " << n_bits << "-bits, construct(" << val << ")\n"; Chris@102: # endif Chris@102: detail::store_little_endian(this->m_value, val); Chris@102: } Chris@102: # endif Chris@102: endian_arithmetic& operator=(T val) BOOST_NOEXCEPT Chris@102: { detail::store_little_endian(this->m_value, val); return *this; } Chris@102: operator value_type() const BOOST_NOEXCEPT { return this->value(); } Chris@102: }; Chris@102: Chris@102: // align::yes specializations; only n_bits == 16/32/64 supported Chris@102: Chris@102: // aligned big endian specialization Chris@102: template Chris@102: class endian_arithmetic Chris@102: : public endian_buffer< order::big, T, n_bits, align::yes >, Chris@102: cover_operators, T> Chris@102: { Chris@102: BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); Chris@102: BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 ); Chris@102: public: Chris@102: typedef T value_type; Chris@102: # ifndef BOOST_ENDIAN_NO_CTORS Chris@102: endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT Chris@102: BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT Chris@102: { Chris@102: # ifdef BOOST_ENDIAN_LOG Chris@102: if ( endian_log ) Chris@102: std::cout << "big, aligned, " << n_bits << "-bits, construct(" << val << ")\n"; Chris@102: # endif Chris@102: this->m_value = ::boost::endian::native_to_big(val); Chris@102: } Chris@102: Chris@102: # endif Chris@102: endian_arithmetic& operator=(T val) BOOST_NOEXCEPT Chris@102: { Chris@102: this->m_value = ::boost::endian::native_to_big(val); Chris@102: return *this; Chris@102: } Chris@102: operator value_type() const BOOST_NOEXCEPT { return this->value(); } Chris@102: }; Chris@102: Chris@102: // aligned little endian specialization Chris@102: template Chris@102: class endian_arithmetic Chris@102: : public endian_buffer< order::little, T, n_bits, align::yes >, Chris@102: cover_operators, T> Chris@102: { Chris@102: BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); Chris@102: BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 ); Chris@102: public: Chris@102: typedef T value_type; Chris@102: # ifndef BOOST_ENDIAN_NO_CTORS Chris@102: endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT Chris@102: BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT Chris@102: { Chris@102: # ifdef BOOST_ENDIAN_LOG Chris@102: if ( endian_log ) Chris@102: std::cout << "little, aligned, " << n_bits << "-bits, construct(" << val << ")\n"; Chris@102: # endif Chris@102: this->m_value = ::boost::endian::native_to_little(val); Chris@102: } Chris@102: # endif Chris@102: endian_arithmetic& operator=(T val) BOOST_NOEXCEPT Chris@102: { Chris@102: this->m_value = ::boost::endian::native_to_little(val); Chris@102: return *this; Chris@102: } Chris@102: operator value_type() const BOOST_NOEXCEPT { return this->value(); } Chris@102: }; Chris@102: Chris@102: } // namespace endian Chris@102: } // namespace boost Chris@102: Chris@102: #if defined(__BORLANDC__) || defined( __CODEGEARC__) Chris@102: # pragma pack(pop) Chris@102: #endif Chris@102: Chris@102: #if defined(_MSC_VER) Chris@102: # pragma warning(pop) Chris@102: #endif Chris@102: Chris@102: #endif // BOOST_ENDIAN_ARITHMETIC_HPP