1
0
forked from 0ad/0ad

merge with local codebase: fix PopulationCount

This was SVN commit r12907.
This commit is contained in:
janwas 2012-12-01 03:06:24 +00:00
parent 9001cba02c
commit 3787f17e4d

View File

@ -147,11 +147,12 @@ inline size_t SparsePopulationCount(T mask)
template<typename T>
static inline size_t PopulationCount(T x)
{
cassert(!std::numeric_limits<T>::is_signed);
const T mask = T(~T(0));
x -= (x >> 1) & (mask/3); // count 2 bits
x = (x & (mask/15*3)) + ((x >> 2) & (mask/15*3)); // count 4 bits
x = (x + (x >> 4)) & (mask/255*15); // count 8 bits
return (x * (mask/255)) >> ((sizeof(T)-1)*CHAR_BIT);
return T(x * (mask/255)) >> ((sizeof(T)-1)*CHAR_BIT);
}