Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerBuiltinsMsvc.h
35262 views
//===- FuzzerBuiltinsMSVC.h - Internal header for builtins ------*- C++ -* ===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// Wrapper functions and marcos that use intrinsics instead of builtin functions8// which cannot be compiled by MSVC.9//===----------------------------------------------------------------------===//1011#ifndef LLVM_FUZZER_BUILTINS_MSVC_H12#define LLVM_FUZZER_BUILTINS_MSVC_H1314#include "FuzzerPlatform.h"1516#if LIBFUZZER_MSVC17#include <intrin.h>18#include <cstdint>19#include <cstdlib>2021// __builtin_return_address() cannot be compiled with MSVC. Use the equivalent22// from <intrin.h>23#define GET_CALLER_PC() _ReturnAddress()2425namespace fuzzer {2627inline uint8_t Bswap(uint8_t x) { return x; }28// Use alternatives to __builtin functions from <stdlib.h> and <intrin.h> on29// Windows since the builtins are not supported by MSVC.30inline uint16_t Bswap(uint16_t x) { return _byteswap_ushort(x); }31inline uint32_t Bswap(uint32_t x) { return _byteswap_ulong(x); }32inline uint64_t Bswap(uint64_t x) { return _byteswap_uint64(x); }3334// The functions below were mostly copied from35// compiler-rt/lib/builtins/int_lib.h which defines the __builtin functions used36// outside of Windows.37inline uint32_t Clzll(uint64_t X) {38unsigned long LeadZeroIdx = 0;3940#if !defined(_M_ARM) && !defined(_M_X64)41// Scan the high 32 bits.42if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X >> 32)))43return static_cast<int>(4463 - (LeadZeroIdx + 32)); // Create a bit offset from the MSB.45// Scan the low 32 bits.46if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X)))47return static_cast<int>(63 - LeadZeroIdx);4849#else50if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;51#endif52return 64;53}5455inline int Popcountll(unsigned long long X) {56#if !defined(_M_ARM) && !defined(_M_X64)57return __popcnt(X) + __popcnt(X >> 32);58#else59return __popcnt64(X);60#endif61}6263} // namespace fuzzer6465#endif // LIBFUZER_MSVC66#endif // LLVM_FUZZER_BUILTINS_MSVC_H676869