From f0d3237c320fb4b3e5d9d05c7182f33fdca7abe6 Mon Sep 17 00:00:00 2001 From: costan Date: Tue, 1 Aug 2017 10:01:27 -0700 Subject: [PATCH] Use _BitScanForward and _BitScanReverse on MSVC. Based on https://github.com/google/snappy/pull/30 --- snappy-stubs-internal.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/snappy-stubs-internal.h b/snappy-stubs-internal.h index 881f7ad..841e4a0 100644 --- a/snappy-stubs-internal.h +++ b/snappy-stubs-internal.h @@ -45,6 +45,10 @@ #include #endif +#if defined(_MSC_VER) +#include +#endif // defined(_MSC_VER) + #include "snappy-stubs-public.h" #if defined(__x86_64__) @@ -347,7 +351,10 @@ class Bits { // undefined value if n == 0. FindLSBSetNonZero() is similar to ffs() except // that it's 0-indexed. static int FindLSBSetNonZero(uint32 n); + +#if defined(ARCH_K8) || defined(ARCH_PPC) static int FindLSBSetNonZero64(uint64 n); +#endif // defined(ARCH_K8) || defined(ARCH_PPC) private: // No copying @@ -365,9 +372,36 @@ inline int Bits::FindLSBSetNonZero(uint32 n) { return __builtin_ctz(n); } +#if defined(ARCH_K8) || defined(ARCH_PPC) inline int Bits::FindLSBSetNonZero64(uint64 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(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(where); + return 64; +} +#endif // defined(ARCH_K8) || defined(ARCH_PPC) #else // Portable versions. @@ -401,6 +435,7 @@ inline int Bits::FindLSBSetNonZero(uint32 n) { return rc; } +#if defined(ARCH_K8) || defined(ARCH_PPC) // FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero(). inline int Bits::FindLSBSetNonZero64(uint64 n) { const uint32 bottombits = static_cast(n); @@ -411,6 +446,7 @@ inline int Bits::FindLSBSetNonZero64(uint64 n) { return FindLSBSetNonZero(bottombits); } } +#endif // defined(ARCH_K8) || defined(ARCH_PPC) #endif // End portable versions.