3 ms·
It's a great way to get individual functions for odd tasks instead of needing to pull in a whole library. Just last week I needed to implement POPCNT in C# sinc
by ziml77 6y ago
It's a great way to get individual functions for odd tasks instead of needing to pull in a whole library. Just last week I needed to implement POPCNT in C# since we're not using .NET 5 so BitOperations.PopCount is unavailable. The naive implementation is easy but this was needed in our core networking code so easy performance wins are good to go look for.
A quick Google search brought me to a question on Stack Overflow from someone looking for the exact same thing: a good implementation of POPCNT that works for a single byte[1]. I checked out the answers, found the one that looked the best, pasted it into LINQPad to test all the combinations, and then pasted it into our code with a comment that had the Stack Overflow answer's permalink. I don't see anything wrong with copy-pasting from SO that way. It's quite handy.
[1]: Yes, if I'm looking for maximum performance, working with just a byte at a time is not great. But for the data I'm working with, you need to know the population of a byte to find the next byte that needs to be counted.