1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QNUMERIC_H
5#define QNUMERIC_H
6
7#if 0
8#pragma qt_class(QtNumeric)
9#endif
10
11#include <QtCore/qtconfigmacros.h>
12#include <QtCore/qtcoreexports.h>
13#include <QtCore/qtypes.h>
14
15#include <cmath>
16#include <limits>
17#include <type_traits>
18
19// min() and max() may be #defined by windows.h if that is included before, but we need them
20// for std::numeric_limits below. You should not use the min() and max() macros, so we just #undef.
21#ifdef min
22# undef min
23# undef max
24#endif
25
26#if defined(Q_CC_MSVC)
27# include <intrin.h>
28# include <float.h>
29# if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64)
30# define Q_INTRINSIC_MUL_OVERFLOW64
31# define Q_UMULH(v1, v2) __umulh(v1, v2);
32# define Q_SMULH(v1, v2) __mulh(v1, v2);
33# pragma intrinsic(__umulh)
34# pragma intrinsic(__mulh)
35# endif
36#endif
37
38# if defined(Q_OS_INTEGRITY) && defined(Q_PROCESSOR_ARM_64)
39# include <arm64_ghs.h>
40# define Q_INTRINSIC_MUL_OVERFLOW64
41# define Q_UMULH(v1, v2) __MULUH64(v1, v2);
42# define Q_SMULH(v1, v2) __MULSH64(v1, v2);
43#endif
44
45QT_BEGIN_NAMESPACE
46
47// To match std::is{inf,nan,finite} functions:
48template <typename T>
49constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
50qIsInf(T) { return false; }
51template <typename T>
52constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
53qIsNaN(T) { return false; }
54template <typename T>
55constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
56qIsFinite(T) { return true; }
57
58// Floating-point types (see qfloat16.h for its overloads).
59Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(double d);
60Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(double d);
61Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(double d);
62Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(double val);
63Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(float f);
64Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(float f);
65Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(float f);
66Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(float val);
67
68#if QT_CONFIG(signaling_nan)
69Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qSNaN();
70#endif
71Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN();
72Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf();
73
74Q_CORE_EXPORT quint32 qFloatDistance(float a, float b);
75Q_CORE_EXPORT quint64 qFloatDistance(double a, double b);
76
77#define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)())
78#if QT_CONFIG(signaling_nan)
79# define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)())
80#endif
81#define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)())
82
83// Overflow math.
84// This provides efficient implementations for int, unsigned, qsizetype and
85// size_t. Implementations for 8- and 16-bit types will work but may not be as
86// efficient. Implementations for 64-bit may be missing on 32-bit platforms.
87
88#if (Q_CC_GNU >= 500 || __has_builtin(__builtin_add_overflow)) \
89 && !(QT_POINTER_SIZE == 4 && defined(Q_CC_CLANG))
90// GCC 5 and Clang 3.8 have builtins to detect overflows
91// 32 bit Clang has the builtins but tries to link a library which hasn't
92#define Q_INTRINSIC_MUL_OVERFLOW64
93
94template <typename T> inline
95typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool>
96qAddOverflow(T v1, T v2, T *r)
97{ return __builtin_add_overflow(v1, v2, r); }
98
99template <typename T> inline
100typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool>
101qSubOverflow(T v1, T v2, T *r)
102{ return __builtin_sub_overflow(v1, v2, r); }
103
104template <typename T> inline
105typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool>
106qMulOverflow(T v1, T v2, T *r)
107{ return __builtin_mul_overflow(v1, v2, r); }
108
109#else
110// Generic implementations
111
112template <typename T> inline typename std::enable_if_t<std::is_unsigned_v<T>, bool>
113qAddOverflow(T v1, T v2, T *r)
114{
115 // unsigned additions are well-defined
116 *r = v1 + v2;
117 return v1 > T(v1 + v2);
118}
119
120template <typename T> inline typename std::enable_if_t<std::is_signed_v<T>, bool>
121qAddOverflow(T v1, T v2, T *r)
122{
123 // Here's how we calculate the overflow:
124 // 1) unsigned addition is well-defined, so we can always execute it
125 // 2) conversion from unsigned back to signed is implementation-
126 // defined and in the implementations we use, it's a no-op.
127 // 3) signed integer overflow happens if the sign of the two input operands
128 // is the same but the sign of the result is different. In other words,
129 // the sign of the result must be the same as the sign of either
130 // operand.
131
132 using U = typename std::make_unsigned_t<T>;
133 *r = T(U(v1) + U(v2));
134
135 // If int is two's complement, assume all integer types are too.
136 if (std::is_same_v<int32_t, int>) {
137 // Two's complement equivalent (generates slightly shorter code):
138 // x ^ y is negative if x and y have different signs
139 // x & y is negative if x and y are negative
140 // (x ^ z) & (y ^ z) is negative if x and z have different signs
141 // AND y and z have different signs
142 return ((v1 ^ *r) & (v2 ^ *r)) < 0;
143 }
144
145 bool s1 = (v1 < 0);
146 bool s2 = (v2 < 0);
147 bool sr = (*r < 0);
148 return s1 != sr && s2 != sr;
149 // also: return s1 == s2 && s1 != sr;
150}
151
152template <typename T> inline typename std::enable_if_t<std::is_unsigned_v<T>, bool>
153qSubOverflow(T v1, T v2, T *r)
154{
155 // unsigned subtractions are well-defined
156 *r = v1 - v2;
157 return v1 < v2;
158}
159
160template <typename T> inline typename std::enable_if_t<std::is_signed_v<T>, bool>
161qSubOverflow(T v1, T v2, T *r)
162{
163 // See above for explanation. This is the same with some signs reversed.
164 // We can't use qAddOverflow(v1, -v2, r) because it would be UB if
165 // v2 == std::numeric_limits<T>::min().
166
167 using U = typename std::make_unsigned_t<T>;
168 *r = T(U(v1) - U(v2));
169
170 if (std::is_same_v<int32_t, int>)
171 return ((v1 ^ *r) & (~v2 ^ *r)) < 0;
172
173 bool s1 = (v1 < 0);
174 bool s2 = !(v2 < 0);
175 bool sr = (*r < 0);
176 return s1 != sr && s2 != sr;
177 // also: return s1 == s2 && s1 != sr;
178}
179
180template <typename T> inline
181typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool>
182qMulOverflow(T v1, T v2, T *r)
183{
184 // use the next biggest type
185 // Note: for 64-bit systems where __int128 isn't supported, this will cause an error.
186 using LargerInt = QIntegerForSize<sizeof(T) * 2>;
187 using Larger = typename std::conditional_t<std::is_signed_v<T>,
188 typename LargerInt::Signed, typename LargerInt::Unsigned>;
189 Larger lr = Larger(v1) * Larger(v2);
190 *r = T(lr);
191 return lr > (std::numeric_limits<T>::max)() || lr < (std::numeric_limits<T>::min)();
192}
193
194# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
195template <> inline bool qMulOverflow(quint64 v1, quint64 v2, quint64 *r)
196{
197 *r = v1 * v2;
198 return Q_UMULH(v1, v2);
199}
200template <> inline bool qMulOverflow(qint64 v1, qint64 v2, qint64 *r)
201{
202 // This is slightly more complex than the unsigned case above: the sign bit
203 // of 'low' must be replicated as the entire 'high', so the only valid
204 // values for 'high' are 0 and -1. Use unsigned multiply since it's the same
205 // as signed for the low bits and use a signed right shift to verify that
206 // 'high' is nothing but sign bits that match the sign of 'low'.
207
208 qint64 high = Q_SMULH(v1, v2);
209 *r = qint64(quint64(v1) * quint64(v2));
210 return (*r >> 63) != high;
211}
212
213# if defined(Q_OS_INTEGRITY) && defined(Q_PROCESSOR_ARM_64)
214template <> inline bool qMulOverflow(uint64_t v1, uint64_t v2, uint64_t *r)
215{
216 return qMulOverflow<quint64>(v1,v2,reinterpret_cast<quint64*>(r));
217}
218
219template <> inline bool qMulOverflow(int64_t v1, int64_t v2, int64_t *r)
220{
221 return qMulOverflow<qint64>(v1,v2,reinterpret_cast<qint64*>(r));
222}
223# endif // OS_INTEGRITY ARM64
224# endif // Q_INTRINSIC_MUL_OVERFLOW64
225
226# if defined(Q_CC_MSVC) && defined(Q_PROCESSOR_X86)
227// We can use intrinsics for the unsigned operations with MSVC
228template <> inline bool qAddOverflow(unsigned v1, unsigned v2, unsigned *r)
229{ return _addcarry_u32(0, v1, v2, r); }
230
231// 32-bit qMulOverflow is fine with the generic code above
232
233template <> inline bool qAddOverflow(quint64 v1, quint64 v2, quint64 *r)
234{
235# if defined(Q_PROCESSOR_X86_64)
236 return _addcarry_u64(0, v1, v2, reinterpret_cast<unsigned __int64 *>(r));
237# else
238 uint low, high;
239 uchar carry = _addcarry_u32(0, unsigned(v1), unsigned(v2), &low);
240 carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high);
241 *r = (quint64(high) << 32) | low;
242 return carry;
243# endif // !x86-64
244}
245# endif // MSVC X86
246#endif // !GCC
247
248// Implementations for addition, subtraction or multiplication by a
249// compile-time constant. For addition and subtraction, we simply call the code
250// that detects overflow at runtime. For multiplication, we compare to the
251// maximum possible values before multiplying to ensure no overflow happens.
252
253template <typename T, T V2> bool qAddOverflow(T v1, std::integral_constant<T, V2>, T *r)
254{
255 return qAddOverflow(v1, V2, r);
256}
257
258template <auto V2, typename T> bool qAddOverflow(T v1, T *r)
259{
260 return qAddOverflow(v1, std::integral_constant<T, V2>{}, r);
261}
262
263template <typename T, T V2> bool qSubOverflow(T v1, std::integral_constant<T, V2>, T *r)
264{
265 return qSubOverflow(v1, V2, r);
266}
267
268template <auto V2, typename T> bool qSubOverflow(T v1, T *r)
269{
270 return qSubOverflow(v1, std::integral_constant<T, V2>{}, r);
271}
272
273template <typename T, T V2> bool qMulOverflow(T v1, std::integral_constant<T, V2>, T *r)
274{
275 // Runtime detection for anything smaller than or equal to a register
276 // width, as most architectures' multiplication instructions actually
277 // produce a result twice as wide as the input registers, allowing us to
278 // efficiently detect the overflow.
279 if constexpr (sizeof(T) <= sizeof(qregisteruint)) {
280 return qMulOverflow(v1, V2, r);
281
282#ifdef Q_INTRINSIC_MUL_OVERFLOW64
283 } else if constexpr (sizeof(T) <= sizeof(quint64)) {
284 // If we have intrinsics detecting overflow of 64-bit multiplications,
285 // then detect overflows through them up to 64 bits.
286 return qMulOverflow(v1, V2, r);
287#endif
288
289 } else if constexpr (V2 == 0 || V2 == 1) {
290 // trivial cases (and simplify logic below due to division by zero)
291 *r = v1 * V2;
292 return false;
293 } else if constexpr (V2 == -1) {
294 // multiplication by -1 is valid *except* for signed minimum values
295 // (necessary to avoid diving min() by -1, which is an overflow)
296 if (v1 < 0 && v1 == (std::numeric_limits<T>::min)())
297 return true;
298 *r = -v1;
299 return false;
300 } else {
301 // For 64-bit multiplications on 32-bit platforms, let's instead compare v1
302 // against the bounds that would overflow.
303 constexpr T Highest = (std::numeric_limits<T>::max)() / V2;
304 constexpr T Lowest = (std::numeric_limits<T>::min)() / V2;
305 if constexpr (Highest > Lowest) {
306 if (v1 > Highest || v1 < Lowest)
307 return true;
308 } else {
309 // this can only happen if V2 < 0
310 static_assert(V2 < 0);
311 if (v1 > Lowest || v1 < Highest)
312 return true;
313 }
314
315 *r = v1 * V2;
316 return false;
317 }
318}
319
320template <auto V2, typename T> bool qMulOverflow(T v1, T *r)
321{
322 if constexpr (V2 == 2)
323 return qAddOverflow(v1, v1, r);
324 return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
325}
326
327template <typename T>
328constexpr inline T qAbs(const T &t) { return t >= 0 ? t : -t; }
329
330// gcc < 10 doesn't have __has_builtin
331#if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG)
332// ARM64 has a single instruction that can do C++ rounding with conversion to integer.
333// Note current clang versions have non-constexpr __builtin_round, ### allow clang this path when they fix it.
334constexpr inline int qRound(double d)
335{ return int(__builtin_round(d)); }
336constexpr inline int qRound(float f)
337{ return int(__builtin_roundf(f)); }
338constexpr inline qint64 qRound64(double d)
339{ return qint64(__builtin_round(d)); }
340constexpr inline qint64 qRound64(float f)
341{ return qint64(__builtin_roundf(f)); }
342#elif defined(__SSE2__) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU))
343// SSE has binary operations directly on floating point making copysign fast
344constexpr inline int qRound(double d)
345{ return int(d + __builtin_copysign(0.5, d)); }
346constexpr inline int qRound(float f)
347{ return int(f + __builtin_copysignf(0.5f, f)); }
348constexpr inline qint64 qRound64(double d)
349{ return qint64(d + __builtin_copysign(0.5, d)); }
350constexpr inline qint64 qRound64(float f)
351{ return qint64(f + __builtin_copysignf(0.5f, f)); }
352#else
353constexpr inline int qRound(double d)
354{ return d >= 0.0 ? int(d + 0.5) : int(d - 0.5); }
355constexpr inline int qRound(float d)
356{ return d >= 0.0f ? int(d + 0.5f) : int(d - 0.5f); }
357
358constexpr inline qint64 qRound64(double d)
359{ return d >= 0.0 ? qint64(d + 0.5) : qint64(d - 0.5); }
360constexpr inline qint64 qRound64(float d)
361{ return d >= 0.0f ? qint64(d + 0.5f) : qint64(d - 0.5f); }
362#endif
363
364namespace QtPrivate {
365template <typename T>
366constexpr inline const T &min(const T &a, const T &b) { return (a < b) ? a : b; }
367}
368
369[[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2)
370{
371 return (qAbs(t: p1 - p2) * 1000000000000. <= QtPrivate::min(a: qAbs(t: p1), b: qAbs(t: p2)));
372}
373
374[[nodiscard]] constexpr bool qFuzzyCompare(float p1, float p2)
375{
376 return (qAbs(t: p1 - p2) * 100000.f <= QtPrivate::min(a: qAbs(t: p1), b: qAbs(t: p2)));
377}
378
379[[nodiscard]] constexpr bool qFuzzyIsNull(double d)
380{
381 return qAbs(t: d) <= 0.000000000001;
382}
383
384[[nodiscard]] constexpr bool qFuzzyIsNull(float f)
385{
386 return qAbs(t: f) <= 0.00001f;
387}
388
389QT_WARNING_PUSH
390QT_WARNING_DISABLE_FLOAT_COMPARE
391
392[[nodiscard]] constexpr bool qIsNull(double d) noexcept
393{
394 return d == 0.0;
395}
396
397[[nodiscard]] constexpr bool qIsNull(float f) noexcept
398{
399 return f == 0.0f;
400}
401
402QT_WARNING_POP
403
404inline int qIntCast(double f) { return int(f); }
405inline int qIntCast(float f) { return int(f); }
406
407QT_END_NAMESPACE
408
409#endif // QNUMERIC_H
410

source code of qtbase/src/corelib/global/qnumeric.h