xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerBuiltinsMsvc.h (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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 
155ffd83dbSDimitry 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 
Bswap(uint8_t x)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.
Bswap(uint16_t x)310b57cec5SDimitry Andric inline uint16_t Bswap(uint16_t x) { return _byteswap_ushort(x); }
Bswap(uint32_t x)320b57cec5SDimitry Andric inline uint32_t Bswap(uint32_t x) { return _byteswap_ulong(x); }
Bswap(uint64_t 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.
Clzll(uint64_t X)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)))
44*349cc55cSDimitry Andric     return static_cast<int>(
45*349cc55cSDimitry Andric         63 - (LeadZeroIdx + 32)); // Create a bit offset from the MSB.
4668d75effSDimitry Andric   // Scan the low 32 bits.
4768d75effSDimitry Andric   if (_BitScanReverse(&LeadZeroIdx, static_cast<unsigned long>(X)))
4868d75effSDimitry Andric     return static_cast<int>(63 - LeadZeroIdx);
4968d75effSDimitry Andric 
5068d75effSDimitry Andric #else
510b57cec5SDimitry Andric   if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
5268d75effSDimitry Andric #endif
530b57cec5SDimitry Andric   return 64;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
Popcountll(unsigned long long X)5668d75effSDimitry Andric inline int Popcountll(unsigned long long X) {
5768d75effSDimitry Andric #if !defined(_M_ARM) && !defined(_M_X64)
5868d75effSDimitry Andric   return __popcnt(X) + __popcnt(X >> 32);
5968d75effSDimitry Andric #else
6068d75effSDimitry Andric   return __popcnt64(X);
6168d75effSDimitry Andric #endif
6268d75effSDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric }  // namespace fuzzer
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric #endif  // LIBFUZER_MSVC
670b57cec5SDimitry Andric #endif  // LLVM_FUZZER_BUILTINS_MSVC_H
68