diff options
Diffstat (limited to 'boost/endian')
-rw-r--r-- | boost/endian/arithmetic.hpp | 413 | ||||
-rw-r--r-- | boost/endian/buffers.hpp | 515 | ||||
-rw-r--r-- | boost/endian/conversion.hpp | 487 | ||||
-rw-r--r-- | boost/endian/detail/config.hpp | 62 | ||||
-rw-r--r-- | boost/endian/detail/cover_operators.hpp | 142 | ||||
-rw-r--r-- | boost/endian/detail/disable_warnings.hpp | 33 | ||||
-rw-r--r-- | boost/endian/detail/disable_warnings_pop.hpp | 12 | ||||
-rw-r--r-- | boost/endian/detail/intrinsic.hpp | 64 | ||||
-rw-r--r-- | boost/endian/detail/lightweight_test.hpp | 223 | ||||
-rw-r--r-- | boost/endian/endian.hpp | 19 | ||||
-rw-r--r-- | boost/endian/std_pair.hpp | 38 |
11 files changed, 2008 insertions, 0 deletions
diff --git a/boost/endian/arithmetic.hpp b/boost/endian/arithmetic.hpp new file mode 100644 index 0000000000..d8bcbe62b4 --- /dev/null +++ b/boost/endian/arithmetic.hpp @@ -0,0 +1,413 @@ +// boost/endian/arithmetic.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_ARITHMETIC_HPP +#define BOOST_ENDIAN_ARITHMETIC_HPP + +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable:4365) // conversion ... signed/unsigned mismatch +#endif + +#ifdef BOOST_ENDIAN_LOG +# include <iostream> +#endif + +#if defined(__BORLANDC__) || defined( __CODEGEARC__) +# pragma pack(push, 1) +#endif + +#include <boost/config.hpp> +#include <boost/predef/detail/endian_compat.h> +#include <boost/endian/conversion.hpp> +#include <boost/endian/buffers.hpp> +#define BOOST_ENDIAN_MINIMAL_COVER_OPERATORS +#include <boost/endian/detail/cover_operators.hpp> +#undef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS +#include <boost/type_traits/is_signed.hpp> +#include <boost/cstdint.hpp> +#include <boost/static_assert.hpp> +#include <boost/core/scoped_enum.hpp> +#include <iosfwd> +#include <climits> + +# 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 + +# ifndef BOOST_ENDIAN_EXPLICIT_CTORS +# define BOOST_ENDIAN_EXPLICIT_OPT +# else +# define BOOST_ENDIAN_EXPLICIT_OPT explicit +# endif + +//---------------------------------- synopsis ----------------------------------------// + +namespace boost +{ +namespace endian +{ + + template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits, + BOOST_SCOPED_ENUM(align) A = align::no> + class endian_arithmetic; + + // big endian signed integer aligned types + typedef endian_arithmetic<order::big, int8_t, 8, align::yes> big_int8_at; + typedef endian_arithmetic<order::big, int16_t, 16, align::yes> big_int16_at; + typedef endian_arithmetic<order::big, int32_t, 32, align::yes> big_int32_at; + typedef endian_arithmetic<order::big, int64_t, 64, align::yes> big_int64_at; + + // big endian unsigned integer aligned types + typedef endian_arithmetic<order::big, uint8_t, 8, align::yes> big_uint8_at; + typedef endian_arithmetic<order::big, uint16_t, 16, align::yes> big_uint16_at; + typedef endian_arithmetic<order::big, uint32_t, 32, align::yes> big_uint32_at; + typedef endian_arithmetic<order::big, uint64_t, 64, align::yes> big_uint64_at; + + // little endian signed integer aligned types + typedef endian_arithmetic<order::little, int8_t, 8, align::yes> little_int8_at; + typedef endian_arithmetic<order::little, int16_t, 16, align::yes> little_int16_at; + typedef endian_arithmetic<order::little, int32_t, 32, align::yes> little_int32_at; + typedef endian_arithmetic<order::little, int64_t, 64, align::yes> little_int64_at; + + // little endian unsigned integer aligned types + typedef endian_arithmetic<order::little, uint8_t, 8, align::yes> little_uint8_at; + typedef endian_arithmetic<order::little, uint16_t, 16, align::yes> little_uint16_at; + typedef endian_arithmetic<order::little, uint32_t, 32, align::yes> little_uint32_at; + typedef endian_arithmetic<order::little, uint64_t, 64, align::yes> little_uint64_at; + + // aligned native endian typedefs are not provided because + // <cstdint> types are superior for this use case + + // big endian signed integer unaligned types + typedef endian_arithmetic<order::big, int_least8_t, 8> big_int8_t; + typedef endian_arithmetic<order::big, int_least16_t, 16> big_int16_t; + typedef endian_arithmetic<order::big, int_least32_t, 24> big_int24_t; + typedef endian_arithmetic<order::big, int_least32_t, 32> big_int32_t; + typedef endian_arithmetic<order::big, int_least64_t, 40> big_int40_t; + typedef endian_arithmetic<order::big, int_least64_t, 48> big_int48_t; + typedef endian_arithmetic<order::big, int_least64_t, 56> big_int56_t; + typedef endian_arithmetic<order::big, int_least64_t, 64> big_int64_t; + + // big endian unsigned integer unaligned types + typedef endian_arithmetic<order::big, uint_least8_t, 8> big_uint8_t; + typedef endian_arithmetic<order::big, uint_least16_t, 16> big_uint16_t; + typedef endian_arithmetic<order::big, uint_least32_t, 24> big_uint24_t; + typedef endian_arithmetic<order::big, uint_least32_t, 32> big_uint32_t; + typedef endian_arithmetic<order::big, uint_least64_t, 40> big_uint40_t; + typedef endian_arithmetic<order::big, uint_least64_t, 48> big_uint48_t; + typedef endian_arithmetic<order::big, uint_least64_t, 56> big_uint56_t; + typedef endian_arithmetic<order::big, uint_least64_t, 64> big_uint64_t; + + // little endian signed integer unaligned types + typedef endian_arithmetic<order::little, int_least8_t, 8> little_int8_t; + typedef endian_arithmetic<order::little, int_least16_t, 16> little_int16_t; + typedef endian_arithmetic<order::little, int_least32_t, 24> little_int24_t; + typedef endian_arithmetic<order::little, int_least32_t, 32> little_int32_t; + typedef endian_arithmetic<order::little, int_least64_t, 40> little_int40_t; + typedef endian_arithmetic<order::little, int_least64_t, 48> little_int48_t; + typedef endian_arithmetic<order::little, int_least64_t, 56> little_int56_t; + typedef endian_arithmetic<order::little, int_least64_t, 64> little_int64_t; + + // little endian unsigned integer unaligned types + typedef endian_arithmetic<order::little, uint_least8_t, 8> little_uint8_t; + typedef endian_arithmetic<order::little, uint_least16_t, 16> little_uint16_t; + typedef endian_arithmetic<order::little, uint_least32_t, 24> little_uint24_t; + typedef endian_arithmetic<order::little, uint_least32_t, 32> little_uint32_t; + typedef endian_arithmetic<order::little, uint_least64_t, 40> little_uint40_t; + typedef endian_arithmetic<order::little, uint_least64_t, 48> little_uint48_t; + typedef endian_arithmetic<order::little, uint_least64_t, 56> little_uint56_t; + typedef endian_arithmetic<order::little, uint_least64_t, 64> little_uint64_t; + +# ifdef BOOST_BIG_ENDIAN + // native endian signed integer unaligned types + typedef big_int8_t native_int8_t; + typedef big_int16_t native_int16_t; + typedef big_int24_t native_int24_t; + typedef big_int32_t native_int32_t; + typedef big_int40_t native_int40_t; + typedef big_int48_t native_int48_t; + typedef big_int56_t native_int56_t; + typedef big_int64_t native_int64_t; + + // native endian unsigned integer unaligned types + typedef big_uint8_t native_uint8_t; + typedef big_uint16_t native_uint16_t; + typedef big_uint24_t native_uint24_t; + typedef big_uint32_t native_uint32_t; + typedef big_uint40_t native_uint40_t; + typedef big_uint48_t native_uint48_t; + typedef big_uint56_t native_uint56_t; + typedef big_uint64_t native_uint64_t; +# else + // native endian signed integer unaligned types + typedef little_int8_t native_int8_t; + typedef little_int16_t native_int16_t; + typedef little_int24_t native_int24_t; + typedef little_int32_t native_int32_t; + typedef little_int40_t native_int40_t; + typedef little_int48_t native_int48_t; + typedef little_int56_t native_int56_t; + typedef little_int64_t native_int64_t; + + // native endian unsigned integer unaligned types + typedef little_uint8_t native_uint8_t; + typedef little_uint16_t native_uint16_t; + typedef little_uint24_t native_uint24_t; + typedef little_uint32_t native_uint32_t; + typedef little_uint40_t native_uint40_t; + typedef little_uint48_t native_uint48_t; + typedef little_uint56_t native_uint56_t; + typedef little_uint64_t native_uint64_t; +# endif + +# ifdef BOOST_ENDIAN_DEPRECATED_NAMES + + typedef order endianness; + typedef align alignment; + +# ifndef BOOST_NO_CXX11_TEMPLATE_ALIASES + template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits, + BOOST_SCOPED_ENUM(align) Align = align::no> + using endian = endian_arithmetic<Order, T, n_bits, Align>; +# endif + + // unaligned big endian signed integer types + typedef endian_arithmetic< order::big, int_least8_t, 8 > big8_t; + typedef endian_arithmetic< order::big, int_least16_t, 16 > big16_t; + typedef endian_arithmetic< order::big, int_least32_t, 24 > big24_t; + typedef endian_arithmetic< order::big, int_least32_t, 32 > big32_t; + typedef endian_arithmetic< order::big, int_least64_t, 40 > big40_t; + typedef endian_arithmetic< order::big, int_least64_t, 48 > big48_t; + typedef endian_arithmetic< order::big, int_least64_t, 56 > big56_t; + typedef endian_arithmetic< order::big, int_least64_t, 64 > big64_t; + + // unaligned big endian_arithmetic unsigned integer types + typedef endian_arithmetic< order::big, uint_least8_t, 8 > ubig8_t; + typedef endian_arithmetic< order::big, uint_least16_t, 16 > ubig16_t; + typedef endian_arithmetic< order::big, uint_least32_t, 24 > ubig24_t; + typedef endian_arithmetic< order::big, uint_least32_t, 32 > ubig32_t; + typedef endian_arithmetic< order::big, uint_least64_t, 40 > ubig40_t; + typedef endian_arithmetic< order::big, uint_least64_t, 48 > ubig48_t; + typedef endian_arithmetic< order::big, uint_least64_t, 56 > ubig56_t; + typedef endian_arithmetic< order::big, uint_least64_t, 64 > ubig64_t; + + // unaligned little endian_arithmetic signed integer types + typedef endian_arithmetic< order::little, int_least8_t, 8 > little8_t; + typedef endian_arithmetic< order::little, int_least16_t, 16 > little16_t; + typedef endian_arithmetic< order::little, int_least32_t, 24 > little24_t; + typedef endian_arithmetic< order::little, int_least32_t, 32 > little32_t; + typedef endian_arithmetic< order::little, int_least64_t, 40 > little40_t; + typedef endian_arithmetic< order::little, int_least64_t, 48 > little48_t; + typedef endian_arithmetic< order::little, int_least64_t, 56 > little56_t; + typedef endian_arithmetic< order::little, int_least64_t, 64 > little64_t; + + // unaligned little endian_arithmetic unsigned integer types + typedef endian_arithmetic< order::little, uint_least8_t, 8 > ulittle8_t; + typedef endian_arithmetic< order::little, uint_least16_t, 16 > ulittle16_t; + typedef endian_arithmetic< order::little, uint_least32_t, 24 > ulittle24_t; + typedef endian_arithmetic< order::little, uint_least32_t, 32 > ulittle32_t; + typedef endian_arithmetic< order::little, uint_least64_t, 40 > ulittle40_t; + typedef endian_arithmetic< order::little, uint_least64_t, 48 > ulittle48_t; + typedef endian_arithmetic< order::little, uint_least64_t, 56 > ulittle56_t; + typedef endian_arithmetic< order::little, uint_least64_t, 64 > ulittle64_t; + + // unaligned native endian_arithmetic signed integer types + typedef endian_arithmetic< order::native, int_least8_t, 8 > native8_t; + typedef endian_arithmetic< order::native, int_least16_t, 16 > native16_t; + typedef endian_arithmetic< order::native, int_least32_t, 24 > native24_t; + typedef endian_arithmetic< order::native, int_least32_t, 32 > native32_t; + typedef endian_arithmetic< order::native, int_least64_t, 40 > native40_t; + typedef endian_arithmetic< order::native, int_least64_t, 48 > native48_t; + typedef endian_arithmetic< order::native, int_least64_t, 56 > native56_t; + typedef endian_arithmetic< order::native, int_least64_t, 64 > native64_t; + + // unaligned native endian_arithmetic unsigned integer types + typedef endian_arithmetic< order::native, uint_least8_t, 8 > unative8_t; + typedef endian_arithmetic< order::native, uint_least16_t, 16 > unative16_t; + typedef endian_arithmetic< order::native, uint_least32_t, 24 > unative24_t; + typedef endian_arithmetic< order::native, uint_least32_t, 32 > unative32_t; + typedef endian_arithmetic< order::native, uint_least64_t, 40 > unative40_t; + typedef endian_arithmetic< order::native, uint_least64_t, 48 > unative48_t; + typedef endian_arithmetic< order::native, uint_least64_t, 56 > unative56_t; + typedef endian_arithmetic< order::native, uint_least64_t, 64 > unative64_t; + + // aligned native endian_arithmetic typedefs are not provided because + // <cstdint> types are superior for this use case + + typedef endian_arithmetic< order::big, int16_t, 16, align::yes > aligned_big16_t; + typedef endian_arithmetic< order::big, uint16_t, 16, align::yes > aligned_ubig16_t; + typedef endian_arithmetic< order::little, int16_t, 16, align::yes > aligned_little16_t; + typedef endian_arithmetic< order::little, uint16_t, 16, align::yes > aligned_ulittle16_t; + + typedef endian_arithmetic< order::big, int32_t, 32, align::yes > aligned_big32_t; + typedef endian_arithmetic< order::big, uint32_t, 32, align::yes > aligned_ubig32_t; + typedef endian_arithmetic< order::little, int32_t, 32, align::yes > aligned_little32_t; + typedef endian_arithmetic< order::little, uint32_t, 32, align::yes > aligned_ulittle32_t; + + typedef endian_arithmetic< order::big, int64_t, 64, align::yes > aligned_big64_t; + typedef endian_arithmetic< order::big, uint64_t, 64, align::yes > aligned_ubig64_t; + typedef endian_arithmetic< order::little, int64_t, 64, align::yes > aligned_little64_t; + typedef endian_arithmetic< order::little, uint64_t, 64, align::yes > aligned_ulittle64_t; + +# endif + +//---------------------------------- end synopsis ------------------------------------// + +// endian 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 signness of the desired integer and get the appropriate + // corresponding integer type for the interface. + + // unaligned integer big endian specialization + template <typename T, std::size_t n_bits> + class endian_arithmetic< order::big, T, n_bits, align::no > + : public endian_buffer< order::big, T, n_bits, align::no >, + cover_operators<endian_arithmetic<order::big, T, n_bits>, T> + { + BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); + public: + typedef T value_type; +# ifndef BOOST_ENDIAN_NO_CTORS + endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT + BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(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<T, n_bits/8>(this->m_value, val); + } +# endif + endian_arithmetic& operator=(T val) BOOST_NOEXCEPT + { detail::store_big_endian<T, n_bits/8>(this->m_value, val); return *this; } + operator value_type() const BOOST_NOEXCEPT { return this->value(); } + }; + + // unaligned little endian specialization + template <typename T, std::size_t n_bits> + class endian_arithmetic< order::little, T, n_bits, align::no > + : public endian_buffer< order::little, T, n_bits, align::no >, + cover_operators< endian_arithmetic< order::little, T, n_bits >, T > + { + BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); + public: + typedef T value_type; +# ifndef BOOST_ENDIAN_NO_CTORS + endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT + BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(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<T, n_bits/8>(this->m_value, val); + } +# endif + endian_arithmetic& operator=(T val) BOOST_NOEXCEPT + { detail::store_little_endian<T, n_bits/8>(this->m_value, val); return *this; } + operator value_type() const BOOST_NOEXCEPT { return this->value(); } + }; + + // align::yes specializations; only n_bits == 16/32/64 supported + + // aligned big endian specialization + template <typename T, std::size_t n_bits> + class endian_arithmetic<order::big, T, n_bits, align::yes> + : public endian_buffer< order::big, T, n_bits, align::yes >, + cover_operators<endian_arithmetic<order::big, T, n_bits, align::yes>, T> + { + 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_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT + BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT + { +# ifdef BOOST_ENDIAN_LOG + if ( endian_log ) + std::cout << "big, aligned, " << n_bits << "-bits, construct(" << val << ")\n"; +# endif + this->m_value = ::boost::endian::native_to_big(val); + } + +# endif + endian_arithmetic& operator=(T val) BOOST_NOEXCEPT + { + this->m_value = ::boost::endian::native_to_big(val); + return *this; + } + operator value_type() const BOOST_NOEXCEPT { return this->value(); } + }; + + // aligned little endian specialization + template <typename T, std::size_t n_bits> + class endian_arithmetic<order::little, T, n_bits, align::yes> + : public endian_buffer< order::little, T, n_bits, align::yes >, + cover_operators<endian_arithmetic<order::little, T, n_bits, align::yes>, T> + { + 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_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT + BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT + { +# ifdef BOOST_ENDIAN_LOG + if ( endian_log ) + std::cout << "little, aligned, " << n_bits << "-bits, construct(" << val << ")\n"; +# endif + this->m_value = ::boost::endian::native_to_little(val); + } +# endif + endian_arithmetic& operator=(T val) BOOST_NOEXCEPT + { + this->m_value = ::boost::endian::native_to_little(val); + return *this; + } + operator value_type() const BOOST_NOEXCEPT { return this->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_ARITHMETIC_HPP diff --git a/boost/endian/buffers.hpp b/boost/endian/buffers.hpp new file mode 100644 index 0000000000..b48b8de7d4 --- /dev/null +++ b/boost/endian/buffers.hpp @@ -0,0 +1,515 @@ +// 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 <iostream> +#endif + +#if defined(__BORLANDC__) || defined( __CODEGEARC__) +# pragma pack(push, 1) +#endif + +#include <boost/config.hpp> +#include <boost/predef/detail/endian_compat.h> +#include <boost/endian/conversion.hpp> +#include <boost/type_traits/is_signed.hpp> +#include <boost/cstdint.hpp> +#include <boost/static_assert.hpp> +#include <boost/core/scoped_enum.hpp> +#include <iosfwd> +#include <climits> + +# 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 <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits, + BOOST_SCOPED_ENUM(align) A = align::no> + class endian_buffer; + + // aligned big endian signed integer buffers + typedef endian_buffer<order::big, int8_t, 8, align::yes> big_int8_buf_at; + typedef endian_buffer<order::big, int16_t, 16, align::yes> big_int16_buf_at; + typedef endian_buffer<order::big, int32_t, 32, align::yes> big_int32_buf_at; + typedef endian_buffer<order::big, int64_t, 64, align::yes> big_int64_buf_at; + + // aligned big endian unsigned integer buffers + typedef endian_buffer<order::big, uint8_t, 8, align::yes> big_uint8_buf_at; + typedef endian_buffer<order::big, uint16_t, 16, align::yes> big_uint16_buf_at; + typedef endian_buffer<order::big, uint32_t, 32, align::yes> big_uint32_buf_at; + typedef endian_buffer<order::big, uint64_t, 64, align::yes> big_uint64_buf_at; + + // aligned little endian signed integer buffers + typedef endian_buffer<order::little, int8_t, 8, align::yes> little_int8_buf_at; + typedef endian_buffer<order::little, int16_t, 16, align::yes> little_int16_buf_at; + typedef endian_buffer<order::little, int32_t, 32, align::yes> little_int32_buf_at; + typedef endian_buffer<order::little, int64_t, 64, align::yes> little_int64_buf_at; + + // aligned little endian unsigned integer buffers + typedef endian_buffer<order::little, uint8_t, 8, align::yes> little_uint8_buf_at; + typedef endian_buffer<order::little, uint16_t, 16, align::yes> little_uint16_buf_at; + typedef endian_buffer<order::little, uint32_t, 32, align::yes> little_uint32_buf_at; + typedef endian_buffer<order::little, uint64_t, 64, align::yes> little_uint64_buf_at; + + // aligned native endian typedefs are not provided because + // <cstdint> types are superior for this use case + + // unaligned big endian signed integer buffers + typedef endian_buffer<order::big, int_least8_t, 8> big_int8_buf_t; + typedef endian_buffer<order::big, int_least16_t, 16> big_int16_buf_t; + typedef endian_buffer<order::big, int_least32_t, 24> big_int24_buf_t; + typedef endian_buffer<order::big, int_least32_t, 32> big_int32_buf_t; + typedef endian_buffer<order::big, int_least64_t, 40> big_int40_buf_t; + typedef endian_buffer<order::big, int_least64_t, 48> big_int48_buf_t; + typedef endian_buffer<order::big, int_least64_t, 56> big_int56_buf_t; + typedef endian_buffer<order::big, int_least64_t, 64> big_int64_buf_t; + + // unaligned big endian unsigned integer buffers + typedef endian_buffer<order::big, uint_least8_t, 8> big_uint8_buf_t; + typedef endian_buffer<order::big, uint_least16_t, 16> big_uint16_buf_t; + typedef endian_buffer<order::big, uint_least32_t, 24> big_uint24_buf_t; + typedef endian_buffer<order::big, uint_least32_t, 32> big_uint32_buf_t; + typedef endian_buffer<order::big, uint_least64_t, 40> big_uint40_buf_t; + typedef endian_buffer<order::big, uint_least64_t, 48> big_uint48_buf_t; + typedef endian_buffer<order::big, uint_least64_t, 56> big_uint56_buf_t; + typedef endian_buffer<order::big, uint_least64_t, 64> big_uint64_buf_t; + + // unaligned little endian signed integer buffers + typedef endian_buffer<order::little, int_least8_t, 8> little_int8_buf_t; + typedef endian_buffer<order::little, int_least16_t, 16> little_int16_buf_t; + typedef endian_buffer<order::little, int_least32_t, 24> little_int24_buf_t; + typedef endian_buffer<order::little, int_least32_t, 32> little_int32_buf_t; + typedef endian_buffer<order::little, int_least64_t, 40> little_int40_buf_t; + typedef endian_buffer<order::little, int_least64_t, 48> little_int48_buf_t; + typedef endian_buffer<order::little, int_least64_t, 56> little_int56_buf_t; + typedef endian_buffer<order::little, int_least64_t, 64> little_int64_buf_t; + + // unaligned little endian unsigned integer buffers + typedef endian_buffer<order::little, uint_least8_t, 8> little_uint8_buf_t; + typedef endian_buffer<order::little, uint_least16_t, 16> little_uint16_buf_t; + typedef endian_buffer<order::little, uint_least32_t, 24> little_uint24_buf_t; + typedef endian_buffer<order::little, uint_least32_t, 32> little_uint32_buf_t; + typedef endian_buffer<order::little, uint_least64_t, 40> little_uint40_buf_t; + typedef endian_buffer<order::little, uint_least64_t, 48> little_uint48_buf_t; + typedef endian_buffer<order::little, uint_least64_t, 56> little_uint56_buf_t; + typedef endian_buffer<order::little, uint_least64_t, 64> 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 <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T, + std::size_t n_bits, BOOST_SCOPED_ENUM(align) A> + std::basic_ostream<charT, traits>& + operator<<(std::basic_ostream<charT, traits>& os, + const endian_buffer<Order, T, n_bits, A>& x) + { + return os << x.value(); + } + + // Stream extractor + template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T, + std::size_t n_bits, BOOST_SCOPED_ENUM(align) A> + std::basic_istream<charT, traits>& + operator>>(std::basic_istream<charT, traits>& is, + endian_buffer<Order, T, n_bits, A>& x) + { + T i; + if (is >> i) + x = i; + return is; + } + +//---------------------------------- end synopsis ------------------------------------// + + namespace detail + { + + // Unrolled loops for loading and storing streams of bytes. + + template <typename T, std::size_t n_bytes, + bool sign=boost::is_signed<T>::value > + struct unrolled_byte_loops + { + typedef unrolled_byte_loops<T, n_bytes - 1, sign> next; + + static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT + { return static_cast<T>(*(bytes - 1) | (next::load_big(bytes - 1) << 8)); } + static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT + { return static_cast<T>(*bytes | (next::load_little(bytes + 1) << 8)); } + + static void store_big(char* bytes, T value) BOOST_NOEXCEPT + { + *(bytes - 1) = static_cast<char>(value); + next::store_big(bytes - 1, static_cast<T>(value >> 8)); + } + static void store_little(char* bytes, T value) BOOST_NOEXCEPT + { + *bytes = static_cast<char>(value); + next::store_little(bytes + 1, static_cast<T>(value >> 8)); + } + }; + + template <typename T> + struct unrolled_byte_loops<T, 1, false> + { + 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<char>(value); } + static void store_little(char* bytes, T value) BOOST_NOEXCEPT + { *bytes = static_cast<char>(value); } + + }; + + template <typename T> + struct unrolled_byte_loops<T, 1, true> + { + static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT + { return *reinterpret_cast<const signed char*>(bytes - 1); } + static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT + { return *reinterpret_cast<const signed char*>(bytes); } + static void store_big(char* bytes, T value) BOOST_NOEXCEPT + { *(bytes - 1) = static_cast<char>(value); } + static void store_little(char* bytes, T value) BOOST_NOEXCEPT + { *bytes = static_cast<char>(value); } + }; + + template <typename T, std::size_t n_bytes> + inline + T load_big_endian(const void* bytes) BOOST_NOEXCEPT + { + return unrolled_byte_loops<T, n_bytes>::load_big + (static_cast<const unsigned char*>(bytes) + n_bytes); + } + + template <typename T, std::size_t n_bytes> + 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<T const *>(bytes); + } +# endif + return unrolled_byte_loops<T, n_bytes>::load_little + (static_cast<const unsigned char*>(bytes)); + } + + template <typename T, std::size_t n_bytes> + inline + void store_big_endian(void* bytes, T value) BOOST_NOEXCEPT + { + unrolled_byte_loops<T, n_bytes>::store_big + (static_cast<char*>(bytes) + n_bytes, value); + } + + template <typename T, std::size_t n_bytes> + 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<T *>(bytes) = value; + return; + } +# endif + unrolled_byte_loops<T, n_bytes>::store_little + (static_cast<char*>(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 <typename T, std::size_t n_bits> + 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<T, n_bits/8>(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<T, n_bits/8>(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<T, n_bits/8>(m_value) << ")\n"; +# endif + return detail::load_big_endian<T, n_bits/8>(m_value); + } + const char* data() const BOOST_NOEXCEPT { return m_value; } + protected: + char m_value[n_bits/8]; + }; + + // unaligned little endian_buffer specialization + template <typename T, std::size_t n_bits> + 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<T, n_bits/8>(m_value, val); + } +# endif + endian_buffer & operator=(T val) BOOST_NOEXCEPT + { detail::store_little_endian<T, n_bits/8>(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<T, n_bits/8>(m_value) << ")\n"; +# endif + return detail::load_little_endian<T, n_bits/8>(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 <typename T, std::size_t n_bits> + class endian_buffer<order::big, T, n_bits, align::yes> + { + 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<const char*>(&m_value);} + protected: + T m_value; + }; + + // aligned little endian_buffer specialization + template <typename T, std::size_t n_bits> + class endian_buffer<order::little, T, n_bits, align::yes> + { + 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<const char*>(&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 diff --git a/boost/endian/conversion.hpp b/boost/endian/conversion.hpp new file mode 100644 index 0000000000..1d89596ebf --- /dev/null +++ b/boost/endian/conversion.hpp @@ -0,0 +1,487 @@ +// boost/endian/conversion.hpp -------------------------------------------------------// + +// Copyright Beman Dawes 2010, 2011, 2014 + +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_ENDIAN_CONVERSION_HPP +#define BOOST_ENDIAN_CONVERSION_HPP + +#include <boost/config.hpp> +#include <boost/predef/detail/endian_compat.h> +#include <boost/cstdint.hpp> +#include <boost/endian/detail/intrinsic.hpp> +#include <boost/core/scoped_enum.hpp> +#include <boost/static_assert.hpp> +#include <algorithm> +#include <cstring> // for memcpy + +//------------------------------------- synopsis ---------------------------------------// + +namespace boost +{ +namespace endian +{ +#ifndef BOOST_ENDIAN_ORDER_ENUM_DEFINED + BOOST_SCOPED_ENUM_START(order) + { + big, little, +# ifdef BOOST_BIG_ENDIAN + native = big +# else + native = little +# endif + }; BOOST_SCOPED_ENUM_END +# define BOOST_ENDIAN_ORDER_ENUM_DEFINED +#endif + +//--------------------------------------------------------------------------------------// +// // +// return-by-value interfaces // +// suggested by Phil Endecott // +// // +// user-defined types (UDTs) // +// // +// All return-by-value conversion function templates are required to be implemented in // +// terms of an unqualified call to "endian_reverse(x)", a function returning the // +// value of x with endianness reversed. This provides a customization point for any // +// UDT that provides a "endian_reverse" free-function meeting the requirements. // +// It must be defined in the same namespace as the UDT itself so that it will be found // +// by argument dependent lookup (ADL). // +// // +//--------------------------------------------------------------------------------------// + + // customization for exact-length arithmetic types. See doc/conversion.html/#FAQ + inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT; + inline int16_t endian_reverse(int16_t x) BOOST_NOEXCEPT; + inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT; + inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT; + inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT; + inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT; + inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT; + inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT; + + // reverse byte order unless native endianness is big + template <class EndianReversible > + inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT; + // Returns: x if native endian order is big, otherwise endian_reverse(x) + template <class EndianReversible > + inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT; + // Returns: x if native endian order is big, otherwise endian_reverse(x) + + // reverse byte order unless native endianness is little + template <class EndianReversible > + inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT; + // Returns: x if native endian order is little, otherwise endian_reverse(x) + template <class EndianReversible > + inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT; + // Returns: x if native endian order is little, otherwise endian_reverse(x) + + // generic conditional reverse byte order + template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, + class EndianReversible> + inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT; + // Returns: If From == To have different values, from. + // Otherwise endian_reverse(from). + // Remarks: The From == To test, and as a consequence which form the return takes, is + // is determined at compile time. + + // runtime conditional reverse byte order + template <class EndianReversible > + inline EndianReversible conditional_reverse(EndianReversible from, + BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) + BOOST_NOEXCEPT; + // Returns: from_order == to_order ? from : endian_reverse(from). + + //------------------------------------------------------------------------------------// + + + // Q: What happended to bswap, htobe, and the other synonym functions based on names + // popularized by BSD, OS X, and Linux? + // A: Turned out these may be implemented as macros on some systems. Ditto POSIX names + // for such functionality. Since macros would cause endless problems with functions + // of the same names, and these functions are just synonyms anyhow, they have been + // removed. + + + //------------------------------------------------------------------------------------// + // // + // reverse in place interfaces // + // // + // user-defined types (UDTs) // + // // + // All reverse in place function templates are required to be implemented in terms // + // of an unqualified call to "endian_reverse_inplace(x)", a function reversing // + // the endianness of x, which is a non-const reference. This provides a // + // customization point for any UDT that provides a "reverse_inplace" free-function // + // meeting the requirements. The free-function must be declared in the same // + // namespace as the UDT itself so that it will be found by argument-dependent // + // lookup (ADL). // + // // + //------------------------------------------------------------------------------------// + + // reverse in place + template <class EndianReversible> + inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT; + // Effects: x = endian_reverse(x) + + // reverse in place unless native endianness is big + template <class EndianReversibleInplace> + inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; + // Effects: none if native byte-order is big, otherwise endian_reverse_inplace(x) + template <class EndianReversibleInplace> + inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; + // Effects: none if native byte-order is big, otherwise endian_reverse_inplace(x) + + // reverse in place unless native endianness is little + template <class EndianReversibleInplace> + inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; + // Effects: none if native byte-order is little, otherwise endian_reverse_inplace(x); + template <class EndianReversibleInplace> + inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; + // Effects: none if native byte-order is little, otherwise endian_reverse_inplace(x); + + // generic conditional reverse in place + template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, + class EndianReversibleInplace> + inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; + + // runtime reverse in place + template <class EndianReversibleInplace> + inline void conditional_reverse_inplace(EndianReversibleInplace& x, + BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) + BOOST_NOEXCEPT; + +//----------------------------------- end synopsis -------------------------------------// + + namespace detail + { + // generic reverse function template implementation approach using std::reverse + // suggested by Mathias Gaunard. Primary motivation for inclusion is to have an + // independent implementation to test against. + + template <class T> + inline T std_endian_reverse(T x) BOOST_NOEXCEPT + { + T tmp(x); + std::reverse( + reinterpret_cast<unsigned char*>(&tmp), + reinterpret_cast<unsigned char*>(&tmp) + sizeof(T)); + return tmp; + } + + // conditional unaligned reverse copy, patterned after std::reverse_copy + template <class T> + inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT; + template <class T> + inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT; + template <class T> + inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT; + template <class T> + inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT; + } // namespace detail + +//--------------------------------------------------------------------------------------// +// // +// return-by-value implementation // +// // +// -- portable approach suggested by tymofey, with avoidance of undefined behavior // +// as suggested by Giovanni Piero Deretta, with a further refinement suggested // +// by Pyry Jahkola. // +// -- intrinsic approach suggested by reviewers, and by David Stone, who provided // +// his Boost licensed macro implementation (detail/intrinsic.hpp) // +// // +//--------------------------------------------------------------------------------------// + + inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT + { + return x; + } + + inline int16_t endian_reverse(int16_t x) BOOST_NOEXCEPT + { +# ifdef BOOST_ENDIAN_NO_INTRINSICS + return (static_cast<uint16_t>(x) << 8) + | (static_cast<uint16_t>(x) >> 8); +# else + return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(static_cast<uint16_t>(x)); +# endif + } + + inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT + { +# ifdef BOOST_ENDIAN_NO_INTRINSICS + uint32_t step16; + step16 = static_cast<uint32_t>(x) << 16 | static_cast<uint32_t>(x) >> 16; + return + ((static_cast<uint32_t>(step16) << 8) & 0xff00ff00) + | ((static_cast<uint32_t>(step16) >> 8) & 0x00ff00ff); +# else + return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(static_cast<uint32_t>(x)); +# endif + } + + inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT + { +# ifdef BOOST_ENDIAN_NO_INTRINSICS + uint64_t step32, step16; + step32 = static_cast<uint64_t>(x) << 32 | static_cast<uint64_t>(x) >> 32; + step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 + | (step32 & 0xFFFF0000FFFF0000ULL) >> 16; + return static_cast<int64_t>((step16 & 0x00FF00FF00FF00FFULL) << 8 + | (step16 & 0xFF00FF00FF00FF00ULL) >> 8); +# else + return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(static_cast<uint64_t>(x)); +# endif + } + + inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT + { + return x; + } + + inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT + { +# ifdef BOOST_ENDIAN_NO_INTRINSICS + return (x << 8) + | (x >> 8); +# else + return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x); +# endif + } + + inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT + { +# ifdef BOOST_ENDIAN_NO_INTRINSICS + uint32_t step16; + step16 = x << 16 | x >> 16; + return + ((step16 << 8) & 0xff00ff00) + | ((step16 >> 8) & 0x00ff00ff); +# else + return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x); +# endif + } + + inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT + { +# ifdef BOOST_ENDIAN_NO_INTRINSICS + uint64_t step32, step16; + step32 = x << 32 | x >> 32; + step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 + | (step32 & 0xFFFF0000FFFF0000ULL) >> 16; + return (step16 & 0x00FF00FF00FF00FFULL) << 8 + | (step16 & 0xFF00FF00FF00FF00ULL) >> 8; +# else + return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x); +# endif + } + + template <class EndianReversible > + inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT + { +# ifdef BOOST_BIG_ENDIAN + return x; +# else + return endian_reverse(x); +# endif + } + + template <class EndianReversible > + inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT + { +# ifdef BOOST_BIG_ENDIAN + return x; +# else + return endian_reverse(x); +# endif + } + + template <class EndianReversible > + inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT + { +# ifdef BOOST_LITTLE_ENDIAN + return x; +# else + return endian_reverse(x); +# endif + } + + template <class EndianReversible > + inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT + { +# ifdef BOOST_LITTLE_ENDIAN + return x; +# else + return endian_reverse(x); +# endif + } + + namespace detail + { + // Primary template and specializations to support endian_reverse(). + // See rationale in endian_reverse() below. + template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, + class EndianReversible> + class value_converter ; // primary template + template <class T> class value_converter <order::big, order::big, T> + {public: T operator()(T x) BOOST_NOEXCEPT {return x;}}; + template <class T> class value_converter <order::little, order::little, T> + {public: T operator()(T x) BOOST_NOEXCEPT {return x;}}; + template <class T> class value_converter <order::big, order::little, T> + {public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}}; + template <class T> class value_converter <order::little, order::big, T> + {public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}}; + } + + // generic conditional reverse + template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, + class EndianReversible> + inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT { + // work around lack of function template partial specialization by instantiating + // a function object of a class that is partially specialized on the two order + // template parameters, and then calling its operator(). + detail::value_converter <From, To, EndianReversible> tmp; + return tmp(from); + } + + // runtime conditional reverse + template <class EndianReversible > + inline EndianReversible conditional_reverse(EndianReversible from, + BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT + { + return from_order == to_order ? from : endian_reverse(from); + } + +//--------------------------------------------------------------------------------------// +// reverse-in-place implementation // +//--------------------------------------------------------------------------------------// + + // reverse in place + template <class EndianReversible> + inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT + { + x = endian_reverse(x); + } + + template <class EndianReversibleInplace> +# ifdef BOOST_BIG_ENDIAN + inline void big_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {} +# else + inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT + { endian_reverse_inplace(x); } +# endif + template <class EndianReversibleInplace> +# ifdef BOOST_BIG_ENDIAN + inline void native_to_big_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {} +# else + inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT + { + endian_reverse_inplace(x); + } +# endif + + template <class EndianReversibleInplace> +# ifdef BOOST_LITTLE_ENDIAN + inline void little_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {} +# else + inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT + { endian_reverse_inplace(x); } +# endif + template <class EndianReversibleInplace> +# ifdef BOOST_LITTLE_ENDIAN + inline void native_to_little_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {} +# else + inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT + { + endian_reverse_inplace(x); + } +# endif + + namespace detail + { + // Primary template and specializations support generic + // endian_reverse_inplace(). + // See rationale in endian_reverse_inplace() below. + template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, + class EndianReversibleInplace> + class converter; // primary template + template <class T> class converter<order::big, order::big, T> + {public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}}; + template <class T> class converter<order::little, order::little, T> + {public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}}; + template <class T> class converter<order::big, order::little, T> + {public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }}; + template <class T> class converter<order::little, order::big, T> + {public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }}; + } // namespace detail + + // generic conditional reverse in place + template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, + class EndianReversibleInplace> + inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT + { + // work around lack of function template partial specialization by instantiating + // a function object of a class that is partially specialized on the two order + // template parameters, and then calling its operator(). + detail::converter<From, To, EndianReversibleInplace> tmp; + tmp(x); // call operator () + } + + // runtime reverse in place + template <class EndianReversibleInplace> + inline void conditional_reverse_inplace(EndianReversibleInplace& x, + BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) + BOOST_NOEXCEPT + { + if (from_order != to_order) + endian_reverse_inplace(x); + } + + + namespace detail + { + template <class T> + inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT + { +# ifdef BOOST_BIG_ENDIAN + std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T)); +# else + std::reverse_copy(reinterpret_cast<const char*>(&from), + reinterpret_cast<const char*>(&from) + sizeof(T), to); +# endif + } + template <class T> + inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT + { +# ifdef BOOST_BIG_ENDIAN + std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T)); +# else + std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to)); +# endif + } + template <class T> + inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT + { +# ifdef BOOST_LITTLE_ENDIAN + std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T)); +# else + std::reverse_copy(reinterpret_cast<const char*>(&from), + reinterpret_cast<const char*>(&from) + sizeof(T), to); +# endif + } + template <class T> + inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT + { +# ifdef BOOST_LITTLE_ENDIAN + std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T)); +# else + std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to)); +# endif + } + } // namespace detail +} // namespace endian +} // namespace boost + +#endif // BOOST_ENDIAN_CONVERSION_HPP diff --git a/boost/endian/detail/config.hpp b/boost/endian/detail/config.hpp new file mode 100644 index 0000000000..8ece0a1e9f --- /dev/null +++ b/boost/endian/detail/config.hpp @@ -0,0 +1,62 @@ +// boost/endian/detail/config.hpp ----------------------------------------------------// + +// Copyright Beman Dawes 2003, 2010 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +//--------------------------------------------------------------------------------------// + +#ifndef BOOST_ENDIAN_CONFIG_HPP +#define BOOST_ENDIAN_CONFIG_HPP + +// This header implements separate compilation features as described in +// http://www.boost.org/more/separate_compilation.html + +#include <boost/config.hpp> +#include <boost/system/api_config.hpp> // for BOOST_POSIX_API or BOOST_WINDOWS_API + +// throw an exception ----------------------------------------------------------------// +// +// Exceptions were originally thrown via boost::throw_exception(). +// As throw_exception() became more complex, it caused user error reporting +// to be harder to interpret, since the exception reported became much more complex. +// The immediate fix was to throw directly, wrapped in a macro to make any later change +// easier. + +#define BOOST_ENDIAN_THROW(EX) throw EX + +// enable dynamic linking -------------------------------------------------------------// + +#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_ENDIAN_DYN_LINK) +# if defined(BOOST_ENDIAN_SOURCE) +# define BOOST_ENDIAN_DECL BOOST_SYMBOL_EXPORT +# else +# define BOOST_ENDIAN_DECL BOOST_SYMBOL_IMPORT +# endif +#else +# define BOOST_ENDIAN_DECL +#endif + +// enable automatic library variant selection ----------------------------------------// + +#if !defined(BOOST_ENDIAN_SOURCE) && !defined(BOOST_ALL_NO_LIB) \ + && !defined(BOOST_ENDIAN_NO_LIB) +// +// Set the name of our library, this will get undef'ed by auto_link.hpp +// once it's done with it: +// +#define BOOST_LIB_NAME boost_endian +// +// If we're importing code from a dll, then tell auto_link.hpp about it: +// +#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_ENDIAN_DYN_LINK) +# define BOOST_DYN_LINK +#endif +// +// And include the header that does the work: +// +#include <boost/config/auto_link.hpp> +#endif // auto-linking disabled + +#endif // BOOST_ENDIAN_CONFIG_HPP diff --git a/boost/endian/detail/cover_operators.hpp b/boost/endian/detail/cover_operators.hpp new file mode 100644 index 0000000000..f4fd052803 --- /dev/null +++ b/boost/endian/detail/cover_operators.hpp @@ -0,0 +1,142 @@ +// boost/endian/detail/cover_operators.hpp ----------------------------------// + +// Copyright Darin Adler 2000 +// Copyright Beman Dawes 2008 + +// 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_COVER_OPERATORS_HPP +#define BOOST_ENDIAN_COVER_OPERATORS_HPP + +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable:4365) // conversion ... signed/unsigned mismatch +#endif + +# ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS +# include <boost/operators.hpp> +# endif + +#include <boost/config.hpp> +#include <iosfwd> + +namespace boost +{ + namespace endian + { + +//--------------------------------------------------------------------------------------// + +// A class that adds arithmetic operators to an arithmetic cover class +// +// Uses the curiously recurring template pattern (CRTP). +// +// If the class being covered has a non-explicit conversion to an integer type +// then a smaller number of cover operations are needed. Define the macro +// BOOST_ENDIAN_MINIMAL_COVER_OPERATORS to indicate this. +// +// Define BOOST_NO_IO_COVER_OPERATORS if I/O cover operations are not desired. + +//--------------------------------------------------------------------------------------// + + template <class D, // D is the CRTP derived type, i.e. the cover class + class ArithmeticT> + class cover_operators +# ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS + : boost::operators<D> +# endif + { + // The other operations take advantage of the type conversion that's + // built into unary +. + + // Unary operations. + friend ArithmeticT operator+(const D& x) BOOST_NOEXCEPT { return x; } +# ifndef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS + friend ArithmeticT operator-(const D& x) BOOST_NOEXCEPT { return -+x; } + friend ArithmeticT operator~(const D& x) BOOST_NOEXCEPT { return ~+x; } + friend ArithmeticT operator!(const D& x) BOOST_NOEXCEPT { return !+x; } + + // The basic ordering operations. + friend bool operator==(const D& x, ArithmeticT y) BOOST_NOEXCEPT { return +x == y; } + friend bool operator<(const D& x, ArithmeticT y) BOOST_NOEXCEPT { return +x < y; } +# endif + + // The basic arithmetic operations. + friend D& operator+=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x + y); } + friend D& operator-=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x - y); } + friend D& operator*=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x * y); } + friend D& operator/=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x / y); } + friend D& operator%=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x % y); } + friend D& operator&=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x & y); } + friend D& operator|=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x | y); } + friend D& operator^=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x ^ y); } + friend D& operator<<=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x << y); } + friend D& operator>>=(D& x, ArithmeticT y) BOOST_NOEXCEPT + { return x = static_cast<ArithmeticT>(+x >> y); } + + // A few binary arithmetic operations not covered by operators base class. + friend ArithmeticT operator<<(const D& x, ArithmeticT y) BOOST_NOEXCEPT + { return static_cast<ArithmeticT>(+x << y); } + friend ArithmeticT operator>>(const D& x, ArithmeticT y) BOOST_NOEXCEPT + { return static_cast<ArithmeticT>(+x >> y); } + + // Auto-increment and auto-decrement can be defined in terms of the + // arithmetic operations. + friend D& operator++(D& x) BOOST_NOEXCEPT { return x += 1; } + friend D& operator--(D& x) BOOST_NOEXCEPT { return x -= 1; } + +# ifdef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS + friend D operator++(D& x, int) BOOST_NOEXCEPT + { + D tmp(x); + x += 1; + return tmp; + } + friend D operator--(D& x, int) BOOST_NOEXCEPT + { + D tmp(x); + x -= 1; + return tmp; + } +# endif + +# ifndef BOOST_NO_IO_COVER_OPERATORS + + // Stream inserter + template <class charT, class traits> + friend std::basic_ostream<charT, traits>& + operator<<(std::basic_ostream<charT, traits>& os, const D& x) + { + return os << +x; + } + + // Stream extractor + template <class charT, class traits> + friend std::basic_istream<charT, traits>& + operator>>(std::basic_istream<charT, traits>& is, D& x) + { + ArithmeticT i; + if (is >> i) + x = i; + return is; + } +# endif + }; + } // namespace endian +} // namespace boost + +#if defined(_MSC_VER) +# pragma warning(pop) +#endif + +#endif // BOOST_ENDIAN_COVER_OPERATORS_HPP diff --git a/boost/endian/detail/disable_warnings.hpp b/boost/endian/detail/disable_warnings.hpp new file mode 100644 index 0000000000..082db8abfe --- /dev/null +++ b/boost/endian/detail/disable_warnings.hpp @@ -0,0 +1,33 @@ +// disable_warnings.hpp --------------------------------------------------------------// + +// Copyright Beman Dawes 2011 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +//--------------------------------------------------------------------------------------// + +#ifdef _MSC_VER + +#ifndef _SCL_SECURE_NO_WARNINGS +# define _SCL_SECURE_NO_WARNINGS +#endif + +#ifndef _CRT_SECURE_NO_WARNINGS +# define _CRT_SECURE_NO_WARNINGS +#endif + +# pragma warning(push) + +// triggered by boost/detail/lightweight_test.hpp +# pragma warning( disable : 4640 ) // ... construction of local static object is not thread-safe + +// triggered by Microsoft's own headers, so disable +# pragma warning( disable : 4820 ) // padding added after data member +# pragma warning( disable : 4548 ) // expression before comma has no effect +# pragma warning( disable : 4668 ) // ... is not defined as a preprocessor macro +# pragma warning( disable : 4514 ) // ... unreferenced inline function has been removed +# pragma warning( disable : 4710 ) // ... function not inlined +# pragma warning( disable : 4986 ) // ... exception specification does not match previous declaration +# pragma warning( disable : 4711 ) // ... selected for automatic inline expansion +#endif diff --git a/boost/endian/detail/disable_warnings_pop.hpp b/boost/endian/detail/disable_warnings_pop.hpp new file mode 100644 index 0000000000..0ebc74a452 --- /dev/null +++ b/boost/endian/detail/disable_warnings_pop.hpp @@ -0,0 +1,12 @@ +// disable_warnings_pop.hpp ----------------------------------------------------------// + +// Copyright Beman Dawes 2011 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +//--------------------------------------------------------------------------------------// + +#ifdef _MSC_VER +# pragma warning(push) +#endif diff --git a/boost/endian/detail/intrinsic.hpp b/boost/endian/detail/intrinsic.hpp new file mode 100644 index 0000000000..6ead6813a2 --- /dev/null +++ b/boost/endian/detail/intrinsic.hpp @@ -0,0 +1,64 @@ +// endian/detail/intrinsic.hpp -------------------------------------------------------// + +// Copyright (C) 2012 David Stone +// Copyright Beman Dawes 2013 + +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_ENDIAN_INTRINSIC_HPP +#define BOOST_ENDIAN_INTRINSIC_HPP + +// Allow user to force BOOST_ENDIAN_NO_INTRINSICS in case they aren't available for a +// particular platform/compiler combination. Please report such platform/compiler +// combinations to the Boost mailing list. +#ifndef BOOST_ENDIAN_NO_INTRINSICS + +#ifndef __has_builtin // Optional of course + #define __has_builtin(x) 0 // Compatibility with non-clang compilers +#endif + +// GCC and Clang recent versions provide intrinsic byte swaps via builtins +#if (defined(__clang__) && __has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)) \ + || (defined(__GNUC__ ) && \ + (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) +# define BOOST_ENDIAN_INTRINSIC_MSG "__builtin_bswap16, etc." +// prior to 4.8, gcc did not provide __builtin_bswap16 on some platforms so we emulate it +// see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624 +// Clang has a similar problem, but their feature test macros make it easier to detect +# if (defined(__clang__) && __has_builtin(__builtin_bswap16)) \ + || (defined(__GNUC__) &&(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap16(x) +# else +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap32((x) << 16) +# endif +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) __builtin_bswap32(x) +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) __builtin_bswap64(x) + +// Linux systems provide the byteswap.h header, with +#elif defined(__linux__) +// don't check for obsolete forms defined(linux) and defined(__linux) on the theory that +// compilers that predefine only these are so old that byteswap.h probably isn't present. +# define BOOST_ENDIAN_INTRINSIC_MSG "byteswap.h bswap_16, etc." +# include <byteswap.h> +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) bswap_16(x) +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) bswap_32(x) +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) bswap_64(x) + +#elif defined(_MSC_VER) +// Microsoft documents these as being compatible since Windows 95 and specificly +// lists runtime library support since Visual Studio 2003 (aka 7.1). +# define BOOST_ENDIAN_INTRINSIC_MSG "cstdlib _byteswap_ushort, etc." +# include <cstdlib> +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) _byteswap_ushort(x) +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) _byteswap_ulong(x) +# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) _byteswap_uint64(x) +#else +# define BOOST_ENDIAN_NO_INTRINSICS +# define BOOST_ENDIAN_INTRINSIC_MSG "no byte swap intrinsics" +#endif + +#elif !defined(BOOST_ENDIAN_INTRINSIC_MSG) +# define BOOST_ENDIAN_INTRINSIC_MSG "no byte swap intrinsics" +#endif // BOOST_ENDIAN_NO_INTRINSICS +#endif // BOOST_ENDIAN_INTRINSIC_HPP diff --git a/boost/endian/detail/lightweight_test.hpp b/boost/endian/detail/lightweight_test.hpp new file mode 100644 index 0000000000..eba315c954 --- /dev/null +++ b/boost/endian/detail/lightweight_test.hpp @@ -0,0 +1,223 @@ +// boost/endian/detail/lightweight_test.hpp --------------------------------------------// + +#ifndef BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP +#define BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP + +// MS compatible compilers support #pragma once + +#if defined(_MSC_VER) +# pragma once +#endif + +// +// Copyright (c) 2002, 2009, 2014 Peter Dimov +// Copyright (2) Beman Dawes 2010, 2011, 2015 +// Copyright (3) Ion Gaztanaga 2013 +// +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt +// + +#include <boost/assert.hpp> +#include <boost/current_function.hpp> +#include <boost/core/no_exceptions_support.hpp> +#include <cstring> // for memcmp +#include <iostream> + +// IDE's like Visual Studio perform better if output goes to std::cout or +// some other stream, so allow user to configure output stream: +#ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM +# define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr +#endif + +namespace boost +{ +namespace endian +{ +namespace detail +{ + +struct report_errors_reminder +{ + bool called_report_errors_function; + + report_errors_reminder() : called_report_errors_function(false) {} + + ~report_errors_reminder() + { + BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called + } +}; + +inline report_errors_reminder& report_errors_remind() +{ + static report_errors_reminder r; + return r; +} + +inline int & test_errors() +{ + static int x = 0; + report_errors_remind(); + return x; +} + +inline void test_failed_impl(char const * expr, char const * file, int line, char const * function) +{ + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): test '" << expr << "' failed in function '" + << function << "'" << std::endl; + ++test_errors(); +} + +inline void error_impl(char const * msg, char const * file, int line, char const * function) +{ + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): " << msg << " in function '" + << function << "'" << std::endl; + ++test_errors(); +} + +inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function) +{ + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): Exception '" << excep << "' not thrown in function '" + << function << "'" << std::endl; + ++test_errors(); +} + +template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2, + char const * file, int line, char const * function, T const & t, U const & u ) +{ + if( t == u ) + { + report_errors_remind(); + } + else + { + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): test '" << expr1 << " == " << expr2 + << "' failed in function '" << function << "': " + << "'" << t << "' != '" << u << "'" << std::endl; + ++test_errors(); + } +} + +template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2, + char const * file, int line, char const * function, T const & t, U const & u ) +{ + if( t != u ) + { + report_errors_remind(); + } + else + { + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): test '" << expr1 << " != " << expr2 + << "' failed in function '" << function << "': " + << "'" << t << "' == '" << u << "'" << std::endl; + ++test_errors(); + } +} + +template <class T> +std::string to_hex(const T& x) +{ + const char hex[] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; + std::string tmp; + const unsigned char* p = reinterpret_cast<const unsigned char*>(&x); + const unsigned char* e = p + sizeof(T); + + for (; p < e; ++p) + { + tmp += hex[*p >> 4]; // high-order nibble + tmp += hex[*p & 0x0f]; // low-order nibble + } + return tmp; +} + +template<class T, class U> inline bool test_memcmp_eq_impl(char const * expr1, + char const * expr2, char const * file, int line, char const * function, T const & t, + U const & u) +{ + BOOST_ASSERT(sizeof(T) == sizeof(U)); + if (sizeof(T) == sizeof(U) + && std::memcmp(&t, &u, sizeof(T)) == 0) + { + report_errors_remind(); + return true; + } + else + { + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): test 'std::memcmp(" << expr1 << ", " << expr2 + << ") == 0' fails in function '" << function << "': " + << " with values '" << to_hex(t) << "' and '" << to_hex(u) << "'" << std::endl; + ++test_errors(); + return false; + } +} + +} // namespace detail + +inline int report_errors() +{ + boost::endian::detail::report_errors_remind().called_report_errors_function = true; + + int errors = boost::endian::detail::test_errors(); + + if( errors == 0 ) + { + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << "No errors detected." << std::endl; + return 0; + } + else + { + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl; + return 1; + } +} + +} // namespace endian +} // namespace boost + +////////////////////////////////////////////////////////////////////////////////////////// +// TODO: Should all test macros return bool? See BOOST_TEST_MEM_EQ usage in fp_exaustive_test,cpp +////////////////////////////////////////////////////////////////////////////////////////// + + +#define BOOST_TEST(expr) \ + ((expr)? (void)0: ::boost::endian::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)) + +#define BOOST_ERROR(msg) \ + ( ::boost::endian::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) ) + +#define BOOST_TEST_EQ(expr1,expr2) \ + ( ::boost::endian::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) +#define BOOST_TEST_NE(expr1,expr2) \ + ( ::boost::endian::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) + +#define BOOST_TEST_MEM_EQ(expr1,expr2) \ + (::boost::endian::detail::test_memcmp_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2)) + +#ifndef BOOST_NO_EXCEPTIONS + #define BOOST_TEST_THROWS( EXPR, EXCEP ) \ + try { \ + EXPR; \ + ::boost::detail::throw_failed_impl \ + (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \ + } \ + catch(EXCEP const&) { \ + } \ + catch(...) { \ + ::boost::detail::throw_failed_impl \ + (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \ + } \ + // +#else + #define BOOST_TEST_THROWS( EXPR, EXCEP ) +#endif + +#endif // #ifndef BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP diff --git a/boost/endian/endian.hpp b/boost/endian/endian.hpp new file mode 100644 index 0000000000..6c610c4a9a --- /dev/null +++ b/boost/endian/endian.hpp @@ -0,0 +1,19 @@ +// boost/endian/endian.hpp -----------------------------------------------------------// + +// Copyright Beman Dawes 2015 + +// 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 + +#ifndef BOOST_ENDIAN_ENDIAN_HPP +#define BOOST_ENDIAN_ENDIAN_HPP + +#ifndef BOOST_ENDIAN_DEPRECATED_NAMES +# error "<boost/endian/endian.hpp> is deprecated. Define BOOST_ENDIAN_DEPRECATED_NAMES to use." +#endif + +#include <boost/endian/arithmetic.hpp> + +#endif //BOOST_ENDIAN_ENDIAN_HPP diff --git a/boost/endian/std_pair.hpp b/boost/endian/std_pair.hpp new file mode 100644 index 0000000000..8da74b4f70 --- /dev/null +++ b/boost/endian/std_pair.hpp @@ -0,0 +1,38 @@ +// boost/endian/std_pair.hpp ---------------------------------------------------------// + +// Copyright Beman Dawes 2013 + +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +//--------------------------------------------------------------------------------------// + +#ifndef BOOST_ENDIAN_STD_PAIR_HPP +#define BOOST_ENDIAN_STD_PAIR_HPP + +#include <boost/endian/conversion.hpp> +#include <utility> + +namespace boost +{ +namespace endian +{ + template <class ReversibleValueT, class ReversibleValueU> + std::pair<ReversibleValueT, ReversibleValueU> + reverse_value(std::pair<ReversibleValueT, ReversibleValueU> x) + { + return std::pair<ReversibleValueT, ReversibleValueU>(reverse_value(x.first), + reverse_value(x.second)); + } + + template <class ReversibleT, class ReversibleU> + void reverse(std::pair<ReversibleT, ReversibleU>& x) + { + reverse(x.first); + reverse(x.second); + } + +} +} + +#endif // BOOST_ENDIAN_STD_PAIR_HPP |