Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 2 | //===----------------------- support/win32/support.h ----------------------===// |
Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 11 | #ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H |
| 12 | #define _LIBCPP_SUPPORT_WIN32_SUPPORT_H |
| 13 | |
Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 14 | /* |
| 15 | Functions and constants used in libc++ that are missing from the Windows C library. |
| 16 | */ |
| 17 | |
Howard Hinnant | 5f878d4 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 18 | #include <wchar.h> // mbstate_t |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 +0000 | [diff] [blame] | 19 | #include <cstdarg> // va_ macros |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 20 | // "builtins" not implemented here for Clang or GCC as they provide implementations. |
| 21 | // Assuming required for elsewhere else, certainly MSVC. |
| 22 | #if defined(_LIBCPP_MSVC) |
| 23 | #include <intrin.h> |
| 24 | #endif |
| 25 | #if defined(_LIBCPP_MSVCRT) |
| 26 | #include <xlocinfo.h> |
| 27 | #endif |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 28 | #define swprintf _snwprintf |
| 29 | #define vswprintf _vsnwprintf |
| 30 | |
Howard Hinnant | 5f878d4 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 31 | #ifndef NOMINMAX |
| 32 | #define NOMINMAX |
| 33 | #endif |
Howard Hinnant | 5f878d4 | 2013-09-17 01:34:47 +0000 | [diff] [blame] | 34 | |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 +0000 | [diff] [blame] | 35 | extern "C" { |
| 36 | |
| 37 | int vasprintf( char **sptr, const char *__restrict fmt, va_list ap ); |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 38 | int asprintf( char **sptr, const char *__restrict fmt, ...); |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 39 | size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src, |
| 40 | size_t nmc, size_t len, mbstate_t *__restrict ps ); |
| 41 | size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src, |
| 42 | size_t nwc, size_t len, mbstate_t *__restrict ps ); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 +0000 | [diff] [blame] | 43 | } |
Howard Hinnant | 75689c1 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 44 | |
Howard Hinnant | 0be8f64 | 2013-08-01 18:17:34 +0000 | [diff] [blame] | 45 | #if defined(_LIBCPP_MSVCRT) |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 46 | #define snprintf _snprintf |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 47 | #define atoll _atoi64 |
| 48 | #define strtoll _strtoi64 |
| 49 | #define strtoull _strtoui64 |
| 50 | #define wcstoll _wcstoi64 |
| 51 | #define wcstoull _wcstoui64 |
| 52 | _LIBCPP_ALWAYS_INLINE float strtof( const char *nptr, char **endptr ) |
| 53 | { return _Stof(nptr, endptr, 0); } |
| 54 | _LIBCPP_ALWAYS_INLINE double strtod( const char *nptr, char **endptr ) |
| 55 | { return _Stod(nptr, endptr, 0); } |
| 56 | _LIBCPP_ALWAYS_INLINE long double strtold( const char *nptr, char **endptr ) |
| 57 | { return _Stold(nptr, endptr, 0); } |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 58 | |
| 59 | #define _Exit _exit |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 60 | #endif |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 61 | |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 62 | #if defined(_LIBCPP_MSVC) |
| 63 | |
| 64 | // Bit builtin's make these assumptions when calling _BitScanForward/Reverse etc. |
| 65 | // These assumptions are expected to be true for Win32/Win64 which this file supports. |
| 66 | static_assert(sizeof(unsigned long long)==8,""); |
| 67 | static_assert(sizeof(unsigned long)==4,""); |
| 68 | static_assert(sizeof(unsigned int)==4,""); |
Howard Hinnant | a5bc2f8 | 2011-12-02 17:22:38 +0000 | [diff] [blame] | 69 | |
| 70 | _LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x) { |
| 71 | static const unsigned int m1 = 0x55555555; //binary: 0101... |
| 72 | static const unsigned int m2 = 0x33333333; //binary: 00110011.. |
| 73 | static const unsigned int m4 = 0x0f0f0f0f; //binary: 4 zeros, 4 ones ... |
| 74 | static const unsigned int h01= 0x01010101; //the sum of 256 to the power of 0,1,2,3... |
| 75 | x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits |
| 76 | x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits |
| 77 | x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits |
| 78 | return (x * h01) >> 24; //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) |
| 79 | } |
| 80 | |
| 81 | _LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) { |
| 82 | return __builtin_popcount(static_cast<int>(x)); |
| 83 | } |
| 84 | |
| 85 | _LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) { |
| 86 | static const unsigned long long m1 = 0x5555555555555555; //binary: 0101... |
| 87 | static const unsigned long long m2 = 0x3333333333333333; //binary: 00110011.. |
| 88 | static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ... |
| 89 | static const unsigned long long h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3... |
| 90 | x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits |
| 91 | x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits |
| 92 | x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits |
| 93 | return static_cast<int>((x * h01)>>56); //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... |
| 94 | } |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 95 | |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 96 | // Returns the number of trailing 0-bits in x, |
| 97 | // starting at the least significant bit position. |
| 98 | // If x is 0, the result is undefined. |
| 99 | _LIBCPP_ALWAYS_INLINE int __builtin_ctzll( unsigned long long mask ) |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 100 | { |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 101 | unsigned long where; |
| 102 | // Search from LSB to MSB for first set bit. |
| 103 | // Returns zero if no set bit is found. |
| 104 | #if defined(_WIN64) |
| 105 | if (_BitScanForward64(&where, mask)) |
| 106 | return static_cast<int>(where); |
| 107 | #elif defined(_WIN32) |
| 108 | // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. |
| 109 | // Scan the Low Word |
| 110 | if (_BitScanForward(&where, static_cast<unsigned long>(mask))) |
| 111 | return static_cast<int>(where); |
| 112 | // Scan the High Word |
| 113 | if (_BitScanForward(&where, static_cast<unsigned long>(mask >> 32))) |
| 114 | return static_cast<int>(where+32); // Create a bit offset from the LSB. |
| 115 | #else |
| 116 | # error "Implementation of __builtin_ctzll required" |
| 117 | #endif |
| 118 | return 64; |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 119 | } |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 +0000 | [diff] [blame] | 120 | |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 121 | _LIBCPP_ALWAYS_INLINE int __builtin_ctzl( unsigned long mask ) |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 122 | { |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 123 | unsigned long where; |
| 124 | // Search from LSB to MSB for first set bit. |
| 125 | // Returns zero if no set bit is found. |
| 126 | if (_BitScanForward(&where, mask)) |
| 127 | return static_cast<int>(where); |
| 128 | return 32; |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 129 | } |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 130 | |
| 131 | _LIBCPP_ALWAYS_INLINE int __builtin_ctz( unsigned int mask ) |
| 132 | { |
| 133 | // Win32 and Win64 expectations. |
| 134 | static_assert(sizeof(mask)==4,""); |
| 135 | static_assert(sizeof(unsigned long)==4,""); |
| 136 | return __builtin_ctzl(static_cast<unsigned long>(mask)); |
| 137 | } |
| 138 | |
| 139 | // Returns the number of leading 0-bits in x, |
| 140 | // starting at the most significant bit position. |
| 141 | // If x is 0, the result is undefined. |
| 142 | _LIBCPP_ALWAYS_INLINE int __builtin_clzll( unsigned long long mask ) |
| 143 | { |
| 144 | unsigned long where; |
| 145 | // BitScanReverse scans from MSB to LSB for first set bit. |
| 146 | // Returns 0 if no set bit is found. |
| 147 | #if defined(_WIN64) |
| 148 | if (_BitScanReverse64(&where, mask)) |
| 149 | return static_cast<int>(63-where); |
| 150 | #elif defined(_WIN32) |
| 151 | // Scan the high 32 bits. |
| 152 | if (_BitScanReverse(&where, static_cast<unsigned long>(mask >> 32))) |
| 153 | return static_cast<int>(63-(where+32)); // Create a bit offset from the MSB. |
| 154 | // Scan the low 32 bits. |
| 155 | if (_BitScanReverse(&where, static_cast<unsigned long>(mask))) |
| 156 | return static_cast<int>(63-where); |
| 157 | #else |
| 158 | # error "Implementation of __builtin_clzll required" |
| 159 | #endif |
| 160 | return 64; // Undefined Behavior. |
| 161 | } |
| 162 | |
| 163 | _LIBCPP_ALWAYS_INLINE int __builtin_clzl( unsigned long mask ) |
| 164 | { |
| 165 | unsigned long where; |
| 166 | // Search from LSB to MSB for first set bit. |
| 167 | // Returns zero if no set bit is found. |
| 168 | if (_BitScanReverse(&where, mask)) |
| 169 | return static_cast<int>(31-where); |
| 170 | return 32; // Undefined Behavior. |
| 171 | } |
| 172 | |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 173 | _LIBCPP_ALWAYS_INLINE int __builtin_clz( unsigned int x ) |
| 174 | { |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 175 | return __builtin_clzl(x); |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 176 | } |
Yaron Keren | f8f5675 | 2014-01-04 08:56:00 +0000 | [diff] [blame^] | 177 | #endif // _LIBCPP_MSVC |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 178 | |
Bob Wilson | a4fd70e | 2012-02-20 16:56:13 +0000 | [diff] [blame] | 179 | #endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H |