blob: 82709cfe7b404b1dc8a82543f0781a20b22f79e2 [file] [log] [blame]
metzman40132972019-01-09 21:46:09 +00001//===- FuzzerBuiltinsMSVC.h - Internal header for builtins ------*- C++ -* ===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// 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
metzman40132972019-01-09 21:46:09 +00006//
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>
metzman1c6a8592019-01-22 18:59:25 +000027#define GET_CALLER_PC() _ReturnAddress()
metzman40132972019-01-09 21:46:09 +000028
29namespace fuzzer {
30
31inline 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.
34inline uint16_t Bswap(uint16_t x) { return _byteswap_ushort(x); }
35inline uint32_t Bswap(uint32_t x) { return _byteswap_ulong(x); }
36inline 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.
41inline uint32_t Clzll(uint64_t X) {
42 unsigned long LeadZeroIdx = 0;
43 if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
44 return 64;
45}
46
47inline uint32_t Clz(uint32_t X) {
48 unsigned long LeadZeroIdx = 0;
49 if (_BitScanReverse(&LeadZeroIdx, X)) return 31 - LeadZeroIdx;
50 return 32;
51}
52
53inline 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