Skip to content

Instantly share code, notes, and snippets.

@izanbf1803
Last active June 24, 2019 17:44
Show Gist options
  • Save izanbf1803/d00b071f674a297858c411bf62a4095a to your computer and use it in GitHub Desktop.
Save izanbf1803/d00b071f674a297858c411bf62a4095a to your computer and use it in GitHub Desktop.
Fast popcount method.
typedef unsigned int uint;
uint popcount_short[0xffff];
void popcount_setup()
{
popcount_short[0] = 0;
for (uint mask = 1; mask < 0xffff; ++mask) {
popcount_short[mask] = (mask&1) + popcount_short[mask>>1];
}
}
inline uint popcount(uint x)
{
return popcount_short[x&0xffff] + popcount_short[x>>16&0xffff];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment