Add countr_zero, countr_one

This commit is contained in:
Peter Dimov 2020-12-28 21:52:02 +02:00
parent 045487ba96
commit a47eebf41a
5 changed files with 114 additions and 4 deletions

View File

@ -17,6 +17,7 @@
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>
#include <limits> #include <limits>
#include <cstring> #include <cstring>
@ -28,8 +29,9 @@ namespace core
{ {
// bit_cast // bit_cast
template<class To, class From> template<class To, class From>
To bit_cast( From const & from) BOOST_NOEXCEPT To bit_cast( From const & from ) BOOST_NOEXCEPT
{ {
BOOST_STATIC_ASSERT( sizeof(To) == sizeof(From) ); BOOST_STATIC_ASSERT( sizeof(To) == sizeof(From) );
@ -39,6 +41,17 @@ To bit_cast( From const & from) BOOST_NOEXCEPT
} }
// counting // counting
namespace detail
{
BOOST_CXX14_CONSTEXPR int countl_impl( boost::uint32_t x ) BOOST_NOEXCEPT
{
return 0;
}
} // namespace detail
template<class T> template<class T>
BOOST_CORE_BIT_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT; BOOST_CORE_BIT_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT;
@ -48,8 +61,56 @@ BOOST_CORE_BIT_CONSTEXPR int countl_one( T x ) BOOST_NOEXCEPT
return boost::core::countl_zero( ~x ); return boost::core::countl_zero( ~x );
} }
namespace detail
{
inline int countr_impl( boost::uint32_t x ) BOOST_NOEXCEPT
{
static unsigned char const mod37[] = { 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18 };
return mod37[ ( -(boost::int32_t)x & x ) % 37 ];
}
inline int countr_impl( boost::uint64_t x ) BOOST_NOEXCEPT
{
return static_cast<boost::uint32_t>( x ) != 0?
boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) ):
boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x >> 32 ) ) + 32;
}
inline int countr_impl( boost::uint8_t x ) BOOST_NOEXCEPT
{
return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x100 );
}
inline int countr_impl( boost::uint16_t x ) BOOST_NOEXCEPT
{
return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) | 0x10000 );
}
} // namespace detail
template<class T> template<class T>
BOOST_CORE_BIT_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT; BOOST_CORE_BIT_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) );
if( sizeof(T) == sizeof(boost::uint8_t) )
{
return boost::core::detail::countr_impl( static_cast<boost::uint8_t>( x ) );
}
else if( sizeof(T) == sizeof(boost::uint16_t) )
{
return boost::core::detail::countr_impl( static_cast<boost::uint16_t>( x ) );
}
else if( sizeof(T) == sizeof(boost::uint32_t) )
{
return boost::core::detail::countr_impl( static_cast<boost::uint32_t>( x ) );
}
else
{
return boost::core::detail::countr_impl( static_cast<boost::uint64_t>( x ) );
}
}
template<class T> template<class T>
BOOST_CORE_BIT_CONSTEXPR int countr_one( T x ) BOOST_NOEXCEPT BOOST_CORE_BIT_CONSTEXPR int countr_one( T x ) BOOST_NOEXCEPT
@ -61,6 +122,7 @@ template<class T>
BOOST_CORE_BIT_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT; BOOST_CORE_BIT_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT;
// rotating // rotating
template<class T> template<class T>
BOOST_CXX14_CONSTEXPR T rotl( T x, int s ) BOOST_NOEXCEPT BOOST_CXX14_CONSTEXPR T rotl( T x, int s ) BOOST_NOEXCEPT
{ {
@ -76,6 +138,7 @@ BOOST_CXX14_CONSTEXPR T rotr( T x, int s ) BOOST_NOEXCEPT
} }
// integral powers of 2 // integral powers of 2
template<class T> template<class T>
BOOST_CONSTEXPR bool has_single_bit( T x ) BOOST_NOEXCEPT BOOST_CONSTEXPR bool has_single_bit( T x ) BOOST_NOEXCEPT
{ {

View File

@ -224,6 +224,7 @@ run cmath_test.cpp ;
run bit_cast_test.cpp ; run bit_cast_test.cpp ;
run bit_rotate_test.cpp ; run bit_rotate_test.cpp ;
run bit_countr_test.cpp ;
use-project /boost/core/swap : ./swap ; use-project /boost/core/swap : ./swap ;
build-project ./swap ; build-project ./swap ;

View File

@ -1,4 +1,4 @@
// Test for boost/core/bit.hpp // Test for boost/core/bit.hpp (bit_cast)
// //
// Copyright 2020 Peter Dimov // Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0. // Distributed under the Boost Software License, Version 1.0.

46
test/bit_countr_test.cpp Normal file
View File

@ -0,0 +1,46 @@
// Test for boost/core/bit.hpp (countr_zero, countr_one)
//
// 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/core/detail/splitmix64.hpp>
#include <boost/cstdint.hpp>
#include <limits>
template<class T> void test_countr( T x )
{
x |= 1;
for( int i = 0; i <= std::numeric_limits<T>::digits; ++i, x <<= 1 )
{
BOOST_TEST_EQ( boost::core::countr_zero( x ), i );
BOOST_TEST_EQ( boost::core::countr_one( static_cast<T>( ~x ) ), i );
}
}
int main()
{
test_countr( static_cast<unsigned char>( 0 ) );
test_countr( static_cast<unsigned short>( 0 ) );
test_countr( static_cast<unsigned int>( 0 ) );
test_countr( static_cast<unsigned long>( 0 ) );
test_countr( static_cast<unsigned long long>( 0 ) );
boost::detail::splitmix64 rng;
for( int i = 0; i < 1000; ++i )
{
boost::uint64_t x = rng();
test_countr( static_cast<unsigned char>( x ) );
test_countr( static_cast<unsigned short>( x ) );
test_countr( static_cast<unsigned int>( x ) );
test_countr( static_cast<unsigned long>( x ) );
test_countr( static_cast<unsigned long long>( x ) );
}
return boost::report_errors();
}

View File

@ -1,4 +1,4 @@
// Test for boost/core/bit.hpp // Test for boost/core/bit.hpp (rotl, rotr)
// //
// Copyright 2020 Peter Dimov // Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0. // Distributed under the Boost Software License, Version 1.0.