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>
This commit is contained in:
parent
6fd301b231
commit
aa136d46e1
@ -626,6 +626,29 @@ using qsizetype = QIntegerForSizeof<std::size_t>::Signed;
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr inline T qAbs(const T &t) { return t >= 0 ? t : -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)
|
constexpr inline int qRound(double d)
|
||||||
{ return d >= 0.0 ? int(d + 0.5) : int(d - 0.5); }
|
{ return d >= 0.0 ? int(d + 0.5) : int(d - 0.5); }
|
||||||
constexpr inline int qRound(float d)
|
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); }
|
{ return d >= 0.0 ? qint64(d + 0.5) : qint64(d - 0.5); }
|
||||||
constexpr inline qint64 qRound64(float d)
|
constexpr inline qint64 qRound64(float d)
|
||||||
{ return d >= 0.0f ? qint64(d + 0.5f) : qint64(d - 0.5f); }
|
{ return d >= 0.0f ? qint64(d + 0.5f) : qint64(d - 0.5f); }
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace QTypeTraits {
|
namespace QTypeTraits {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user