Commit 436efbfe authored by REDMOND\acoates's avatar REDMOND\acoates Committed by Facebook Github Bot

Allow building x86 and arm on msvc (#1147)

Summary:
A couple of changes to support building folly in MSVC/win32 for x86 and arm platforms.
Pull Request resolved: https://github.com/facebook/folly/pull/1147

Reviewed By: Orvid

Differential Revision: D15592309

Pulled By: yfeldblum

fbshipit-source-id: 97860ab9309e5492bfadac56079af735741d4a2c
parent f8b36eab
......@@ -50,6 +50,19 @@ extern int (
#ifdef _MSC_VER
// We emulate weak linkage for MSVC. The symbols we're
// aliasing to are hiding in MallocImpl.cpp
#if defined(_M_IX86)
#pragma comment(linker, "/alternatename:_mallocx=_mallocxWeak")
#pragma comment(linker, "/alternatename:_rallocx=_rallocxWeak")
#pragma comment(linker, "/alternatename:_xallocx=_xallocxWeak")
#pragma comment(linker, "/alternatename:_sallocx=_sallocxWeak")
#pragma comment(linker, "/alternatename:_dallocx=_dallocxWeak")
#pragma comment(linker, "/alternatename:_sdallocx=_sdallocxWeak")
#pragma comment(linker, "/alternatename:_nallocx=_nallocxWeak")
#pragma comment(linker, "/alternatename:_mallctl=_mallctlWeak")
#pragma comment( \
linker, "/alternatename:_mallctlnametomib=_mallctlnametomibWeak")
#pragma comment(linker, "/alternatename:_mallctlbymib=_mallctlbymibWeak")
#else
#pragma comment(linker, "/alternatename:mallocx=mallocxWeak")
#pragma comment(linker, "/alternatename:rallocx=rallocxWeak")
#pragma comment(linker, "/alternatename:xallocx=xallocxWeak")
......@@ -62,4 +75,5 @@ extern int (
#pragma comment(linker, "/alternatename:mallctlbymib=mallctlbymibWeak")
#endif
#endif
#endif
}
......@@ -50,10 +50,21 @@ FOLLY_ALWAYS_INLINE int __builtin_clzl(unsigned long x) {
return __builtin_clz((unsigned int)x);
}
#if defined(_M_IX86) || defined(_M_ARM)
FOLLY_ALWAYS_INLINE int __builtin_clzll(unsigned long long x) {
if (x == 0) {
return 64;
}
unsigned int msb = (unsigned int)(x >> 32);
unsigned int lsb = (unsigned int)x;
return (msb != 0) ? __builtin_clz(msb) : 32 + __builtin_clz(lsb);
}
#else
FOLLY_ALWAYS_INLINE int __builtin_clzll(unsigned long long x) {
unsigned long index;
return int(_BitScanReverse64(&index, x) ? 63 - index : 64);
}
#endif
FOLLY_ALWAYS_INLINE int __builtin_ctz(unsigned int x) {
unsigned long index;
......@@ -64,10 +75,23 @@ FOLLY_ALWAYS_INLINE int __builtin_ctzl(unsigned long x) {
return __builtin_ctz((unsigned int)x);
}
#if defined(_M_IX86) || defined(_M_ARM)
FOLLY_ALWAYS_INLINE int __builtin_ctzll(unsigned long long x) {
unsigned long index;
unsigned int msb = (unsigned int)(x >> 32);
unsigned int lsb = (unsigned int)x;
if (lsb != 0) {
return (int)(_BitScanForward(&index, lsb) ? index : 64);
} else {
return (int)(_BitScanForward(&index, msb) ? index + 32 : 64);
}
}
#else
FOLLY_ALWAYS_INLINE int __builtin_ctzll(unsigned long long x) {
unsigned long index;
return int(_BitScanForward64(&index, x) ? index : 64);
}
#endif
FOLLY_ALWAYS_INLINE int __builtin_ffs(int x) {
unsigned long index;
......@@ -78,6 +102,12 @@ FOLLY_ALWAYS_INLINE int __builtin_ffsl(long x) {
return __builtin_ffs(int(x));
}
#if defined(_M_IX86) || defined(_M_ARM)
FOLLY_ALWAYS_INLINE int __builtin_ffsll(long long x) {
int ctzll = __builtin_ctzll((unsigned long long)x);
return ctzll != 64 ? ctzll + 1 : 0;
}
#else
FOLLY_ALWAYS_INLINE int __builtin_ffsll(long long x) {
unsigned long index;
return int(_BitScanForward64(&index, (unsigned long long)x) ? index + 1 : 0);
......@@ -91,10 +121,18 @@ FOLLY_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) {
static_assert(sizeof(x) == 4, "");
return int(__popcnt(x));
}
#endif
#if defined(_M_IX86)
FOLLY_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
return int(__popcnt((unsigned int)(x >> 32))) +
int(__popcnt((unsigned int)x));
}
#elif defined(_M_X64)
FOLLY_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
return int(__popcnt64(x));
}
#endif
FOLLY_ALWAYS_INLINE void* __builtin_return_address(unsigned int frame) {
// I really hope frame is zero...
......
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