diff options
Diffstat (limited to 'include/support')
-rw-r--r-- | include/support/win32/math_win32.h | 2 | ||||
-rw-r--r-- | include/support/win32/support.h | 41 |
2 files changed, 33 insertions, 10 deletions
diff --git a/include/support/win32/math_win32.h b/include/support/win32/math_win32.h index 80eabc1..41c50d6 100644 --- a/include/support/win32/math_win32.h +++ b/include/support/win32/math_win32.h @@ -110,4 +110,4 @@ _LIBCPP_ALWAYS_INLINE int fpclassify( double num ) #endif // _MSC_VER -#endif // _LIBCPP_SUPPORT_WIN32_MATH_WIN32_H
\ No newline at end of file +#endif // _LIBCPP_SUPPORT_WIN32_MATH_WIN32_H diff --git a/include/support/win32/support.h b/include/support/win32/support.h index 6ffd3d2..0b8a912 100644 --- a/include/support/win32/support.h +++ b/include/support/win32/support.h @@ -31,7 +31,7 @@ size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src, size_t nmc, size_t len, mbstate_t *__restrict ps ); size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src, size_t nwc, size_t len, mbstate_t *__restrict ps ); - + #if defined(_MSC_VER) #define snprintf _snprintf @@ -52,9 +52,32 @@ _LIBCPP_ALWAYS_INLINE long double strtold( const char *nptr, char **endptr ) #ifndef __clang__ // MSVC-based Clang also defines _MSC_VER #include <intrin.h> -#define __builtin_popcount __popcnt -#define __builtin_popcountl __popcnt -#define __builtin_popcountll(__i) static_cast<int>(__popcnt64(__i)) + +_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x) { + static const unsigned int m1 = 0x55555555; //binary: 0101... + static const unsigned int m2 = 0x33333333; //binary: 00110011.. + static const unsigned int m4 = 0x0f0f0f0f; //binary: 4 zeros, 4 ones ... + static const unsigned int h01= 0x01010101; //the sum of 256 to the power of 0,1,2,3... + x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits + x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits + x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits + return (x * h01) >> 24; //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) +} + +_LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) { + return __builtin_popcount(static_cast<int>(x)); +} + +_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) { + static const unsigned long long m1 = 0x5555555555555555; //binary: 0101... + static const unsigned long long m2 = 0x3333333333333333; //binary: 00110011.. + static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ... + static const unsigned long long h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3... + x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits + x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits + x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits + return static_cast<int>((x * h01)>>56); //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... +} _LIBCPP_ALWAYS_INLINE int __builtin_ctz( unsigned int x ) { @@ -68,8 +91,8 @@ _LIBCPP_ALWAYS_INLINE int __builtin_ctzl( unsigned long x ) _LIBCPP_ALWAYS_INLINE int __builtin_ctzll( unsigned long long x ) { DWORD r = 0; - _BitScanReverse64(&r, x); - return static_cast<int>(r); + _BitScanReverse64(&r, x); + return static_cast<int>(r); } _LIBCPP_ALWAYS_INLINE int __builtin_clz( unsigned int x ) { @@ -83,10 +106,10 @@ _LIBCPP_ALWAYS_INLINE int __builtin_clzl( unsigned long x ) _LIBCPP_ALWAYS_INLINE int __builtin_clzll( unsigned long long x ) { DWORD r = 0; - _BitScanForward64(&r, x); - return static_cast<int>(r); + _BitScanForward64(&r, x); + return static_cast<int>(r); } #endif // !__clang__ #endif // _MSC_VER -#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H
\ No newline at end of file +#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H |