From 13868474f9aac7a0138a1a8d226973713b46a9ba Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Wed, 6 Jul 2022 14:59:04 +0200 Subject: Move numeric functions from qglobal.h to qnumeric.h Task-number: QTBUG-99313 Change-Id: Ic7f7d6ea6d2b81a8593c7cfe05aee1d62921afd1 Reviewed-by: Marc Mutz --- src/corelib/global/qnumeric.h | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'src/corelib/global/qnumeric.h') 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 -- cgit v1.2.3