summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qalgorithms.h
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2012-05-20 13:10:27 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-04-03 11:38:35 +0200
commit52ebf1f1914cea563b52baffc607407dbc6a7313 (patch)
treeb968c5e5b0196a55b201015db8401b56fe8a00a1 /src/corelib/tools/qalgorithms.h
parent76e0223619da02911d02813961ef631a5e02d826 (diff)
With GCC, use __builtin_popcount{,l,ll} for qPopulationCount()
Change-Id: Ied7a98fa17404f1d9678bfbc1ced4817ab52f40e Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Diffstat (limited to 'src/corelib/tools/qalgorithms.h')
-rw-r--r--src/corelib/tools/qalgorithms.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/corelib/tools/qalgorithms.h b/src/corelib/tools/qalgorithms.h
index 2658cfc2c2..fecb9120eb 100644
--- a/src/corelib/tools/qalgorithms.h
+++ b/src/corelib/tools/qalgorithms.h
@@ -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)