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. 215170d7d88f35a757f5fa89b09238e2d729fd22b4Eric Fiselier#if defined(_LIBCPP_COMPILER_MSVC) 22c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#include <intrin.h> 23c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif 24c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#if defined(_LIBCPP_MSVCRT) 2546658931bbdd78ff886aa623153ec3c716f3f0f2Saleem Abdulrasool#include <crtversion.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 344c888397167ac6b3c6a88987dc5823aa13bba7e7Dan Albert// The mingw headers already define these as static. 354c888397167ac6b3c6a88987dc5823aa13bba7e7Dan Albert#ifndef __MINGW32__ 369e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnantextern "C" { 37efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant 386c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenint vasprintf(char **sptr, const char *__restrict fmt, va_list ap); 396c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenint asprintf(char **sptr, const char *__restrict fmt, ...); 406c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerensize_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src, 416c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren size_t nmc, size_t len, mbstate_t *__restrict ps); 426c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerensize_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src, 436c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren size_t nwc, size_t len, mbstate_t *__restrict ps); 449e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnant} 454c888397167ac6b3c6a88987dc5823aa13bba7e7Dan Albert#endif // __MINGW32__ 46f6d875f7f589ab9efcf01307a5fd6e375c14fc26Howard Hinnant 4746658931bbdd78ff886aa623153ec3c716f3f0f2Saleem Abdulrasool#if defined(_VC_CRT_MAJOR_VERSION) && _VC_CRT_MAJOR_VERSION < 14 4878b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define snprintf _snprintf 4978b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant#define _Exit _exit 50c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif 5178b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant 525170d7d88f35a757f5fa89b09238e2d729fd22b4Eric Fiselier#if defined(_LIBCPP_COMPILER_MSVC) 53c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren 546c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Bit builtin's make these assumptions when calling _BitScanForward/Reverse 556c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// etc. These assumptions are expected to be true for Win32/Win64 which this 566c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// file supports. 576c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenstatic_assert(sizeof(unsigned long long) == 8, ""); 586c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenstatic_assert(sizeof(unsigned long) == 4, ""); 596c272502231b7e66da54d20b9be88b7d316a87a1Yaron Kerenstatic_assert(sizeof(unsigned int) == 4, ""); 606c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren 616c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x) 626c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{ 636c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Binary: 0101... 646c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static const unsigned int m1 = 0x55555555; 656c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Binary: 00110011.. 666c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static const unsigned int m2 = 0x33333333; 676c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Binary: 4 zeros, 4 ones ... 686c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static const unsigned int m4 = 0x0f0f0f0f; 696c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // The sum of 256 to the power of 0,1,2,3... 706c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static const unsigned int h01 = 0x01010101; 716c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Put count of each 2 bits into those 2 bits. 726c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren x -= (x >> 1) & m1; 736c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Put count of each 4 bits into those 4 bits. 746c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren x = (x & m2) + ((x >> 2) & m2); 756c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Put count of each 8 bits into those 8 bits. 766c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren x = (x + (x >> 4)) & m4; 776c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24). 786c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return (x * h01) >> 24; 79438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant} 80438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant 816c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) 826c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{ 83438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant return __builtin_popcount(static_cast<int>(x)); 84438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant} 85438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant 866c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) 876c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren{ 886c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Binary: 0101... 896c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static const unsigned long long m1 = 0x5555555555555555; 906c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Binary: 00110011.. 916c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static const unsigned long long m2 = 0x3333333333333333; 926c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Binary: 4 zeros, 4 ones ... 936c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f; 946c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // The sum of 256 to the power of 0,1,2,3... 956c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static const unsigned long long h01 = 0x0101010101010101; 966c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Put count of each 2 bits into those 2 bits. 976c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren x -= (x >> 1) & m1; 986c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Put count of each 4 bits into those 4 bits. 996c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren x = (x & m2) + ((x >> 2) & m2); 1006c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Put count of each 8 bits into those 8 bits. 1016c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren x = (x + (x >> 4)) & m4; 1026c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... 1036c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>((x * h01) >> 56); 104438377c051ccfb662b88e38e86f085909ed2c2d7Howard Hinnant} 10578b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant 1066c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Returns the number of trailing 0-bits in x, starting at the least significant 1076c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// bit position. If x is 0, the result is undefined. 1086c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_ctzll(unsigned long long mask) 109c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren{ 1106c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren unsigned long where; 1116c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Search from LSB to MSB for first set bit. 1126c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Returns zero if no set bit is found. 1135c737d839520d95399d94c237f6ed9fb05ef5131Saleem Abdulrasool#if defined(_LIBCPP_HAS_BITSCAN64) 114e34f9d5ef356cced5f05d9a06a7e4de9442ddfc7Saleem Abdulrasool (defined(_M_AMD64) || defined(__x86_64__)) 1156c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren if (_BitScanForward64(&where, mask)) 1166c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>(where); 117e34f9d5ef356cced5f05d9a06a7e4de9442ddfc7Saleem Abdulrasool#else 1186c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. 1196c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Scan the Low Word. 1206c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren if (_BitScanForward(&where, static_cast<unsigned long>(mask))) 1216c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>(where); 1226c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Scan the High Word. 1236c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren if (_BitScanForward(&where, static_cast<unsigned long>(mask >> 32))) 1246c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>(where + 32); // Create a bit offset from the LSB. 125c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif 1266c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return 64; 127c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren} 128c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren 1296c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_ctzl(unsigned long mask) 13078b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant{ 1316c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren unsigned long where; 1326c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Search from LSB to MSB for first set bit. 1336c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Returns zero if no set bit is found. 1346c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren if (_BitScanForward(&where, mask)) 1356c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>(where); 1366c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return 32; 13778b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant} 1389e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnant 1396c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_ctz(unsigned int mask) 140c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren{ 1416c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Win32 and Win64 expectations. 1426c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static_assert(sizeof(mask) == 4, ""); 1436c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren static_assert(sizeof(unsigned long) == 4, ""); 1446c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return __builtin_ctzl(static_cast<unsigned long>(mask)); 145c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren} 1469e98b34a8c03460dacab05068d900dd424e45e53Howard Hinnant 1476c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Returns the number of leading 0-bits in x, starting at the most significant 1486c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// bit position. If x is 0, the result is undefined. 1496c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_clzll(unsigned long long mask) 15078b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant{ 1516c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren unsigned long where; 1526c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// BitScanReverse scans from MSB to LSB for first set bit. 1536c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren// Returns 0 if no set bit is found. 1545c737d839520d95399d94c237f6ed9fb05ef5131Saleem Abdulrasool#if defined(_LIBCPP_HAS_BITSCAN64) 1556c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren if (_BitScanReverse64(&where, mask)) 1566c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>(63 - where); 157e34f9d5ef356cced5f05d9a06a7e4de9442ddfc7Saleem Abdulrasool#else 1586c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Scan the high 32 bits. 1596c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren if (_BitScanReverse(&where, static_cast<unsigned long>(mask >> 32))) 1606c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>(63 - 1616c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren (where + 32)); // Create a bit offset from the MSB. 1626c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Scan the low 32 bits. 1636c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren if (_BitScanReverse(&where, static_cast<unsigned long>(mask))) 1646c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>(63 - where); 165c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif 1666c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return 64; // Undefined Behavior. 16778b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant} 168c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren 1696c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_clzl(unsigned long mask) 17078b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant{ 1716c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren unsigned long where; 1726c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Search from LSB to MSB for first set bit. 1736c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren // Returns zero if no set bit is found. 1746c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren if (_BitScanReverse(&where, mask)) 1756c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return static_cast<int>(31 - where); 1766c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return 32; // Undefined Behavior. 17778b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant} 178c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren 1796c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren_LIBCPP_ALWAYS_INLINE int __builtin_clz(unsigned int x) 18078b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant{ 1816c272502231b7e66da54d20b9be88b7d316a87a1Yaron Keren return __builtin_clzl(x); 18278b6828f1420a266e02687685bc2ab993a54eaddHoward Hinnant} 183c8d1bbab02e2c1ee62ee69ac67e2c155031797a9Yaron Keren#endif // _LIBCPP_MSVC 184efbe4067f225856814465913bf51e87ec8f17bdbHoward Hinnant 185d41b60b2b42114bc7664b016f6be619c46095e59Bob Wilson#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H 186