update boost

This commit is contained in:
2023-11-24 12:56:13 -06:00
parent cfc99971af
commit 19d727037a
9260 changed files with 849256 additions and 299957 deletions

View File

@@ -2,6 +2,7 @@
// (C) Copyright Darin Adler 2000
// (C) Copyright Beman Dawes 2006, 2009, 2014
// (C) Copyright Peter Dimov 2019
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
@@ -26,29 +27,20 @@
# 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/endian/detail/requires_cxx11.hpp>
#include <boost/endian/buffers.hpp>
#include <boost/core/scoped_enum.hpp>
#include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <boost/predef/other/endian.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 defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
# pragma pack(push, 1)
#endif
# if CHAR_BIT != 8
# error Platforms with CHAR_BIT != 8 are not supported
# endif
@@ -78,7 +70,7 @@ namespace endian
{
template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
BOOST_SCOPED_ENUM(align) A = align::no>
BOOST_SCOPED_ENUM(align) Align = align::no>
class endian_arithmetic;
// big endian signed integer aligned types
@@ -105,6 +97,12 @@ namespace endian
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 floating point types
typedef endian_arithmetic<order::big, float, 32, align::yes> big_float32_at;
typedef endian_arithmetic<order::big, double, 64, align::yes> big_float64_at;
typedef endian_arithmetic<order::little, float, 32, align::yes> little_float32_at;
typedef endian_arithmetic<order::little, double, 64, align::yes> little_float64_at;
// aligned native endian typedefs are not provided because
// <cstdint> types are superior for this use case
@@ -148,263 +146,221 @@ namespace endian
typedef endian_arithmetic<order::little, uint_least64_t, 56> little_uint56_t;
typedef endian_arithmetic<order::little, uint_least64_t, 64> little_uint64_t;
# if BOOST_ENDIAN_BIG_BYTE
// 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;
typedef endian_arithmetic<order::native, int_least8_t, 8> native_int8_t;
typedef endian_arithmetic<order::native, int_least16_t, 16> native_int16_t;
typedef endian_arithmetic<order::native, int_least32_t, 24> native_int24_t;
typedef endian_arithmetic<order::native, int_least32_t, 32> native_int32_t;
typedef endian_arithmetic<order::native, int_least64_t, 40> native_int40_t;
typedef endian_arithmetic<order::native, int_least64_t, 48> native_int48_t;
typedef endian_arithmetic<order::native, int_least64_t, 56> native_int56_t;
typedef endian_arithmetic<order::native, int_least64_t, 64> 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;
typedef endian_arithmetic<order::native, uint_least8_t, 8> native_uint8_t;
typedef endian_arithmetic<order::native, uint_least16_t, 16> native_uint16_t;
typedef endian_arithmetic<order::native, uint_least32_t, 24> native_uint24_t;
typedef endian_arithmetic<order::native, uint_least32_t, 32> native_uint32_t;
typedef endian_arithmetic<order::native, uint_least64_t, 40> native_uint40_t;
typedef endian_arithmetic<order::native, uint_least64_t, 48> native_uint48_t;
typedef endian_arithmetic<order::native, uint_least64_t, 56> native_uint56_t;
typedef endian_arithmetic<order::native, uint_least64_t, 64> native_uint64_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
// unaligned floating point types
typedef endian_arithmetic<order::big, float, 32, align::no> big_float32_t;
typedef endian_arithmetic<order::big, double, 64, align::no> big_float64_t;
typedef endian_arithmetic<order::little, float, 32, align::no> little_float32_t;
typedef endian_arithmetic<order::little, double, 64, align::no> little_float64_t;
typedef endian_arithmetic<order::native, float, 32, align::no> native_float32_t;
typedef endian_arithmetic<order::native, double, 64, align::no> native_float64_t;
//---------------------------------- end synopsis ------------------------------------//
// endian class template specializations ---------------------------------------------//
template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
BOOST_SCOPED_ENUM(align) Align>
class endian_arithmetic
{
private:
// 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.
typedef endian_buffer<Order, T, n_bits, Align> buffer_type;
// 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>
#ifdef BOOST_ENDIAN_NO_CTORS
public:
#else
private:
#endif
buffer_type buf_;
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: buf_( val )
{
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 >
#endif
endian_arithmetic& operator=( T val ) BOOST_NOEXCEPT
{
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(); }
};
buf_ = val;
return *this;
}
// 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>
value_type value() const BOOST_NOEXCEPT
{
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);
}
return buf_.value();
}
# 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>
unsigned char const * data() const BOOST_NOEXCEPT
{
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
return buf_.data();
}
unsigned char * data() BOOST_NOEXCEPT
{
return buf_.data();
}
operator value_type() const BOOST_NOEXCEPT
{
return this->value();
}
operator buffer_type& () BOOST_NOEXCEPT
{
return buf_;
}
operator buffer_type const& () BOOST_NOEXCEPT
{
return buf_;
}
// operators
T operator+() const BOOST_NOEXCEPT
{
return this->value();
}
endian_arithmetic& operator+=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() + y );
return *this;
}
endian_arithmetic& operator-=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() - y );
return *this;
}
endian_arithmetic& operator*=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() * y );
return *this;
}
endian_arithmetic& operator/=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() / y );
return *this;
}
endian_arithmetic& operator%=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() % y );
return *this;
}
endian_arithmetic& operator&=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() & y );
return *this;
}
endian_arithmetic& operator|=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() | y );
return *this;
}
endian_arithmetic& operator^=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() ^ y );
return *this;
}
endian_arithmetic& operator<<=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() << y );
return *this;
}
endian_arithmetic& operator>>=( T y ) BOOST_NOEXCEPT
{
*this = static_cast<T>( this->value() >> y );
return *this;
}
endian_arithmetic& operator++() BOOST_NOEXCEPT
{
*this += 1;
return *this;
}
endian_arithmetic& operator--() BOOST_NOEXCEPT
{
*this -= 1;
return *this;
}
endian_arithmetic operator++(int) BOOST_NOEXCEPT
{
endian_arithmetic tmp( *this );
*this += 1;
return tmp;
}
endian_arithmetic operator--(int) BOOST_NOEXCEPT
{
endian_arithmetic tmp( *this );
*this -= 1;
return tmp;
}
template<class Ch, class Tr>
friend std::basic_ostream<Ch, Tr>&
operator<<( std::basic_ostream<Ch, Tr>& os, endian_arithmetic const& x )
{
return os << x.value();
}
template<class Ch, class Tr>
friend std::basic_istream<Ch, Tr>&
operator>>( std::basic_istream<Ch, Tr>& is, endian_arithmetic& x )
{
T i;
if( is >> i )
{
# 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);
x = i;
}
# 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(); }
};
return is;
}
};
} // namespace endian
} // namespace boost
#if defined(__BORLANDC__) || defined( __CODEGEARC__)
#if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
# pragma pack(pop)
#endif

