summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qnumeric.h
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2022-07-06 14:59:04 +0200
committerIvan Solovev <ivan.solovev@qt.io>2022-07-08 09:50:05 +0200
commit13868474f9aac7a0138a1a8d226973713b46a9ba (patch)
treeb3a85de5de524302cc5b0489a325498da633dd1b /src/corelib/global/qnumeric.h
parente44edaac2c575df9b2f065b18b716cb21a0c2b75 (diff)
Move numeric functions from qglobal.h to qnumeric.h
Task-number: QTBUG-99313 Change-Id: Ic7f7d6ea6d2b81a8593c7cfe05aee1d62921afd1 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/global/qnumeric.h')
-rw-r--r--src/corelib/global/qnumeric.h75
1 files changed, 75 insertions, 0 deletions
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 <auto V2, typename T> bool qMulOverflow(T v1, T *r)
return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
}
+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)
+{ 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