Am 27.09.22 um 15:03 schrieb Richard Biener:
On Tue, 27 Sep 2022, Jan Engelhardt wrote:
If the compiler sees an opportunity, sure.
unsigned int popcnt_for_the_poor(unsigned int v) { unsigned int z =0; for (int i =0; i < 32; ++i) if (v & (1 << i)) ++z; return z; }
We only handle
int fancy_popcnt (long b) { int c = 0; while (b) { b &= b - 1; c++; } return c; }
not your very poor variant. Produces
PopCount: .LFB0: .cfi_startproc xorl %eax, %eax popcntq %rdi, %rax ret
with v2.
Same for LLVM [1], though I'm beginning to think we should support the "very poor" variant as it naturally comes up when using e.g. <algorithm> on an std::bitset or std::vector<bool>. After all, if you can write the "b &= b - 1" loop, you can also write __builtin_popcount, so we're essentially just doing this to optimize legacy code. It's not really "necessary". Aaron [1] <https://github.com/llvm/llvm-project/blob/llvmorg-15.0.1/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp#L1616-L1744>