What is the equivalent to __builtin_popcount
as found in GCC and Clang, for MSVC-10?
With this code snippet you get the GCC builtin when building with MSVC :
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif
(Works from Visual Studio 2008).
Using the comments provided:
__popcnt
available through <intrin.h>
_mm_popcnt_u64
with SSE4 and <nmmintrin.h>
The __popcnt
intrinsic mentioned above doesn't work on ARM, or even all x86 CPUs (it requires ABM instruction set). You shouldn't use it directly; instead, if you're on x86/amd64 you should use the __cpuid
intrinsic to determine at runtime if the processor supports popcnt
.
Keep in mind that you probably don't want to issue a cpuid
for every popcnt
call; you'll want to store the result somewhere. If your code is always going to be single-threaded this is trivial, but if you have to be thread-safe you'll have to use something like a One-Time Initialization. That will only work with Windows ≥ Vista, though; if you need to work with older versions you'll need to roll your own (or use something from a third-party).
For machines without ABM (or if runtime detection isn't worth it), there are several portable versions at Bit Twiddling Hacks (look for "Counting bits set"). My favorite version works for any type T
up to 128-bits:
v = v - ((v >> 1) & (T)~(T)0/3); // temp
v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp
v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp
c = (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * CHAR_BIT; // count
If you want a drop-in version you can use the builtin module in portable-snippets (full disclosure: portable-snippets is one of my projects), which should work pretty much anywhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With