背景
长期以来,gcc has been providing有许多内置的位旋转功能,尤其是尾随和前导0位的数量(也适用于long unsigned
和long long unsigned
,其后缀为l
和ll
):
— Built-in Function:
int __builtin_clz (unsigned int x)
Returns the number of leading 0-bits in
x
, starting at the most significant bit position. Ifx
is 0, the result is undefined.— Built-in Function:
int __builtin_ctz (unsigned int x)
Returns the number of trailing 0-bits in
x
, starting at the least significant bit position. Ifx
is 0, the result is undefined.
但是,在我测试的每个在线(免责声明:仅x64)编译器上,结果是
clz(0)
和ctz(0)
都返回基础内置类型的位数,例如#include <iostream>
#include <limits>
int main()
{
// prints 32 32 32 on most systems
std::cout << std::numeric_limits<unsigned>::digits << " " << __builtin_ctz(0) << " " << __builtin_clz(0);
}
Live Example。
尝试的解决方法
std=c++1y
模式下的最新Clang SVN干线使所有这些函数都放松了C++ 14 constexpr
,这使它们可以在SFINAE表达式中用于3个ctz
/ clz
内置unsigned
,unsigned long
和unsigned long long
的包装器函数模板。template<class T> // wrapper class specialized for u, ul, ull (not shown)
constexpr int ctznz(T x) { return wrapper_class_around_builtin_ctz<T>()(x); }
// overload for platforms where ctznz returns size of underlying type
template<class T>
constexpr auto ctz(T x)
-> typename std::enable_if<ctznz(0) == std::numeric_limits<T>::digits, int>::type
{ return ctznz(x); }
// overload for platforms where ctznz does something else
template<class T>
constexpr auto ctz(T x)
-> typename std::enable_if<ctznz(0) != std::numeric_limits<T>::digits, int>::type
{ return x ? ctznz(x) : std::numeric_limits<T>::digits; }
这种攻击的好处在于,为
ctz(0)
提供所需结果的平台可以省略测试x==0
的额外条件(这似乎是微优化的,但是当您已经达到内置位纠缠功能的水平时,它会可以有很大的不同)问题
内置函数
clz(0)
和ctz(0)
的系列如何不确定?std::invalid_argument
异常吗? 最佳答案
不幸的是,甚至x86-64的实现也可能有所不同-与Intel的instruction set reference,BSF
和BSR
相比,源操作数值为(0)
,目标未定义,并设置了ZF
(零标志)。因此,这种行为在微体系结构或AMD和Intel之间可能不一致。 (我相信AMD会将目的地保持不变。)
较新的LZCNT
和TZCNT
指令并非无处不在。两者仅在Haswell架构(适用于Intel)上存在。
关于c++ - __builtin_ctz(0)或__builtin_clz(0)有多不确定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64252652/