Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnumeric.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2025 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:critical reason:data-parser
5
6#ifndef QNUMERIC_H
7#define QNUMERIC_H
8
9#if 0
10#pragma qt_class(QtNumeric)
11#endif
12
13#include <QtCore/qassert.h>
14#include <QtCore/qminmax.h>
15#include <QtCore/qtconfigmacros.h>
16#include <QtCore/qtcoreexports.h>
17#include <QtCore/qtypes.h>
18
19#include <cmath>
20#include <limits>
21#include <QtCore/q20type_traits.h>
22
23// min() and max() may be #defined by windows.h if that is included before, but we need them
24// for std::numeric_limits below. You should not use the min() and max() macros, so we just #undef.
25#ifdef min
26# undef min
27# undef max
28#endif
29
30//
31// SIMDe (SIMD Everywhere) can't be used if intrin.h has been included as many definitions
32// conflict. Defining Q_NUMERIC_NO_INTRINSICS allows SIMDe users to use Qt, at the cost of
33// falling back to the prior implementations of qMulOverflow and qAddOverflow.
34//
35#if defined(Q_CC_MSVC) && !defined(Q_NUMERIC_NO_INTRINSICS)
36# include <intrin.h>
37# include <float.h>
38# if defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_X86_64)
39# define Q_HAVE_ADDCARRY
40# endif
41# if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64)
42# define Q_INTRINSIC_MUL_OVERFLOW64
43# define Q_UMULH(v1, v2) __umulh(v1, v2)
44# define Q_SMULH(v1, v2) __mulh(v1, v2)
45# pragma intrinsic(__umulh)
46# pragma intrinsic(__mulh)
47# endif
48#endif
49
50QT_BEGIN_NAMESPACE
51
52// To match std::is{inf,nan,finite} functions:
53template <typename T>
54constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
55qIsInf(T) { return false; }
56template <typename T>
57constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
58qIsNaN(T) { return false; }
59template <typename T>
60constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
61qIsFinite(T) { return true; }
62
63// Floating-point types (see qfloat16.h for its overloads).
64Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(double d);
65Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(double d);
66Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(double d);
67Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(double val);
68Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(float f);
69Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(float f);
70Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(float f);
71Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(float val);
72
73#if QT_CONFIG(signaling_nan)
74Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qSNaN();
75#endif
76Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN();
77Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf();
78
79Q_CORE_EXPORT quint32 qFloatDistance(float a, float b);
80Q_CORE_EXPORT quint64 qFloatDistance(double a, double b);
81
82#define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)())
83#if QT_CONFIG(signaling_nan)
84# define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)())
85#endif
86#define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)())
87
88// Overflow math.
89// This provides efficient implementations for int, unsigned, qsizetype and
90// size_t. Implementations for 8- and 16-bit types will work but may not be as
91// efficient. Implementations for 64-bit may be missing on 32-bit platforms.
92
93// All the GCC and Clang versions we support have constexpr
94// builtins for overflowing arithmetic.
95#if defined(Q_CC_GNU_ONLY)
96 || defined(Q_CC_CLANG_ONLY)
97 || __has_builtin(__builtin_add_overflow)
98# define Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
99// On 32-bit, Clang < 14 will fail to link if multiplying 64-bit
100// quantities (emits an unresolved call to __mulodi4), so we can't use
101// the builtin in that case.
102# if !(QT_POINTER_SIZE == 4 && defined(Q_CC_CLANG_ONLY) && Q_CC_CLANG_ONLY < 1400)
103# define Q_INTRINSIC_MUL_OVERFLOW64
104# endif
105#endif
106
107namespace QtPrivate {
108// Generic versions of (some) overflowing math functions, private API.
109template <typename T>
110constexpr inline
111typename std::enable_if_t<std::is_unsigned_v<T>, bool>
112qAddOverflowGeneric(T v1, T v2, T *r)
113{
114 // unsigned additions are well-defined
115 *r = v1 + v2;
116 return v1 > T(v1 + v2);
117}
118
119// Wide multiplication.
120// It has been isolated in its own function so that it can be tested.
121// Note that this implementation requires a T that doesn't undergo
122// promotions.
123template <typename T>
124constexpr inline
125typename std::enable_if_t<std::is_same_v<T, decltype(+T{})>, bool>
127{
128 // This is a glorified long/school-grade multiplication,
129 // that considers each input of N bits as two halves of N/2 bits:
130 //
131 // v1 = 2^(N/2) * v1_hi + v1_lo
132 // v2 = 2^(N/2) * v2_hi + v2_lo
133 //
134 // Therefore, v1*v2 = 2^N * v1_hi * v2_hi +
135 // 2^(N/2) * v1_hi * v2_lo +
136 // 2^(N/2) * v1_lo * v2_hi +
137 // * v1_lo * v2_lo
138 //
139 // Using the N bits of precision we have we can perform the hi*lo
140 // multiplications safely; that is never going to overflow.
141 //
142 // Then we can sum together these partial results:
143 //
144 // [ v1_hi | v1_lo ] *
145 // [ v2_hi | v2_lo ] =
146 // -------------------
147 // [ v1_lo * v2_lo ] +
148 // [ v1_hi * v2_lo ] + // shifted because it's * 2^(N/2)
149 // [ v2_hi * v1_lo ] + // shifted because it's * 2^(N/2)
150 // [ v1_hi * v2_hi ] = // shifted because it's * 2^N
151 // -------------------------------
152 // [ high ][ low ] // exact result (in 2^(2N) bits)
153 //
154 // ... except that this way we'll need to bring some carries, so
155 // we'll do a slightly smarter sum.
156 //
157 // We need high for detecting overflows, even if we are not returning it.
158
159 // Get multiplication by zero out of the way
160 if (v1 == 0 || v2 == 0) {
161 *r = T(0);
162 return false;
163 }
164
165 // Extract the absolute values as unsigned
166 // (will fix the sign later)
167 using U = std::make_unsigned_t<T>;
168 const U v1_abs = (v1 >= 0) ? U(v1) : (U(0) - U(v1));
169 const U v2_abs = (v2 >= 0) ? U(v2) : (U(0) - U(v2));
170
171 // Masks for N/2 bits
172 constexpr std::size_t half_width = (sizeof(U) * 8) / 2;
173 const U half_mask = ~U(0) >> half_width;
174
175 // Split in low and half quantities
176 const U v1_lo = v1_abs & half_mask;
177 const U v1_hi = v1_abs >> half_width;
178 const U v2_lo = v2_abs & half_mask;
179 const U v2_hi = v2_abs >> half_width;
180
181 // Cross-product; this will never overflow
182 const U lo_lo = v1_lo * v2_lo;
183 const U lo_hi = v1_lo * v2_hi;
184 const U hi_lo = v1_hi * v2_lo;
185 const U hi_hi = v1_hi * v2_hi;
186
187 // We could sum directly the cross-products, but then we'd have to
188 // keep track of carries. This avoids it.
189 const U tmp = (lo_lo >> half_width) + (hi_lo & half_mask) + lo_hi;
190 U result_hi = (hi_lo >> half_width) + (tmp >> half_width) + hi_hi;
191 U result_lo = (tmp << half_width) | (lo_lo & half_mask);
192
193 if constexpr (std::is_unsigned_v<T>) {
194 // If the source was unsigned, we're done; a non-zero high
195 // signals overflow.
196 *r = result_lo;
197 return result_hi != U(0);
198 } else {
199 // We need to set the correct sign back, and check for overflow.
200 const bool isNegative = (v1 < T(0)) != (v2 < T(0));
201 if (isNegative) {
202 // Result is negative; calculate two's complement of the
203 // [high, low] pair, by inverting the bits and adding 1,
204 // which is equivalent to negating it in unsigned
205 // arithmetic.
206 // This operation should be done on the pair as a whole,
207 // but we have the individual components, so start by
208 // calculating two's complement of low:
209 result_lo = U(0) - result_lo;
210
211 // If result_lo is 0, it means that the addition of 1 into
212 // it has overflown, so now we have a carry to add into the
213 // inverted high:
214 result_hi = ~result_hi;
215 if (result_lo == 0)
216 result_hi += U(1);
217 }
218
219 *r = result_lo;
220 // Overflow has happened if result_hi is not a sign extension
221 // of the sign bit of result_lo. Note the usage of T, not U.
222 return result_hi != U(*r >> std::numeric_limits<T>::digits);
223 }
224}
225
226template <typename T, typename Enable = void>
227constexpr inline bool HasLargerInt = false;
228template <typename T>
229constexpr inline bool HasLargerInt<T, std::void_t<typename QIntegerForSize<sizeof(T) * 2>::Unsigned>> = true;
230
231template <typename T>
232constexpr inline
233typename std::enable_if_t<(std::is_unsigned_v<T> || std::is_signed_v<T>), bool>
234qMulOverflowGeneric(T v1, T v2, T *r)
235{
236 // This function is a generic fallback for qMulOverflow,
237 // called either by constant or non-constant evaluation,
238 // if the compiler does not have builtins or intrinsics itself.
239 //
240 // (For instance, this is never going to be called on GCC or recent
241 // Clang, as their builtins will be used in all cases.)
242 //
243 // If a compiler does have builtins, please amend qMulOverflow
244 // directly.
245
246 if constexpr (HasLargerInt<T>) {
247 // Use the next biggest type if available
248 using LargerInt = QIntegerForSize<sizeof(T) * 2>;
249 using Larger = typename std::conditional_t<std::is_signed_v<T>,
250 typename LargerInt::Signed, typename LargerInt::Unsigned>;
251 Larger lr = Larger(v1) * Larger(v2);
252 *r = T(lr);
253 return lr > (std::numeric_limits<T>::max)() || lr < (std::numeric_limits<T>::min)();
254 } else {
255 // Otherwise fall back to a wide multiplication
256 return qMulOverflowWideMultiplication(v1, v2, r);
257 }
258}
259} // namespace QtPrivate
260
261template <typename T>
262constexpr inline
263typename std::enable_if_t<std::is_unsigned_v<T>, bool>
264qAddOverflow(T v1, T v2, T *r)
265{
266 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
267#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
268 return __builtin_add_overflow(v1, v2, r);
269#else
270 if (q20::is_constant_evaluated())
271 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
272# if defined(Q_HAVE_ADDCARRY)
273 // We can use intrinsics for the unsigned operations with MSVC
274 if constexpr (std::is_same_v<T, unsigned>) {
275 return _addcarry_u32(0, v1, v2, r);
276 } else if constexpr (std::is_same_v<T, quint64>) {
277# if defined(Q_PROCESSOR_X86_64)
278 return _addcarry_u64(0, v1, v2, reinterpret_cast<unsigned __int64 *>(r));
279# else
280 uint low, high;
281 uchar carry = _addcarry_u32(0, unsigned(v1), unsigned(v2), &low);
282 carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high);
283 *r = (quint64(high) << 32) | low;
284 return carry;
285# endif // defined(Q_PROCESSOR_X86_64)
286 }
287# endif // defined(Q_HAVE_ADDCARRY)
288 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
289#endif // defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
290}
291
292template <typename T>
293constexpr inline
294typename std::enable_if_t<std::is_signed_v<T>, bool>
295qAddOverflow(T v1, T v2, T *r)
296{
297 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
298#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
299 return __builtin_add_overflow(v1, v2, r);
300#else
301 // Here's how we calculate the overflow:
302 // 1) unsigned addition is well-defined, so we can always execute it
303 // 2) conversion from unsigned back to signed is implementation-
304 // defined and in the implementations we use, it's a no-op.
305 // 3) signed integer overflow happens if the sign of the two input operands
306 // is the same but the sign of the result is different. In other words,
307 // the sign of the result must be the same as the sign of either
308 // operand.
309
310 using U = typename std::make_unsigned_t<T>;
311 *r = T(U(v1) + U(v2));
312
313 // Two's complement equivalent (generates slightly shorter code):
314 // x ^ y is negative if x and y have different signs
315 // x & y is negative if x and y are negative
316 // (x ^ z) & (y ^ z) is negative if x and z have different signs
317 // AND y and z have different signs
318 return ((v1 ^ *r) & (v2 ^ *r)) < 0;
319#endif // defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
320}
321
322template <typename T>
323constexpr inline
324typename std::enable_if_t<std::is_unsigned_v<T>, bool>
325qSubOverflow(T v1, T v2, T *r)
326{
327 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
328#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
329 return __builtin_sub_overflow(v1, v2, r);
330#else
331 // unsigned subtractions are well-defined
332 *r = v1 - v2;
333 return v1 < v2;
334#endif
335}
336
337template <typename T>
338constexpr inline
339typename std::enable_if_t<std::is_signed_v<T>, bool>
340qSubOverflow(T v1, T v2, T *r)
341{
342 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
343#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
344 return __builtin_sub_overflow(v1, v2, r);
345#else
346 // See above for explanation. This is the same with some signs reversed.
347 // We can't use qAddOverflow(v1, -v2, r) because it would be UB if
348 // v2 == std::numeric_limits<T>::min().
349
350 using U = typename std::make_unsigned_t<T>;
351 *r = T(U(v1) - U(v2));
352
353 return ((v1 ^ *r) & (~v2 ^ *r)) < 0;
354#endif // defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
355}
356
357template <typename T>
358constexpr inline
359typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool>
360qMulOverflow(T v1, T v2, T *r)
361{
362 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
363#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
364# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
365 return __builtin_mul_overflow(v1, v2, r);
366# else
367 if constexpr (sizeof(T) <= 4)
368 return __builtin_mul_overflow(v1, v2, r);
369 else
370 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
371# endif
372#else
373 if (q20::is_constant_evaluated())
374 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
375
376# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
377 if constexpr (std::is_unsigned_v<T> && (sizeof(T) == sizeof(quint64))) {
378 // T is 64 bit; either unsigned long long,
379 // or unsigned long on LP64 platforms.
380 *r = v1 * v2;
381 return T(Q_UMULH(v1, v2));
382 } else if constexpr (std::is_signed_v<T> && (sizeof(T) == sizeof(qint64))) {
383 // This is slightly more complex than the unsigned case above: the sign bit
384 // of 'low' must be replicated as the entire 'high', so the only valid
385 // values for 'high' are 0 and -1. Use unsigned multiply since it's the same
386 // as signed for the low bits and use a signed right shift to verify that
387 // 'high' is nothing but sign bits that match the sign of 'low'.
388
389 qint64 high = Q_SMULH(v1, v2);
390 *r = qint64(quint64(v1) * quint64(v2));
391 return (*r >> 63) != high;
392 }
393# endif // defined(Q_INTRINSIC_MUL_OVERFLOW64)
394
395 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
396#endif // defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
397}
398
399#undef Q_HAVE_ADDCARRY
400#undef Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
401
402// Implementations for addition, subtraction or multiplication by a
403// compile-time constant. For addition and subtraction, we simply call the code
404// that detects overflow at runtime. For multiplication, we compare to the
405// maximum possible values before multiplying to ensure no overflow happens.
406
407template <typename T, T V2> constexpr bool qAddOverflow(T v1, std::integral_constant<T, V2>, T *r)
408{
409 return qAddOverflow(v1, V2, r);
410}
411
412template <auto V2, typename T> constexpr bool qAddOverflow(T v1, T *r)
413{
414 return qAddOverflow(v1, std::integral_constant<T, V2>{}, r);
415}
416
417template <typename T, T V2> constexpr bool qSubOverflow(T v1, std::integral_constant<T, V2>, T *r)
418{
419 return qSubOverflow(v1, V2, r);
420}
421
422template <auto V2, typename T> constexpr bool qSubOverflow(T v1, T *r)
423{
424 return qSubOverflow(v1, std::integral_constant<T, V2>{}, r);
425}
426
427template <typename T, T V2> constexpr bool qMulOverflow(T v1, std::integral_constant<T, V2>, T *r)
428{
429 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
430
431 // Runtime detection for anything smaller than or equal to a register
432 // width, as most architectures' multiplication instructions actually
433 // produce a result twice as wide as the input registers, allowing us to
434 // efficiently detect the overflow.
435 if constexpr (sizeof(T) <= sizeof(qregisteruint)) {
436 return qMulOverflow(v1, V2, r);
437
438#ifdef Q_INTRINSIC_MUL_OVERFLOW64
439 } else if constexpr (sizeof(T) <= sizeof(quint64)) {
440 // If we have intrinsics detecting overflow of 64-bit multiplications,
441 // then detect overflows through them up to 64 bits.
442 return qMulOverflow(v1, V2, r);
443#endif
444
445 } else if constexpr (V2 == 0 || V2 == 1) {
446 // trivial cases (and simplify logic below due to division by zero)
447 *r = v1 * V2;
448 return false;
449 } else if constexpr (V2 == -1) {
450 // multiplication by -1 is valid *except* for signed minimum values
451 // (necessary to avoid diving min() by -1, which is an overflow)
452 if (v1 < 0 && v1 == (std::numeric_limits<T>::min)())
453 return true;
454 *r = -v1;
455 return false;
456 } else {
457 // For 64-bit multiplications on 32-bit platforms, let's instead compare v1
458 // against the bounds that would overflow.
459 constexpr T Highest = (std::numeric_limits<T>::max)() / V2;
460 constexpr T Lowest = (std::numeric_limits<T>::min)() / V2;
461 if constexpr (Highest > Lowest) {
462 if (v1 > Highest || v1 < Lowest)
463 return true;
464 } else {
465 // this can only happen if V2 < 0
466 static_assert(V2 < 0);
467 if (v1 > Lowest || v1 < Highest)
468 return true;
469 }
470
471 *r = v1 * V2;
472 return false;
473 }
474}
475
476template <auto V2, typename T> constexpr bool qMulOverflow(T v1, T *r)
477{
478 if constexpr (V2 == 2)
479 return qAddOverflow(v1, v1, r);
480 return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
481}
482
483template <typename T>
484constexpr inline T qAbs(const T &t)
485{
486 if constexpr (std::is_integral_v<T> && std::is_signed_v<T>)
487 Q_ASSERT(t != std::numeric_limits<T>::min());
488 return t >= 0 ? t : -t;
489}
490
491namespace QtPrivate {
492template <typename T,
493 typename std::enable_if_t<std::is_integral_v<T>, bool> = true>
494constexpr inline auto qUnsignedAbs(T t)
495{
496 using U = std::make_unsigned_t<T>;
497 return (t >= 0) ? U(t) : U(~U(t) + U(1));
498}
499
500template <typename Result,
501 typename FP,
502 typename std::enable_if_t<std::is_integral_v<Result>, bool> = true,
503 typename std::enable_if_t<std::is_floating_point_v<FP>, bool> = true>
504constexpr inline Result qCheckedFPConversionToInteger(FP value)
505{
506#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
507 if (!q20::is_constant_evaluated())
508 Q_ASSERT(!std::isnan(value));
509#endif
510
511 constexpr Result minimal = (std::numeric_limits<Result>::min)();
512 constexpr Result maximal = (std::numeric_limits<Result>::max)();
513
514 // We want to check that `value > minimal-1`. `minimal` is
515 // precisely representable as FP (it's -2^N), but `minimal-1`
516 // may not be. Just rearrange the terms:
517 Q_ASSERT(value - FP(minimal) > FP(-1));
518
519 // Symmetrically, `maximal` may not have a precise
520 // representation, but `maximal+1` has, so calculate that:
521 constexpr FP maximalPlusOne = FP(2) * (maximal / 2 + 1);
522 // And check that we're below that:
523 Q_ASSERT(value < maximalPlusOne);
524
525 // If both checks passed, the result of truncation is representable
526 // as `Result`:
527 return Result(value);
528}
529
530namespace QRoundImpl {
531// gcc < 10 doesn't have __has_builtin
532#if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG)
533// ARM64 has a single instruction that can do C++ rounding with conversion to integer.
534// Note current clang versions have non-constexpr __builtin_round, ### allow clang this path when they fix it.
535constexpr inline double qRound(double d)
536{ return __builtin_round(d); }
537constexpr inline float qRound(float f)
538{ return __builtin_roundf(f); }
539#elif defined(__SSE2__) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU))
540// SSE has binary operations directly on floating point making copysign fast
541constexpr inline double qRound(double d)
542{ return d + __builtin_copysign(0.5, d); }
543constexpr inline float qRound(float f)
544{ return f + __builtin_copysignf(0.5f, f); }
545#else
546constexpr inline double qRound(double d)
547{ return d >= 0.0 ? d + 0.5 : d - 0.5; }
548constexpr inline float qRound(float d)
549{ return d >= 0.0f ? d + 0.5f : d - 0.5f; }
550#endif
551} // namespace QRoundImpl
552
553// Like qRound, but have well-defined saturating behavior.
554// NaN is not handled.
555template <typename FP,
556 typename std::enable_if_t<std::is_floating_point_v<FP>, bool> = true>
557constexpr inline int qSaturateRound(FP value)
558{
559#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
560 if (!q20::is_constant_evaluated())
561 Q_ASSERT(!qIsNaN(value));
562#endif
563 constexpr FP MinBound = FP((std::numeric_limits<int>::min)());
564 constexpr FP MaxBound = FP((std::numeric_limits<int>::max)());
565 const FP beforeTruncation = QRoundImpl::qRound(value);
566 return int(qBound(MinBound, beforeTruncation, MaxBound));
567}
568} // namespace QtPrivate
569
570constexpr inline int qRound(double d)
571{
572 return QtPrivate::qCheckedFPConversionToInteger<int>(QtPrivate::QRoundImpl::qRound(d));
573}
574
575constexpr inline int qRound(float f)
576{
577 return QtPrivate::qCheckedFPConversionToInteger<int>(QtPrivate::QRoundImpl::qRound(f));
578}
579
580constexpr inline qint64 qRound64(double d)
581{
582 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(d));
583}
584
585constexpr inline qint64 qRound64(float f)
586{
587 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(f));
588}
589
590namespace QtPrivate {
591template <typename T>
592constexpr inline const T &min(const T &a, const T &b) { return (a < b) ? a : b; }
593}
594
595[[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2) noexcept
596{
597 return (qAbs(p1 - p2) * 1000000000000. <= QtPrivate::min(qAbs(p1), qAbs(p2)));
598}
599
600[[nodiscard]] constexpr bool qFuzzyCompare(float p1, float p2) noexcept
601{
602 return (qAbs(p1 - p2) * 100000.f <= QtPrivate::min(qAbs(p1), qAbs(p2)));
603}
604
605[[nodiscard]] constexpr bool qFuzzyIsNull(double d) noexcept
606{
607 return qAbs(d) <= 0.000000000001;
608}
609
610[[nodiscard]] constexpr bool qFuzzyIsNull(float f) noexcept
611{
612 return qAbs(f) <= 0.00001f;
613}
614
615QT_WARNING_PUSH
616QT_WARNING_DISABLE_FLOAT_COMPARE
617
618[[nodiscard]] constexpr bool qIsNull(double d) noexcept
619{
620 return d == 0.0;
621}
622
623[[nodiscard]] constexpr bool qIsNull(float f) noexcept
624{
625 return f == 0.0f;
626}
627
628QT_WARNING_POP
629
630namespace QtPrivate {
631/*
632 A version of qFuzzyCompare that works for all values (qFuzzyCompare()
633 requires that neither argument is numerically 0).
634
635 It's private because we need a fix for the many qFuzzyCompare() uses that
636 ignore the precondition, even for older branches.
637
638 See QTBUG-142020 for discussion of a longer-term solution.
639*/
640template <typename T, typename S>
641[[nodiscard]] constexpr bool fuzzyCompare(const T &lhs, const S &rhs) noexcept
642{
643 static_assert(noexcept(qIsNull(lhs) && qIsNull(rhs) && qFuzzyIsNull(lhs - rhs) && qFuzzyCompare(lhs, rhs)),
644 "The operations qIsNull(), qFuzzyIsNull() and qFuzzyCompare() must be noexcept "
645 "for both argument types!");
646 return qIsNull(lhs) || qIsNull(rhs) ? qFuzzyIsNull(lhs - rhs) : qFuzzyCompare(lhs, rhs);
647}
648} // namespace QtPrivate
649
650
651inline int qIntCast(double f) { return int(f); }
652inline int qIntCast(float f) { return int(f); }
653
654QT_END_NAMESPACE
655
656#endif // QNUMERIC_H
Combined button and popup list for selecting options.
constexpr const T & min(const T &a, const T &b)
Definition qnumeric.h:592
constexpr bool HasLargerInt
Definition qnumeric.h:227
constexpr int qSaturateRound(FP value)
Definition qnumeric.h:557
constexpr std::enable_if_t<(std::is_unsigned_v< T >||std::is_signed_v< T >), bool > qMulOverflowGeneric(T v1, T v2, T *r)
Definition qnumeric.h:234
constexpr auto qUnsignedAbs(T t)
Definition qnumeric.h:494
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflowGeneric(T v1, T v2, T *r)
Definition qnumeric.h:112
constexpr std::enable_if_t< std::is_same_v< T, decltype(+T{})>, bool > qMulOverflowWideMultiplication(T v1, T v2, T *r)
Definition qnumeric.h:126
constexpr bool fuzzyCompare(const T &lhs, const S &rhs) noexcept
Definition qnumeric.h:641
constexpr Result qCheckedFPConversionToInteger(FP value)
Definition qnumeric.h:504
#define __has_builtin(x)
static quint64 d2i(double d)
Definition qnumeric.cpp:181
Q_CORE_EXPORT int qFpClassify(float val)
Definition qnumeric.cpp:102
Q_CORE_EXPORT int qFpClassify(double val)
Definition qnumeric.cpp:101
static quint32 f2i(float f)
Definition qnumeric.cpp:108
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(float f)
int qIntCast(double f)
Definition qnumeric.h:651
constexpr T qAbs(const T &t)
Definition qnumeric.h:484
constexpr std::enable_if< std::is_integral< T >::value, bool >::type qIsFinite(T)
Definition qnumeric.h:61
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(double d)
constexpr bool qSubOverflow(T v1, T *r)
Definition qnumeric.h:422
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf()
constexpr bool qMulOverflow(T v1, T *r)
Definition qnumeric.h:476
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(float f)
constexpr bool qFuzzyIsNull(double d) noexcept
Definition qnumeric.h:605
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(double d)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(float f)
constexpr bool qFuzzyCompare(float p1, float p2) noexcept
Definition qnumeric.h:600
constexpr bool qSubOverflow(T v1, std::integral_constant< T, V2 >, T *r)
Definition qnumeric.h:417
constexpr bool qAddOverflow(T v1, T *r)
Definition qnumeric.h:412
constexpr std::enable_if< std::is_integral< T >::value, bool >::type qIsNaN(T)
Definition qnumeric.h:58
constexpr std::enable_if_t< std::is_unsigned_v< T >||std::is_signed_v< T >, bool > qMulOverflow(T v1, T v2, T *r)
Definition qnumeric.h:360
constexpr qint64 qRound64(double d)
Definition qnumeric.h:580
constexpr int qRound(float f)
Definition qnumeric.h:575
int qIntCast(float f)
Definition qnumeric.h:652
constexpr int qRound(double d)
Definition qnumeric.h:570
constexpr bool qIsNull(float f) noexcept
Definition qnumeric.h:623
constexpr bool qFuzzyIsNull(float f) noexcept
Definition qnumeric.h:610
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qSubOverflow(T v1, T v2, T *r)
Definition qnumeric.h:325
constexpr bool qFuzzyCompare(double p1, double p2) noexcept
Definition qnumeric.h:595
constexpr qint64 qRound64(float f)
Definition qnumeric.h:585
constexpr bool qMulOverflow(T v1, std::integral_constant< T, V2 >, T *r)
Definition qnumeric.h:427
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN()
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflow(T v1, T v2, T *r)
Definition qnumeric.h:264
constexpr bool qAddOverflow(T v1, std::integral_constant< T, V2 >, T *r)
Definition qnumeric.h:407
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(double d)
QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE constexpr bool qIsNull(double d) noexcept
Definition qnumeric.h:618