carb::cpp::countl_one
Defined in carb/cpp/Bit.h
-
template<class T, std::enable_if_t<detail::IsUnsignedIntegral<T>::value, bool> = false>
int carb::cpp::countl_one(T val) noexcept Returns the number of consecutive 1 bits in the value of val, starting from the most significant bit (“left”).
Note
Unlike std::countl_one, 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. If a constexpr implementation is required, use countl_one_constexpr().
Note
(Intel/AMD CPUs) To support backwards compatibility with older CPUs, by default this is implemented with a
bsr
instruction (i386+), that is slightly less performant (~3%) than the more modernlzcnt
instruction. This function implementation will switch to usinglzcnt
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). Thelzcnt
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 1 bits in the provided value, starting from the most significant bit.