bitwise: add popcount

This commit is contained in:
Danny Robson 2015-11-25 13:46:13 +11:00
parent a3bdf1b7a1
commit caa70a2063

View File

@ -30,6 +30,7 @@ const uint8_t BITMASK_6BITS = 0x3F;
const uint8_t BITMASK_7BITS = 0x7F; const uint8_t BITMASK_7BITS = 0x7F;
const uint8_t BITMASK_8BITS = 0xFF; const uint8_t BITMASK_8BITS = 0xFF;
///////////////////////////////////////////////////////////////////////////////
#define MODT(x) ((x) % (sizeof (T) * 8)) #define MODT(x) ((x) % (sizeof (T) * 8))
template <typename T> template <typename T>
@ -49,6 +50,8 @@ rotater (const T &value, size_t magnitude) {
#undef MODT #undef MODT
///////////////////////////////////////////////////////////////////////////////
// TODO: make constexpr for C++14 // TODO: make constexpr for C++14
template <typename T> template <typename T>
T T
@ -66,5 +69,14 @@ reverse (T value) {
return out; return out;
} }
///////////////////////////////////////////////////////////////////////////////
template <typename T>
constexpr T
popcount (std::enable_if_t<std::is_integral<T>::value,T> t)
{
return __builtin_popcount (t);
}
#endif #endif