]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/support/win32/support.h
Vendor import of libc++ trunk r291012:
[FreeBSD/FreeBSD.git] / include / support / win32 / support.h
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 // Functions and constants used in libc++ that
15 // are missing from the Windows C library.
16
17 #include <wchar.h> // mbstate_t
18 #include <cstdarg> // va_ macros
19 // "builtins" not implemented here for Clang or GCC as they provide
20 // implementations. Assuming required for elsewhere else, certainly MSVC.
21 #if defined(_LIBCPP_MSVC)
22 #include <intrin.h>
23 #endif
24 #if defined(_LIBCPP_MSVCRT)
25 #include <crtversion.h>
26 #endif
27 #define swprintf _snwprintf
28 #define vswprintf _vsnwprintf
29
30 #ifndef NOMINMAX
31 #define NOMINMAX
32 #endif
33
34 // The mingw headers already define these as static.
35 #ifndef __MINGW32__
36 extern "C" {
37
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);
44 }
45 #endif // __MINGW32__
46
47 #if defined(_VC_CRT_MAJOR_VERSION) && _VC_CRT_MAJOR_VERSION < 14
48 #define snprintf _snprintf
49 #define _Exit _exit
50 #endif
51
52 #if defined(_LIBCPP_MSVC)
53
54 // Bit builtin's make these assumptions when calling _BitScanForward/Reverse
55 // etc. These assumptions are expected to be true for Win32/Win64 which this
56 // file supports.
57 static_assert(sizeof(unsigned long long) == 8, "");
58 static_assert(sizeof(unsigned long) == 4, "");
59 static_assert(sizeof(unsigned int) == 4, "");
60
61 _LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
62 {
63   // Binary: 0101...
64   static const unsigned int m1 = 0x55555555;
65   // Binary: 00110011..
66   static const unsigned int m2 = 0x33333333;
67   // Binary:  4 zeros,  4 ones ...
68   static const unsigned int m4 = 0x0f0f0f0f;
69   // The sum of 256 to the power of 0,1,2,3...
70   static const unsigned int h01 = 0x01010101;
71   // Put count of each 2 bits into those 2 bits.
72   x -= (x >> 1) & m1;
73   // Put count of each 4 bits into those 4 bits.
74   x = (x & m2) + ((x >> 2) & m2);
75   // Put count of each 8 bits into those 8 bits.
76   x = (x + (x >> 4)) & m4;
77   // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24).
78   return (x * h01) >> 24;
79 }
80
81 _LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x)
82 {
83   return __builtin_popcount(static_cast<int>(x));
84 }
85
86 _LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x)
87 {
88   // Binary: 0101...
89   static const unsigned long long m1 = 0x5555555555555555;
90   // Binary: 00110011..
91   static const unsigned long long m2 = 0x3333333333333333;
92   // Binary:  4 zeros,  4 ones ...
93   static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f;
94   // The sum of 256 to the power of 0,1,2,3...
95   static const unsigned long long h01 = 0x0101010101010101;
96   // Put count of each 2 bits into those 2 bits.
97   x -= (x >> 1) & m1;
98   // Put count of each 4 bits into those 4 bits.
99   x = (x & m2) + ((x >> 2) & m2);
100   // Put count of each 8 bits into those 8 bits.
101   x = (x + (x >> 4)) & m4;
102   // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
103   return static_cast<int>((x * h01) >> 56);
104 }
105
106 // Returns the number of trailing 0-bits in x, starting at the least significant
107 // bit position. If x is 0, the result is undefined.
108 _LIBCPP_ALWAYS_INLINE int __builtin_ctzll(unsigned long long mask)
109 {
110   unsigned long where;
111 // Search from LSB to MSB for first set bit.
112 // Returns zero if no set bit is found.
113 #if defined(_LIBCPP_HAS_BITSCAN64)
114     (defined(_M_AMD64) || defined(__x86_64__))
115   if (_BitScanForward64(&where, mask))
116     return static_cast<int>(where);
117 #else
118   // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
119   // Scan the Low Word.
120   if (_BitScanForward(&where, static_cast<unsigned long>(mask)))
121     return static_cast<int>(where);
122   // Scan the High Word.
123   if (_BitScanForward(&where, static_cast<unsigned long>(mask >> 32)))
124     return static_cast<int>(where + 32); // Create a bit offset from the LSB.
125 #endif
126   return 64;
127 }
128
129 _LIBCPP_ALWAYS_INLINE int __builtin_ctzl(unsigned long mask)
130 {
131   unsigned long where;
132   // Search from LSB to MSB for first set bit.
133   // Returns zero if no set bit is found.
134   if (_BitScanForward(&where, mask))
135     return static_cast<int>(where);
136   return 32;
137 }
138
139 _LIBCPP_ALWAYS_INLINE int __builtin_ctz(unsigned int mask)
140 {
141   // Win32 and Win64 expectations.
142   static_assert(sizeof(mask) == 4, "");
143   static_assert(sizeof(unsigned long) == 4, "");
144   return __builtin_ctzl(static_cast<unsigned long>(mask));
145 }
146
147 // Returns the number of leading 0-bits in x, starting at the most significant
148 // bit position. If x is 0, the result is undefined.
149 _LIBCPP_ALWAYS_INLINE int __builtin_clzll(unsigned long long mask)
150 {
151   unsigned long where;
152 // BitScanReverse scans from MSB to LSB for first set bit.
153 // Returns 0 if no set bit is found.
154 #if defined(_LIBCPP_HAS_BITSCAN64)
155   if (_BitScanReverse64(&where, mask))
156     return static_cast<int>(63 - where);
157 #else
158   // Scan the high 32 bits.
159   if (_BitScanReverse(&where, static_cast<unsigned long>(mask >> 32)))
160     return static_cast<int>(63 -
161                             (where + 32)); // Create a bit offset from the MSB.
162   // Scan the low 32 bits.
163   if (_BitScanReverse(&where, static_cast<unsigned long>(mask)))
164     return static_cast<int>(63 - where);
165 #endif
166   return 64; // Undefined Behavior.
167 }
168
169 _LIBCPP_ALWAYS_INLINE int __builtin_clzl(unsigned long mask)
170 {
171   unsigned long where;
172   // Search from LSB to MSB for first set bit.
173   // Returns zero if no set bit is found.
174   if (_BitScanReverse(&where, mask))
175     return static_cast<int>(31 - where);
176   return 32; // Undefined Behavior.
177 }
178
179 _LIBCPP_ALWAYS_INLINE int __builtin_clz(unsigned int x)
180 {
181   return __builtin_clzl(x);
182 }
183 #endif // _LIBCPP_MSVC
184
185 #endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H