support.h 6.5 KB

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