support.h revision ef5aa93b9cdc5bfea3e4c59bafd784a9ff968dec
1// -*- C++ -*-
2//===----------------------- support/win32/support.h ----------------------===//
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
11#ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H
12#define _LIBCPP_SUPPORT_WIN32_SUPPORT_H
13
14/*
15   Functions and constants used in libc++ that are missing from the Windows C library.
16  */
17
18#include <wchar.h>  // mbstate_t
19#include <cstdarg> // va_ macros
20#define swprintf _snwprintf
21#define vswprintf _vsnwprintf
22
23#ifndef NOMINMAX
24#define NOMINMAX
25#endif
26#include <Windows.h>
27
28extern "C" {
29
30int vasprintf( char **sptr, const char *__restrict fmt, va_list ap );
31int asprintf( char **sptr, const char *__restrict fmt, ...);
32size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
33                   size_t nmc, size_t len, mbstate_t *__restrict ps );
34size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
35                   size_t nwc, size_t len, mbstate_t *__restrict ps );
36}
37
38#if defined(_LIBCPP_MSVCRT)
39#define snprintf _snprintf
40#include <xlocinfo.h>
41#define atoll _atoi64
42#define strtoll _strtoi64
43#define strtoull _strtoui64
44#define wcstoll _wcstoi64
45#define wcstoull _wcstoui64
46_LIBCPP_ALWAYS_INLINE float strtof( const char *nptr, char **endptr )
47{ return _Stof(nptr, endptr, 0); }
48_LIBCPP_ALWAYS_INLINE double strtod( const char *nptr, char **endptr )
49{ return _Stod(nptr, endptr, 0); }
50_LIBCPP_ALWAYS_INLINE long double strtold( const char *nptr, char **endptr )
51{ return _Stold(nptr, endptr, 0); }
52
53#define _Exit _exit
54
55#ifndef __clang__ // MSVC-based Clang also defines _MSC_VER
56#include <intrin.h>
57
58_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x) {
59   static const unsigned int m1 = 0x55555555; //binary: 0101...
60   static const unsigned int m2 = 0x33333333; //binary: 00110011..
61   static const unsigned int m4 = 0x0f0f0f0f; //binary:  4 zeros,  4 ones ...
62   static const unsigned int h01= 0x01010101; //the sum of 256 to the power of 0,1,2,3...
63   x -= (x >> 1) & m1;             //put count of each 2 bits into those 2 bits
64   x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits
65   x = (x + (x >> 4)) & m4;        //put count of each 8 bits into those 8 bits
66   return (x * h01) >> 24;  //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24)
67}
68
69_LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) {
70  return __builtin_popcount(static_cast<int>(x));
71}
72
73_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
74   static const unsigned long long m1  = 0x5555555555555555; //binary: 0101...
75   static const unsigned long long m2  = 0x3333333333333333; //binary: 00110011..
76   static const unsigned long long m4  = 0x0f0f0f0f0f0f0f0f; //binary:  4 zeros,  4 ones ...
77   static const unsigned long long h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3...
78   x -= (x >> 1) & m1;             //put count of each 2 bits into those 2 bits
79   x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits
80   x = (x + (x >> 4)) & m4;        //put count of each 8 bits into those 8 bits
81   return static_cast<int>((x * h01)>>56);  //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
82}
83
84_LIBCPP_ALWAYS_INLINE int __builtin_ctz( unsigned int x )
85{
86   DWORD r = 0;
87   _BitScanReverse(&r, x);
88   return static_cast<int>(r);
89}
90
91// sizeof(long) == sizeof(int) on Windows
92_LIBCPP_ALWAYS_INLINE int __builtin_ctzl( unsigned long x )
93{ return __builtin_ctz( static_cast<int>(x) ); }
94
95_LIBCPP_ALWAYS_INLINE int __builtin_ctzll( unsigned long long x )
96{
97    DWORD r = 0;
98    _BitScanReverse64(&r, x);
99    return static_cast<int>(r);
100}
101_LIBCPP_ALWAYS_INLINE int __builtin_clz( unsigned int x )
102{
103   DWORD r = 0;
104   _BitScanForward(&r, x);
105   return static_cast<int>(r);
106}
107// sizeof(long) == sizeof(int) on Windows
108_LIBCPP_ALWAYS_INLINE int __builtin_clzl( unsigned long x )
109{ return __builtin_clz( static_cast<int>(x) ); }
110_LIBCPP_ALWAYS_INLINE int __builtin_clzll( unsigned long long x )
111{
112    DWORD r = 0;
113    _BitScanForward64(&r, x);
114    return static_cast<int>(r);
115}
116#endif // !__clang__
117#endif // _LIBCPP_MSVCRT
118
119#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H
120