Skip to content

Commit

Permalink
libtorrent: Make cpu popcount default (#10)
Browse files Browse the repository at this point in the history
* libtorrent: Add CPU popcount support

This commit adds a native c-implementation to calculate the population count of the bitfield. It may be faster for CPUs which don't support SSE instructions. This is disabled by default and must be enabled by configuring with `--enable-cpu-popcount`.

* libtorrent: Make cpu popcount default

Benchmarked 30-50% reduction in CPU usage for uploading.
  • Loading branch information
stickz authored Jul 5, 2024
1 parent acc2c21 commit 5e93c1d
Showing 1 changed file with 0 additions and 21 deletions.
21 changes: 0 additions & 21 deletions libtorrent/rak/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ make_base(_InputIter __first, _InputIter __last, _Ftor __ftor) {
return __base;
}

#if USE_CPU_POPCOUNT
inline int countBit1Fast(unsigned int n) {
n = (n & 0x55555555u) + ((n >> 1) & 0x55555555u);
n = (n & 0x33333333u) + ((n >> 2) & 0x33333333u);
Expand All @@ -165,30 +164,10 @@ inline int countBit1Fast(unsigned int n) {
n = (n & 0x0000ffffu) + ((n >>16) & 0x0000ffffu);
return n;
}
#endif

template<typename T>
inline int popcount_wrapper(T t) {
#if USE_CPU_POPCOUNT
return countBit1Fast(t);
#else
#if USE_BUILTIN_POPCOUNT
if (std::numeric_limits<T>::digits <= std::numeric_limits<unsigned int>::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
}

}
Expand Down

0 comments on commit 5e93c1d

Please sign in to comment.