192a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant// -*- C++ -*-
2efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant//===----------------------- support/win32/support.h ----------------------===//
392a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant//
492a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant//                     The LLVM Compiler Infrastructure
592a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant//
692a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
792a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant// Source Licenses. See LICENSE.TXT for details.
892a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant//
992a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant//===----------------------------------------------------------------------===//
1092a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant
11efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant#ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H
12efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant#define _LIBCPP_SUPPORT_WIN32_SUPPORT_H
13efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant
146c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Functions and constants used in libc++ that
156c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// are missing from the Windows C library.
1692a07003b2ce449b22709fbd9b4f1e49b3a2fd3eHoward Hinnant
176c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren#include <wchar.h> // mbstate_t
189e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnant#include <cstdarg> // va_ macros
196c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// "builtins" not implemented here for Clang or GCC as they provide
206c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// implementations. Assuming required for elsewhere else, certainly MSVC.
21c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#if defined(_LIBCPP_MSVC)
22c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#include <intrin.h>
23c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif
24c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#if defined(_LIBCPP_MSVCRT)
25c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#include <xlocinfo.h>
26c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif
27efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant#define swprintf _snwprintf
28efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant#define vswprintf _vsnwprintf
29efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant
30ef5aa93b9cdc5bfea3e4c59bafd784a9ff968decHoward Hinnant#ifndef NOMINMAX
31ef5aa93b9cdc5bfea3e4c59bafd784a9ff968decHoward Hinnant#define NOMINMAX
32ef5aa93b9cdc5bfea3e4c59bafd784a9ff968decHoward Hinnant#endif
33ef5aa93b9cdc5bfea3e4c59bafd784a9ff968decHoward Hinnant
349e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnantextern "C" {
35efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant
366c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenint vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
376c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenint asprintf(char **sptr, const char *__restrict fmt, ...);
386c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerensize_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
396c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren                  size_t nmc, size_t len, mbstate_t *__restrict ps);
406c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerensize_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
416c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren                  size_t nwc, size_t len, mbstate_t *__restrict ps);
429e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnant}
43f6d875f7f589ab9efcf01307a5fd6e375c14fc26Howard Hinnant
44e9df0a5c6c864f1ea86486881a3ee559c56105e7Howard Hinnant#if defined(_LIBCPP_MSVCRT)
4578b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define snprintf _snprintf
4678b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define atoll _atoi64
4778b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define strtoll _strtoi64
4878b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define strtoull _strtoui64
4978b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define wcstoll _wcstoi64
5078b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define wcstoull _wcstoui64
516c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE float strtof(const char *nptr, char **endptr)
526c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{
536c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return _Stof(nptr, endptr, 0);
546c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren}
556c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE double strtod(const char *nptr, char **endptr)
566c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{
576c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return _Stod(nptr, endptr, 0);
586c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren}
596c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE long double strtold(const char *nptr, char **endptr)
606c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{
616c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return _Stold(nptr, endptr, 0);
626c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren}
6378b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant
6478b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define _Exit _exit
65c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif
6678b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant
67c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#if defined(_LIBCPP_MSVC)
68c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren
696c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Bit builtin's make these assumptions when calling _BitScanForward/Reverse
706c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// etc. These assumptions are expected to be true for Win32/Win64 which this
716c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// file supports.
726c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenstatic_assert(sizeof(unsigned long long) == 8, "");
736c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenstatic_assert(sizeof(unsigned long) == 4, "");
746c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenstatic_assert(sizeof(unsigned int) == 4, "");
756c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren
766c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
776c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{
786c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Binary: 0101...
796c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static const unsigned int m1 = 0x55555555;
806c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Binary: 00110011..
816c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static const unsigned int m2 = 0x33333333;
826c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Binary:  4 zeros,  4 ones ...
836c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static const unsigned int m4 = 0x0f0f0f0f;
846c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // The sum of 256 to the power of 0,1,2,3...
856c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static const unsigned int h01 = 0x01010101;
866c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Put count of each 2 bits into those 2 bits.
876c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  x -= (x >> 1) & m1;
886c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Put count of each 4 bits into those 4 bits.
896c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  x = (x & m2) + ((x >> 2) & m2);
906c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Put count of each 8 bits into those 8 bits.
916c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  x = (x + (x >> 4)) & m4;
926c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24).
936c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return (x * h01) >> 24;
94438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant}
95438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant
966c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x)
976c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{
98438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant  return __builtin_popcount(static_cast<int>(x));
99438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant}
100438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant
1016c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x)
1026c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{
1036c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Binary: 0101...
1046c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static const unsigned long long m1 = 0x5555555555555555;
1056c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Binary: 00110011..
1066c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static const unsigned long long m2 = 0x3333333333333333;
1076c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Binary:  4 zeros,  4 ones ...
1086c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f;
1096c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // The sum of 256 to the power of 0,1,2,3...
1106c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static const unsigned long long h01 = 0x0101010101010101;
1116c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Put count of each 2 bits into those 2 bits.
1126c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  x -= (x >> 1) & m1;
1136c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Put count of each 4 bits into those 4 bits.
1146c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  x = (x & m2) + ((x >> 2) & m2);
1156c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Put count of each 8 bits into those 8 bits.
1166c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  x = (x + (x >> 4)) & m4;
1176c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
1186c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return static_cast<int>((x * h01) >> 56);
119438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant}
12078b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant
1216c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Returns the number of trailing 0-bits in x, starting at the least significant
1226c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// bit position. If x is 0, the result is undefined.
1236c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_ctzll(unsigned long long mask)
124c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren{
1256c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  unsigned long where;
1266c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Search from LSB to MSB for first set bit.
1276c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Returns zero if no set bit is found.
128c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#if defined(_WIN64)
1296c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  if (_BitScanForward64(&where, mask))
1306c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren    return static_cast<int>(where);
131c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#elif defined(_WIN32)
1326c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
1336c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Scan the Low Word.
1346c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  if (_BitScanForward(&where, static_cast<unsigned long>(mask)))
1356c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren    return static_cast<int>(where);
1366c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Scan the High Word.
1376c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  if (_BitScanForward(&where, static_cast<unsigned long>(mask >> 32)))
1386c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren    return static_cast<int>(where + 32); // Create a bit offset from the LSB.
139c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#else
1406c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren#error "Implementation of __builtin_ctzll required"
141c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif
1426c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return 64;
143c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren}
144c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren
1456c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_ctzl(unsigned long mask)
14678b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant{
1476c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  unsigned long where;
1486c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Search from LSB to MSB for first set bit.
1496c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Returns zero if no set bit is found.
1506c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  if (_BitScanForward(&where, mask))
1516c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren    return static_cast<int>(where);
1526c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return 32;
15378b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant}
1549e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnant
1556c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_ctz(unsigned int mask)
156c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren{
1576c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Win32 and Win64 expectations.
1586c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static_assert(sizeof(mask) == 4, "");
1596c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  static_assert(sizeof(unsigned long) == 4, "");
1606c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return __builtin_ctzl(static_cast<unsigned long>(mask));
161c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren}
1629e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnant
1636c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Returns the number of leading 0-bits in x, starting at the most significant
1646c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// bit position. If x is 0, the result is undefined.
1656c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_clzll(unsigned long long mask)
16678b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant{
1676c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  unsigned long where;
1686c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// BitScanReverse scans from MSB to LSB for first set bit.
1696c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Returns 0 if no set bit is found.
170c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#if defined(_WIN64)
1716c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  if (_BitScanReverse64(&where, mask))
1726c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren    return static_cast<int>(63 - where);
173c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#elif defined(_WIN32)
1746c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Scan the high 32 bits.
1756c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  if (_BitScanReverse(&where, static_cast<unsigned long>(mask >> 32)))
1766c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren    return static_cast<int>(63 -
1776c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren                            (where + 32)); // Create a bit offset from the MSB.
1786c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Scan the low 32 bits.
1796c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  if (_BitScanReverse(&where, static_cast<unsigned long>(mask)))
1806c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren    return static_cast<int>(63 - where);
181c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#else
1826c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren#error "Implementation of __builtin_clzll required"
183c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif
1846c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return 64; // Undefined Behavior.
18578b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant}
186c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren
1876c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_clzl(unsigned long mask)
18878b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant{
1896c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  unsigned long where;
1906c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Search from LSB to MSB for first set bit.
1916c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  // Returns zero if no set bit is found.
1926c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  if (_BitScanReverse(&where, mask))
1936c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren    return static_cast<int>(31 - where);
1946c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return 32; // Undefined Behavior.
19578b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant}
196c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren
1976c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_clz(unsigned int x)
19878b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant{
1996c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren  return __builtin_clzl(x);
20078b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant}
201c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif // _LIBCPP_MSVC
202efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant
203d41b60b2b42114bc7664b016f6be619c46095e59Bob Wilson#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H
204