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