3 ms·
I just found that there is a standard way to access it in C/C++ https://en.cppreference.com/w/cpp/numeric/popcount https://en.cppreference.com/w/cpp/numeric/po
by ape4 5y ago
I just found that there is a standard way to access it in C/C++
https://en.cppreference.com/w/cpp/numeric/popcount https://en.cppreference.com/w/cpp/numeric/popcount
- junon 5y agoThat's C++, not C. two different languages. Further, that was introduced in C++ '20, which means it's relatively recent and thus not as widely supported as some of the older compiler extensions to implement a popcount intrinsic.
- ncmncm 5y agoUnfortunately, if you compile with MSVC, or with Gcc or Clang but don't specify a more-recent-than-2003 target (e.g. "-mavx" or "-march=native"), you won't get a POPCNT instruction. Likewise, for the conforming extension __builtin_popcount. On MSVC, _popcount gets you POPCNT regardless of target. But its std::bitset and std::popcount still won't use it. C does not, in fact, offer any Standard way to get a popcount instruction. Gcc's and Clang's optimizers recognize certain naive counting loops and can substitute a POPCNT instruction if told that the target supports it. On RISC-V, you don't get a POPC under any circumstances, nohow. I have been told that the RVA-22 target (supposed to be used in desk machines) will have it, but cannot find any confirmation online. I expect it to be tricky to get compilers for RISC-V to emit it, into the indefinite future, regardless of support on particular machines.