10b57cec5SDimitry Andric //===- FuzzerBuiltinsMSVC.h - Internal header for builtins ------*- C++ -* ===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // Wrapper functions and marcos that use intrinsics instead of builtin functions 90b57cec5SDimitry Andric // which cannot be compiled by MSVC. 100b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #ifndef LLVM_FUZZER_BUILTINS_MSVC_H 130b57cec5SDimitry Andric #define LLVM_FUZZER_BUILTINS_MSVC_H 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "FuzzerDefs.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #if LIBFUZZER_MSVC 180b57cec5SDimitry Andric #include <intrin.h> 190b57cec5SDimitry Andric #include <cstdint> 200b57cec5SDimitry Andric #include <cstdlib> 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric // __builtin_return_address() cannot be compiled with MSVC. Use the equivalent 230b57cec5SDimitry Andric // from <intrin.h> 240b57cec5SDimitry Andric #define GET_CALLER_PC() _ReturnAddress() 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric namespace fuzzer { 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric inline uint8_t Bswap(uint8_t x) { return x; } 290b57cec5SDimitry Andric // Use alternatives to __builtin functions from <stdlib.h> and <intrin.h> on 300b57cec5SDimitry Andric // Windows since the builtins are not supported by MSVC. 310b57cec5SDimitry Andric inline uint16_t Bswap(uint16_t x) { return _byteswap_ushort(x); } 320b57cec5SDimitry Andric inline uint32_t Bswap(uint32_t x) { return _byteswap_ulong(x); } 330b57cec5SDimitry Andric inline uint64_t Bswap(uint64_t x) { return _byteswap_uint64(x); } 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric // The functions below were mostly copied from 360b57cec5SDimitry Andric // compiler-rt/lib/builtins/int_lib.h which defines the __builtin functions used 370b57cec5SDimitry Andric // outside of Windows. 380b57cec5SDimitry Andric inline uint32_t Clzll(uint64_t X) { 390b57cec5SDimitry Andric unsigned long LeadZeroIdx = 0; 40*68d75effSDimitry Andric 41*68d75effSDimitry Andric #if !defined(_M_ARM) && !defined(_M_X64) 42*68d75effSDimitry Andric // Scan the high 32 bits. 43*68d75effSDimitry Andric if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X >> 32))) 44*68d75effSDimitry Andric return static_cast<int>(63 - (LeadZeroIdx + 32)); // Create a bit offset from the MSB. 45*68d75effSDimitry Andric // Scan the low 32 bits. 46*68d75effSDimitry Andric if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X))) 47*68d75effSDimitry Andric return static_cast<int>(63 - LeadZeroIdx); 48*68d75effSDimitry Andric 49*68d75effSDimitry Andric #else 500b57cec5SDimitry Andric if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx; 51*68d75effSDimitry Andric #endif 520b57cec5SDimitry Andric return 64; 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric inline uint32_t Clz(uint32_t X) { 560b57cec5SDimitry Andric unsigned long LeadZeroIdx = 0; 570b57cec5SDimitry Andric if (_BitScanReverse(&LeadZeroIdx, X)) return 31 - LeadZeroIdx; 580b57cec5SDimitry Andric return 32; 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 61*68d75effSDimitry Andric inline int Popcountll(unsigned long long X) { 62*68d75effSDimitry Andric #if !defined(_M_ARM) && !defined(_M_X64) 63*68d75effSDimitry Andric return __popcnt(X) + __popcnt(X >> 32); 64*68d75effSDimitry Andric #else 65*68d75effSDimitry Andric return __popcnt64(X); 66*68d75effSDimitry Andric #endif 67*68d75effSDimitry Andric } 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric } // namespace fuzzer 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric #endif // LIBFUZER_MSVC 720b57cec5SDimitry Andric #endif // LLVM_FUZZER_BUILTINS_MSVC_H 73