View File

@@ -2,6 +2,7 @@
// (C) Copyright Darin Adler 2000
// (C) Copyright Beman Dawes 2006, 2009, 2014
// (C) Copyright Peter Dimov 2019
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
@@ -26,30 +27,22 @@
# pragma warning(disable: 4127) // conditional expression is constant
#endif
#ifdef BOOST_ENDIAN_LOG
# include <iostream>
#endif
#if defined(__BORLANDC__) || defined( __CODEGEARC__)
# pragma pack(push, 1)
#endif
#include <boost/endian/detail/requires_cxx11.hpp>
#include <boost/endian/detail/endian_store.hpp>
#include <boost/endian/detail/endian_load.hpp>
#include <boost/core/scoped_enum.hpp>
#include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <boost/predef/other/endian.h>
#include <boost/endian/conversion.hpp>
#include <boost/type_traits/is_signed.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/type_traits/conditional.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/type_identity.hpp>
#include <boost/cstdint.hpp>
#include <boost/static_assert.hpp>
#include <boost/core/scoped_enum.hpp>
#include <iosfwd>
#include <climits>
#include <cstring>
#if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
# pragma pack(push, 1)
#endif
# if CHAR_BIT != 8
# error Platforms with CHAR_BIT != 8 are not supported
# endif
@@ -107,6 +100,12 @@ namespace endian
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 floating point buffers
typedef endian_buffer<order::big, float, 32, align::yes> big_float32_buf_at;
typedef endian_buffer<order::big, double, 64, align::yes> big_float64_buf_at;
typedef endian_buffer<order::little, float, 32, align::yes> little_float32_buf_at;
typedef endian_buffer<order::little, double, 64, align::yes> little_float64_buf_at;
// aligned native endian typedefs are not provided because
// <cstdint> types are superior for this use case
@@ -150,47 +149,33 @@ namespace endian
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;
# if BOOST_ENDIAN_BIG_BYTE
// 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;
typedef endian_buffer<order::native, int_least8_t, 8> native_int8_buf_t;
typedef endian_buffer<order::native, int_least16_t, 16> native_int16_buf_t;
typedef endian_buffer<order::native, int_least32_t, 24> native_int24_buf_t;
typedef endian_buffer<order::native, int_least32_t, 32> native_int32_buf_t;
typedef endian_buffer<order::native, int_least64_t, 40> native_int40_buf_t;
typedef endian_buffer<order::native, int_least64_t, 48> native_int48_buf_t;
typedef endian_buffer<order::native, int_least64_t, 56> native_int56_buf_t;
typedef endian_buffer<order::native, int_least64_t, 64> 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;
typedef endian_buffer<order::native, uint_least8_t, 8> native_uint8_buf_t;
typedef endian_buffer<order::native, uint_least16_t, 16> native_uint16_buf_t;
typedef endian_buffer<order::native, uint_least32_t, 24> native_uint24_buf_t;
typedef endian_buffer<order::native, uint_least32_t, 32> native_uint32_buf_t;
typedef endian_buffer<order::native, uint_least64_t, 40> native_uint40_buf_t;
typedef endian_buffer<order::native, uint_least64_t, 48> native_uint48_buf_t;
typedef endian_buffer<order::native, uint_least64_t, 56> native_uint56_buf_t;
typedef endian_buffer<order::native, uint_least64_t, 64> native_uint64_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
// unaligned floating point buffers
typedef endian_buffer<order::big, float, 32, align::no> big_float32_buf_t;
typedef endian_buffer<order::big, double, 64, align::no> big_float64_buf_t;
typedef endian_buffer<order::little, float, 32, align::no> little_float32_buf_t;
typedef endian_buffer<order::little, double, 64, align::no> little_float64_buf_t;
typedef endian_buffer<order::native, float, 32, align::no> native_float32_buf_t;
typedef endian_buffer<order::native, double, 64, align::no> native_float64_buf_t;
// Stream inserter
template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T,
@@ -217,328 +202,175 @@ namespace endian
//---------------------------------- 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;
// shifting a negative number is flagged by -fsanitize=undefined
// so use the corresponding unsigned type for the shifts
typedef typename boost::conditional<
boost::is_integral<T>::value,
boost::make_unsigned<T>, boost::type_identity<T> >::type::type U;
static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT
{ return static_cast<T>(*(bytes - 1) | (static_cast<U>(next::load_big(bytes - 1)) << 8)); }
static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT
{ return static_cast<T>(*bytes | (static_cast<U>(next::load_little(bytes + 1)) << 8)); }
static void store_big(unsigned char* bytes, T value) BOOST_NOEXCEPT
{
*(bytes - 1) = static_cast<unsigned char>(value);
next::store_big(bytes - 1, static_cast<T>(static_cast<U>(value) >> 8));
}
static void store_little(unsigned char* bytes, T value) BOOST_NOEXCEPT
{
*bytes = static_cast<unsigned char>(value);
next::store_little(bytes + 1, static_cast<T>(static_cast<U>(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(unsigned char* bytes, T value) BOOST_NOEXCEPT
{ *(bytes - 1) = static_cast<unsigned char>(value); }
static void store_little(unsigned char* bytes, T value) BOOST_NOEXCEPT
{ *bytes = static_cast<unsigned 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(unsigned char* bytes, T value) BOOST_NOEXCEPT
{ *(bytes - 1) = static_cast<unsigned char>(value); }
static void store_little(unsigned char* bytes, T value) BOOST_NOEXCEPT
{ *bytes = static_cast<unsigned char>(value); }
};
template <typename T, std::size_t n_bytes>
inline
T load_big_endian(const void* bytes) BOOST_NOEXCEPT
{
# if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
// All major x86 compilers elide this test and optimize out memcpy
// (the x86 architecture allows unaligned loads, but -fsanitize=undefined does not)
if (sizeof(T) == n_bytes)
{
T t;
std::memcpy( &t, bytes, sizeof(T) );
return boost::endian::big_to_native(t);
}
# endif
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)
// All major x86 compilers elide this test and optimize out memcpy
// (the x86 architecture allows unaligned loads, but -fsanitize=undefined does not)
if (sizeof(T) == n_bytes)
{
T t;
std::memcpy( &t, bytes, sizeof(T) );
return t; // or endian::little_to_native(t) if we ever extend the #ifdef to non-x86
}
# 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
{
# if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
// All major x86 compilers elide this test and optimize out memcpy
// (the x86 architecture allows unaligned loads, but -fsanitize=undefined does not)
if (sizeof(T) == n_bytes)
{
boost::endian::native_to_big_inplace(value);
std::memcpy( bytes, &value, sizeof(T) );
return;
}
# endif
unrolled_byte_loops<T, n_bytes>::store_big
(static_cast<unsigned 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)
// All major x86 compilers elide this test and optimize out memcpy
// (the x86 architecture allows unaligned loads, but -fsanitize=undefined does not)
if (sizeof(T) == n_bytes)
{
// if we ever extend the #ifdef to non-x86:
// endian::native_to_little_inplace(value);
std::memcpy( bytes, &value, sizeof(T) );
return;
}
# endif
unrolled_byte_loops<T, n_bytes>::store_little
(static_cast<unsigned 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.
// 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.
// 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 >
// unaligned endian_buffer specialization
template< BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits >
class endian_buffer<Order, T, n_bits, align::no>
{
#ifdef BOOST_ENDIAN_NO_CTORS
public:
#endif
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
unsigned char value_[ 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
{
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];
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
}
#endif
endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
return *this;
}
value_type value() const BOOST_NOEXCEPT
{
return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
}
unsigned char const * data() const BOOST_NOEXCEPT
{
return value_;
}
unsigned char * data() BOOST_NOEXCEPT
{
return value_;
}
};
// aligned specializations; only n_bits == 16/32/64 supported
// aligned endian_buffer specialization
template< BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits >
class endian_buffer<Order, T, n_bits, align::yes>
{
private:
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
union
{
unsigned char value_[ n_bits / 8 ];
T align_;
};
// unaligned little endian_buffer specialization
template <typename T, std::size_t n_bits>
class endian_buffer< order::little, T, n_bits, align::no >
public:
typedef T value_type;
#ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer( T val ) BOOST_NOEXCEPT
{
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];
};
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
}
// align::yes specializations; only n_bits == 16/32/64 supported
#endif
// aligned big endian_buffer specialization
template <typename T, std::size_t n_bits>
class endian_buffer<order::big, T, n_bits, align::yes>
endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
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);
}
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
return *this;
}
# 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>
value_type value() const BOOST_NOEXCEPT
{
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);
}
return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
}
# 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;
};
unsigned char const * data() const BOOST_NOEXCEPT
{
return value_;
}
unsigned char * data() BOOST_NOEXCEPT
{
return value_;
}
};
// aligned native endian_buffer specialization
template< class T, std::size_t n_bits >
class endian_buffer<order::native, T, n_bits, align::yes>
{
private:
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
T value_;
public:
typedef T value_type;
#ifndef BOOST_ENDIAN_NO_CTORS
endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
explicit endian_buffer( T val ) BOOST_NOEXCEPT: value_( val )
{
}
#endif
endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
value_ = val;
return *this;
}
value_type value() const BOOST_NOEXCEPT
{
return value_;
}
unsigned char const * data() const BOOST_NOEXCEPT
{
return reinterpret_cast< unsigned char const* >( &value_ );
}
unsigned char * data() BOOST_NOEXCEPT
{
return reinterpret_cast< unsigned char* >( &value_ );
}
};
} // namespace endian
} // namespace boost
#if defined(__BORLANDC__) || defined( __CODEGEARC__)
#if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
# pragma pack(pop)
#endif

