Skip to content

Commit 6f7c795

Browse files
author
mcgov
committed
LibFuzzer support for 32bit MSVC
This fixes the two build errors when trying to compile LibFuzzer for 32bit with MSVC. - authored by Max Shavrick (mxms at microsoft) git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/fuzzer@369704 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent cb29929 commit 6f7c795

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

FuzzerBuiltinsMsvc.h

+18-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
#include "FuzzerDefs.h"
1616

1717
#if LIBFUZZER_MSVC
18-
#if !defined(_M_ARM) && !defined(_M_X64)
19-
#error "_BitScanReverse64 unavailable on this platform so MSVC is unsupported."
20-
#endif
2118
#include <intrin.h>
2219
#include <cstdint>
2320
#include <cstdlib>
@@ -40,7 +37,18 @@ inline uint64_t Bswap(uint64_t x) { return _byteswap_uint64(x); }
4037
// outside of Windows.
4138
inline uint32_t Clzll(uint64_t X) {
4239
unsigned long LeadZeroIdx = 0;
40+
41+
#if !defined(_M_ARM) && !defined(_M_X64)
42+
// Scan the high 32 bits.
43+
if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X >> 32)))
44+
return static_cast<int>(63 - (LeadZeroIdx + 32)); // Create a bit offset from the MSB.
45+
// Scan the low 32 bits.
46+
if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X)))
47+
return static_cast<int>(63 - LeadZeroIdx);
48+
49+
#else
4350
if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
51+
#endif
4452
return 64;
4553
}
4654

@@ -50,7 +58,13 @@ inline uint32_t Clz(uint32_t X) {
5058
return 32;
5159
}
5260

53-
inline int Popcountll(unsigned long long X) { return __popcnt64(X); }
61+
inline int Popcountll(unsigned long long X) {
62+
#if !defined(_M_ARM) && !defined(_M_X64)
63+
return __popcnt(X) + __popcnt(X >> 32);
64+
#else
65+
return __popcnt64(X);
66+
#endif
67+
}
5468

5569
} // namespace fuzzer
5670

0 commit comments

Comments
 (0)