xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerBuiltinsMsvc.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 
15*5ffd83dbSDimitry Andric #include "FuzzerPlatform.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;
4068d75effSDimitry Andric 
4168d75effSDimitry Andric #if !defined(_M_ARM) && !defined(_M_X64)
4268d75effSDimitry Andric   // Scan the high 32 bits.
4368d75effSDimitry Andric   if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X >> 32)))
4468d75effSDimitry Andric     return static_cast<int>(63 - (LeadZeroIdx + 32)); // Create a bit offset from the MSB.
4568d75effSDimitry Andric   // Scan the low 32 bits.
4668d75effSDimitry Andric   if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X)))
4768d75effSDimitry Andric     return static_cast<int>(63 - LeadZeroIdx);
4868d75effSDimitry Andric 
4968d75effSDimitry Andric #else
500b57cec5SDimitry Andric   if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
5168d75effSDimitry 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 
6168d75effSDimitry Andric inline int Popcountll(unsigned long long X) {
6268d75effSDimitry Andric #if !defined(_M_ARM) && !defined(_M_X64)
6368d75effSDimitry Andric   return __popcnt(X) + __popcnt(X >> 32);
6468d75effSDimitry Andric #else
6568d75effSDimitry Andric   return __popcnt64(X);
6668d75effSDimitry Andric #endif
6768d75effSDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric }  // namespace fuzzer
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric #endif  // LIBFUZER_MSVC
720b57cec5SDimitry Andric #endif  // LLVM_FUZZER_BUILTINS_MSVC_H
73