Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSVC equivalent to __builtin_popcount?

What is the equivalent to __builtin_popcount as found in GCC and Clang, for MSVC-10?

like image 803
Matt Joiner Avatar asked Oct 03 '10 10:10

Matt Joiner


3 Answers

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).

like image 195
Eric Nicolas Avatar answered Nov 14 '22 22:11

Eric Nicolas


Using the comments provided:

  • __popcnt available through <intrin.h>
  • _mm_popcnt_u64 with SSE4 and <nmmintrin.h>
like image 21
Matt Joiner Avatar answered Nov 14 '22 20:11

Matt Joiner


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.

like image 7
nemequ Avatar answered Nov 14 '22 20:11

nemequ