libjmmcg  build_2783
A C++ library containing an eclectic mix of useful, advanced components.
jmmcg::dyn::unroll::count_setbits Struct Reference

Count the number of set bits in the input number. More...

#include <count_setbits.hpp>

Public Types

typedef unsigned long long element_type
 

Static Public Member Functions

constexpr static element_type result (element_type num) noexcept(true)
 
constexpr static float efficiency () noexcept(true)
 

Detailed Description

Count the number of set bits in the input number.

Because the operator>>() is poorly defined this only works for unsigned types. This is because there may be a sign bit or two's complement representation of the negative number. Then shifting might cause the sign bit to be shifted into the number itself, possibly causing an infinite loop.

Complexity: run-time: O(n) where n is at most the number of bits used to represent the input type.

Definition at line 322 of file count_setbits.hpp.

Member Typedef Documentation

◆ element_type

Definition at line 323 of file count_setbits.hpp.

Member Function Documentation

◆ efficiency()

constexpr static float jmmcg::dyn::unroll::count_setbits::efficiency ( )
inlinestaticconstexprnoexcept

Definition at line 337 of file count_setbits.hpp.

◆ result()

constexpr static element_type jmmcg::dyn::unroll::count_setbits::result ( element_type  num)
inlinestaticconstexprnoexcept

A fully-unrolled loop-based implementation. This assumes that the compiler is clever enough to inline all of the recursively generated functions, and then re-roll the repeated sequence into an optimal loop for the specified architecture. But also that the processor has enough bitwise-& functional units for the re-rolled loops to contain enough overlapped bitwise-& operations to make it efficient. Moreover the compiler knows the loop bounds statically, so may be able to generate branch-prediction indicators to ensure that the loop is optimally executed.

Definition at line 333 of file count_setbits.hpp.


The documentation for this struct was generated from the following file: