namespace pdinklag
{
    public static class Popcount
    {
        /// <summary>
        /// Computes the number of set bits in the input word.
        /// 
        /// Since there seems to be no access to CPU intrinsics, we use a method described here:
        /// https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
        /// </summary>
        /// <param name="v">the input word</param>
        public static int CountSetBits(this uint v)
        {
            v = v - ((v >> 1) & 0x55555555U);
            v = (v & 0x33333333U) + ((v >> 2) & 0x33333333U);
            return (int)(((v + (v >> 4) & 0xF0F0F0FU) * 0x1010101U) >> 24);
        }
    }
}
