How to use MSVC intrinsics to get the equivalent of this GCC code?

后端 未结 5 1156
甜味超标
甜味超标 2020-11-29 06:46

The following code calls the builtin functions for clz/ctz in GCC and, on other systems, has C versions. Obviously, the C versions are a bit suboptimal if the system has a

5条回答
  •  情书的邮戳
    2020-11-29 07:11

    There are two intrinsics "_BitScanForward" and "_BitScanReverse", which suits the same purpose for MSVC. Include . The functions are:

    #ifdef _MSC_VER
    #include 
    
    static uint32_t __inline ctz( uint32_t x )
    {
       int r = 0;
       _BitScanReverse(&r, x);
       return r;
    }
    
    static uint32_t __inline clz( uint32_t x )
    {
       int r = 0;
       _BitScanForward(&r, x);
       return r;
    }
    #endif
    

    There are equivalent 64bit versions "_BitScanForward64" and "_BitScanReverse64".

    Read more here:

    x86 Intrinsics on MSDN

提交回复
热议问题