diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index cbc11893087..c6b5c5a96f1 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -626,6 +626,29 @@ using qsizetype = QIntegerForSizeof::Signed; template 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 {