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