From a136d77458514201b8593824dca33289b0c9b69e Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 9 Dec 2016 16:33:18 -1000 Subject: [PATCH] Use __builtin_popcountll for systems that support it --- include/zeus/Math.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/zeus/Math.hpp b/include/zeus/Math.hpp index d9732d3..8713aac 100644 --- a/include/zeus/Math.hpp +++ b/include/zeus/Math.hpp @@ -105,6 +105,9 @@ int ceilingPowerOfTwo(int x); template typename std::enable_if::value && std::is_integral::value, int>::type PopCount(U x) { +#if __GNUC__ >= 4 + return __builtin_popcountll(x); +#else const U m1 = U(0x5555555555555555); // binary: 0101... const U m2 = U(0x3333333333333333); // binary: 00110011.. const U m4 = U(0x0f0f0f0f0f0f0f0f); // binary: 4 zeros, 4 ones ... @@ -114,6 +117,7 @@ typename std::enable_if::value && std::is_integral::value, i 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) >> ((sizeof(U) - 1) * 8); // returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... +#endif } template