diff --git a/include/boost/core/bit.hpp b/include/boost/core/bit.hpp index dd85b2a..ed9b159 100644 --- a/include/boost/core/bit.hpp +++ b/include/boost/core/bit.hpp @@ -506,13 +506,43 @@ BOOST_CXX14_CONSTEXPR T bit_ceil( T x ) BOOST_NOEXCEPT // endian +#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + +# define BOOST_CORE_BIT_NATIVE_INITIALIZER =little + +#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + +# define BOOST_CORE_BIT_NATIVE_INITIALIZER =big + +#elif defined(__BYTE_ORDER__) && defined(__ORDER_PDP_ENDIAN__) && __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__ + +# define BOOST_CORE_BIT_NATIVE_INITIALIZER + +#elif defined(__LITTLE_ENDIAN__) + +# define BOOST_CORE_BIT_NATIVE_INITIALIZER =little + +#elif defined(__BIG_ENDIAN__) + +# define BOOST_CORE_BIT_NATIVE_INITIALIZER =big + +#elif defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__) + +# define BOOST_CORE_BIT_NATIVE_INITIALIZER =little + +#else + +# define BOOST_CORE_BIT_NATIVE_INITIALIZER + +#endif + #if !defined(BOOST_NO_CXX11_SCOPED_ENUMS) enum class endian { - little, big, - // native = /* see description */ + little, + native BOOST_CORE_BIT_NATIVE_INITIALIZER }; typedef endian endian_type; @@ -524,9 +554,9 @@ namespace endian enum type { - little, big, - // native = /* see description */ + little, + native BOOST_CORE_BIT_NATIVE_INITIALIZER }; } // namespace endian @@ -535,6 +565,8 @@ typedef endian::type endian_type; #endif +#undef BOOST_CORE_BIT_NATIVE_INITIALIZER + } // namespace core } // namespace boost diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index e2ec8ac..44a45d2 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -231,6 +231,7 @@ run has_single_bit_test.cpp ; run bit_floor_test.cpp ; run bit_ceil_test.cpp ; run bit_popcount_test.cpp ; +run bit_endian_test.cpp ; use-project /boost/core/swap : ./swap ; build-project ./swap ; diff --git a/test/bit_endian_test.cpp b/test/bit_endian_test.cpp new file mode 100644 index 0000000..361df16 --- /dev/null +++ b/test/bit_endian_test.cpp @@ -0,0 +1,36 @@ +// Test for boost/core/bit.hpp (bit_ceil) +// +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +int main() +{ + boost::uint64_t v = static_cast( 0x0102030405060708ull ); + + if( boost::core::endian::native == boost::core::endian::little ) + { + unsigned char w[] = { 8, 7, 6, 5, 4, 3, 2, 1 }; + BOOST_TEST( std::memcmp( &v, w, 8 ) == 0 ); + } + else if( boost::core::endian::native == boost::core::endian::big ) + { + unsigned char w[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + BOOST_TEST( std::memcmp( &v, w, 8 ) == 0 ); + } + else + { + unsigned char w1[] = { 8, 7, 6, 5, 4, 3, 2, 1 }; + BOOST_TEST( std::memcmp( &v, w1, 8 ) != 0 ); + + unsigned char w2[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + BOOST_TEST( std::memcmp( &v, w2, 8 ) != 0 ); + } + + return boost::report_errors(); +}