5 ms·
It uses SWAR technique for counting bits because target platform wasn’t specified in compiler flags properly. popcnt and family are specific family extensions a
by mrcode007 2y ago
It uses SWAR technique for counting bits because target platform wasn’t specified in compiler flags properly. popcnt and family are specific family extensions and without specifying the target flags the compiler generates lowest common denominator among available instructions
Edit: add -msse4 and you will see popcnt appear because popcnt is the SSE4 extension
- deleted 2y ago[deleted]
- fyrn_ 2y agoThe article says all of this?
- upon_drumhead 2y ago> It was a Clang compiler, ARM-based platform > I do think it is a bug in this case, native popcnt could maybe be defended, but should not be used if compiling for architecture that does not support it.
- mrcode007 2y ago> If you would like to experiment yourself, Compiler Explorer snippet can be found here https://gcc.godbolt.org/z/qGzWo39b6 https://gcc.godbolt.org/z/qGzWo39b6 Which is an x86-64 sample
- upon_drumhead 2y agoIt's a little impossible to show the difference between using popcnt and the expanded emulation code on a platform that doesn't support popcnt. It doesn't change the fact that the actual program itself was being complied on a platform that doesn't support popcnt, so no amount of compiler arguments would fix that.
- mrcode007 2y agoNEON extension on ARM comes with CNT instruction that counts set bits.
- IshKebab 2y agoI think the issue that the article is not explaining very well, is that what it's actually trying to do is check if a number is a power of two, and one part of the compiler says "use popcnt!" and then later another part says "oh sorry, we'll have to emulate that with a ton of instructions". Whereas it could have just used a shift and and.
- mrcode007 2y agoThe power of two test is simply x & (x-1) == 0 which could be special cased through templates too