summaryrefslogtreecommitdiffstats
path: root/libcxx/include/support
diff options
context:
space:
mode:
authorHoward Hinnant <hhinnant@apple.com>2011-12-02 17:22:38 +0000
committerHoward Hinnant <hhinnant@apple.com>2011-12-02 17:22:38 +0000
commita5bc2f877f331db59497c64729378cfff8fd8288 (patch)
tree1dae66fbdce9f58e3280fa8eb75842f19dc7c401 /libcxx/include/support
parent7b8e4bc83f161fccecf6a18c3925901e0e140274 (diff)
downloadbcm5719-llvm-a5bc2f877f331db59497c64729378cfff8fd8288.tar.gz
bcm5719-llvm-a5bc2f877f331db59497c64729378cfff8fd8288.zip
Jean-Daniel: __builtin_popcountll support for Windows
llvm-svn: 145684
Diffstat (limited to 'libcxx/include/support')
-rw-r--r--libcxx/include/support/win32/support.h29
1 files changed, 26 insertions, 3 deletions
diff --git a/libcxx/include/support/win32/support.h b/libcxx/include/support/win32/support.h
index 6ffd3d25890..38ea51e46b1 100644
--- a/libcxx/include/support/win32/support.h
+++ b/libcxx/include/support/win32/support.h
@@ -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 )
{
OpenPOWER on IntegriCloud