From e8aa0c75b4382f5ed9e24df807c78dca736aef8f Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 29 Dec 2020 02:02:33 +0200 Subject: [PATCH] Use _BitScanForward --- include/boost/core/bit.hpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/include/boost/core/bit.hpp b/include/boost/core/bit.hpp index 5324c43..81898d8 100644 --- a/include/boost/core/bit.hpp +++ b/include/boost/core/bit.hpp @@ -23,8 +23,10 @@ #if defined(_MSC_VER) # include +# pragma intrinsic(_BitScanForward) # pragma intrinsic(_BitScanReverse) # if defined(_M_X64) +# pragma intrinsic(_BitScanForward64) # pragma intrinsic(_BitScanReverse64) # endif #endif // defined(_MSC_VER) @@ -235,15 +237,49 @@ namespace detail inline int countr_impl( boost::uint32_t x ) BOOST_NOEXCEPT { +#if defined(_MSC_VER) + + unsigned long r; + + if( _BitScanForward( &r, x ) ) + { + return static_cast( r ); + } + else + { + return 32; + } + +#else + static unsigned char const mod37[ 37 ] = { 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 ]; + +#endif } inline int countr_impl( boost::uint64_t x ) BOOST_NOEXCEPT { +#if defined(_MSC_VER) && defined(_M_X64) + + unsigned long r; + + if( _BitScanForward64( &r, x ) ) + { + return static_cast( r ); + } + else + { + return 64; + } + +#else + return static_cast( x ) != 0? boost::core::detail::countr_impl( static_cast( x ) ): boost::core::detail::countr_impl( static_cast( x >> 32 ) ) + 32; + +#endif } inline int countr_impl( boost::uint8_t x ) BOOST_NOEXCEPT