From 0784d6540f3cc8d2719e47a06e9c71ad44d91884 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sun, 2 Oct 2011 15:57:22 +0000 Subject: [PATCH] * Switched to using '__builtin_popcount' for counting bits in bitfield. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1292 e378c898-3ddf-0310-93e7-cc216c733640 --- rak/algorithm.h | 20 ++++++++++++++++++++ scripts/common.m4 | 15 +++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/rak/algorithm.h b/rak/algorithm.h index 3dfa9f36..5f4f022c 100644 --- a/rak/algorithm.h +++ b/rak/algorithm.h @@ -155,6 +155,26 @@ make_base(_InputIter __first, _InputIter __last, _Ftor __ftor) { return __base; } +template +inline int popcount_wrapper(T t) { +#if USE_BUILTIN_POPCOUNT + if (std::numeric_limits::digits <= std::numeric_limits::digits) + return __builtin_popcount(t); + else + return __builtin_popcountll(t); +#else +#error __builtin_popcount not found. + unsigned int count = 0; + + while (t) { + count += t & 0x1; + t >> 1; + } + + return count; +#endif +} + } #endif diff --git a/scripts/common.m4 b/scripts/common.m4 index 197bf997..dc482c59 100644 --- a/scripts/common.m4 +++ b/scripts/common.m4 @@ -181,6 +181,21 @@ AC_DEFUN([TORRENT_CHECK_MADVISE], [ ]) ]) +AC_DEFUN([TORRENT_CHECK_POPCOUNT], [ + AC_MSG_CHECKING(for __builtin_popcount) + + AC_COMPILE_IFELSE( + [[ + void f() { __builtin_popcount(0); } + ]], + [ + AC_MSG_RESULT(yes) + AC_DEFINE(USE_BUILTIN_POPCOUNT, 1, Use __builtin_popcount.) + ], [ + AC_MSG_RESULT(no) + ]) +]) + AC_DEFUN([TORRENT_CHECK_CACHELINE], [ AC_MSG_CHECKING(for cacheline)