Fixes use of builtins in CountBits for small and/or signed input types.

This commit is contained in:
Jonathan G Rennison
2016-09-12 18:37:28 +01:00
parent e85e302a68
commit 0bd33eff1e

View File

@@ -12,6 +12,8 @@
#ifndef BITMATH_FUNC_HPP #ifndef BITMATH_FUNC_HPP
#define BITMATH_FUNC_HPP #define BITMATH_FUNC_HPP
#include <type_traits>
/** /**
* Fetch \a n bits from \a x, started at bit \a s. * Fetch \a n bits from \a x, started at bit \a s.
* *
@@ -268,12 +270,13 @@ template <typename T>
static inline uint CountBits(T value) static inline uint CountBits(T value)
{ {
#ifdef WITH_BITMATH_BUILTINS #ifdef WITH_BITMATH_BUILTINS
if (sizeof(T) == sizeof(unsigned int)) { typename std::make_unsigned<T>::type unsigned_value = value;
return __builtin_popcount(value); if (sizeof(T) <= sizeof(unsigned int)) {
return __builtin_popcount(unsigned_value);
} else if (sizeof(T) == sizeof(unsigned long)) { } else if (sizeof(T) == sizeof(unsigned long)) {
return __builtin_popcountl(value); return __builtin_popcountl(unsigned_value);
} else { } else {
return __builtin_popcountll(value); return __builtin_popcountll(unsigned_value);
} }
#else #else
uint num; uint num;