View File

@@ -8,14 +8,17 @@
#ifndef BOOST_ENDIAN_CONVERSION_HPP
#define BOOST_ENDIAN_CONVERSION_HPP
#include <boost/config.hpp>
#include <boost/predef/other/endian.h>
#include <boost/cstdint.hpp>
#include <boost/endian/detail/intrinsic.hpp>
#include <boost/core/scoped_enum.hpp>
#include <boost/endian/detail/requires_cxx11.hpp>
#include <boost/endian/detail/endian_reverse.hpp>
#include <boost/endian/detail/endian_load.hpp>
#include <boost/endian/detail/endian_store.hpp>
#include <boost/endian/detail/order.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/type_traits/is_array.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/static_assert.hpp>
#include <algorithm>
#include <cstring> // for memcpy
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
//------------------------------------- synopsis ---------------------------------------//
@@ -23,15 +26,6 @@ namespace boost
{
namespace endian
{
BOOST_SCOPED_ENUM_START(order)
{
big, little,
# if BOOST_ENDIAN_BIG_BYTE
native = big
# else
native = little
# endif
}; BOOST_SCOPED_ENUM_END
//--------------------------------------------------------------------------------------//
// //
@@ -49,40 +43,32 @@ namespace endian
// //
//--------------------------------------------------------------------------------------//
// customization for exact-length arithmetic types. See doc/conversion.html/#FAQ.
// Note: The omission of a overloads for the arithmetic type (typically long, or
// long long) not assigned to one of the exact length typedefs is a deliberate
// design decision. Such overloads would be non-portable and thus error prone.
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
// requires T to be a non-bool integral type
// in detail/endian_reverse.hpp
//
// template<class T> inline BOOST_CONSTEXPR T endian_reverse( T x ) BOOST_NOEXCEPT;
// reverse byte order unless native endianness is big
template <class EndianReversible >
inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT;
inline BOOST_CONSTEXPR 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;
inline BOOST_CONSTEXPR 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;
inline BOOST_CONSTEXPR 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;
inline BOOST_CONSTEXPR 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;
inline BOOST_CONSTEXPR 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
@@ -90,7 +76,7 @@ namespace endian
// runtime conditional reverse byte order
template <class EndianReversible >
inline EndianReversible conditional_reverse(EndianReversible from,
inline BOOST_CONSTEXPR 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).
@@ -123,9 +109,12 @@ namespace endian
//------------------------------------------------------------------------------------//
// reverse in place
template <class EndianReversible>
inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT;
// Effects: x = endian_reverse(x)
// in detail/endian_reverse.hpp
//
// 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>
@@ -156,335 +145,446 @@ namespace endian
//----------------------------------- 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 EndianReversible>
inline BOOST_CONSTEXPR EndianReversible big_to_native( EndianReversible x ) BOOST_NOEXCEPT
{
return boost::endian::conditional_reverse<order::big, order::native>( x );
}
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;
}
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible native_to_big( EndianReversible x ) BOOST_NOEXCEPT
{
return boost::endian::conditional_reverse<order::native, order::big>( x );
}
// 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
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible little_to_native( EndianReversible x ) BOOST_NOEXCEPT
{
return boost::endian::conditional_reverse<order::little, order::native>( x );
}
//--------------------------------------------------------------------------------------//
// //
// 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) //
// //
//--------------------------------------------------------------------------------------//
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible native_to_little( EndianReversible x ) BOOST_NOEXCEPT
{
return boost::endian::conditional_reverse<order::native, order::little>( x );
}
inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT
{
namespace detail
{
template<class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible conditional_reverse_impl( EndianReversible x, boost::true_type ) 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 static_cast<int16_t>(
BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(static_cast<uint16_t>(x)));
# endif
}
template<class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible conditional_reverse_impl( EndianReversible x, boost::false_type ) BOOST_NOEXCEPT
{
return endian_reverse( x );
}
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 static_cast<int32_t>(
BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(static_cast<uint32_t>(x)));
# endif
}
} // namespace detail
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 static_cast<int64_t>(
BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(static_cast<uint64_t>(x)));
# endif
}
// generic conditional reverse
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible conditional_reverse( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( boost::is_class<EndianReversible>::value || detail::is_endian_reversible<EndianReversible>::value );
return detail::conditional_reverse_impl( x, boost::integral_constant<bool, From == To>() );
}
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
{
# if BOOST_ENDIAN_BIG_BYTE
return x;
# else
return endian_reverse(x);
# endif
}
template <class EndianReversible >
inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT
{
# if BOOST_ENDIAN_BIG_BYTE
return x;
# else
return endian_reverse(x);
# endif
}
template <class EndianReversible >
inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT
{
# if BOOST_ENDIAN_LITTLE_BYTE
return x;
# else
return endian_reverse(x);
# endif
}
template <class EndianReversible >
inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT
{
# if BOOST_ENDIAN_LITTLE_BYTE
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);
}
// runtime conditional reverse
template <class EndianReversible>
inline BOOST_CONSTEXPR EndianReversible conditional_reverse( EndianReversible x,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( boost::is_class<EndianReversible>::value || detail::is_endian_reversible<EndianReversible>::value );
return from_order == to_order? x: endian_reverse( x );
}
//--------------------------------------------------------------------------------------//
// 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>
inline void big_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
boost::endian::conditional_reverse_inplace<order::big, order::native>( x );
}
template <class EndianReversibleInplace>
# if BOOST_ENDIAN_BIG_BYTE
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>
# if BOOST_ENDIAN_BIG_BYTE
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>
inline void native_to_big_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
boost::endian::conditional_reverse_inplace<order::native, order::big>( x );
}
template <class EndianReversibleInplace>
# if BOOST_ENDIAN_LITTLE_BYTE
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>
# if BOOST_ENDIAN_LITTLE_BYTE
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
template <class EndianReversibleInplace>
inline void little_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
boost::endian::conditional_reverse_inplace<order::little, order::native>( x );
}
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
template <class EndianReversibleInplace>
inline void native_to_little_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
boost::endian::conditional_reverse_inplace<order::native, order::little>( 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
{
// 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 ()
}
namespace detail
{
// 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);
}
template<class EndianReversibleInplace>
inline void conditional_reverse_inplace_impl( EndianReversibleInplace&, boost::true_type ) BOOST_NOEXCEPT
{
}
template<class EndianReversibleInplace>
inline void conditional_reverse_inplace_impl( EndianReversibleInplace& x, boost::false_type ) BOOST_NOEXCEPT
{
endian_reverse_inplace( x );
}
namespace detail
{
template <class T>
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT
} // 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
{
BOOST_STATIC_ASSERT(
boost::is_class<EndianReversibleInplace>::value ||
boost::is_array<EndianReversibleInplace>::value ||
detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
detail::conditional_reverse_inplace_impl( x, boost::integral_constant<bool, From == To>() );
}
// 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
{
BOOST_STATIC_ASSERT(
boost::is_class<EndianReversibleInplace>::value ||
boost::is_array<EndianReversibleInplace>::value ||
detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
if( from_order != to_order )
{
# if BOOST_ENDIAN_BIG_BYTE
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
endian_reverse_inplace( x );
}
template <class T>
inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
{
# if BOOST_ENDIAN_BIG_BYTE
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
{
# if BOOST_ENDIAN_LITTLE_BYTE
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
{
# if BOOST_ENDIAN_LITTLE_BYTE
std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
# else
std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
# endif
}
} // namespace detail
}
// load/store convenience functions
// load 16
inline boost::int16_t load_little_s16( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int16_t, 2, order::little>( p );
}
inline boost::uint16_t load_little_u16( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint16_t, 2, order::little>( p );
}
inline boost::int16_t load_big_s16( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int16_t, 2, order::big>( p );
}
inline boost::uint16_t load_big_u16( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint16_t, 2, order::big>( p );
}
// load 24
inline boost::int32_t load_little_s24( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int32_t, 3, order::little>( p );
}
inline boost::uint32_t load_little_u24( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint32_t, 3, order::little>( p );
}
inline boost::int32_t load_big_s24( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int32_t, 3, order::big>( p );
}
inline boost::uint32_t load_big_u24( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint32_t, 3, order::big>( p );
}
// load 32
inline boost::int32_t load_little_s32( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int32_t, 4, order::little>( p );
}
inline boost::uint32_t load_little_u32( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint32_t, 4, order::little>( p );
}
inline boost::int32_t load_big_s32( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int32_t, 4, order::big>( p );
}
inline boost::uint32_t load_big_u32( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint32_t, 4, order::big>( p );
}
// load 40
inline boost::int64_t load_little_s40( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 5, order::little>( p );
}
inline boost::uint64_t load_little_u40( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 5, order::little>( p );
}
inline boost::int64_t load_big_s40( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 5, order::big>( p );
}
inline boost::uint64_t load_big_u40( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 5, order::big>( p );
}
// load 48
inline boost::int64_t load_little_s48( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 6, order::little>( p );
}
inline boost::uint64_t load_little_u48( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 6, order::little>( p );
}
inline boost::int64_t load_big_s48( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 6, order::big>( p );
}
inline boost::uint64_t load_big_u48( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 6, order::big>( p );
}
// load 56
inline boost::int64_t load_little_s56( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 7, order::little>( p );
}
inline boost::uint64_t load_little_u56( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 7, order::little>( p );
}
inline boost::int64_t load_big_s56( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 7, order::big>( p );
}
inline boost::uint64_t load_big_u56( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 7, order::big>( p );
}
// load 64
inline boost::int64_t load_little_s64( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 8, order::little>( p );
}
inline boost::uint64_t load_little_u64( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 8, order::little>( p );
}
inline boost::int64_t load_big_s64( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::int64_t, 8, order::big>( p );
}
inline boost::uint64_t load_big_u64( unsigned char const * p ) BOOST_NOEXCEPT
{
return boost::endian::endian_load<boost::uint64_t, 8, order::big>( p );
}
// store 16
inline void store_little_s16( unsigned char * p, boost::int16_t v )
{
boost::endian::endian_store<boost::int16_t, 2, order::little>( p, v );
}
inline void store_little_u16( unsigned char * p, boost::uint16_t v )
{
boost::endian::endian_store<boost::uint16_t, 2, order::little>( p, v );
}
inline void store_big_s16( unsigned char * p, boost::int16_t v )
{
boost::endian::endian_store<boost::int16_t, 2, order::big>( p, v );
}
inline void store_big_u16( unsigned char * p, boost::uint16_t v )
{
boost::endian::endian_store<boost::uint16_t, 2, order::big>( p, v );
}
// store 24
inline void store_little_s24( unsigned char * p, boost::int32_t v )
{
boost::endian::endian_store<boost::int32_t, 3, order::little>( p, v );
}
inline void store_little_u24( unsigned char * p, boost::uint32_t v )
{
boost::endian::endian_store<boost::uint32_t, 3, order::little>( p, v );
}
inline void store_big_s24( unsigned char * p, boost::int32_t v )
{
boost::endian::endian_store<boost::int32_t, 3, order::big>( p, v );
}
inline void store_big_u24( unsigned char * p, boost::uint32_t v )
{
boost::endian::endian_store<boost::uint32_t, 3, order::big>( p, v );
}
// store 32
inline void store_little_s32( unsigned char * p, boost::int32_t v )
{
boost::endian::endian_store<boost::int32_t, 4, order::little>( p, v );
}
inline void store_little_u32( unsigned char * p, boost::uint32_t v )
{
boost::endian::endian_store<boost::uint32_t, 4, order::little>( p, v );
}
inline void store_big_s32( unsigned char * p, boost::int32_t v )
{
boost::endian::endian_store<boost::int32_t, 4, order::big>( p, v );
}
inline void store_big_u32( unsigned char * p, boost::uint32_t v )
{
boost::endian::endian_store<boost::uint32_t, 4, order::big>( p, v );
}
// store 40
inline void store_little_s40( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 5, order::little>( p, v );
}
inline void store_little_u40( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 5, order::little>( p, v );
}
inline void store_big_s40( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 5, order::big>( p, v );
}
inline void store_big_u40( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 5, order::big>( p, v );
}
// store 48
inline void store_little_s48( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 6, order::little>( p, v );
}
inline void store_little_u48( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 6, order::little>( p, v );
}
inline void store_big_s48( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 6, order::big>( p, v );
}
inline void store_big_u48( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 6, order::big>( p, v );
}
// store 56
inline void store_little_s56( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 7, order::little>( p, v );
}
inline void store_little_u56( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 7, order::little>( p, v );
}
inline void store_big_s56( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 7, order::big>( p, v );
}
inline void store_big_u56( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 7, order::big>( p, v );
}
// store 64
inline void store_little_s64( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 8, order::little>( p, v );
}
inline void store_little_u64( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 8, order::little>( p, v );
}
inline void store_big_s64( unsigned char * p, boost::int64_t v )
{
boost::endian::endian_store<boost::int64_t, 8, order::big>( p, v );
}
inline void store_big_u64( unsigned char * p, boost::uint64_t v )
{
boost::endian::endian_store<boost::uint64_t, 8, order::big>( p, v );
}
} // namespace endian
} // namespace boost

View File

@@ -1,142 +0,0 @@
// 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

View File

@@ -0,0 +1,589 @@
#ifndef BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/detail/endian_reverse.hpp>
#include <boost/endian/detail/order.hpp>
#include <boost/endian/detail/integral_by_size.hpp>
#include <boost/endian/detail/is_trivially_copyable.hpp>
#include <boost/type_traits/is_signed.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/static_assert.hpp>
#include <cstddef>
#include <cstring>
namespace boost
{
namespace endian
{
namespace detail
{
template<class T, std::size_t N1, BOOST_SCOPED_ENUM(order) O1, std::size_t N2, BOOST_SCOPED_ENUM(order) O2> struct endian_load_impl
{
};
} // namespace detail
// Requires:
//
// sizeof(T) must be 1, 2, 4, or 8
// 1 <= N <= sizeof(T)
// T is TriviallyCopyable
// if N < sizeof(T), T is integral or enum
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) Order>
inline T endian_load( unsigned char const * p ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8 );
BOOST_STATIC_ASSERT( N >= 1 && N <= sizeof(T) );
return detail::endian_load_impl<T, sizeof(T), order::native, N, Order>()( p );
}
namespace detail
{
// same endianness, same size
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O> struct endian_load_impl<T, N, O, N, O>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );
T t;
std::memcpy( &t, p, N );
return t;
}
};
// same size, reverse endianness
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O1, BOOST_SCOPED_ENUM(order) O2> struct endian_load_impl<T, N, O1, N, O2>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );
typename integral_by_size<N>::type tmp;
std::memcpy( &tmp, p, N );
endian_reverse_inplace( tmp );
T t;
std::memcpy( &t, &tmp, N );
return t;
}
};
// expanding load 1 -> 2
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 2, Order, 1, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 2 ];
tmp[0] = p[0];
tmp[1] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
return boost::endian::endian_load<T, 2, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 2, Order, 1, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 2 ];
tmp[0] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[1] = p[0];
return boost::endian::endian_load<T, 2, order::big>( tmp );
}
};
// expanding load 1 -> 4
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 1, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
return boost::endian::endian_load<T, 4, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 1, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = p[0];
return boost::endian::endian_load<T, 4, order::big>( tmp );
}
};
// expanding load 2 -> 4
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 2, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
unsigned char fill = boost::is_signed<T>::value && ( p[1] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = fill;
tmp[3] = fill;
return boost::endian::endian_load<T, 4, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 2, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = p[0];
tmp[3] = p[1];
return boost::endian::endian_load<T, 4, order::big>( tmp );
}
};
// expanding load 3 -> 4
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 3, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = boost::is_signed<T>::value && ( p[2] & 0x80 )? 0xFF: 0x00;
return boost::endian::endian_load<T, 4, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 3, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
tmp[0] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[1] = p[0];
tmp[2] = p[1];
tmp[3] = p[2];
return boost::endian::endian_load<T, 4, order::big>( tmp );
}
};
// expanding load 1 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 1, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 1, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = p[0];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 2 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 2, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[1] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 2, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = p[0];
tmp[7] = p[1];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 3 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 3, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[2] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 3, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = fill;
tmp[5] = p[0];
tmp[6] = p[1];
tmp[7] = p[2];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 4 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 4, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[3] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = p[3];
tmp[4] = fill;
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 4, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = fill;
tmp[4] = p[0];
tmp[5] = p[1];
tmp[6] = p[2];
tmp[7] = p[3];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 5 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 5, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[4] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = p[3];
tmp[4] = p[4];
tmp[5] = fill;
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 5, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = fill;
tmp[3] = p[0];
tmp[4] = p[1];
tmp[5] = p[2];
tmp[6] = p[3];
tmp[7] = p[4];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 6 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 6, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[5] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = p[3];
tmp[4] = p[4];
tmp[5] = p[5];
tmp[6] = fill;
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 6, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = fill;
tmp[2] = p[0];
tmp[3] = p[1];
tmp[4] = p[2];
tmp[5] = p[3];
tmp[6] = p[4];
tmp[7] = p[5];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
// expanding load 7 -> 8
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 7, order::little>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[6] & 0x80 )? 0xFF: 0x00;
tmp[0] = p[0];
tmp[1] = p[1];
tmp[2] = p[2];
tmp[3] = p[3];
tmp[4] = p[4];
tmp[5] = p[5];
tmp[6] = p[6];
tmp[7] = fill;
return boost::endian::endian_load<T, 8, order::little>( tmp );
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 7, order::big>
{
inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
tmp[0] = fill;
tmp[1] = p[0];
tmp[2] = p[1];
tmp[3] = p[2];
tmp[4] = p[3];
tmp[5] = p[4];
tmp[6] = p[5];
tmp[7] = p[6];
return boost::endian::endian_load<T, 8, order::big>( tmp );
}
};
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED

