// True if any of the 8 bytes in the integer is 0
bool anyZeroByte( uint64_t v )
{
// Compute bitwise OR of 8 bits in each byte
v |= ( v >> 4 ) & 0x0F0F0F0F0F0F0F0Full;
v |= ( v >> 2 ) & 0x0303030303030303ull;
constexpr uint64_t lowMask = 0x0101010101010101ull;
v |= ( v >> 1 ) & lowMask;
// Isolate the lowest bit
v &= lowMask;
// Now these bits are 0 for zero bytes, 1 for non-zero;
// Invert that bit
v ^= lowMask;
// Now these bits are 1 for zero bytes, 0 for non-zero
// Compute the result
return 0 != v;
}
1条答案
按热度按时间ars1skjm1#
从技术上讲,你可以这样做:
但是SIMD会更快。SSE是x64架构的绝对要求,世界上所有的AMD64处理器都需要支持SSE1和SSE2。下面是SSE2版本:
这是6条指令而不是16条:link。