With GCC, use __builtin_popcount{,l,ll} for qPopulationCount()

Change-Id: Ied7a98fa17404f1d9678bfbc1ced4817ab52f40e
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Marc Mutz 2012-05-20 13:10:27 +02:00 committed by The Qt Project
parent 76e0223619
commit 52ebf1f191

View File

@ -518,28 +518,43 @@ Q_OUTOFLINE_TEMPLATE RandomAccessIterator qBinaryFindHelper(RandomAccessIterator
Q_DECL_CONSTEXPR inline uint qPopulationCount(quint32 v)
{
#ifdef Q_CC_GNU
return __builtin_popcount(v);
#else
// See http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
return
(((v ) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f +
(((v >> 12) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f +
(((v >> 24) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
#endif
}
Q_DECL_CONSTEXPR inline uint qPopulationCount(quint8 v)
{
#ifdef Q_CC_GNU
return __builtin_popcount(v);
#else
return
(((v ) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
#endif
}
Q_DECL_CONSTEXPR inline uint qPopulationCount(quint16 v)
{
#ifdef Q_CC_GNU
return __builtin_popcount(v);
#else
return
(((v ) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f +
(((v >> 12) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
#endif
}
Q_DECL_CONSTEXPR inline uint qPopulationCount(quint64 v)
{
#ifdef Q_CC_GNU
return __builtin_popcountll(v);
#else
return
(((v ) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f +
(((v >> 12) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f +
@ -547,6 +562,7 @@ Q_DECL_CONSTEXPR inline uint qPopulationCount(quint64 v)
(((v >> 36) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f +
(((v >> 48) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f +
(((v >> 60) & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
#endif
}
Q_DECL_CONSTEXPR inline uint qPopulationCount(long unsigned int v)