From 13868474f9aac7a0138a1a8d226973713b46a9ba Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Wed, 6 Jul 2022 14:59:04 +0200 Subject: [PATCH] Move numeric functions from qglobal.h to qnumeric.h Task-number: QTBUG-99313 Change-Id: Ic7f7d6ea6d2b81a8593c7cfe05aee1d62921afd1 Reviewed-by: Marc Mutz --- src/corelib/global/qglobal.cpp | 18 ++++---- src/corelib/global/qglobal.h | 75 ---------------------------------- src/corelib/global/qnumeric.h | 75 ++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 84 deletions(-) diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index c894471ad6..2cbbd8d68d 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -1113,7 +1113,7 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined"); */ /*! \fn template T qAbs(const T &t) - \relates + \relates Compares \a t to the 0 of type T and returns the absolute value. Thus if T is \e {double}, then \a t is compared to @@ -1125,7 +1125,7 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined"); */ /*! \fn int qRound(double d) - \relates + \relates Rounds \a d to the nearest integer. @@ -1139,7 +1139,7 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined"); */ /*! \fn int qRound(float d) - \relates + \relates Rounds \a d to the nearest integer. @@ -1153,7 +1153,7 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined"); */ /*! \fn qint64 qRound64(double d) - \relates + \relates Rounds \a d to the nearest 64-bit integer. @@ -1167,7 +1167,7 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined"); */ /*! \fn qint64 qRound64(float d) - \relates + \relates Rounds \a d to the nearest 64-bit integer. @@ -4468,7 +4468,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters) /*! \fn bool qFuzzyCompare(double p1, double p2) - \relates + \relates \since 4.4 \threadsafe @@ -4488,7 +4488,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters) /*! \fn bool qFuzzyCompare(float p1, float p2) - \relates + \relates \since 4.4 \threadsafe @@ -4501,7 +4501,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters) /*! \fn bool qFuzzyIsNull(double d) - \relates + \relates \since 4.4 \threadsafe @@ -4510,7 +4510,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters) /*! \fn bool qFuzzyIsNull(float f) - \relates + \relates \since 4.4 \threadsafe diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 559dab889f..bbff1d2926 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -669,43 +669,6 @@ using qsizetype = QIntegerForSizeof::Signed; Utility macros and inline functions */ -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) -{ return d >= 0.0f ? int(d + 0.5f) : int(d - 0.5f); } - -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 - #ifndef Q_FORWARD_DECLARE_OBJC_CLASS # ifdef __OBJC__ # define Q_FORWARD_DECLARE_OBJC_CLASS(classname) @class classname @@ -995,41 +958,6 @@ constexpr inline QTypeTraits::Promoted qBound(const U &min, const T &val, return qMax(min, qMin(max, val)); } -[[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2) -{ - return (qAbs(p1 - p2) * 1000000000000. <= qMin(qAbs(p1), qAbs(p2))); -} - -[[nodiscard]] constexpr bool qFuzzyCompare(float p1, float p2) -{ - return (qAbs(p1 - p2) * 100000.f <= qMin(qAbs(p1), qAbs(p2))); -} - -[[nodiscard]] constexpr bool qFuzzyIsNull(double d) -{ - return qAbs(d) <= 0.000000000001; -} - -[[nodiscard]] constexpr bool qFuzzyIsNull(float f) -{ - return qAbs(f) <= 0.00001f; -} - -QT_WARNING_PUSH -QT_WARNING_DISABLE_FLOAT_COMPARE - -[[nodiscard]] constexpr bool qIsNull(double d) noexcept -{ - return d == 0.0; -} - -[[nodiscard]] constexpr bool qIsNull(float f) noexcept -{ - return f == 0.0f; -} - -QT_WARNING_POP - /* Compilers which follow outdated template instantiation rules require a class to have a comparison operator to exist when @@ -1289,9 +1217,6 @@ Q_CORE_EXPORT bool qEnvironmentVariableIsEmpty(const char *varName) noexcept; Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept; Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept; -inline int qIntCast(double f) { return int(f); } -inline int qIntCast(float f) { return int(f); } - #define QT_MODULE(x) #if defined(QT_BOOTSTRAPPED) || defined(QT_USE_PROTECTED_VISIBILITY) || !defined(__ELF__) || defined(__PIC__) diff --git a/src/corelib/global/qnumeric.h b/src/corelib/global/qnumeric.h index b10936b38f..24fa782f06 100644 --- a/src/corelib/global/qnumeric.h +++ b/src/corelib/global/qnumeric.h @@ -319,6 +319,81 @@ template bool qMulOverflow(T v1, T *r) return qMulOverflow(v1, std::integral_constant{}, r); } +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) +{ return d >= 0.0f ? int(d + 0.5f) : int(d - 0.5f); } + +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 + +[[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2) +{ + return (qAbs(p1 - p2) * 1000000000000. <= qMin(qAbs(p1), qAbs(p2))); +} + +[[nodiscard]] constexpr bool qFuzzyCompare(float p1, float p2) +{ + return (qAbs(p1 - p2) * 100000.f <= qMin(qAbs(p1), qAbs(p2))); +} + +[[nodiscard]] constexpr bool qFuzzyIsNull(double d) +{ + return qAbs(d) <= 0.000000000001; +} + +[[nodiscard]] constexpr bool qFuzzyIsNull(float f) +{ + return qAbs(f) <= 0.00001f; +} + +QT_WARNING_PUSH +QT_WARNING_DISABLE_FLOAT_COMPARE + +[[nodiscard]] constexpr bool qIsNull(double d) noexcept +{ + return d == 0.0; +} + +[[nodiscard]] constexpr bool qIsNull(float f) noexcept +{ + return f == 0.0f; +} + +QT_WARNING_POP + +inline int qIntCast(double f) { return int(f); } +inline int qIntCast(float f) { return int(f); } + QT_END_NAMESPACE #endif // QNUMERIC_H