]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/fuzzer/FuzzerBuiltinsMsvc.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / fuzzer / FuzzerBuiltinsMsvc.h
1 //===- FuzzerBuiltinsMSVC.h - Internal header for builtins ------*- C++ -* ===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // Wrapper functions and marcos that use intrinsics instead of builtin functions
9 // which cannot be compiled by MSVC.
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_FUZZER_BUILTINS_MSVC_H
13 #define LLVM_FUZZER_BUILTINS_MSVC_H
14
15 #include "FuzzerDefs.h"
16
17 #if LIBFUZZER_MSVC
18 #if !defined(_M_ARM) && !defined(_M_X64)
19 #error "_BitScanReverse64 unavailable on this platform so MSVC is unsupported."
20 #endif
21 #include <intrin.h>
22 #include <cstdint>
23 #include <cstdlib>
24
25 // __builtin_return_address() cannot be compiled with MSVC. Use the equivalent
26 // from <intrin.h>
27 #define GET_CALLER_PC() _ReturnAddress()
28
29 namespace fuzzer {
30
31 inline uint8_t  Bswap(uint8_t x)  { return x; }
32 // Use alternatives to __builtin functions from <stdlib.h> and <intrin.h> on
33 // Windows since the builtins are not supported by MSVC.
34 inline uint16_t Bswap(uint16_t x) { return _byteswap_ushort(x); }
35 inline uint32_t Bswap(uint32_t x) { return _byteswap_ulong(x); }
36 inline uint64_t Bswap(uint64_t x) { return _byteswap_uint64(x); }
37
38 // The functions below were mostly copied from
39 // compiler-rt/lib/builtins/int_lib.h which defines the __builtin functions used
40 // outside of Windows.
41 inline uint32_t Clzll(uint64_t X) {
42   unsigned long LeadZeroIdx = 0;
43   if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
44   return 64;
45 }
46
47 inline uint32_t Clz(uint32_t X) {
48   unsigned long LeadZeroIdx = 0;
49   if (_BitScanReverse(&LeadZeroIdx, X)) return 31 - LeadZeroIdx;
50   return 32;
51 }
52
53 inline int Popcountll(unsigned long long X) { return __popcnt64(X); }
54
55 }  // namespace fuzzer
56
57 #endif  // LIBFUZER_MSVC
58 #endif  // LLVM_FUZZER_BUILTINS_MSVC_H