MSVC equivalent to

对于MSVC-10,在GCC和Clang中找到的__builtin_popcount相当于什么?


Using the comments provided:

  • __popcnt available through <intrin.h>
  • _mm_popcnt_u64 with SSE4 and <nmmintrin.h>

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


    The __popcount 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.

    链接地址: http://www.djcxy.com/p/12592.html

    上一篇: bitset :: count()的这个实现是如何工作的?

    下一篇: MSVC等同于