From 0bd33eff1ef6a6abf1cc3d063621d5cbdd06d12c Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 12 Sep 2016 18:37:28 +0100 Subject: [PATCH] Fixes use of builtins in CountBits for small and/or signed input types. --- src/core/bitmath_func.hpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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;