Jean-Daniel: __builtin_popcountll support for Windows

llvm-svn: 145684
This commit is contained in:
Howard Hinnant 2011-12-02 17:22:38 +00:00
parent 7b8e4bc83f
commit a5bc2f877f
1 changed files with 26 additions and 3 deletions

View File

@ -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 )
{