1
0
forked from boostorg/core

Implement boost::core::endian

This commit is contained in:
Peter Dimov
2020-12-29 02:41:15 +02:00
parent 919b98d425
commit 16e9536146
3 changed files with 73 additions and 4 deletions

View File

@ -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

View File

@ -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 ;

36
test/bit_endian_test.cpp Normal file
View File

@ -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 <boost/core/bit.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/cstdint.hpp>
#include <cstring>
int main()
{
boost::uint64_t v = static_cast<boost::uint64_t>( 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();
}