[cfe-commits] [libcxx] r145684 - /libcxx/trunk/include/support/win32/support.h
Howard Hinnant
hhinnant at apple.com
Fri Dec 2 09:22:39 PST 2011
Author: hhinnant
Date: Fri Dec 2 11:22:38 2011
New Revision: 145684
URL: http://llvm.org/viewvc/llvm-project?rev=145684&view=rev
Log:
Jean-Daniel: __builtin_popcountll support for Windows
Modified:
libcxx/trunk/include/support/win32/support.h
Modified: libcxx/trunk/include/support/win32/support.h
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/support/win32/support.h?rev=145684&r1=145683&r2=145684&view=diff
==============================================================================
--- libcxx/trunk/include/support/win32/support.h (original)
+++ libcxx/trunk/include/support/win32/support.h Fri Dec 2 11:22:38 2011
@@ -52,9 +52,32 @@
#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 )
{
More information about the cfe-commits
mailing list