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)