2018-08-24 16:07:17 +00:00
|
|
|
#ifndef LLARP_BITS_HPP
|
|
|
|
#define LLARP_BITS_HPP
|
|
|
|
|
2018-12-15 16:21:52 +00:00
|
|
|
#include <cstddef>
|
|
|
|
|
2018-08-24 16:07:17 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace bits
|
|
|
|
{
|
|
|
|
template < typename Int_t >
|
2018-12-15 16:21:52 +00:00
|
|
|
constexpr std::size_t
|
2018-08-24 16:07:17 +00:00
|
|
|
count_bits(const Int_t& i)
|
|
|
|
{
|
|
|
|
return i == 0 ? 0
|
|
|
|
: ((i & 0x01) == 0x01) ? 1UL + count_bits(i >> 1)
|
|
|
|
: count_bits(i >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename T >
|
2018-12-15 16:21:52 +00:00
|
|
|
constexpr std::size_t
|
|
|
|
__count_array_bits(const T& array, std::size_t idx)
|
2018-08-24 16:07:17 +00:00
|
|
|
{
|
|
|
|
return idx < sizeof(T)
|
|
|
|
? count_bits(array[idx]) + __count_array_bits(array, idx + 1)
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename T >
|
2018-12-15 16:21:52 +00:00
|
|
|
constexpr std::size_t
|
2018-08-24 16:07:17 +00:00
|
|
|
count_array_bits(const T& array)
|
|
|
|
{
|
|
|
|
return __count_array_bits(array, 0);
|
|
|
|
}
|
|
|
|
} // namespace bits
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|