Counting bits in a ulong

Paul Backus snarwin at gmail.com
Thu Jun 4 01:27:39 UTC 2020


On Thursday, 4 June 2020 at 01:16:13 UTC, H. S. Teoh wrote:
> So recently I needed to implement a function to count the 
> number of 1
> bits in a ulong inside a (very) hot inner loop.  There's of 
> course the
> naïve method (`val` is the input ulong):
[...]
> This illustrates how optimization is often input-dependent, and 
> what works well in one case may work not so well in another 
> case. And even an overall better choice (in this case 
> Kernighan's trick) will in some niche cases perform less well.

Interesting stuff. Did you compare your implementations with the 
popcnt function in core.bitop? There could be a potential 
improvement here.


More information about the Digitalmars-d mailing list