blob: ed1986e6954acac80bb372c49df4ea6a5f1e0b5d [file] [log] [blame]
Howard Hinnant3c78ca02011-09-22 19:10:18 +00001// -*- C++ -*-
Howard Hinnant34388892011-09-28 21:39:20 +00002//===----------------------- support/win32/support.h ----------------------===//
Howard Hinnant3c78ca02011-09-22 19:10:18 +00003//
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 Hinnant34388892011-09-28 21:39:20 +000011#ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H
12#define _LIBCPP_SUPPORT_WIN32_SUPPORT_H
13
Howard Hinnant3c78ca02011-09-22 19:10:18 +000014/*
15 Functions and constants used in libc++ that are missing from the Windows C library.
16 */
17
Howard Hinnant5f878d42013-09-17 01:34:47 +000018#include <wchar.h> // mbstate_t
Howard Hinnant9daaf572013-05-16 17:13:40 +000019#include <cstdarg> // va_ macros
Howard Hinnant34388892011-09-28 21:39:20 +000020#define swprintf _snwprintf
21#define vswprintf _vsnwprintf
22
Howard Hinnant5f878d42013-09-17 01:34:47 +000023#ifndef NOMINMAX
24#define NOMINMAX
25#endif
Howard Hinnant5f878d42013-09-17 01:34:47 +000026
Howard Hinnant9daaf572013-05-16 17:13:40 +000027extern "C" {
28
29int vasprintf( char **sptr, const char *__restrict fmt, va_list ap );
Howard Hinnante4383372011-10-22 20:59:45 +000030int asprintf( char **sptr, const char *__restrict fmt, ...);
Howard Hinnante4383372011-10-22 20:59:45 +000031size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
32 size_t nmc, size_t len, mbstate_t *__restrict ps );
33size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
34 size_t nwc, size_t len, mbstate_t *__restrict ps );
Howard Hinnant9daaf572013-05-16 17:13:40 +000035}
Howard Hinnant75689c12011-12-02 19:36:40 +000036
Howard Hinnant0be8f642013-08-01 18:17:34 +000037#if defined(_LIBCPP_MSVCRT)
Howard Hinnante4383372011-10-22 20:59:45 +000038#define snprintf _snprintf
Howard Hinnante4383372011-10-22 20:59:45 +000039#include <xlocinfo.h>
40#define atoll _atoi64
41#define strtoll _strtoi64
42#define strtoull _strtoui64
43#define wcstoll _wcstoi64
44#define wcstoull _wcstoui64
45_LIBCPP_ALWAYS_INLINE float strtof( const char *nptr, char **endptr )
46{ return _Stof(nptr, endptr, 0); }
47_LIBCPP_ALWAYS_INLINE double strtod( const char *nptr, char **endptr )
48{ return _Stod(nptr, endptr, 0); }
49_LIBCPP_ALWAYS_INLINE long double strtold( const char *nptr, char **endptr )
50{ return _Stold(nptr, endptr, 0); }
Howard Hinnante4383372011-10-22 20:59:45 +000051
52#define _Exit _exit
53
Howard Hinnant9563a092011-10-27 16:24:42 +000054#ifndef __clang__ // MSVC-based Clang also defines _MSC_VER
Howard Hinnante4383372011-10-22 20:59:45 +000055#include <intrin.h>
Howard Hinnanta5bc2f82011-12-02 17:22:38 +000056
57_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x) {
58 static const unsigned int m1 = 0x55555555; //binary: 0101...
59 static const unsigned int m2 = 0x33333333; //binary: 00110011..
60 static const unsigned int m4 = 0x0f0f0f0f; //binary: 4 zeros, 4 ones ...
61 static const unsigned int h01= 0x01010101; //the sum of 256 to the power of 0,1,2,3...
62 x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits
63 x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits
64 x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits
65 return (x * h01) >> 24; //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24)
66}
67
68_LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) {
69 return __builtin_popcount(static_cast<int>(x));
70}
71
72_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
73 static const unsigned long long m1 = 0x5555555555555555; //binary: 0101...
74 static const unsigned long long m2 = 0x3333333333333333; //binary: 00110011..
75 static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ...
76 static const unsigned long long h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3...
77 x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits
78 x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits
79 x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits
80 return static_cast<int>((x * h01)>>56); //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
81}
Howard Hinnante4383372011-10-22 20:59:45 +000082
83_LIBCPP_ALWAYS_INLINE int __builtin_ctz( unsigned int x )
84{
85 DWORD r = 0;
86 _BitScanReverse(&r, x);
87 return static_cast<int>(r);
88}
Howard Hinnant9daaf572013-05-16 17:13:40 +000089
Howard Hinnante4383372011-10-22 20:59:45 +000090// sizeof(long) == sizeof(int) on Windows
91_LIBCPP_ALWAYS_INLINE int __builtin_ctzl( unsigned long x )
92{ return __builtin_ctz( static_cast<int>(x) ); }
Howard Hinnant9daaf572013-05-16 17:13:40 +000093
Howard Hinnante4383372011-10-22 20:59:45 +000094_LIBCPP_ALWAYS_INLINE int __builtin_ctzll( unsigned long long x )
95{
96 DWORD r = 0;
Howard Hinnant75689c12011-12-02 19:36:40 +000097 _BitScanReverse64(&r, x);
98 return static_cast<int>(r);
Howard Hinnante4383372011-10-22 20:59:45 +000099}
100_LIBCPP_ALWAYS_INLINE int __builtin_clz( unsigned int x )
101{
102 DWORD r = 0;
103 _BitScanForward(&r, x);
104 return static_cast<int>(r);
105}
106// sizeof(long) == sizeof(int) on Windows
107_LIBCPP_ALWAYS_INLINE int __builtin_clzl( unsigned long x )
108{ return __builtin_clz( static_cast<int>(x) ); }
109_LIBCPP_ALWAYS_INLINE int __builtin_clzll( unsigned long long x )
110{
111 DWORD r = 0;
Howard Hinnant75689c12011-12-02 19:36:40 +0000112 _BitScanForward64(&r, x);
113 return static_cast<int>(r);
Howard Hinnante4383372011-10-22 20:59:45 +0000114}
Howard Hinnant9563a092011-10-27 16:24:42 +0000115#endif // !__clang__
Howard Hinnant0be8f642013-08-01 18:17:34 +0000116#endif // _LIBCPP_MSVCRT
Howard Hinnant34388892011-09-28 21:39:20 +0000117
Bob Wilsona4fd70e2012-02-20 16:56:13 +0000118#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H