Unverified Commit d5f5725c authored by Feng Xiao's avatar Feng Xiao Committed by GitHub

Merge pull request #4310 from KindDragon/patch-1

Support using MSVC intrinsics in Log2FloorNonZero
parents 89b5333a 22c477d9
...@@ -91,6 +91,7 @@ ...@@ -91,6 +91,7 @@
// These #includes are for the byte swap functions declared later on. // These #includes are for the byte swap functions declared later on.
#ifdef _MSC_VER #ifdef _MSC_VER
#include <stdlib.h> // NOLINT(build/include) #include <stdlib.h> // NOLINT(build/include)
#include <intrin.h>
#elif defined(__APPLE__) #elif defined(__APPLE__)
#include <libkern/OSByteOrder.h> #include <libkern/OSByteOrder.h>
#elif defined(__GLIBC__) || defined(__CYGWIN__) #elif defined(__GLIBC__) || defined(__CYGWIN__)
...@@ -394,12 +395,10 @@ class Bits { ...@@ -394,12 +395,10 @@ class Bits {
static uint32 Log2FloorNonZero(uint32 n) { static uint32 Log2FloorNonZero(uint32 n) {
#if defined(__GNUC__) #if defined(__GNUC__)
return 31 ^ static_cast<uint32>(__builtin_clz(n)); return 31 ^ static_cast<uint32>(__builtin_clz(n));
#elif defined(COMPILER_MSVC) && defined(_M_IX86) #elif defined(_MSC_VER)
_asm { unsigned long where;
bsr ebx, n _BitScanReverse(&where, n);
mov n, ebx return where;
}
return n;
#else #else
return Log2FloorNonZero_Portable(n); return Log2FloorNonZero_Portable(n);
#endif #endif
...@@ -414,6 +413,10 @@ class Bits { ...@@ -414,6 +413,10 @@ class Bits {
// implementation instead. // implementation instead.
#if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_USE_PORTABLE_LOG2) #if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_USE_PORTABLE_LOG2)
return 63 ^ static_cast<uint32>(__builtin_clzll(n)); return 63 ^ static_cast<uint32>(__builtin_clzll(n));
#elif defined(_MSC_VER) && defined(_M_X64)
unsigned long where;
_BitScanReverse64(&where, n);
return where;
#else #else
return Log2FloorNonZero64_Portable(n); return Log2FloorNonZero64_Portable(n);
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment