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 | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 18 | #include <__config> |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 19 | #include <wchar.h> // mbstate_t |
| 20 | #include <stdio.h> // _snwprintf |
| 21 | #define swprintf _snwprintf |
| 22 | #define vswprintf _vsnwprintf |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 23 | #define vfscnaf fscanf |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 24 | |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 25 | int vasprintf( char **sptr, const char *__restrict fmt , va_list ap ); |
| 26 | int asprintf( char **sptr, const char *__restrict fmt, ...); |
| 27 | //int vfscanf( FILE *__restrict stream, const char *__restrict format, |
| 28 | // va_list arg); |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 29 | |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 30 | size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src, |
| 31 | size_t nmc, size_t len, mbstate_t *__restrict ps ); |
| 32 | size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src, |
| 33 | size_t nwc, size_t len, mbstate_t *__restrict ps ); |
| 34 | |
| 35 | #if defined(_MSC_VER) |
| 36 | #define snprintf _snprintf |
Howard Hinnant | 9563a09 | 2011-10-27 16:24:42 +0000 | [diff] [blame] | 37 | |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 38 | #include <xlocinfo.h> |
| 39 | #define atoll _atoi64 |
| 40 | #define strtoll _strtoi64 |
| 41 | #define strtoull _strtoui64 |
| 42 | #define wcstoll _wcstoi64 |
| 43 | #define wcstoull _wcstoui64 |
| 44 | _LIBCPP_ALWAYS_INLINE float strtof( const char *nptr, char **endptr ) |
| 45 | { return _Stof(nptr, endptr, 0); } |
| 46 | _LIBCPP_ALWAYS_INLINE double strtod( const char *nptr, char **endptr ) |
| 47 | { return _Stod(nptr, endptr, 0); } |
| 48 | _LIBCPP_ALWAYS_INLINE long double strtold( const char *nptr, char **endptr ) |
| 49 | { return _Stold(nptr, endptr, 0); } |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 50 | |
| 51 | #define _Exit _exit |
| 52 | |
Howard Hinnant | 9563a09 | 2011-10-27 16:24:42 +0000 | [diff] [blame] | 53 | #ifndef __clang__ // MSVC-based Clang also defines _MSC_VER |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 54 | #include <intrin.h> |
| 55 | #define __builtin_popcount __popcnt |
| 56 | #define __builtin_popcountl __popcnt |
| 57 | #define __builtin_popcountll(__i) static_cast<int>(__popcnt64(__i)) |
| 58 | |
| 59 | _LIBCPP_ALWAYS_INLINE int __builtin_ctz( unsigned int x ) |
| 60 | { |
| 61 | DWORD r = 0; |
| 62 | _BitScanReverse(&r, x); |
| 63 | return static_cast<int>(r); |
| 64 | } |
| 65 | // sizeof(long) == sizeof(int) on Windows |
| 66 | _LIBCPP_ALWAYS_INLINE int __builtin_ctzl( unsigned long x ) |
| 67 | { return __builtin_ctz( static_cast<int>(x) ); } |
| 68 | _LIBCPP_ALWAYS_INLINE int __builtin_ctzll( unsigned long long x ) |
| 69 | { |
| 70 | DWORD r = 0; |
| 71 | _BitScanReverse64(&r, x); |
| 72 | return static_cast<int>(r); |
| 73 | } |
| 74 | _LIBCPP_ALWAYS_INLINE int __builtin_clz( unsigned int x ) |
| 75 | { |
| 76 | DWORD r = 0; |
| 77 | _BitScanForward(&r, x); |
| 78 | return static_cast<int>(r); |
| 79 | } |
| 80 | // sizeof(long) == sizeof(int) on Windows |
| 81 | _LIBCPP_ALWAYS_INLINE int __builtin_clzl( unsigned long x ) |
| 82 | { return __builtin_clz( static_cast<int>(x) ); } |
| 83 | _LIBCPP_ALWAYS_INLINE int __builtin_clzll( unsigned long long x ) |
| 84 | { |
| 85 | DWORD r = 0; |
| 86 | _BitScanForward64(&r, x); |
| 87 | return static_cast<int>(r); |
| 88 | } |
Howard Hinnant | 9563a09 | 2011-10-27 16:24:42 +0000 | [diff] [blame] | 89 | #endif // !__clang__ |
| 90 | #endif // _MSC_VER |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 91 | |
| 92 | #endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H |