summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qnumeric.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-12-01 11:54:43 +0100
committerUlf Hermann <ulf.hermann@qt.io>2020-12-07 15:37:25 +0100
commit2b8674a2fc67089ff87d3751ef2977cbb37616a2 (patch)
treec767cf155ab1f8acee28c3aeb40ea82701afe65d /src/corelib/global/qnumeric.cpp
parent9082cc8e8d5a6441dabe5e7a95bc0cd9085b95fe (diff)
Make the overflow math functions public
As the standard library does not provide equivalent functionality, those functions are really useful to everyone, not only to Qt itself. [ChangeLog][QtCore] The overflow-safe math functions qAddOverflow(), qSubOverflow(), qMulOverflow() were added. Change-Id: I5a0a4742dae9b6f0caa65d0e93edcf3658bee9f8 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/global/qnumeric.cpp')
-rw-r--r--src/corelib/global/qnumeric.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/corelib/global/qnumeric.cpp b/src/corelib/global/qnumeric.cpp
index e00f0af283e..63055a0b61c 100644
--- a/src/corelib/global/qnumeric.cpp
+++ b/src/corelib/global/qnumeric.cpp
@@ -254,5 +254,99 @@ Q_CORE_EXPORT quint64 qFloatDistance(double a, double b)
return a > b ? d2i(a) - d2i(b) : d2i(b) - d2i(a);
}
+/*!
+ \fn template<typename T> bool qAddOverflow(T v1, T v2, T *result)
+ \since 6.1
+
+ Adds two values \a v1 and \a v2, of a numeric type \c T and records the
+ value in \a result. If the addition overflows the valid range for type \c T,
+ returns \c true, otherwise returns \c false.
+
+ An implementation is guaranteed to be available for 8-, 16-, and 32-bit
+ integer types, as well as integer types of the size of a pointer. Overflow
+ math for other types, if available, is considered private API.
+*/
+
+/*!
+ \fn template <typename T, T V2> bool qAddOverflow(T v1, std::integral_constant<T, V2>, T *r)
+ \since 6.1
+ \internal
+
+ Equivalent to qAddOverflow(v1, v2, r) with \a v1 as first argument, the
+ compile time constant \c V2 as second argument, and \a r as third argument.
+*/
+
+/*!
+ \fn template <auto V2, typename T> bool qAddOverflow(T v1, T *r)
+ \since 6.1
+ \internal
+
+ Equivalent to qAddOverflow(v1, v2, r) with \a v1 as first argument, the
+ compile time constant \c V2 as second argument, and \a r as third argument.
+*/
+
+/*!
+ \fn template<typename T> bool qSubOverflow(T v1, T v2, T *result)
+ \since 6.1
+
+ Subtracts \a v2 from \a v1 and records the resulting value in \a result. If
+ the subtraction overflows the valid range for type \c T, returns \c true,
+ otherwise returns \c false.
+
+ An implementation is guaranteed to be available for 8-, 16-, and 32-bit
+ integer types, as well as integer types of the size of a pointer. Overflow
+ math for other types, if available, is considered private API.
+*/
+
+/*!
+ \fn template <typename T, T V2> bool qSubOverflow(T v1, std::integral_constant<T, V2>, T *r)
+ \since 6.1
+ \internal
+
+ Equivalent to qSubOverflow(v1, v2, r) with \a v1 as first argument, the
+ compile time constant \c V2 as second argument, and \a r as third argument.
+*/
+
+/*!
+ \fn template <auto V2, typename T> bool qSubOverflow(T v1, T *r)
+ \since 6.1
+ \internal
+
+ Equivalent to qSubOverflow(v1, v2, r) with \a v1 as first argument, the
+ compile time constant \c V2 as second argument, and \a r as third argument.
+*/
+
+/*!
+ \fn template<typename T> bool qMulOverflow(T v1, T v2, T *result)
+ \since 6.1
+
+ Multiplies \a v1 and \a v2, and records the resulting value in \a result. If
+ the multiplication overflows the valid range for type \c T, returns
+ \c true, otherwise returns \c false.
+
+ An implementation is guaranteed to be available for 8-, 16-, and 32-bit
+ integer types, as well as integer types of the size of a pointer. Overflow
+ math for other types, if available, is considered private API.
+*/
+
+/*!
+ \fn template <typename T, T V2> bool qMulOverflow(T v1, std::integral_constant<T, V2>, T *r)
+ \since 6.1
+ \internal
+
+ Equivalent to qMulOverflow(v1, v2, r) with \a v1 as first argument, the
+ compile time constant \c V2 as second argument, and \a r as third argument.
+ This can be faster than calling the version with only variable arguments.
+*/
+
+/*!
+ \fn template <auto V2, typename T> bool qMulOverflow(T v1, T *r)
+ \since 6.1
+ \internal
+
+ Equivalent to qMulOverflow(v1, v2, r) with \a v1 as first argument, the
+ compile time constant \c V2 as second argument, and \a r as third argument.
+ This can be faster than calling the version with only variable arguments.
+*/
QT_END_NAMESPACE