Commit 7dcf1524 authored by Alexander Sklar's avatar Alexander Sklar Committed by Facebook GitHub Bot

Fix Folly to build with current MSVC (#1450)

Summary:
Fixes https://github.com/facebook/folly/issues/1449

The current public VS releases (16.3 up to 16.7) have builtins support. In commit https://github.com/facebook/folly/commit/7f1bda25627503ca8af2536cc81298a7d4688375 the #ifdef that was gating the definition of builtins was removed, as 16.8 will remove it. However by removign this gate, it makes current VS versions unable to build folly which breaks projects like react-native-windows.

Pull Request resolved: https://github.com/facebook/folly/pull/1450

Reviewed By: yfeldblum

Differential Revision: D23702071

Pulled By: Orvid

fbshipit-source-id: 6e05f63293e68783b6caae68cc5bd80ad3679766
parent 650dde9a
......@@ -22,6 +22,14 @@
#include <intrin.h>
#include <stdint.h>
// MSVC had added support for __builtin_clz etc. in 16.3 (1923) but it will be
// removed in 16.8 (1928).
#if (_MSC_VER >= 1923) && (_MSC_VER < 1928)
#define FOLLY_DETAILFOLLY_DETAIL_MSC_BUILTIN_SUPPORT 1
#else
#define FOLLY_DETAILFOLLY_DETAIL_MSC_BUILTIN_SUPPORT 0
#endif
namespace folly {
namespace portability {
namespace detail {
......@@ -41,6 +49,7 @@ FOLLY_ALWAYS_INLINE void __builtin___clear_cache(char* begin, char* end) {
}
}
#if !defined(_MSC_VER) || !defined(FOLLY_DETAIL_MSC_BUILTIN_SUPPORT)
FOLLY_ALWAYS_INLINE int __builtin_clz(unsigned int x) {
unsigned long index;
return int(_BitScanReverse(&index, (unsigned long)x) ? 31 - index : 32);
......@@ -92,6 +101,7 @@ FOLLY_ALWAYS_INLINE int __builtin_ctzll(unsigned long long x) {
return int(_BitScanForward64(&index, x) ? index : 64);
}
#endif
#endif // !defined(_MSC_VER) || !defined(FOLLY_DETAIL_MSC_BUILTIN_SUPPORT)
FOLLY_ALWAYS_INLINE int __builtin_ffs(int x) {
unsigned long index;
......@@ -117,12 +127,15 @@ FOLLY_ALWAYS_INLINE int __builtin_popcount(unsigned int x) {
return int(__popcnt(x));
}
#if !defined(_MSC_VER) || !defined(FOLLY_DETAIL_MSC_BUILTIN_SUPPORT)
FOLLY_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) {
static_assert(sizeof(x) == 4, "");
return int(__popcnt(x));
}
#endif // !defined(_MSC_VER) || !defined(FOLLY_DETAIL_MSC_BUILTIN_SUPPORT)
#endif
#if !defined(_MSC_VER) || !defined(FOLLY_DETAIL_MSC_BUILTIN_SUPPORT)
#if defined(_M_IX86)
FOLLY_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
return int(__popcnt((unsigned int)(x >> 32))) +
......@@ -133,6 +146,7 @@ FOLLY_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
return int(__popcnt64(x));
}
#endif
#endif // !defined(_MSC_VER) || !defined(FOLLY_DETAIL_MSC_BUILTIN_SUPPORT)
FOLLY_ALWAYS_INLINE void* __builtin_return_address(unsigned int frame) {
// I really hope frame is zero...
......@@ -141,3 +155,5 @@ FOLLY_ALWAYS_INLINE void* __builtin_return_address(unsigned int frame) {
return _ReturnAddress();
}
#endif
#undef FOLLY_DETAIL_MSC_BUILTIN_SUPPORT
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