From ed2f5281fe7b9ca6bff233cfcbfbf7fbcc31157e Mon Sep 17 00:00:00 2001 From: Gennaro Prota Date: Wed, 10 Sep 2025 18:57:11 +0200 Subject: [PATCH] Make lowest_bit() more efficient As a nice bonus, this also removes the dependency on Boost.Integer. --- CMakeLists.txt | 3 +-- build.jam | 1 - include/boost/dynamic_bitset/detail/lowest_bit.hpp | 8 +++----- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e31af44..261c8ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,7 @@ if (BOOST_DYNAMIC_BITSET_IS_ROOT) # If BOOST_SRC_DIR is valid, fallback to find_package set(CMAKE_FOLDER Dependencies) - set(BOOST_INCLUDE_LIBRARIES assert config container_hash core integer move throw_exception) + set(BOOST_INCLUDE_LIBRARIES assert config container_hash core move throw_exception) set(BOOST_EXCLUDE_LIBRARIES dynamic_bitset) set(PREV_BUILD_TESTING ${BUILD_TESTING}) set(BUILD_TESTING OFF CACHE BOOL "Build the tests." FORCE) @@ -62,7 +62,6 @@ target_link_libraries(boost_dynamic_bitset Boost::config Boost::container_hash Boost::core - Boost::integer Boost::move Boost::throw_exception ) diff --git a/build.jam b/build.jam index 67e5e65..a5949b5 100644 --- a/build.jam +++ b/build.jam @@ -10,7 +10,6 @@ constant boost_dependencies : /boost/config//boost_config /boost/container_hash//boost_container_hash /boost/core//boost_core - /boost/integer//boost_integer /boost/move//boost_move /boost/throw_exception//boost_throw_exception ; diff --git a/include/boost/dynamic_bitset/detail/lowest_bit.hpp b/include/boost/dynamic_bitset/detail/lowest_bit.hpp index 79f7225..bd2f947 100644 --- a/include/boost/dynamic_bitset/detail/lowest_bit.hpp +++ b/include/boost/dynamic_bitset/detail/lowest_bit.hpp @@ -15,7 +15,8 @@ #define BOOST_LOWEST_BIT_HPP_GP_20030301 #include "boost/assert.hpp" -#include "boost/integer/integer_log2.hpp" +#include "boost/core/bit.hpp" +#include namespace boost { namespace detail { @@ -26,10 +27,7 @@ lowest_bit( T x ) { BOOST_ASSERT( x >= 1 ); - // Clear all the bits that are set except the rightmost one, - // then calculate the logarithm to base 2. - // - return boost::integer_log2< T >( x - ( x & ( x - 1 ) ) ); + return boost::core::countr_zero( static_cast< typename std::make_unsigned< T >::type >( x ) ); } }