MSVC equivalent to __builtin_popcount?

Matt Joiner picture Matt Joiner · Oct 3, 2010 · Viewed 14.2k times · Source

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

Answer

Eric Nicolas picture Eric Nicolas · Jul 3, 2014

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