View File

@@ -0,0 +1,178 @@
#ifndef BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED
// Copyright 2019, 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/detail/integral_by_size.hpp>
#include <boost/endian/detail/intrinsic.hpp>
#include <boost/endian/detail/is_scoped_enum.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/enable_if.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
#include <cstddef>
#include <cstring>
#if defined(BOOST_ENDIAN_NO_INTRINSICS)
# if defined(BOOST_NO_CXX14_CONSTEXPR)
# define BOOST_ENDIAN_CONSTEXPR
# else
# define BOOST_ENDIAN_CONSTEXPR constexpr
# endif
#else
# if defined(BOOST_ENDIAN_CONSTEXPR_INTRINSICS)
# define BOOST_ENDIAN_CONSTEXPR BOOST_CONSTEXPR
# else
# define BOOST_ENDIAN_CONSTEXPR
# endif
#endif
namespace boost
{
namespace endian
{
namespace detail
{
// -- 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 uint8_t BOOST_CONSTEXPR endian_reverse_impl( uint8_t x ) BOOST_NOEXCEPT
{
return x;
}
inline uint16_t BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( 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 BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint32_t x ) BOOST_NOEXCEPT
{
#ifdef BOOST_ENDIAN_NO_INTRINSICS
uint32_t 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 BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint64_t x ) BOOST_NOEXCEPT
{
#ifdef BOOST_ENDIAN_NO_INTRINSICS
uint64_t step32 = x << 32 | x >> 32;
uint64_t step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
return (step16 & 0x00FF00FF00FF00FFULL) << 8 | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
#else
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x);
# endif
}
#if defined(BOOST_HAS_INT128)
inline uint128_type BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint128_type x ) BOOST_NOEXCEPT
{
return endian_reverse_impl( static_cast<uint64_t>( x >> 64 ) ) |
static_cast<uint128_type>( endian_reverse_impl( static_cast<uint64_t>( x ) ) ) << 64;
}
#endif
// is_endian_reversible
template<class T> struct is_endian_reversible: boost::integral_constant<bool,
(boost::is_integral<T>::value && !boost::is_same<T, bool>::value) || is_scoped_enum<T>::value>
{
};
// is_endian_reversible_inplace
template<class T> struct is_endian_reversible_inplace: boost::integral_constant<bool,
boost::is_integral<T>::value || boost::is_enum<T>::value || boost::is_same<T, float>::value || boost::is_same<T, double>::value>
{
};
} // namespace detail
// Requires:
// T is non-bool integral or scoped enumeration type
template<class T> inline BOOST_CONSTEXPR
typename enable_if_< !is_class<T>::value, T >::type
endian_reverse( T x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<T>::value );
typedef typename detail::integral_by_size< sizeof(T) >::type uintN_t;
return static_cast<T>( detail::endian_reverse_impl( static_cast<uintN_t>( x ) ) );
}
// Requires:
// T is integral, enumeration, float or double
template<class T> inline
typename enable_if_< !is_class<T>::value >::type
endian_reverse_inplace( T & x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<T>::value );
typename detail::integral_by_size< sizeof(T) >::type x2;
std::memcpy( &x2, &x, sizeof(T) );
x2 = detail::endian_reverse_impl( x2 );
std::memcpy( &x, &x2, sizeof(T) );
}
// Default implementation for user-defined types
template<class T> inline
typename enable_if_< is_class<T>::value >::type
endian_reverse_inplace( T & x ) BOOST_NOEXCEPT
{
x = endian_reverse( x );
}
// endian_reverse_inplace for arrays
template<class T, std::size_t N>
inline void endian_reverse_inplace( T (&x)[ N ] ) BOOST_NOEXCEPT
{
for( std::size_t i = 0; i < N; ++i )
{
endian_reverse_inplace( x[i] );
}
}
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_ENDIAN_REVERSE_HPP_INCLUDED

