blob: 38ea51e46b1e7bad4fd008f4559db5bd3340b18d [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 Hinnante4383372011-10-22 20:59:45 +000018#include <__config>
Howard Hinnant34388892011-09-28 21:39:20 +000019#include <wchar.h> // mbstate_t
20#include <stdio.h> // _snwprintf
21#define swprintf _snwprintf
22#define vswprintf _vsnwprintf
Howard Hinnante4383372011-10-22 20:59:45 +000023#define vfscnaf fscanf
Howard Hinnant34388892011-09-28 21:39:20 +000024
Howard Hinnante4383372011-10-22 20:59:45 +000025int vasprintf( char **sptr, const char *__restrict fmt , va_list ap );
26int asprintf( char **sptr, const char *__restrict fmt, ...);
27//int vfscanf( FILE *__restrict stream, const char *__restrict format,
28// va_list arg);
Howard Hinnant34388892011-09-28 21:39:20 +000029
Howard Hinnante4383372011-10-22 20:59:45 +000030size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
31 size_t nmc, size_t len, mbstate_t *__restrict ps );
32size_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 Hinnant9563a092011-10-27 16:24:42 +000037
Howard Hinnante4383372011-10-22 20:59:45 +000038#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 Hinnante4383372011-10-22 20:59:45 +000050
51#define _Exit _exit
52
Howard Hinnant9563a092011-10-27 16:24:42 +000053#ifndef __clang__ // MSVC-based Clang also defines _MSC_VER
Howard Hinnante4383372011-10-22 20:59:45 +000054#include <intrin.h>
Howard Hinnanta5bc2f82011-12-02 17:22:38 +000055
56_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x) {
57 static const unsigned int m1 = 0x55555555; //binary: 0101...
58 static const unsigned int m2 = 0x33333333; //binary: 00110011..
59 static const unsigned int m4 = 0x0f0f0f0f; //binary: 4 zeros, 4 ones ...
60 static const unsigned int h01= 0x01010101; //the sum of 256 to the power of 0,1,2,3...
61 x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits
62 x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits
63 x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits
64 return (x * h01) >> 24; //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24)
65}
66
67_LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) {
68 return __builtin_popcount(static_cast<int>(x));
69}
70
71_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
72 static const unsigned long long m1 = 0x5555555555555555; //binary: 0101...
73 static const unsigned long long m2 = 0x3333333333333333; //binary: 00110011..
74 static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ...
75 static const unsigned long long h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3...
76 x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits
77 x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits
78 x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits
79 return static_cast<int>((x * h01)>>56); //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
80}
Howard Hinnante4383372011-10-22 20:59:45 +000081
82_LIBCPP_ALWAYS_INLINE int __builtin_ctz( unsigned int x )
83{
84 DWORD r = 0;
85 _BitScanReverse(&r, x);
86 return static_cast<int>(r);
87}
88// sizeof(long) == sizeof(int) on Windows
89_LIBCPP_ALWAYS_INLINE int __builtin_ctzl( unsigned long x )
90{ return __builtin_ctz( static_cast<int>(x) ); }
91_LIBCPP_ALWAYS_INLINE int __builtin_ctzll( unsigned long long x )
92{
93 DWORD r = 0;
94 _BitScanReverse64(&r, x);
95 return static_cast<int>(r);
96}
97_LIBCPP_ALWAYS_INLINE int __builtin_clz( unsigned int x )
98{
99 DWORD r = 0;
100 _BitScanForward(&r, x);
101 return static_cast<int>(r);
102}
103// sizeof(long) == sizeof(int) on Windows
104_LIBCPP_ALWAYS_INLINE int __builtin_clzl( unsigned long x )
105{ return __builtin_clz( static_cast<int>(x) ); }
106_LIBCPP_ALWAYS_INLINE int __builtin_clzll( unsigned long long x )
107{
108 DWORD r = 0;
109 _BitScanForward64(&r, x);
110 return static_cast<int>(r);
111}
Howard Hinnant9563a092011-10-27 16:24:42 +0000112#endif // !__clang__
113#endif // _MSC_VER
Howard Hinnant34388892011-09-28 21:39:20 +0000114
115#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H