为何C++ Boost库不使用内置函数?能否增设相关选项?
关于Boost库未使用内置函数实现lowest_bit的疑问
我一直搞不懂Boost库为什么不直接使用内置函数,就算要兼顾语言纯粹性,至少也该提供一个可选的实现方案吧?比如在boost/dynamic_bitset/detail/lowest_bit.hpp中的lowest_bit计算逻辑:
template <typename T> int lowest_bit(T x) { BOOST_ASSERT(x >= 1); // PRE // clear all bits on except the rightmost one, // then calculate the logarithm base 2 // return boost::integer_log2<T>( x - ( x & (x-1) ) ); }
这段代码里的boost::integer_log2<T>函数内部是用循环实现的,但其实直接调用对应版本的__builtin_ctz()就能实现相同功能。
更新:我查到了__builtin_ctz()对应的汇编解析代码:
xor eax, eax rep bsf eax, edi ret
内容的提问来源于stack exchange,提问作者uuu777
相关产品推荐
相关产品推荐

