carb::cpp20::countl_zero

Defined in carb/cpp20/Bit.h

template<class T, std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value, bool> = false>
int carb::cpp20::countl_zero(T val)

Returns the number of consecutive 0 bits in the value of val, starting from the most significant bit (“left”).

See

https://en.cppreference.com/w/cpp/numeric/countl_zero

Note

Unlike std::countl_zero, this function is not constexpr. This is because the compiler intrinsics used to implement this function are not constexpr until C++20, so it was decided to drop constexpr in favor of being able to use compiler intrinsics.

Note

(Intel/AMD CPUs) To support backwards compatiblity with older CPUs, by default this is implemented with a bsr instruction (i386+), that is slightly less performant (~3%) than the more modern lzcnt instruction. This function implementation will switch to using lzcnt if the compiler indicates that instruction is supported. On Visual Studio this is provided by passing /arch:AVX2 command-line switch, or on GCC with -march=haswell (or several other -march options). The lzcnt instruction was introduced on Intel’s Haswell micro- architecture and AMD’s Jaguar and Piledriver micro-architectures.

Template Parameters

T – An unsigned integral type

Parameters

val – An unsigned integral value

Returns

The number of consecutive 0 bits in the provided value, starting from the least significant bit.