Use _BitScanForward and _BitScanReverse on MSVC.

Based on https://github.com/google/snappy/pull/30
This commit is contained in:
costan
2017-08-01 10:01:27 -07:00
committed by Victor Costan
parent 71b8f86887
commit f0d3237c32

View File

@@ -45,6 +45,10 @@
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#if defined(_MSC_VER)
#include <intrin.h>
#endif // defined(_MSC_VER)
#include "snappy-stubs-public.h" #include "snappy-stubs-public.h"
#if defined(__x86_64__) #if defined(__x86_64__)
@@ -347,7 +351,10 @@ class Bits {
// undefined value if n == 0. FindLSBSetNonZero() is similar to ffs() except // undefined value if n == 0. FindLSBSetNonZero() is similar to ffs() except
// that it's 0-indexed. // that it's 0-indexed.
static int FindLSBSetNonZero(uint32 n); static int FindLSBSetNonZero(uint32 n);
#if defined(ARCH_K8) || defined(ARCH_PPC)
static int FindLSBSetNonZero64(uint64 n); static int FindLSBSetNonZero64(uint64 n);
#endif // defined(ARCH_K8) || defined(ARCH_PPC)
private: private:
// No copying // No copying
@@ -365,9 +372,36 @@ inline int Bits::FindLSBSetNonZero(uint32 n) {
return __builtin_ctz(n); return __builtin_ctz(n);
} }
#if defined(ARCH_K8) || defined(ARCH_PPC)
inline int Bits::FindLSBSetNonZero64(uint64 n) { inline int Bits::FindLSBSetNonZero64(uint64 n) {
return __builtin_ctzll(n); return __builtin_ctzll(n);
} }
#endif // defined(ARCH_K8) || defined(ARCH_PPC)
#elif defined(_MSC_VER)
inline int Bits::Log2Floor(uint32 n) {
unsigned long where;
if (_BitScanReverse(&where, n)) {
return where;
} else {
return -1;
}
}
inline int Bits::FindLSBSetNonZero(uint32 n) {
unsigned long where;
if (_BitScanForward(&where, n)) return static_cast<int>(where);
return 32;
}
#if defined(ARCH_K8) || defined(ARCH_PPC)
inline int Bits::FindLSBSetNonZero64(uint64 n) {
unsigned long where;
if (_BitScanForward64(&where, n)) return static_cast<int>(where);
return 64;
}
#endif // defined(ARCH_K8) || defined(ARCH_PPC)
#else // Portable versions. #else // Portable versions.
@@ -401,6 +435,7 @@ inline int Bits::FindLSBSetNonZero(uint32 n) {
return rc; return rc;
} }
#if defined(ARCH_K8) || defined(ARCH_PPC)
// FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero(). // FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero().
inline int Bits::FindLSBSetNonZero64(uint64 n) { inline int Bits::FindLSBSetNonZero64(uint64 n) {
const uint32 bottombits = static_cast<uint32>(n); const uint32 bottombits = static_cast<uint32>(n);
@@ -411,6 +446,7 @@ inline int Bits::FindLSBSetNonZero64(uint64 n) {
return FindLSBSetNonZero(bottombits); return FindLSBSetNonZero(bottombits);
} }
} }
#endif // defined(ARCH_K8) || defined(ARCH_PPC)
#endif // End portable versions. #endif // End portable versions.