diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-09-17 17:36:30 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-11-16 13:27:31 +0200 |
commit | aa136d46e100e65f0d9326ac9319de95673cd6d7 (patch) | |
tree | 324e0c49c50055622e10545891e0a0b6ef672481 /src/corelib/global/qglobal.h | |
parent | 6fd301b231ea571860e308236c83de01c7d4985d (diff) |
Optimize qRound
These variants produce the same code as if std::round was compiled
with -ffast-math.
Change-Id: I8e0d7601928a511b9bc8b8f969cfd94df47c3784
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/global/qglobal.h')
-rw-r--r-- | src/corelib/global/qglobal.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index cbc1189308..c6b5c5a96f 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -626,6 +626,29 @@ using qsizetype = QIntegerForSizeof<std::size_t>::Signed; template <typename T> constexpr inline T qAbs(const T &t) { return t >= 0 ? t : -t; } +// gcc < 10 doesn't have __has_builtin +#if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG) +// ARM64 has a single instruction that can do C++ rounding with conversion to integer. +// Note current clang versions have non-constexpr __builtin_round, ### allow clang this path when they fix it. +constexpr inline int qRound(double d) +{ return int(__builtin_round(d)); } +constexpr inline int qRound(float f) +{ return int(__builtin_roundf(f)); } +constexpr inline qint64 qRound64(double d) +{ return qint64(__builtin_round(d)); } +constexpr inline qint64 qRound64(float f) +{ return qint64(__builtin_roundf(f)); } +#elif defined(__SSE2__) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU)) +// SSE has binary operations directly on floating point making copysign fast +constexpr inline int qRound(double d) +{ return int(d + __builtin_copysign(0.5, d)); } +constexpr inline int qRound(float f) +{ return int(f + __builtin_copysignf(0.5f, f)); } +constexpr inline qint64 qRound64(double d) +{ return qint64(d + __builtin_copysign(0.5, d)); } +constexpr inline qint64 qRound64(float f) +{ return qint64(f + __builtin_copysignf(0.5f, f)); } +#else constexpr inline int qRound(double d) { return d >= 0.0 ? int(d + 0.5) : int(d - 0.5); } constexpr inline int qRound(float d) @@ -635,6 +658,7 @@ constexpr inline qint64 qRound64(double d) { return d >= 0.0 ? qint64(d + 0.5) : qint64(d - 0.5); } constexpr inline qint64 qRound64(float d) { return d >= 0.0f ? qint64(d + 0.5f) : qint64(d - 0.5f); } +#endif namespace QTypeTraits { |