View File

@@ -0,0 +1,442 @@
#ifndef BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/detail/endian_reverse.hpp>
#include <boost/endian/detail/order.hpp>
#include <boost/endian/detail/integral_by_size.hpp>
#include <boost/endian/detail/is_trivially_copyable.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/static_assert.hpp>
#include <cstddef>
#include <cstring>
namespace boost
{
namespace endian
{
namespace detail
{
template<class T, std::size_t N1, BOOST_SCOPED_ENUM(order) O1, std::size_t N2, BOOST_SCOPED_ENUM(order) O2> struct endian_store_impl
{
};
} // namespace detail
// Requires:
//
// sizeof(T) must be 1, 2, 4, or 8
// 1 <= N <= sizeof(T)
// T is TriviallyCopyable
// if N < sizeof(T), T is integral or enum
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) Order>
inline void endian_store( unsigned char * p, T const & v ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8 );
BOOST_STATIC_ASSERT( N >= 1 && N <= sizeof(T) );
return detail::endian_store_impl<T, sizeof(T), order::native, N, Order>()( p, v );
}
namespace detail
{
// same endianness, same size
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O> struct endian_store_impl<T, N, O, N, O>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );
std::memcpy( p, &v, N );
}
};
// same size, reverse endianness
template<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O1, BOOST_SCOPED_ENUM(order) O2> struct endian_store_impl<T, N, O1, N, O2>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );
typename integral_by_size<N>::type tmp;
std::memcpy( &tmp, &v, N );
endian_reverse_inplace( tmp );
std::memcpy( p, &tmp, N );
}
};
// truncating store 2 -> 1
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 2, Order, 1, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 2 ];
boost::endian::endian_store<T, 2, order::little>( tmp, v );
p[0] = tmp[0];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 2, Order, 1, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 2 ];
boost::endian::endian_store<T, 2, order::big>( tmp, v );
p[0] = tmp[1];
}
};
// truncating store 4 -> 1
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 1, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::little>( tmp, v );
p[0] = tmp[0];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 1, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::big>( tmp, v );
p[0] = tmp[3];
}
};
// truncating store 4 -> 2
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 2, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 2, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::big>( tmp, v );
p[0] = tmp[2];
p[1] = tmp[3];
}
};
// truncating store 4 -> 3
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 3, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 4, Order, 3, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 4 ];
boost::endian::endian_store<T, 4, order::big>( tmp, v );
p[0] = tmp[1];
p[1] = tmp[2];
p[2] = tmp[3];
}
};
// truncating store 8 -> 1
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 1, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 1, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[7];
}
};
// truncating store 8 -> 2
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 2, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 2, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[6];
p[1] = tmp[7];
}
};
// truncating store 8 -> 3
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 3, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 3, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[5];
p[1] = tmp[6];
p[2] = tmp[7];
}
};
// truncating store 8 -> 4
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 4, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
p[3] = tmp[3];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 4, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[4];
p[1] = tmp[5];
p[2] = tmp[6];
p[3] = tmp[7];
}
};
// truncating store 8 -> 5
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 5, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
p[3] = tmp[3];
p[4] = tmp[4];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 5, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[3];
p[1] = tmp[4];
p[2] = tmp[5];
p[3] = tmp[6];
p[4] = tmp[7];
}
};
// truncating store 8 -> 6
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 6, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
p[3] = tmp[3];
p[4] = tmp[4];
p[5] = tmp[5];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 6, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[2];
p[1] = tmp[3];
p[2] = tmp[4];
p[3] = tmp[5];
p[4] = tmp[6];
p[5] = tmp[7];
}
};
// truncating store 8 -> 7
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 7, order::little>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::little>( tmp, v );
p[0] = tmp[0];
p[1] = tmp[1];
p[2] = tmp[2];
p[3] = tmp[3];
p[4] = tmp[4];
p[5] = tmp[5];
p[6] = tmp[6];
}
};
template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_store_impl<T, 8, Order, 7, order::big>
{
inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );
unsigned char tmp[ 8 ];
boost::endian::endian_store<T, 8, order::big>( tmp, v );
p[0] = tmp[1];
p[1] = tmp[2];
p[2] = tmp[3];
p[3] = tmp[4];
p[4] = tmp[5];
p[5] = tmp[6];
p[6] = tmp[7];
}
};
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED

