summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2015-12-03 17:37:02 -0800
committerThiago Macieira <thiago.macieira@intel.com>2015-12-05 00:11:28 +0000
commitf5f47987ce369aa3f7553e6c0da509461a1ddf1a (patch)
treeaa2687fdc9cdfff5756ccbdf28098932277823e6 /src/corelib
parent2fb42eb4af3444b11e7b1210323637937ef0b005 (diff)
qCount{Leading,Trailing}ZeroBits: Use __builtin_clzs for 16-bit
If possible. The BSF/BSR/TZCNT/LZCNT Intel instruction does not exist for 8-bit. And it's a good idea to use the 32-bit instruction instead of the 16-bit one for that case, to avoid the Length Changing Prefix (LCP). GCC doesn't allow us to use __builtin_cl[tz]s unless BMI is active, while ICC generates the same code either way (Clang understands __has_builtin). Change-Id: I8de47ed6c7be4847b99bffff141c91603c7024dc Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qalgorithms.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/corelib/tools/qalgorithms.h b/src/corelib/tools/qalgorithms.h
index ffa3082d5e..27630aea87 100644
--- a/src/corelib/tools/qalgorithms.h
+++ b/src/corelib/tools/qalgorithms.h
@@ -620,7 +620,11 @@ Q_DECL_RELAXED_CONSTEXPR inline uint qCountTrailingZeroBits(quint8 v) Q_DECL_NOT
Q_DECL_RELAXED_CONSTEXPR inline uint qCountTrailingZeroBits(quint16 v) Q_DECL_NOTHROW
{
#if defined(Q_CC_GNU)
+# if QT_HAS_BUILTIN(__builtin_ctzs) || defined(__BMI__)
+ return v ? __builtin_ctzs(v) : 16U;
+# else
return v ? __builtin_ctz(v) : 16U;
+# endif
#else
unsigned int c = 16; // c will be the number of zero bits on the right
v &= -signed(v);
@@ -679,7 +683,11 @@ Q_DECL_RELAXED_CONSTEXPR inline uint qCountLeadingZeroBits(quint8 v) Q_DECL_NOTH
Q_DECL_RELAXED_CONSTEXPR inline uint qCountLeadingZeroBits(quint16 v) Q_DECL_NOTHROW
{
#if defined(Q_CC_GNU)
+# if QT_HAS_BUILTIN(__builtin_clzs) || defined(__BMI__)
+ return v ? __builtin_clzs(v) : 16U;
+# else
return v ? __builtin_clz(v)-16U : 16U;
+# endif
#else
v = v | (v >> 1);
v = v | (v >> 2);