#ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H
#define _LIBCPP_SUPPORT_WIN32_SUPPORT_H
#include <wchar.h> // mbstate_t
#include <cstdarg> // va_ macros
#if defined(_LIBCPP_MSVC)
#include <intrin.h>
#endif
#if defined(_LIBCPP_MSVCRT)
#include <xlocinfo.h>
#endif
#define swprintf _snwprintf
#define vswprintf _vsnwprintf
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef __MINGW32__
extern "C" {
int vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
int asprintf(char **sptr, const char *__restrict fmt, ...);
size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
size_t nmc, size_t len, mbstate_t *__restrict ps);
size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
size_t nwc, size_t len, mbstate_t *__restrict ps);
}
#endif // __MINGW32__
#if defined(_LIBCPP_MSVCRT)
#define snprintf _snprintf
#define atoll _atoi64
#define strtoll _strtoi64
#define strtoull _strtoui64
#define wcstoll _wcstoi64
#define wcstoull _wcstoui64
_LIBCPP_ALWAYS_INLINE float strtof(const char *nptr, char **endptr)
{
return _Stof(nptr, endptr, 0);
}
_LIBCPP_ALWAYS_INLINE double strtod(const char *nptr, char **endptr)
{
return _Stod(nptr, endptr, 0);
}
_LIBCPP_ALWAYS_INLINE long double strtold(const char *nptr, char **endptr)
{
return _Stold(nptr, endptr, 0);
}
#define _Exit _exit
#endif
#if defined(_LIBCPP_MSVC)
static_assert(sizeof(unsigned long long) == 8, "");
static_assert(sizeof(unsigned long) == 4, "");
static_assert(sizeof(unsigned int) == 4, "");
_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
{
static const unsigned int m1 = 0x55555555;
static const unsigned int m2 = 0x33333333;
static const unsigned int m4 = 0x0f0f0f0f;
static const unsigned int h01 = 0x01010101;
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
return (x * h01) >> 24;
}
_LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x)
{
return __builtin_popcount(static_cast<int>(x));
}
_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x)
{
static const unsigned long long m1 = 0x5555555555555555;
static const unsigned long long m2 = 0x3333333333333333;
static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f;
static const unsigned long long h01 = 0x0101010101010101;
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
return static_cast<int>((x * h01) >> 56);
}
_LIBCPP_ALWAYS_INLINE int __builtin_ctzll(unsigned long long mask)
{
unsigned long where;
#if defined(_WIN64)
if (_BitScanForward64(&where, mask))
return static_cast<int>(where);
#elif defined(_WIN32)
if (_BitScanForward(&where, static_cast<unsigned long>(mask)))
return static_cast<int>(where);
if (_BitScanForward(&where, static_cast<unsigned long>(mask >> 32)))
return static_cast<int>(where + 32); #else
#error "Implementation of __builtin_ctzll required"
#endif
return 64;
}
_LIBCPP_ALWAYS_INLINE int __builtin_ctzl(unsigned long mask)
{
unsigned long where;
if (_BitScanForward(&where, mask))
return static_cast<int>(where);
return 32;
}
_LIBCPP_ALWAYS_INLINE int __builtin_ctz(unsigned int mask)
{
static_assert(sizeof(mask) == 4, "");
static_assert(sizeof(unsigned long) == 4, "");
return __builtin_ctzl(static_cast<unsigned long>(mask));
}
_LIBCPP_ALWAYS_INLINE int __builtin_clzll(unsigned long long mask)
{
unsigned long where;
#if defined(_WIN64)
if (_BitScanReverse64(&where, mask))
return static_cast<int>(63 - where);
#elif defined(_WIN32)
if (_BitScanReverse(&where, static_cast<unsigned long>(mask >> 32)))
return static_cast<int>(63 -
(where + 32)); if (_BitScanReverse(&where, static_cast<unsigned long>(mask)))
return static_cast<int>(63 - where);
#else
#error "Implementation of __builtin_clzll required"
#endif
return 64; }
_LIBCPP_ALWAYS_INLINE int __builtin_clzl(unsigned long mask)
{
unsigned long where;
if (_BitScanReverse(&where, mask))
return static_cast<int>(31 - where);
return 32; }
_LIBCPP_ALWAYS_INLINE int __builtin_clz(unsigned int x)
{
return __builtin_clzl(x);
}
#endif // _LIBCPP_MSVC
#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H