View File

@@ -0,0 +1,57 @@
#ifndef BOOST_ENDIAN_DETAIL_INTEGRAL_BY_SIZE_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_INTEGRAL_BY_SIZE_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
#include <cstddef>
namespace boost
{
namespace endian
{
namespace detail
{
template<std::size_t N> struct integral_by_size
{
};
template<> struct integral_by_size<1>
{
typedef uint8_t type;
};
template<> struct integral_by_size<2>
{
typedef uint16_t type;
};
template<> struct integral_by_size<4>
{
typedef uint32_t type;
};
template<> struct integral_by_size<8>
{
typedef uint64_t type;
};
#if defined(BOOST_HAS_INT128)
template<> struct integral_by_size<16>
{
typedef uint128_type type;
};
#endif
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_INTEGRAL_BY_SIZE_HPP_INCLUDED

View File

@@ -18,7 +18,7 @@
#define __has_builtin(x) 0 // Compatibility with non-clang compilers
#endif
#if defined(_MSC_VER)
#if defined(_MSC_VER) && ( !defined(__clang__) || defined(__c2__) )
// Microsoft documents these as being compatible since Windows 95 and specifically
// lists runtime library support since Visual Studio 2003 (aka 7.1).
// Clang/c2 uses the Microsoft rather than GCC intrinsics, so we check for
@@ -46,6 +46,8 @@
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) __builtin_bswap32(x)
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) __builtin_bswap64(x)
# define BOOST_ENDIAN_CONSTEXPR_INTRINSICS
// 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

View File

@@ -0,0 +1,35 @@
#ifndef BOOST_ENDIAN_DETAIL_IS_SCOPED_ENUM_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_IS_SCOPED_ENUM_HPP_INCLUDED
// Copyright 2020 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/type_traits/conditional.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/type_traits/is_convertible.hpp>
namespace boost
{
namespace endian
{
namespace detail
{
template<class T> struct negation: boost::integral_constant<bool, !T::value> {};
template<class T> struct is_scoped_enum:
boost::conditional<
boost::is_enum<T>::value,
negation< boost::is_convertible<T, int> >,
boost::false_type
>::type
{
};
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_IS_SCOPED_ENUM_HPP_INCLUDED

View File

@@ -0,0 +1,40 @@
#ifndef BOOST_ENDIAN_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/config.hpp>
#include <boost/type_traits/has_trivial_copy.hpp>
#include <boost/type_traits/has_trivial_assign.hpp>
#include <boost/type_traits/has_trivial_destructor.hpp>
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
# include <type_traits>
#endif
namespace boost
{
namespace endian
{
namespace detail
{
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
using std::is_trivially_copyable;
#else
template<class T> struct is_trivially_copyable: boost::integral_constant<bool,
boost::has_trivial_copy<T>::value && boost::has_trivial_assign<T>::value && boost::has_trivial_destructor<T>::value> {};
#endif
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED

View File

@@ -1,223 +0,0 @@
// 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

View File

@@ -0,0 +1,59 @@
#ifndef BOOST_ENDIAN_DETAIL_ORDER_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_ORDER_HPP_INCLUDED
// Copyright 2019 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/core/scoped_enum.hpp>
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define BOOST_ENDIAN_NATIVE_ORDER_INITIALIZER little
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define BOOST_ENDIAN_NATIVE_ORDER_INITIALIZER big
#elif defined(__BYTE_ORDER__) && defined(__ORDER_PDP_ENDIAN__) && __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
# error The Boost.Endian library does not support platforms with PDP endianness.
#elif defined(__LITTLE_ENDIAN__)
# define BOOST_ENDIAN_NATIVE_ORDER_INITIALIZER little
#elif defined(__BIG_ENDIAN__)
# define BOOST_ENDIAN_NATIVE_ORDER_INITIALIZER big
#elif defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__)
# define BOOST_ENDIAN_NATIVE_ORDER_INITIALIZER little
#else
# error The Boost.Endian library could not determine the endianness of this platform.
#endif
namespace boost
{
namespace endian
{
BOOST_SCOPED_ENUM_START(order)
{
big,
little,
native = BOOST_ENDIAN_NATIVE_ORDER_INITIALIZER
}; BOOST_SCOPED_ENUM_END
} // namespace endian
} // namespace boost
#undef BOOST_ENDIAN_NATIVE_ORDER_INITIALIZER
#endif // BOOST_ENDIAN_DETAIL_ORDER_HPP_INCLUDED

View File

@@ -0,0 +1,23 @@
#ifndef BOOST_ENDIAN_DETAIL_REQUIRES_CXX11_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_REQUIRES_CXX11_HPP_INCLUDED
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || \
defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || \
defined(BOOST_NO_CXX11_DECLTYPE) || \
defined(BOOST_NO_CXX11_CONSTEXPR) || \
defined(BOOST_NO_CXX11_NOEXCEPT) || \
defined(BOOST_NO_CXX11_SCOPED_ENUMS) || \
defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
BOOST_PRAGMA_MESSAGE("C++03 support was deprecated in Boost.Endian 1.82 and will be removed in Boost.Endian 1.84. Please open an issue in https://github.com/boostorg/endian if you want it retained.")
#endif
#endif // #ifndef BOOST_ENDIAN_DETAIL_REQUIRES_CXX11_HPP_INCLUDED

View File

@@ -14,6 +14,105 @@
# error "<boost/endian/endian.hpp> is deprecated. Define BOOST_ENDIAN_DEPRECATED_NAMES to use."
#endif
#include <boost/config/header_deprecated.hpp>
BOOST_HEADER_DEPRECATED( "<boost/endian/arithmetic.hpp>" )
#include <boost/endian/arithmetic.hpp>
#include <boost/config.hpp>
namespace boost
{
namespace endian
{
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;
} // namespace endian
} // namespace boost
#endif //BOOST_ENDIAN_ENDIAN_HPP

View File

@@ -1,38 +0,0 @@
// 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