From e8a8cca0fb9663761d67ae3f2ed159f3701b4f50 Mon Sep 17 00:00:00 2001 From: psychocrypt Date: Tue, 23 May 2017 21:39:58 +0200 Subject: [PATCH] refactor `compat_popcnt()` - refactor impleentation - move function to class member method --- autoAdjustHwloc.hpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/autoAdjustHwloc.hpp b/autoAdjustHwloc.hpp index 325d9bc..07c3b4a 100644 --- a/autoAdjustHwloc.hpp +++ b/autoAdjustHwloc.hpp @@ -3,19 +3,6 @@ #include "console.h" #include -/* - * compat_popcnt is adapted from public domain code by: - * Portable Snippets - https://gitub.com/nemequ/portable-snippets - * Created by Evan Nemerson - */ -int compat_popcnt(size_t x) -{ - x = x - ((x >> 1) & (size_t)~(size_t)0/3); - x = (x & (size_t)~(size_t)0/15*3) + ((x >> 2) & (size_t)~(size_t)0/15*3); - x = (x + (x >> 4)) & (size_t)~(size_t)0/255*15; - return (size_t)(x * ((size_t)~(size_t)0/255)) >> (sizeof(size_t) - 1) * 8; -} - #ifdef _WIN32 #include #else @@ -73,6 +60,17 @@ public: private: + /** count bits with ones */ + template + int compat_popcnt(T x) + { + int count = 0; + for( int i = 0; i < sizeof(T) - 1; ++i ) + if( ( ( x >> i ) & 1 ) == 1 ) + ++count; + return count; + } + int rightZeros(size_t v) { int c;