summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qnumeric.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/global/qnumeric.h')
-rw-r--r--src/corelib/global/qnumeric.h91
1 files changed, 90 insertions, 1 deletions
diff --git a/src/corelib/global/qnumeric.h b/src/corelib/global/qnumeric.h
index d3a74a33e5..2238c13da0 100644
--- a/src/corelib/global/qnumeric.h
+++ b/src/corelib/global/qnumeric.h
@@ -4,7 +4,14 @@
#ifndef QNUMERIC_H
#define QNUMERIC_H
-#include <QtCore/qglobal.h>
+#if 0
+#pragma qt_class(QtNumeric)
+#endif
+
+#include <QtCore/qtconfigmacros.h>
+#include <QtCore/qtcoreexports.h>
+#include <QtCore/qtypes.h>
+
#include <cmath>
#include <limits>
#include <type_traits>
@@ -312,9 +319,91 @@ template <typename T, T V2> bool qMulOverflow(T v1, std::integral_constant<T, V2
template <auto V2, typename T> bool qMulOverflow(T v1, T *r)
{
+ if constexpr (V2 == 2)
+ return qAddOverflow(v1, v1, 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
+
+namespace QtPrivate {
+template <typename T>
+constexpr inline const T &min(const T &a, const T &b) { return (a < b) ? a : b; }
+}
+
+[[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2)
+{
+ return (qAbs(p1 - p2) * 1000000000000. <= QtPrivate::min(qAbs(p1), qAbs(p2)));
+}
+
+[[nodiscard]] constexpr bool qFuzzyCompare(float p1, float p2)
+{
+ return (qAbs(p1 - p2) * 100000.f <= QtPrivate::min(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